Rev 1036 | Rev 1124 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1036 | Rev 1122 | ||
|---|---|---|---|
| Line 18... | Line 18... | ||
| 18 | * Free Software Foundation, Inc., * |
18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | 21 | ||
| 22 | #include "socketeventcallback.h" |
22 | #include "socketeventcallback.h" |
| 23 | - | ||
| - | 23 | #include "../Logger/logger.h" |
|
| 24 | 24 | ||
| 25 | #include "asyncsocket.h" |
25 | #include "asyncsocket.h" |
| 26 | 26 | ||
| 27 | int socketCount = 1; |
27 | int socketCount = 1; |
| 28 | 28 | ||
| Line 53... | Line 53... | ||
| 53 | connect(); |
53 | connect(); |
| 54 | } |
54 | } |
| 55 | else |
55 | else |
| 56 | { |
56 | { |
| 57 | // Start the reconnect loop |
57 | // Start the reconnect loop |
| 58 | reconnectLoop(); |
58 | reconnectLoop(); |
| 59 | } |
59 | } |
| 60 | } |
60 | } |
| 61 | 61 | ||
| 62 | bool loop = true; |
62 | bool loop = true; |
| 63 | try |
63 | try |
| 64 | { |
64 | { |
| Line 66... | Line 66... | ||
| 66 | { |
66 | { |
| 67 | // If we have triggered a forced reconnect do it here |
67 | // If we have triggered a forced reconnect do it here |
| 68 | if (myForceReconnect) |
68 | if (myForceReconnect) |
| 69 | { |
69 | { |
| 70 | myForceReconnect = false; |
70 | myForceReconnect = false; |
| 71 | reconnectLoop(); |
71 | reconnectLoop(); |
| 72 | } |
72 | } |
| 73 | 73 | ||
| 74 | // Receive data |
74 | // Receive data |
| 75 | loop = receiveData(); |
75 | loop = receiveData(); |
| 76 | } |
76 | } |
| 77 | } |
77 | } |
| Line 159... | Line 159... | ||
| 159 | } |
159 | } |
| 160 | else if (status > 0) |
160 | else if (status > 0) |
| 161 | { |
161 | { |
| 162 | // We have received data |
162 | // We have received data |
| 163 | eventAdd(SocketEvent::TYPE_DATA, buffer); |
163 | eventAdd(SocketEvent::TYPE_DATA, buffer); |
| 164 | } |
164 | } |
| 165 | 165 | ||
| 166 | return true; |
166 | return true; |
| 167 | } |
167 | } |
| 168 | 168 | ||
| 169 | void AsyncSocket::silentClose() |
169 | void AsyncSocket::silentClose() |
| 170 | { |
170 | { |
| 171 | if (mySocket != -1) |
171 | if (mySocket != -1) |
| 172 | { |
172 | { |
| 173 | ::close(mySocket); |
173 | ::close(mySocket); |
| 174 | mySocket = -1; |
174 | mySocket = -1; |
| 175 | } |
175 | } |
| 176 | } |
176 | } |
| 177 | 177 | ||
| 178 | void AsyncSocket::close() |
178 | void AsyncSocket::close() |
| 179 | { |
179 | { |
| 180 | silentClose(); |
180 | silentClose(); |
| 181 | eventAdd(SocketEvent::TYPE_CONNECTION_CLOSED); |
181 | eventAdd(SocketEvent::TYPE_CONNECTION_CLOSED); |
| 182 | } |
182 | } |
| 183 | 183 | ||
| 184 | void AsyncSocket::reconnectLoop() |
184 | void AsyncSocket::reconnectLoop() |
| 185 | { |
185 | { |
| 186 | while (true) |
186 | while (true) |
| 187 | { |
187 | { |
| 188 | try |
188 | try |
| 189 | { |
189 | { |
| 190 | connect(); |
190 | connect(); |
| 191 | return; |
191 | return; |
| 192 | } |
192 | } |
| 193 | catch (SocketException *e) |
193 | catch (SocketException *e) |
| 194 | { |
194 | { |
| 195 | eventAdd(SocketEvent::TYPE_CONNECTION_FAILED, e->getDescription()); |
195 | eventAdd(SocketEvent::TYPE_CONNECTION_FAILED, e->getDescription()); |
| 196 | eventAdd(SocketEvent::TYPE_WAITING_RECONNECT); |
196 | eventAdd(SocketEvent::TYPE_WAITING_RECONNECT); |
| 197 | sleep(myReconnectTimeout); |
197 | sleep(myReconnectTimeout); |
| 198 | } |
198 | } |
| 199 | } |
199 | } |
| 200 | } |
200 | } |
| 201 | 201 | ||
| 202 | void AsyncSocket::create() |
202 | void AsyncSocket::create() |
| 203 | { |
203 | { |
| 204 | silentClose(); |
204 | silentClose(); |
| 205 | 205 | ||
| 206 | mySocket = ::socket(AF_INET, SOCK_STREAM, 0); |
206 | mySocket = ::socket(AF_INET, SOCK_STREAM, 0); |
| 207 | 207 | ||
| 208 | int on = 1; |
208 | int on = 1; |
| 209 | int status = setsockopt(mySocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)); |
209 | int status = setsockopt(mySocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)); |
| 210 | if (status == -1) |
210 | if (status == -1) |
| 211 | { |
211 | { |
| 212 | silentClose(); |
212 | silentClose(); |
| 213 | throw new SocketException("Create:Reuseaddress: " + itos(errno)); |
213 | throw new SocketException("Create:Reuseaddress: " + itos(errno)); |
| 214 | } |
214 | } |
| 215 | } |
215 | } |
| 216 | 216 | ||
| 217 | void AsyncSocket::startListen() |
217 | void AsyncSocket::startListen() |
| 218 | { |
218 | { |
| 219 | create(); |
219 | create(); |
| 220 | 220 | ||
| 221 | myAddressStruct.sin_family = AF_INET; |
221 | myAddressStruct.sin_family = AF_INET; |
| Line 223... | Line 223... | ||
| 223 | myAddressStruct.sin_port = htons(myPort); |
223 | myAddressStruct.sin_port = htons(myPort); |
| 224 | 224 | ||
| 225 | int status = ::bind(mySocket, (struct sockaddr *)&myAddressStruct, sizeof(myAddressStruct)); |
225 | int status = ::bind(mySocket, (struct sockaddr *)&myAddressStruct, sizeof(myAddressStruct)); |
| 226 | 226 | ||
| 227 | if (status == -1) |
227 | if (status == -1) |
| 228 | { |
228 | { |
| 229 | silentClose(); |
229 | silentClose(); |
| 230 | switch (errno) |
230 | switch (errno) |
| 231 | { |
231 | { |
| 232 | case EACCES: |
232 | case EACCES: |
| 233 | throw new SocketException("The address is protected, and the user is not the superuser."); |
233 | throw new SocketException("The address is protected, and the user is not the superuser."); |
| 234 | 234 | ||
| 235 | case EADDRINUSE: |
235 | case EADDRINUSE: |
| 236 | throw new SocketException("The given address is already in use."); |
236 | throw new SocketException("The given address is already in use."); |
| 237 | 237 | ||
| 238 | case EBADF: |
238 | case EBADF: |
| 239 | throw new SocketException("sockfd is not a valid descriptor."); |
239 | throw new SocketException("sockfd is not a valid descriptor."); |
| 240 | 240 | ||
| 241 | case EINVAL: |
241 | case EINVAL: |
| 242 | throw new SocketException("The socket is already bound to an address."); |
242 | throw new SocketException("The socket is already bound to an address."); |
| 243 | 243 | ||
| 244 | case ENOTSOCK: |
244 | case ENOTSOCK: |
| 245 | throw new SocketException("sockfd is a descriptor for a file, not a socket."); |
245 | throw new SocketException("sockfd is a descriptor for a file, not a socket."); |
| 246 | 246 | ||
| 247 | //case EACCES: |
247 | //case EACCES: |
| 248 | //throw new SocketException("Search permission is denied on a component of the path prefix. (See also path_resolution(7).)"); |
248 | //throw new SocketException("Search permission is denied on a component of the path prefix. (See also path_resolution(7).)"); |
| 249 | 249 | ||
| 250 | case EADDRNOTAVAIL: |
250 | case EADDRNOTAVAIL: |
| 251 | throw new SocketException("A nonexistent interface was requested or the requested address was not local."); |
251 | throw new SocketException("A nonexistent interface was requested or the requested address was not local."); |
| 252 | 252 | ||
| 253 | case EFAULT: |
253 | case EFAULT: |
| 254 | throw new SocketException("addr points outside the user's accessible address space."); |
254 | throw new SocketException("addr points outside the user's accessible address space."); |
| 255 | 255 | ||
| 256 | //case EINVAL: |
256 | //case EINVAL: |
| 257 | //throw new SocketException("The addrlen is wrong, or the socket was not in the AF_UNIX family."); |
257 | //throw new SocketException("The addrlen is wrong, or the socket was not in the AF_UNIX family."); |
| 258 | 258 | ||
| 259 | case ELOOP: |
259 | case ELOOP: |
| 260 | throw new SocketException("Too many symbolic links were encountered in resolving addr."); |
260 | throw new SocketException("Too many symbolic links were encountered in resolving addr."); |
| 261 | 261 | ||
| 262 | case ENAMETOOLONG: |
262 | case ENAMETOOLONG: |
| 263 | throw new SocketException("addr is too long."); |
263 | throw new SocketException("addr is too long."); |
| 264 | 264 | ||
| 265 | case ENOENT: |
265 | case ENOENT: |
| 266 | throw new SocketException("The file does not exist."); |
266 | throw new SocketException("The file does not exist."); |
| 267 | 267 | ||
| 268 | case ENOMEM: |
268 | case ENOMEM: |
| 269 | throw new SocketException("Insufficient kernel memory was available."); |
269 | throw new SocketException("Insufficient kernel memory was available."); |
| Line 403... | Line 403... | ||
| 403 | void AsyncSocket::sendData(string data) |
403 | void AsyncSocket::sendData(string data) |
| 404 | { |
404 | { |
| 405 | mySendMutex.lock(); |
405 | mySendMutex.lock(); |
| 406 | 406 | ||
| 407 | int status = ::send(mySocket, data.c_str(), data.size(), 0); |
407 | int status = ::send(mySocket, data.c_str(), data.size(), 0); |
| 408 | - | ||
| - | 408 | //Logger::getInstance().add("Sent: \"" + data + "\" status was " + itos(status) + "\n"); |
|
| 409 | mySendMutex.unlock(); |
409 | mySendMutex.unlock(); |
| 410 | 410 | ||
| 411 | //Logger &log = Logger::getInstance(); |
411 | //Logger &log = Logger::getInstance(); |
| 412 | //log.add("Sent: \"" + data + "\" status was " + itos(status) + "\n"); |
412 | //log.add("Sent: \"" + data + "\" status was " + itos(status) + "\n"); |
| 413 | 413 | ||