#include <SocketAccessor.h>
Inheritance diagram for Net::SocketAccessor:
Public Member Functions | |
SocketAccessor (int family, int type, int protocol=0) | |
virtual String * | className (void) const |
Answer receiver class name. | |
virtual void | close (void) |
virtual long | bufferSize (void) |
virtual IPSocketAddress * | myAddress (void) |
virtual IPSocketAddress * | peerAddress (void) |
virtual int | readInto (ByteArray *buffer) |
virtual int | readInto (ByteArray *buffer, long startIndex, long count) |
virtual int | writeFrom (ByteArray *buffer) |
virtual int | writeFrom (ByteArray *buffer, long startIndex, long count) |
virtual void | seekTo (long position) |
virtual void | readWait (void) |
virtual bool | readWaitWithTimeout (int milliseconds) |
virtual void | writeWait (void) |
virtual bool | writeWaitWithTimeout (int milliseconds) |
virtual void | bindTo (IPSocketAddress *address) |
virtual void | connectTo (IPSocketAddress *address) |
virtual void | listenFor (int queueLength) |
virtual SocketAccessor * | accept (void) |
Static Public Member Functions | |
static SocketAccessor * | newTCP (void) |
static SocketAccessor * | newTCPclientToHost (ByteArray *hostAddr, int port) |
static SocketAccessor * | newTCPclientToHost (String *hostName, int port) |
static SocketAccessor * | newTCPserverAtPort (int port) |
static SocketAccessor * | newTCPserverAt (ByteArray *hostAddr, int port) |
static SocketAccessor * | newUDP (void) |
static SocketAccessor * | newUDPclientToHost (ByteArray *hostAddr, int port) |
static SocketAccessor * | newUDPclientToHost (String *hostName, int port) |
static SocketAccessor * | newUDPserverAtPort (int port) |
static SocketAccessor * | newUDPserverAt (ByteArray *hostAddr, int port) |
static String * | getHostname (void) |
Protected Member Functions | |
SocketAccessor (int descr) | |
Private constructor for accepted sockets. | |
Protected Attributes | |
int | descriptor |
Socket file descriptor. | |
int | sckFamily |
|
Private constructor for accepted sockets.
Definition at line 61 of file SocketAccessor.cc. References descriptor. Referenced by accept(), newTCP(), newTCPclientToHost(), newTCPserverAt(), newUDP(), newUDPclientToHost(), and newUDPserverAt(). 00062 { 00063 descriptor = desc; 00064 }
|
|
Definition at line 46 of file SocketAccessor.cc. References descriptor, Core::GenericException::raiseFrom(), and sckFamily. 00047 { 00048 /* Zalozime socket */ 00049 sckFamily = family; 00050 descriptor = socket(family, type, 0); 00051 if (descriptor < 0) { 00052 /* Nepodarilo se zalozit socket */ 00053 perror(__PRETTY_FUNCTION__); 00054 NetworkError *ex; 00055 ex = new NetworkError(new String("Cannot create new socket."), 00056 __PRETTY_FUNCTION__); 00057 ex->raiseFrom(this); 00058 } 00059 }
|
|
Definition at line 452 of file SocketAccessor.cc. References descriptor, Core::GenericException::raiseFrom(), and SocketAccessor(). 00453 { 00454 int result; 00455 00456 do { 00457 result = ::accept(descriptor, NULL, NULL); 00458 } while (result == -1 && errno == EINTR); 00459 00460 if (result < 0) { 00461 perror(__PRETTY_FUNCTION__); 00462 NetworkError *ex; 00463 ex = new NetworkError(new String("Accept failed."), __PRETTY_FUNCTION__); 00464 ex->raiseFrom(this); 00465 } 00466 return new SocketAccessor(result); 00467 }
|
|
Definition at line 398 of file SocketAccessor.cc. References descriptor, Core::GenericException::raiseFrom(), and sckFamily. Referenced by newTCP(), newTCPserverAt(), newUDP(), and newUDPserverAt(). 00399 { 00400 struct sockaddr_in IPaddr; 00401 int result; 00402 00403 bzero(&IPaddr, sizeof(IPaddr)); 00404 IPaddr.sin_family = sckFamily; 00405 IPaddr.sin_addr.s_addr = htonl(address->hostAddress()->asInteger()); 00406 IPaddr.sin_port = htons(address->port()); 00407 result = bind(descriptor, (struct sockaddr *) &IPaddr, sizeof(IPaddr)); 00408 if (result < 0) { 00409 /* Socketu se nepodarilo priradit jmeno */ 00410 perror(__PRETTY_FUNCTION__); 00411 NetworkError *ex; 00412 ex = new NetworkError(new String("Cannot bind socket to machine."), 00413 __PRETTY_FUNCTION__, address); 00414 ex->raiseFrom(this); 00415 } 00416 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 233 of file SocketAccessor.cc. 00234 {
00235 return 20480;
00236 }
|
|
Answer receiver class name. Because there isn't any standard way to obtain class name this method comes to place. Every class should rewrite this method but many didn't (yet). Reimplemented from Core::IOAccessor. Definition at line 67 of file SocketAccessor.cc. 00068 { 00069 return new String("SocketAccessor"); 00070 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 227 of file SocketAccessor.cc. References descriptor. Referenced by newTCP(), newTCPclientToHost(), newTCPserverAt(), newUDP(), newUDPclientToHost(), and newUDPserverAt(). 00228 { 00229 ::close(descriptor); 00230 }
|
|
Definition at line 418 of file SocketAccessor.cc. References descriptor, Core::GenericException::raiseFrom(), and sckFamily. Referenced by newTCPclientToHost(), and newUDPclientToHost(). 00419 { 00420 struct sockaddr_in IPaddr; 00421 int result; 00422 00423 /* Pripojime se k serveru */ 00424 bzero((char *) &IPaddr, sizeof(IPaddr)); 00425 IPaddr.sin_family = sckFamily; 00426 IPaddr.sin_addr.s_addr = htonl(address->hostAddress()->asInteger()); 00427 IPaddr.sin_port = htons(address->port()); 00428 result = connect(descriptor, (struct sockaddr *) &IPaddr, sizeof(IPaddr)); 00429 if (result < 0) { 00430 /* Nepodarilo se navazat spojeni */ 00431 NetworkError *ex; 00432 ex = new NetworkError(new String("Cannot open connection to remote machine."), 00433 __PRETTY_FUNCTION__, address); 00434 ex->raiseFrom(this); 00435 } 00436 }
|
|
Definition at line 221 of file SocketAccessor.cc. 00222 { 00223 return new String(getenv("HOSTNAME")); 00224 }
|
|
Definition at line 438 of file SocketAccessor.cc. References descriptor, and Core::GenericException::raiseFrom(). 00439 { 00440 int result = listen(descriptor, queueLength); 00441 if (result < 0) { 00442 /* Nepodarilo se navazat spojeni */ 00443 perror(__PRETTY_FUNCTION__); 00444 NetworkError *ex; 00445 ex = new NetworkError(new String("Socket cannot listen on the port."), 00446 __PRETTY_FUNCTION__); 00447 ex->raiseFrom(this); 00448 } 00449 }
|
|
Definition at line 238 of file SocketAccessor.cc. References descriptor, Net::IPSocketAddress::hostAddress(), and nil. 00239 { 00240 struct sockaddr_in addr; 00241 socklen_t addrLength = sizeof(addr); 00242 int retCode; 00243 00244 retCode = getsockname(descriptor, (struct sockaddr *) &addr, &addrLength); 00245 if (retCode == 0) { 00246 union { 00247 long int number; 00248 char array[4]; 00249 } converter; 00250 converter.number = addr.sin_addr.s_addr; 00251 ByteArray *baAddr = new ByteArray(converter.array, 4); 00252 return IPSocketAddress::hostAddress(baAddr, ntohs(addr.sin_port)); 00253 } 00254 return nil; 00255 }
|
|
Definition at line 73 of file SocketAccessor.cc. References bindTo(), close(), SocketAccessor(), and Net::IPSocketAddress::thisHostAnyPort(). 00074 { 00075 SocketAccessor *skt; 00076 IPSocketAddress *sa; 00077 00078 skt = new SocketAccessor(AF_INET, SOCK_STREAM); 00079 try { 00080 sa = IPSocketAddress::thisHostAnyPort(); 00081 skt->bindTo(sa); 00082 } 00083 catch (NetworkError *ex) { 00084 skt->close(); 00085 ex->pass(); 00086 } 00087 return skt; 00088 }
|
|
Definition at line 107 of file SocketAccessor.cc. References close(), connectTo(), Net::IPSocketAddress::hostName(), and SocketAccessor(). 00108 { 00109 SocketAccessor *skt; 00110 IPSocketAddress *fa; 00111 00112 skt = new SocketAccessor(AF_INET, SOCK_STREAM); 00113 try { 00114 fa = IPSocketAddress::hostName(hostName, port); 00115 skt->connectTo(fa); 00116 } 00117 catch (NetworkError *ex) { 00118 skt->close(); 00119 ex->pass(); 00120 } 00121 return skt; 00122 }
|
|
Definition at line 90 of file SocketAccessor.cc. References close(), connectTo(), Net::IPSocketAddress::hostAddress(), and SocketAccessor(). 00091 { 00092 SocketAccessor *skt; 00093 IPSocketAddress *fa; 00094 00095 skt = new SocketAccessor(AF_INET, SOCK_STREAM); 00096 try { 00097 fa = IPSocketAddress::hostAddress(hostAddr, port); 00098 skt->connectTo(fa); 00099 } 00100 catch (NetworkError *ex) { 00101 skt->close(); 00102 ex->pass(); 00103 } 00104 return skt; 00105 }
|
|
Definition at line 129 of file SocketAccessor.cc. References bindTo(), close(), Net::IPSocketAddress::hostAddress(), and SocketAccessor(). Referenced by newTCPserverAtPort(). 00130 { 00131 SocketAccessor *skt; 00132 IPSocketAddress *sa; 00133 00134 skt = new SocketAccessor(AF_INET, SOCK_STREAM); 00135 try { 00136 sa = IPSocketAddress::hostAddress(addr, port); 00137 skt->bindTo(sa); 00138 } 00139 catch (NetworkError *ex) { 00140 skt->close(); 00141 ex->pass(); 00142 } 00143 return skt; 00144 }
|
|
Definition at line 124 of file SocketAccessor.cc. References newTCPserverAt(), and Net::IPSocketAddress::thisHost(). 00125 { 00126 return newTCPserverAt(IPSocketAddress::thisHost(), port); 00127 }
|
|
Definition at line 146 of file SocketAccessor.cc. References bindTo(), close(), SocketAccessor(), and Net::IPSocketAddress::thisHostAnyPort(). 00147 { 00148 SocketAccessor *skt; 00149 IPSocketAddress *sa; 00150 00151 skt = new SocketAccessor(AF_INET, SOCK_DGRAM); 00152 try { 00153 sa = IPSocketAddress::thisHostAnyPort(); 00154 skt->bindTo(sa); 00155 } 00156 catch (NetworkError *ex) { 00157 skt->close(); 00158 ex->pass(); 00159 } 00160 return skt; 00161 }
|
|
Definition at line 181 of file SocketAccessor.cc. References close(), connectTo(), Net::IPSocketAddress::hostName(), and SocketAccessor(). 00182 { 00183 SocketAccessor *skt; 00184 IPSocketAddress *fa; 00185 00186 skt = new SocketAccessor(AF_INET, SOCK_DGRAM); 00187 try { 00188 fa = IPSocketAddress::hostName(hostName, port); 00189 skt->connectTo(fa); 00190 } 00191 catch (NetworkError *ex) { 00192 skt->close(); 00193 ex->pass(); 00194 } 00195 return skt; 00196 }
|
|
Definition at line 164 of file SocketAccessor.cc. References close(), connectTo(), Net::IPSocketAddress::hostAddress(), and SocketAccessor(). 00165 { 00166 SocketAccessor *skt; 00167 IPSocketAddress *fa; 00168 00169 skt = new SocketAccessor(AF_INET, SOCK_DGRAM); 00170 try { 00171 fa = IPSocketAddress::hostAddress(hostAddr, port); 00172 skt->connectTo(fa); 00173 } 00174 catch (NetworkError *ex) { 00175 skt->close(); 00176 ex->pass(); 00177 } 00178 return skt; 00179 }
|
|
Definition at line 203 of file SocketAccessor.cc. References bindTo(), close(), Net::IPSocketAddress::hostAddress(), and SocketAccessor(). Referenced by newUDPserverAtPort(). 00204 { 00205 SocketAccessor *skt; 00206 IPSocketAddress *sa; 00207 00208 skt = new SocketAccessor(AF_INET, SOCK_DGRAM); 00209 try { 00210 sa = IPSocketAddress::hostAddress(addr, port); 00211 skt->bindTo(sa); 00212 } 00213 catch (NetworkError *ex) { 00214 skt->close(); 00215 ex->pass(); 00216 } 00217 return skt; 00218 }
|
|
Definition at line 198 of file SocketAccessor.cc. References newUDPserverAt(), and Net::IPSocketAddress::thisHost(). 00199 { 00200 return newUDPserverAt(IPSocketAddress::thisHost(), port); 00201 }
|
|
Definition at line 257 of file SocketAccessor.cc. References descriptor, Net::IPSocketAddress::hostAddress(), and nil. 00258 { 00259 struct sockaddr_in addr; 00260 socklen_t addrLength = sizeof(addr); 00261 int retCode; 00262 00263 retCode = getpeername(descriptor, (struct sockaddr *) &addr, &addrLength); 00264 if (retCode == 0) { 00265 union { 00266 long int number; 00267 char array[4]; 00268 } converter; 00269 converter.number = addr.sin_addr.s_addr; 00270 ByteArray *baAddr = new ByteArray(converter.array, 4); 00271 return IPSocketAddress::hostAddress(baAddr, ntohs(addr.sin_port)); 00272 } 00273 return nil; 00274 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 277 of file SocketAccessor.cc. References descriptor, and Core::ByteArray::replace(). 00278 { 00279 unsigned char bytes[count + 1]; 00280 int retCode; 00281 00282 do { 00283 retCode = recv(descriptor, bytes, count, 0); 00284 } while (retCode == -1 && errno == EINTR); 00285 00286 if (retCode < 0) { 00287 (new OSError(new String(sys_errlist[errno]), __PRETTY_FUNCTION__)) 00288 ->raiseFrom(this); 00289 } 00290 00291 if (retCode > 0) { 00292 buffer->replace(startIndex, startIndex+retCode, bytes); 00293 } 00294 return retCode; 00295 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 80 of file SocketAccessor.h. 00081 { return IOAccessor::readInto(buffer); }
|
|
Reimplemented from Core::IOAccessor. Definition at line 315 of file SocketAccessor.cc. References descriptor. 00316 { 00317 fd_set rfds; 00318 int retCode; 00319 00320 /* Watch for event on socket */ 00321 FD_ZERO(&rfds); 00322 FD_SET(descriptor, &rfds); 00323 do { 00324 retCode = select(descriptor+1, &rfds, NULL, NULL, NULL); 00325 } while (retCode == -1 && errno == EINTR); 00326 00327 if (retCode < 0) 00328 (new OSError(new String(sys_errlist[errno]), __PRETTY_FUNCTION__)) 00329 ->raiseFrom(this); 00330 }
|
|
Definition at line 332 of file SocketAccessor.cc. References descriptor. 00333 { 00334 fd_set rfds; 00335 struct timeval tv; 00336 int retCode; 00337 00338 /* Watch for event on socket */ 00339 FD_ZERO(&rfds); 00340 FD_SET(descriptor, &rfds); 00341 tv.tv_sec = milliseconds / 1000; 00342 tv.tv_usec = (milliseconds % 1000) * 1000; 00343 retCode = select(descriptor+1, &rfds, NULL, NULL, &tv); 00344 00345 if (retCode < 0) { 00346 /* Report stop-by-interrupt as timeout */ 00347 if (errno == EINTR) return true; 00348 /* Otherwise raise an exception */ 00349 (new OSError(new String(sys_errlist[errno]), __PRETTY_FUNCTION__)) 00350 ->raiseFrom(this); 00351 } 00352 00353 return (retCode > 0 && FD_ISSET(descriptor, &rfds)) ? false : true; 00354 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 309 of file SocketAccessor.cc. References Core::Object::shouldNotImplement(). 00310 { 00311 shouldNotImplement(__PRETTY_FUNCTION__); 00312 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 297 of file SocketAccessor.cc. References descriptor. 00298 { 00299 int retCode = send(descriptor, &(buffer->rawBytesReadOnly())[startIndex], count, MSG_NOSIGNAL); 00300 if (retCode < 0) { 00301 /* Vyhodit nejakou chybu */ 00302 (new OSError(new String(sys_errlist[errno]), __PRETTY_FUNCTION__)) 00303 ->raiseFrom(this); 00304 } 00305 return retCode; 00306 }
|
|
Reimplemented from Core::IOAccessor. Definition at line 84 of file SocketAccessor.h. 00085 { return IOAccessor::writeFrom(buffer); }
|
|
Reimplemented from Core::IOAccessor. Definition at line 356 of file SocketAccessor.cc. References descriptor. 00357 { 00358 fd_set wfds; 00359 int retCode; 00360 00361 /* Watch for event on socket */ 00362 FD_ZERO(&wfds); 00363 FD_SET(descriptor, &wfds); 00364 do { 00365 retCode = select(descriptor+1, NULL, &wfds, NULL, NULL); 00366 } while (retCode == -1 && errno == EINTR); 00367 00368 if (retCode < 0) 00369 (new OSError(new String(sys_errlist[errno]), __PRETTY_FUNCTION__)) 00370 ->raiseFrom(this); 00371 }
|
|
Definition at line 373 of file SocketAccessor.cc. References descriptor. 00374 { 00375 fd_set wfds; 00376 struct timeval tv; 00377 int retCode; 00378 00379 /* Watch for event on socket */ 00380 FD_ZERO(&wfds); 00381 FD_SET(descriptor, &wfds); 00382 tv.tv_sec = milliseconds / 1000; 00383 tv.tv_usec = (milliseconds % 1000) * 1000; 00384 retCode = select(descriptor+1, NULL, &wfds, NULL, &tv); 00385 00386 if (retCode < 0) { 00387 /* Report stop-by-interrupt as timeout */ 00388 if (errno == EINTR) return true; 00389 /* Otherwise raise an exception */ 00390 (new OSError(new String(sys_errlist[errno]), __PRETTY_FUNCTION__)) 00391 ->raiseFrom(this); 00392 } 00393 00394 return (retCode > 0 && FD_ISSET(descriptor, &wfds)) ? false : true; 00395 }
|
|
Socket file descriptor.
Definition at line 45 of file SocketAccessor.h. Referenced by accept(), bindTo(), close(), connectTo(), listenFor(), myAddress(), peerAddress(), readInto(), readWait(), readWaitWithTimeout(), SocketAccessor(), writeFrom(), writeWait(), and writeWaitWithTimeout(). |
|
Definition at line 46 of file SocketAccessor.h. Referenced by bindTo(), connectTo(), and SocketAccessor(). |