#include <netsocket.hh>
Inheritance diagram for Network::NetSocket:
Public Member Functions | |
NetSocket (SOCKET_KIND kind, SOCKET_VERSION version=V4) | |
NetSocket (SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version=V4) | |
virtual | ~NetSocket () |
virtual void | writeto (const std::string &str, const std::string &host, int port) |
function used to send a msg to a specific host (UDP) | |
std::string | read () |
function used by >> operator (read a string on current socket) | |
std::string | read (int timeout) |
read a string with a timeout | |
std::string | read (int &port, std::string &host) |
Get a line from socket and store client hostname and port in port and host variable. | |
std::string | read (int &port, std::string &host, int timeout) |
Get a line from socket and store client hostname and port in port and host variable (with a timeout on listen). | |
std::string | readn (unsigned int size) |
read a string from socket | |
std::string | readn (int timeout, unsigned int size) |
read a string with a timeout | |
std::string | readn (int &port, std::string &host, unsigned int size) |
Get a line from socket and store client hostname and port in port and host variable. | |
std::string | readn (int &port, std::string &host, int timeout, unsigned int size) |
Get a line from socket and store client hostname and port in port and host variable (with a timeout on listen). | |
Protected Member Functions | |
sockaddr_in | _get_addr (int port) const |
internal function (construct a sockaddr) | |
sockaddr_in | _get_addr (const std::string &host, int port) const |
internal function (construct a sockaddr) | |
int | _bind (int port, const std::string &host) |
Bind a UDP server. | |
int | _bind (int port) |
Bind a TCP server. | |
int | _accept (int port, int server_socket) const |
Wait for a client. | |
std::string | _get_ip (int port, int socket) const |
Get Client Ip. | |
void | _connect (int socket, int port, const std::string &host) const |
Connect to a hostname. | |
std::string | _read_line (int socket) |
Get a line from socket (when used with textual protocol). | |
std::string | _read_line (int socket, int &port, std::string &host) |
Get a line from socket and store client hostname and port in port and host variable (when used with textual protocol). | |
virtual std::string | _read_line_bin (int socket, int &port, std::string &host, unsigned int pkg_size)=0 |
Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol). | |
virtual std::string | _read_line_bin (int socket, unsigned int size)=0 |
Get a line from socket (when used with binary protocol). | |
void | _write_str (int socket, const std::string &str, const std::string &host, int port) const |
Write a string to a socket to a particular host (UDP) (when used with textual protocol). | |
void | _write_str_bin (int socket, const std::string &str, const std::string &host, int port) const |
Write a string to a socket to a particular host (UDP) (when used with binary protocol). | |
Protected Attributes | |
int | _port |
Definition at line 33 of file netsocket.hh.
|
Definition at line 36 of file netsocket.hh. References _port. 00036 : 00037 Socket(kind, version), _port(0) 00038 { 00039 } NetSocket(SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version = V4) :
|
|
Definition at line 40 of file netsocket.hh. References _port.
|
|
Definition at line 45 of file netsocket.hh. 00045 {}
|
|
Wait for a client.
Definition at line 213 of file netsocket.cc. References _get_addr(), HERE, and Network::V4. 00214 { 00215 #ifdef LIBSOCKET_WIN 00216 int size; 00217 #else 00218 socklen_t size; 00219 #endif 00220 int s; 00221 struct sockaddr_in addr; 00222 #ifdef IPV6_ENABLED 00223 struct sockaddr_in6 addr6; 00224 00225 if (_version == V4) 00226 { 00227 #endif 00228 addr = _get_addr(port); 00229 size = sizeof(addr); 00230 s = accept(socket, (struct sockaddr*)&addr, &size); 00231 #ifdef IPV6_ENABLED 00232 } 00233 else 00234 { 00235 addr6 = _get_addr6(port); 00236 size = sizeof(addr6); 00237 s = accept(socket, (struct sockaddr*)&addr6, &size); 00238 } 00239 #endif 00240 if (s < 0) 00241 throw AcceptError("Accept Error", HERE); 00242 return s; 00243 }
|
|
Bind a TCP server.
Definition at line 128 of file netsocket.cc. References _get_addr(), HERE, Network::TCP, Network::UDP, and Network::V4. 00129 { 00130 int s, on; 00131 00132 if (_kind == TCP) 00133 { 00134 #ifdef IPV6_ENABLED 00135 if (_version == V4) 00136 #endif 00137 s = socket(PF_INET, SOCK_STREAM, 0); 00138 #ifdef IPV6_ENABLED 00139 else 00140 s = socket(PF_INET6, SOCK_STREAM, 0); 00141 #endif 00142 } 00143 else if (_kind == UDP) 00144 { 00145 #ifdef IPV6_ENABLED 00146 if (_version == V4) 00147 #endif 00148 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 00149 #ifdef IPV6_ENABLED 00150 else 00151 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); 00152 #endif 00153 } 00154 else 00155 throw Exception("Unknown Protocole", HERE); 00156 if (s < 0) 00157 throw SocketError("Socket error", HERE); 00158 on = 1; 00159 00160 #ifndef LIBSOCKET_WIN 00161 if (_kind == TCP && setsockopt(s, SOL_SOCKET, 00162 SO_REUSEADDR, (void *)&on, 00163 sizeof (on)) == -1) 00164 throw SetsockoptError("setsockopt error", HERE); 00165 #endif 00166 00167 #ifdef IPV6_ENABLED 00168 if (_version == V4) 00169 { 00170 #endif 00171 struct sockaddr_in addr; 00172 addr = _get_addr(port); 00173 if (bind(s,(struct sockaddr*)&addr, (int)sizeof(addr)) == -1) 00174 throw BindError("Bind error", HERE); 00175 #ifdef IPV6_ENABLED 00176 } 00177 else 00178 { 00179 struct sockaddr_in6 addr6; 00180 addr6 = _get_addr6(port); 00181 if (bind(s,(struct sockaddr*)&addr6, (int)sizeof(addr6)) == -1) 00182 throw BindError("Bind error", HERE); 00183 } 00184 #endif 00185 return s; 00186 }
|
|
Bind a UDP server.
Definition at line 86 of file netsocket.cc. References HERE, Network::TCP, Network::UDP, and Network::V4. Referenced by Network::UdpSocket::connect(), and Network::TcpSocket::connect(). 00087 { 00088 int s; 00089 00090 if (_kind == UDP) 00091 { 00092 #ifdef IPV6_ENABLED 00093 if (_version == V4) 00094 #endif 00095 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 00096 #ifdef IPV6_ENABLED 00097 else 00098 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); 00099 #endif 00100 } 00101 else if (_kind == TCP) 00102 { 00103 #ifdef IPV6_ENABLED 00104 if (_version == V4) 00105 #endif 00106 s = socket(PF_INET, SOCK_STREAM, 0); 00107 #ifdef IPV6_ENABLED 00108 else 00109 s = socket(PF_INET6, SOCK_STREAM, 0); 00110 #endif 00111 } 00112 else 00113 throw Exception("Unknown Protocole", HERE); 00114 00115 if (s < 0) 00116 throw SocketError("Socket error", HERE); 00117 #ifdef IPV6_ENABLED 00118 if (_version == V4) 00119 #endif 00120 _addr = _get_addr(host, port); 00121 #ifdef IPV6_ENABLED 00122 else 00123 _addr6 = _get_addr6(host, port); 00124 #endif 00125 return s; 00126 }
|
|
Connect to a hostname.
Definition at line 188 of file netsocket.cc. References _get_addr(), HERE, and Network::V4. Referenced by Network::TcpSocket::connect(). 00190 { 00191 #ifdef IPV6_ENABLED 00192 if (_version == V4) 00193 { 00194 #endif 00195 struct sockaddr_in addr; 00196 addr = _get_addr(host, port); 00197 if (connect(socket, (struct sockaddr *)&addr, 00198 sizeof (addr)) < 0) 00199 throw ConnectError("Unable to connect", HERE); 00200 #ifdef IPV6_ENABLED 00201 } 00202 else 00203 { 00204 struct sockaddr_in6 addr6; 00205 addr6 = _get_addr6(host, port); 00206 if (connect(socket, (struct sockaddr *)&addr6, 00207 sizeof (addr6)) < 0) 00208 throw ConnectError("Unable to connect", HERE); 00209 } 00210 #endif 00211 }
|
|
internal function (construct a sockaddr)
Definition at line 30 of file netsocket.cc. References HERE. 00032 { 00033 struct hostent *he; 00034 struct sockaddr_in addr; 00035 00036 memset(&addr, 0, sizeof(struct sockaddr_in)); 00037 he = gethostbyname(host.c_str()); 00038 if (!he) 00039 throw HostnameError("Unknown Hostname", HERE); 00040 addr.sin_addr = *((struct in_addr *)he->h_addr); 00041 addr.sin_port = htons(port); 00042 addr.sin_family = AF_INET; 00043 return addr; 00044 }
|
|
internal function (construct a sockaddr)
Definition at line 61 of file netsocket.cc. Referenced by _accept(), _bind(), _connect(), _write_str(), and _write_str_bin(). 00062 { 00063 struct sockaddr_in addr; 00064 00065 memset(&addr, 0, sizeof(struct sockaddr_in)); 00066 addr.sin_addr.s_addr = htonl(INADDR_ANY); 00067 addr.sin_port = htons(port); 00068 addr.sin_family = AF_INET; 00069 return addr; 00070 }
|
|
Get Client Ip.
Definition at line 245 of file netsocket.cc. Referenced by Network::TcpSocket::get_ip(). 00246 { 00247 struct sockaddr_in addr; 00248 #ifdef LIBSOCKET_WIN 00249 int size; 00250 #else 00251 socklen_t size; 00252 #endif 00253 00254 memset(&addr, '\0', sizeof(addr)); 00255 addr.sin_family = AF_INET; 00256 addr.sin_addr.s_addr = htonl(INADDR_ANY); 00257 addr.sin_port = htons(port); 00258 size = sizeof(addr); 00259 getpeername(socket, (struct sockaddr *)&addr, &size); 00260 return(std::string(inet_ntoa(addr.sin_addr))); 00261 }
|
|
Get a line from socket and store client hostname and port in port and host variable (when used with textual protocol).
Definition at line 302 of file netsocket.cc. References Network::Socket::_set_timeout(), Network::Socket::_update_buffer(), HERE, Network::UDP, and Network::V4. 00304 { 00305 char chr[MAXPKTSIZE]; 00306 std::string str = ""; 00307 int res = 1, i; 00308 std::pair<int, int> delim; 00309 struct sockaddr_in addr; 00310 #ifdef IPV6_ENABLED 00311 struct sockaddr_in6 addr6; 00312 #endif 00313 #ifdef LIBSOCKET_WIN 00314 int size; 00315 #else 00316 socklen_t size; 00317 #endif 00318 bool end = false; 00319 00320 #ifdef IPV6_ENABLED 00321 if (V4 == _version) 00322 #endif 00323 size = sizeof(addr); 00324 #ifdef IPV6_ENABLED 00325 else 00326 size = sizeof(addr6); 00327 #endif 00328 if (socket < 0) 00329 throw NoConnection("No Socket", HERE); 00330 if (!_update_buffer(delim, i, str)) 00331 while (!end) 00332 { 00333 if (_state_timeout) 00334 _set_timeout(true, _socket, _state_timeout); 00335 if (_kind == UDP) 00336 { 00337 #ifdef LIBSOCKET_WIN 00338 int flags = 0; 00339 #else 00340 int flags = MSG_TRUNC; 00341 #endif 00342 00343 #ifdef IPV6_ENABLED 00344 if (V4 == _version) 00345 #endif 00346 res = recvfrom(socket, chr, MAXPKTSIZE, flags, 00347 (struct sockaddr *) &addr, &size); 00348 #ifdef IPV6_ENABLED 00349 else 00350 res = recvfrom(socket, chr, MAXPKTSIZE, flags, 00351 (struct sockaddr *) &addr6, &size); 00352 #endif 00353 } 00354 else 00355 { 00356 #ifdef TLS 00357 if (_tls) 00358 res = gnutls_record_recv(_session, chr, MAXPKTSIZE); 00359 else 00360 #endif 00361 res = recvfrom(socket, chr, MAXPKTSIZE, 0, NULL, 0); 00362 #ifdef IPV6_ENABLED 00363 if (V4 == _version) 00364 { 00365 #endif 00366 if (getpeername(socket, (struct sockaddr *) &addr, &size) < 0) 00367 throw GetpeernameError("getpeername error", HERE); 00368 #ifdef IPV6_ENABLED 00369 } 00370 else 00371 if (getpeername(socket, (struct sockaddr *) &addr6, &size) < 0) 00372 throw GetpeernameError("getpeername error", HERE); 00373 #endif 00374 } 00375 if (_check_answer(res, str)) 00376 return str; 00377 _buffer += std::string(chr, res); 00378 if (_update_buffer(delim, i, str)) 00379 end = true; 00380 } 00381 #ifdef IPV6_ENABLED 00382 if (V4 == _version) 00383 { 00384 #endif 00385 host = std::string(inet_ntoa(addr.sin_addr)); 00386 port = ntohs(addr.sin_port); 00387 #ifdef IPV6_ENABLED 00388 } 00389 else 00390 { 00391 char buf[INET6_ADDRSTRLEN]; 00392 if (inet_ntop(AF_INET6, &addr6.sin6_addr, buf, INET6_ADDRSTRLEN) == 0) 00393 throw InetntopError("Not a valid address", HERE); 00394 host = std::string(buf); 00395 port = ntohs(addr6.sin6_port); 00396 } 00397 #endif 00398 _state_timeout = 0; 00399 return str; 00400 }
|
|
Get a line from socket (when used with textual protocol).
Implements Network::Socket. Definition at line 263 of file netsocket.cc. References Network::Socket::_set_timeout(), Network::Socket::_update_buffer(), HERE, and Network::UDP. Referenced by read(). 00264 { 00265 char chr[MAXPKTSIZE]; 00266 std::string str = ""; 00267 int res = 1, i; 00268 std::pair<int, int> delim; 00269 bool end = false; 00270 00271 if (socket < 0) 00272 throw NoConnection("No Socket", HERE); 00273 if (!_update_buffer(delim, i, str)) 00274 while (!end) 00275 { 00276 memset(chr, 0, MAXPKTSIZE); 00277 if (_state_timeout) 00278 _set_timeout(true, _socket, _state_timeout); 00279 if (_kind == UDP) 00280 #ifdef LIBSOCKET_WIN 00281 res = recv(socket, chr, MAXPKTSIZE, 0); 00282 #else 00283 res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC); 00284 #endif 00285 else 00286 #ifdef TLS 00287 if (_tls) 00288 res = gnutls_record_recv(_session, chr, MAXPKTSIZE); 00289 else 00290 #endif 00291 res = recv(socket, chr, MAXPKTSIZE, 0); 00292 if (_check_answer(res, str)) 00293 return str; 00294 _buffer += std::string(chr, res); 00295 if (_update_buffer(delim, i, str)) 00296 end = true; 00297 } 00298 _state_timeout = 0; 00299 return str; 00300 }
|
|
Get a line from socket (when used with binary protocol).
Implements Network::Socket. Implemented in Network::TcpSocket, and Network::UdpSocket. |
|
Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol).
Implemented in Network::TcpSocket, and Network::UdpSocket. |
|
Write a string to a socket to a particular host (UDP) (when used with textual protocol).
Definition at line 402 of file netsocket.cc. References _get_addr(), HERE, SENDTO_FLAGS, and Network::V4. Referenced by writeto(). 00404 { 00405 struct sockaddr_in addr; 00406 #ifdef IPV6_ENABLED 00407 struct sockaddr_in6 addr6; 00408 #endif 00409 int res = 1; 00410 const char *buf = str.c_str(); 00411 unsigned int count = 0; 00412 00413 #ifdef IPV6_ENABLED 00414 if (V4 == _version) 00415 #endif 00416 addr = _get_addr(host, port); 00417 #ifdef IPV6_ENABLED 00418 else 00419 addr6 = _get_addr6(host, port); 00420 #endif 00421 if (socket < 0) 00422 throw NoConnection("No Socket", HERE); 00423 while (res && count < str.size()) 00424 { 00425 #ifdef IPV6_ENABLED 00426 if (V4 == _version) 00427 #endif 00428 #ifdef TLS 00429 if (_tls) 00430 res = gnutls_record_send(_session, buf + count, str.size() - count); 00431 else 00432 #endif 00433 res = sendto(socket, buf + count, 00434 str.size() - count, SENDTO_FLAGS, 00435 (const struct sockaddr*)&addr, sizeof(_addr)); 00436 #ifdef IPV6_ENABLED 00437 else 00438 res = sendto(socket, buf + count, 00439 str.size() - count, SENDTO_FLAGS, 00440 (const struct sockaddr*)&addr6, sizeof(_addr6)); 00441 #endif 00442 if (res <= 0) 00443 throw ConnectionClosed("Connection Closed", HERE); 00444 count += res; 00445 } 00446 }
|
|
Write a string to a socket to a particular host (UDP) (when used with binary protocol).
Definition at line 448 of file netsocket.cc. References _get_addr(), HERE, SENDTO_FLAGS, and Network::V4. Referenced by writeto(). 00450 { 00451 struct sockaddr_in addr; 00452 #ifdef IPV6_ENABLED 00453 struct sockaddr_in6 addr6; 00454 #endif 00455 int res = 1; 00456 unsigned int count = 0; 00457 #ifdef LIBSOCKET_WIN 00458 char* buf = new char[str.size() + 2]; 00459 #else 00460 char buf[str.size() + 2]; 00461 #endif 00462 00463 buf[0] = str.size() / 256; 00464 buf[1] = str.size() % 256; 00465 memcpy(buf + 2, str.c_str(), str.size()); 00466 #ifdef IPV6_ENABLED 00467 if (V4 == _version) 00468 #endif 00469 addr = _get_addr(host, port); 00470 #ifdef IPV6_ENABLED 00471 else 00472 addr6 = _get_addr6(host, port); 00473 #endif 00474 if (socket < 0) 00475 throw NoConnection("No Socket", HERE); 00476 while (res && count < str.size() + 2) 00477 { 00478 #ifdef IPV6_ENABLED 00479 if (V4 == _version) 00480 #endif 00481 #ifdef TLS 00482 if (_tls) 00483 res = gnutls_record_send(_session, buf + count, str.size() + 2 - count); 00484 else 00485 #endif 00486 res = sendto(socket, buf + count, str.size() + 2 - count, 00487 SENDTO_FLAGS, 00488 (const struct sockaddr*)&addr, sizeof(_addr)); 00489 #ifdef IPV6_ENABLED 00490 else 00491 res = sendto(socket, buf + count, str.size() + 2 - count, 00492 SENDTO_FLAGS, 00493 (const struct sockaddr*)&addr6, sizeof(_addr6)); 00494 #endif 00495 if (res <= 0) 00496 throw ConnectionClosed("Connection Closed", HERE); 00497 count += res; 00498 } 00499 #ifdef LIBSOCKET_WIN 00500 delete[] buf; 00501 #endif 00502 }
|
|
Get a line from socket and store client hostname and port in port and host variable (with a timeout on listen).
Definition at line 521 of file netsocket.cc. References _read_line(), _read_line_bin(), Network::Socket::_set_timeout(), and Network::binary. 00522 { 00523 if (_proto_kind == binary) 00524 { 00525 _set_timeout(true, _socket, timeout); 00526 return _read_line_bin(_socket, port, host, 0); 00527 } 00528 else 00529 { 00530 _state_timeout = timeout; 00531 return _read_line(_socket, port, host); 00532 } 00533 }
|
|
Get a line from socket and store client hostname and port in port and host variable.
Definition at line 513 of file netsocket.cc. References _read_line(), _read_line_bin(), and Network::binary. 00514 {
00515 if (_proto_kind == binary)
00516 return _read_line_bin(_socket, port, host, 0);
00517 else
00518 return _read_line(_socket, port, host);
00519 }
|
|
read a string with a timeout
Implements Network::Socket. Definition at line 543 of file netsocket.cc. References _read_line(), _read_line_bin(), Network::Socket::_set_timeout(), and Network::binary. 00544 { 00545 if (_proto_kind == binary) 00546 { 00547 _set_timeout(true, _socket, timeout); 00548 return _read_line_bin(_socket, 0); 00549 } 00550 else 00551 { 00552 _state_timeout = timeout; 00553 return _read_line(_socket); 00554 } 00555 }
|
|
function used by >> operator (read a string on current socket)
Implements Network::Socket. Definition at line 535 of file netsocket.cc. References _read_line(), _read_line_bin(), and Network::binary. 00536 {
00537 if (_proto_kind == binary)
00538 return _read_line_bin(_socket, 0);
00539 else
00540 return _read_line(_socket);
00541 }
|
|
Get a line from socket and store client hostname and port in port and host variable (with a timeout on listen).
Definition at line 566 of file netsocket.cc. References _read_line_bin(), and Network::Socket::_set_timeout(). 00568 { 00569 if (!size || size > _buffer.size()) 00570 _set_timeout(true, _socket, timeout); 00571 // _read_line_bin is bufferised with the same buffer as textual 00572 // protocols, so this function can be used for binary and text 00573 // protocols. 00574 return _read_line_bin(_socket, port, host, size); 00575 }
|
|
Get a line from socket and store client hostname and port in port and host variable.
Definition at line 557 of file netsocket.cc. References _read_line_bin(). 00559 { 00560 // _read_line_bin is bufferised with the same buffer as textual 00561 // protocols, so this function can be used for binary and text 00562 // protocols. 00563 return _read_line_bin(_socket, port, host, size); 00564 }
|
|
read a string with a timeout
Implements Network::Socket. Definition at line 585 of file netsocket.cc. References _read_line_bin(), and Network::Socket::_set_timeout(). 00586 { 00587 if (!size || size > _buffer.size()) 00588 _set_timeout(true, _socket, timeout); 00589 // _read_line_bin is bufferised with the same buffer as textual 00590 // protocols, so this function can be used for binary and text 00591 // protocols. 00592 return _read_line_bin(_socket, size); 00593 }
|
|
read a string from socket
Implements Network::Socket. Definition at line 577 of file netsocket.cc. References _read_line_bin(). 00578 { 00579 // _read_line_bin is bufferised with the same buffer as textual 00580 // protocols, so this function can be used for binary and text 00581 // protocols. 00582 return _read_line_bin(_socket, size); 00583 }
|
|
function used to send a msg to a specific host (UDP)
Definition at line 504 of file netsocket.cc. References _write_str(), _write_str_bin(), and Network::binary. 00506 {
00507 if (_proto_kind == binary)
00508 _write_str_bin(_socket, str, host, port);
00509 else
00510 _write_str(_socket, str, host, port);
00511 }
|
|
Definition at line 161 of file netsocket.hh. Referenced by NetSocket(). |