Diffstat (limited to 'noncore/comm/keypebble/krfbconnection.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/comm/keypebble/krfbconnection.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/noncore/comm/keypebble/krfbconnection.cpp b/noncore/comm/keypebble/krfbconnection.cpp index e6c33c6..b447046 100644 --- a/noncore/comm/keypebble/krfbconnection.cpp +++ b/noncore/comm/keypebble/krfbconnection.cpp | |||
@@ -1,206 +1,206 @@ | |||
1 | #include <assert.h> | 1 | #include <assert.h> |
2 | #include <qsocket.h> | 2 | #include <qsocket.h> |
3 | #include <qtimer.h> | 3 | #include <qtimer.h> |
4 | #include <string.h> | 4 | #include <string.h> |
5 | 5 | ||
6 | #include "krfbconnection.h" | 6 | #include "krfbconnection.h" |
7 | #include "krfblogin.h" | 7 | #include "krfblogin.h" |
8 | #include "krfbdecoder.h" | 8 | #include "krfbdecoder.h" |
9 | #include "krfbbuffer.h" | 9 | #include "krfbbuffer.h" |
10 | 10 | ||
11 | KRFBConnection::KRFBConnection( QObject *parent ) | 11 | KRFBConnection::KRFBConnection( QObject *parent ) |
12 | : QObject( parent, "KRFBConnection" ) | 12 | : QObject( parent, "KRFBConnection" ) |
13 | { | 13 | { |
14 | portBase_ = 5900; | 14 | portBase_ = 5900; |
15 | currentState_ = Disconnected; | 15 | currentState_ = Disconnected; |
16 | sock = 0; | 16 | sock = 0; |
17 | minData_ = 0; | 17 | minData_ = 0; |
18 | options_ = new KRFBServer(); | 18 | options_ = new KRFBServer(); |
19 | updater = 0; | 19 | updater = 0; |
20 | decoder_ = 0; | 20 | decoder_ = 0; |
21 | buffer_ = 0; | 21 | buffer_ = 0; |
22 | } | 22 | } |
23 | 23 | ||
24 | KRFBConnection::~KRFBConnection() | 24 | KRFBConnection::~KRFBConnection() |
25 | { | 25 | { |
26 | if ( ( currentState_ != Disconnected ) && ( currentState_ != Disconnecting ) && sock ) { | 26 | if ( ( currentState_ != Disconnected ) && ( currentState_ != Disconnecting ) && sock ) { |
27 | disconnectDone(); | 27 | disconnectDone(); |
28 | } | 28 | } |
29 | delete options_; | 29 | delete options_; |
30 | } | 30 | } |
31 | 31 | ||
32 | void KRFBConnection::connectTo( KRFBServer server) | 32 | void KRFBConnection::connectTo( KRFBServer server) |
33 | { | 33 | { |
34 | if ( currentState_ != Disconnected ) | 34 | if ( currentState_ != Disconnected ) |
35 | disconnect(); | 35 | disconnect(); |
36 | 36 | ||
37 | (*options_)=server; | 37 | (*options_)=server; |
38 | 38 | ||
39 | sock = new QSocket( this, "rfbSocket" ); | 39 | sock = new QSocket( this, "rfbSocket" ); |
40 | CHECK_PTR( sock ); | 40 | CHECK_PTR( sock ); |
41 | 41 | ||
42 | // Connect to something to notice connection or error | 42 | // Connect to something to notice connection or error |
43 | connect( sock, SIGNAL( error( int ) ), SLOT( gotSocketError( int ) ) ); | 43 | connect( sock, SIGNAL( error(int) ), SLOT( gotSocketError(int) ) ); |
44 | connect( sock, SIGNAL( connected() ), SLOT( gotSocketConnection() ) ); | 44 | connect( sock, SIGNAL( connected() ), SLOT( gotSocketConnection() ) ); |
45 | 45 | ||
46 | qWarning( "Connecting..." ); | 46 | qWarning( "Connecting..." ); |
47 | 47 | ||
48 | currentState_ = Connecting; | 48 | currentState_ = Connecting; |
49 | sock->connectToHost( options_->hostname.latin1(), portBase_ + options_->display ); | 49 | sock->connectToHost( options_->hostname.latin1(), portBase_ + options_->display ); |
50 | } | 50 | } |
51 | 51 | ||
52 | void KRFBConnection::disconnect() | 52 | void KRFBConnection::disconnect() |
53 | { | 53 | { |
54 | qWarning( "Disconnecting from server" ); | 54 | qWarning( "Disconnecting from server" ); |
55 | 55 | ||
56 | if ( ( currentState_ != Disconnected ) | 56 | if ( ( currentState_ != Disconnected ) |
57 | && ( currentState_ != Disconnecting ) | 57 | && ( currentState_ != Disconnecting ) |
58 | && sock ) { | 58 | && sock ) { |
59 | currentState_ = Disconnecting; | 59 | currentState_ = Disconnecting; |
60 | 60 | ||
61 | connect( sock, SIGNAL( delayedCloseFinished() ), SLOT( disconnectDone() ) ); | 61 | connect( sock, SIGNAL( delayedCloseFinished() ), SLOT( disconnectDone() ) ); |
62 | sock->close(); | 62 | sock->close(); |
63 | 63 | ||
64 | if ( sock->state() != QSocket::Closing ) | 64 | if ( sock->state() != QSocket::Closing ) |
65 | disconnectDone(); | 65 | disconnectDone(); |
66 | } | 66 | } |
67 | } | 67 | } |
68 | 68 | ||
69 | void KRFBConnection::disconnectDone() | 69 | void KRFBConnection::disconnectDone() |
70 | { | 70 | { |
71 | currentState_ = Disconnected; | 71 | currentState_ = Disconnected; |
72 | delete sock; | 72 | delete sock; |
73 | sock = 0; | 73 | sock = 0; |
74 | minData_ = 0; | 74 | minData_ = 0; |
75 | delete updater; | 75 | delete updater; |
76 | delete decoder_; | 76 | delete decoder_; |
77 | delete buffer_; | 77 | delete buffer_; |
78 | emit disconnected(); | 78 | emit disconnected(); |
79 | } | 79 | } |
80 | 80 | ||
81 | void KRFBConnection::gotSocketConnection() | 81 | void KRFBConnection::gotSocketConnection() |
82 | { | 82 | { |
83 | currentState_ = LoggingIn; | 83 | currentState_ = LoggingIn; |
84 | 84 | ||
85 | qWarning( "Connected, logging in..." ); | 85 | qWarning( "Connected, logging in..." ); |
86 | 86 | ||
87 | static QString statusMsg = tr( "Connected" ); | 87 | static QString statusMsg = tr( "Connected" ); |
88 | emit statusChanged( statusMsg ); | 88 | emit statusChanged( statusMsg ); |
89 | 89 | ||
90 | // Do some login stuff | 90 | // Do some login stuff |
91 | login = new KRFBLogin( this ); | 91 | login = new KRFBLogin( this ); |
92 | } | 92 | } |
93 | 93 | ||
94 | void KRFBConnection::gotRFBConnection() | 94 | void KRFBConnection::gotRFBConnection() |
95 | { | 95 | { |
96 | qWarning( "Logged into server" ); | 96 | qWarning( "Logged into server" ); |
97 | 97 | ||
98 | currentState_ = Connected; | 98 | currentState_ = Connected; |
99 | emit connected(); | 99 | emit connected(); |
100 | 100 | ||
101 | // Create the decoder and start doing stuff | 101 | // Create the decoder and start doing stuff |
102 | decoder_ = new KRFBDecoder( this ); | 102 | decoder_ = new KRFBDecoder( this ); |
103 | CHECK_PTR( decoder_ ); | 103 | CHECK_PTR( decoder_ ); |
104 | 104 | ||
105 | buffer_ = new KRFBBuffer( decoder_, this, "RFB Buffer" ); | 105 | buffer_ = new KRFBBuffer( decoder_, this, "RFB Buffer" ); |
106 | CHECK_PTR( buffer_ ); | 106 | CHECK_PTR( buffer_ ); |
107 | decoder_->setBuffer( buffer_ ); | 107 | decoder_->setBuffer( buffer_ ); |
108 | 108 | ||
109 | connect( decoder_, SIGNAL( status( const QString & ) ), | 109 | connect( decoder_, SIGNAL( status(const QString&) ), |
110 | this, SIGNAL( statusChanged( const QString & ) ) ); | 110 | this, SIGNAL( statusChanged(const QString&) ) ); |
111 | emit loggedIn(); | 111 | emit loggedIn(); |
112 | 112 | ||
113 | decoder_->start(); | 113 | decoder_->start(); |
114 | 114 | ||
115 | updater = new QTimer; | 115 | updater = new QTimer; |
116 | connect( updater, SIGNAL( timeout() ), SLOT( updateTimer() ) ); | 116 | connect( updater, SIGNAL( timeout() ), SLOT( updateTimer() ) ); |
117 | updater->start( options_->updateRate ); | 117 | updater->start( options_->updateRate ); |
118 | } | 118 | } |
119 | 119 | ||
120 | void KRFBConnection::gotSocketError( int err ) | 120 | void KRFBConnection::gotSocketError( int err ) |
121 | { | 121 | { |
122 | currentState_ = Error; | 122 | currentState_ = Error; |
123 | 123 | ||
124 | // Do some error handling stuff | 124 | // Do some error handling stuff |
125 | qWarning( "KRFBConnection: Socket error %d", err ); | 125 | qWarning( "KRFBConnection: Socket error %d", err ); |
126 | 126 | ||
127 | static QString refused = tr( "Connection Refused" ); | 127 | static QString refused = tr( "Connection Refused" ); |
128 | static QString host = tr( "Host not found" ); | 128 | static QString host = tr( "Host not found" ); |
129 | static QString read = tr( "Read Error: QSocket reported an error reading\n" | 129 | static QString read = tr( "Read Error: QSocket reported an error reading\n" |
130 | "data, the remote host has probably dropped the\n" | 130 | "data, the remote host has probably dropped the\n" |
131 | "connection." ); | 131 | "connection." ); |
132 | static QString confused = tr( "QSocket reported an invalid error code" ); | 132 | static QString confused = tr( "QSocket reported an invalid error code" ); |
133 | 133 | ||
134 | QString msg; | 134 | QString msg; |
135 | switch ( err ) { | 135 | switch ( err ) { |
136 | case QSocket::ErrConnectionRefused: | 136 | case QSocket::ErrConnectionRefused: |
137 | msg = refused; | 137 | msg = refused; |
138 | break; | 138 | break; |
139 | case QSocket::ErrHostNotFound: | 139 | case QSocket::ErrHostNotFound: |
140 | msg = host; | 140 | msg = host; |
141 | break; | 141 | break; |
142 | case QSocket::ErrSocketRead: | 142 | case QSocket::ErrSocketRead: |
143 | msg = read; | 143 | msg = read; |
144 | break; | 144 | break; |
145 | default: | 145 | default: |
146 | msg = confused; | 146 | msg = confused; |
147 | }; | 147 | }; |
148 | 148 | ||
149 | QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) ); | 149 | QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) ); |
150 | delete sock; | 150 | delete sock; |
151 | sock = 0; | 151 | sock = 0; |
152 | currentState_ = Disconnected; | 152 | currentState_ = Disconnected; |
153 | 153 | ||
154 | emit error( msg ); | 154 | emit error( msg ); |
155 | } | 155 | } |
156 | 156 | ||
157 | void KRFBConnection::gotMoreData() | 157 | void KRFBConnection::gotMoreData() |
158 | { | 158 | { |
159 | assert( minData_ > 0 ); | 159 | assert( minData_ > 0 ); |
160 | 160 | ||
161 | if ( sock->size() >= minData_ ) { | 161 | if ( sock->size() >= minData_ ) { |
162 | minData_ = 0; | 162 | minData_ = 0; |
163 | QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) ); | 163 | QObject::disconnect( sock, SIGNAL( readyRead() ), this, SLOT( gotMoreData() ) ); |
164 | emit gotEnoughData(); | 164 | emit gotEnoughData(); |
165 | } | 165 | } |
166 | } | 166 | } |
167 | 167 | ||
168 | void KRFBConnection::waitForData( unsigned int sz ) | 168 | void KRFBConnection::waitForData( unsigned int sz ) |
169 | { | 169 | { |
170 | assert( minData_ == 0 ); | 170 | assert( minData_ == 0 ); |
171 | assert( sz > 0 ); | 171 | assert( sz > 0 ); |
172 | assert( currentState_ != Error ); | 172 | assert( currentState_ != Error ); |
173 | 173 | ||
174 | if ( sock->size() >= sz ) { | 174 | if ( sock->size() >= sz ) { |
175 | // qWarning( "No need to wait for data" ); | 175 | // qWarning( "No need to wait for data" ); |
176 | emit gotEnoughData(); | 176 | emit gotEnoughData(); |
177 | } | 177 | } |
178 | else { | 178 | else { |
179 | // qWarning( "Waiting for %u bytes", sz ); | 179 | // qWarning( "Waiting for %u bytes", sz ); |
180 | minData_ = sz; | 180 | minData_ = sz; |
181 | connect( sock, SIGNAL( readyRead() ), SLOT( gotMoreData() ) ); | 181 | connect( sock, SIGNAL( readyRead() ), SLOT( gotMoreData() ) ); |
182 | } | 182 | } |
183 | } | 183 | } |
184 | 184 | ||
185 | int KRFBConnection::read( void *buf, int sz ) | 185 | int KRFBConnection::read( void *buf, int sz ) |
186 | { | 186 | { |
187 | return sock->readBlock( (char *) buf, sz ); | 187 | return sock->readBlock( (char *) buf, sz ); |
188 | } | 188 | } |
189 | 189 | ||
190 | int KRFBConnection::write( void *buf, int sz ) | 190 | int KRFBConnection::write( void *buf, int sz ) |
191 | { | 191 | { |
192 | return sock->writeBlock( (const char *) buf, sz ); | 192 | return sock->writeBlock( (const char *) buf, sz ); |
193 | } | 193 | } |
194 | 194 | ||
195 | KRFBConnection::State KRFBConnection::state() const | 195 | KRFBConnection::State KRFBConnection::state() const |
196 | { | 196 | { |
197 | return currentState_; | 197 | return currentState_; |
198 | } | 198 | } |
199 | 199 | ||
200 | void KRFBConnection::setPortBase( int base ) | 200 | void KRFBConnection::setPortBase( int base ) |
201 | { | 201 | { |
202 | portBase_ = base; | 202 | portBase_ = base; |
203 | } | 203 | } |
204 | 204 | ||
205 | int KRFBConnection::portBase() const | 205 | int KRFBConnection::portBase() const |
206 | { | 206 | { |