Network::NetSocket Class Referenceabstract

This class represent an abstract socket connection (udp | tcp server | tcp client) More...

`#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) More...
std::string | read ()
| function used by >> operator (read a string on current socket) More...
std::string | read (int timeout)
| read a string with a timeout More...
std::string | read (int &port, std::string &host)
| Get a line from socket and store client hostname and port in port and host variable. More...
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) More...
std::string | readn (unsigned int size)
| read a string from socket More...
std::string | readn (int timeout, unsigned int size)
| read a string with a timeout More...
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. More...
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) More...
Public Member Functions inherited from Network::Socket
| Socket (SOCKET_KIND kind, SOCKET_VERSION version=V4)
| Socket (SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version=V4)
virtual | ~Socket ()
void | write (const std::string &str)
| function used by << operator (write a string on current socket) More...
bool | connected () const
| return true when socket is connected More...
int | get_socket ()
| get socket (fd) warning: be very carefull with this method More...
void | add_delim (const std::string &delim)
| set the delimitor for the text mode More...
void | del_delim (const std::string &delim)
| delete this delimitor for the socket More...
void | allow_empty_lines ()
| , if set, empty lines will be returned in text procols (if not, they are skipped) More...
void | init_tls (GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="")

Exceptions

  TLSSupportError | when TLS is not enabled

More...

void | enable_tls ()
| Enable TLS on socket. More...

## Protected Member Functions

struct sockaddr_in | _get_addr (int port) const
| internal function (construct a sockaddr) More...
struct sockaddr_in | _get_addr (const std::string &host, int port) const

internal function (construct a sockaddr)

Exceptions

  HostnameError | when host is incorrect

More...

int | _bind (int port, const std::string &host)

Bind a UDP server

Exceptions

  SocketError | when socket libc function return a negative value
  Exception | if the selected protocole is incorrect (is you receive this exception, please submit a bug report)

More...

int | _bind (int port)

Bind a TCP server

Exceptions

  SocketError | when socket libc function return a negative value
  SetsockoptError | when setsockopt libc function return a negative value
  BindError | when bind libc function return a negative value
  Exception | if the selected protocole is incorrect (is you receive this exception, please submit a bug report)

More...

int | _accept (int port, int server_socket) const

Wait for a client

Exceptions

  AcceptError | when accept libc function return a negative value.

More...

std::string | _get_ip (int port, int socket) const
| Get Client Ip. More...
void | _connect (int socket, int port, const std::string &host) const

Connect to a hostname

Exceptions

  ConnectError | when connect libc function return a negative value.

More...

std::string | _read_line (int socket)

Get a line from socket (when used with textual protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

More...

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)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection
  GetpeernameError | when getpeername libc function return a negative value.

More...

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)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection
  GetpeernameError | when getpeername libc function return a negative value.

More...

virtual std::string | _read_line_bin (int socket, unsigned int size)=0

Get a line from socket (when used with binary protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

More...

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)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

More...

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)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

More...

Protected Member Functions inherited from Network::Socket
void | _close (int socket) const

Close a connnection

Exceptions

  CloseException | when close libc function return a negative value.

More...

void | _listen (int socket) const

Listen on port

Exceptions

  ListenException | when listen libc function return a negative value.

More...

void | _write_str (int socket, const std::string &str) const

Write a string to a socket (when used with textual protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

More...

void | _write_str_bin (int socket, const std::string &str) const

Write a string to a socket (when used with binary protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

More...

void | _set_timeout (bool enable, int socket, int timeout)
| set a timeout on a socket More...
std::pair< int, int > | _find_delim (const std::string &str, int start) const
bool | _update_buffer (std::pair< int, int > &delim, int &i, std::string &str)
| look delimiter and remove delimiter at begining of buffer if needed More...
bool | _check_answer (int res, std::string &str)
| return the content of the buffer is there is More...

## Protected Attributes

int | _port
Protected Attributes inherited from Network::Socket
SOCKET_KIND | _kind
SOCKET_VERSION | _version
unsigned | _state_timeout
int | _socket
int | _recv_flags
struct sockaddr_in | _addr
PROTO_KIND | _proto_kind
std::list< std::string > | _delim
bool | _empty_lines
std::string | _buffer
bool | _tls

## Detailed Description

This class represent an abstract socket connection (udp | tcp server | tcp client)

Author
  Julien Lemoine <speedblue at="" happycoders="" dot="" org>="">

Definition at line 33 of file netsocket.hh.

## Constructor & Destructor Documentation

Network::NetSocket::NetSocket | ( | SOCKET_KIND | kind,
|  | SOCKET_VERSION | version = `V4`
| ) |  |

inline

Definition at line 36 of file netsocket.hh.

:
Socket(kind, version), _port(0)
{
}

Network::NetSocket::NetSocket | ( | SOCKET_KIND | kind,
|  | PROTO_KIND | pkind,
|  | SOCKET_VERSION | version = `V4`
| ) |  |

inline

Definition at line 40 of file netsocket.hh.

:
Socket(kind, pkind, version), _port(0)
{
}

virtual Network::NetSocket::~NetSocket | ( | ) |  |

inline virtual

Definition at line 45 of file netsocket.hh.

{}

## Member Function Documentation

int Network::NetSocket::_accept | ( | int | port,
|  | int | server_socket
| ) |  | const

protected

Wait for a client

Exceptions

  AcceptError | when accept libc function return a negative value.

Definition at line 211 of file netsocket.cc.

References _get_addr(), Network::Socket::_version, HERE, and Network::V4.

Referenced by Network::TcpSocket::accept().

{
#ifdef LIBSOCKET_WIN
int size;
#else
socklen_t size;
#endif
int s;
struct sockaddr_in addr;
#ifdef IPV6_ENABLED
struct sockaddr_in6 addr6;
if (_version == V4)
{
#endif
addr = _get_addr(port);
size = sizeof(addr);
s = accept(socket, (struct sockaddr*)&addr, &size);
#ifdef IPV6_ENABLED
}
else
{
addr6 = _get_addr6(port);
size = sizeof(addr6);
s = accept(socket, (struct sockaddr*)&addr6, &size);
}
#endif
if (s < 0)
throw AcceptError("Accept Error", HERE);
return s;
}

int Network::NetSocket::_bind | ( | int | port,
|  | const std::string & | host
| ) |  |

protected

Bind a UDP server

Exceptions

  SocketError | when socket libc function return a negative value
  Exception | if the selected protocole is incorrect (is you receive this exception, please submit a bug report)

Definition at line 86 of file netsocket.cc.

References Network::Socket::_addr, _get_addr(), Network::Socket::_kind, Network::Socket::_version, HERE, Network::TCP, Network::UDP, and Network::V4.

Referenced by Network::UdpSocket::connect(), and Network::TcpSocket::connect().

{
int s;
if (_kind == UDP)
{
#ifdef IPV6_ENABLED
if (_version == V4)
#endif
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
#ifdef IPV6_ENABLED
else
s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
#endif
}
else if (_kind == TCP)
{
#ifdef IPV6_ENABLED
if (_version == V4)
#endif
s = socket(PF_INET, SOCK_STREAM, 0);
#ifdef IPV6_ENABLED
else
s = socket(PF_INET6, SOCK_STREAM, 0);
#endif
}
else
throw Exception("Unknown Protocole", HERE);
if (s < 0)
throw SocketError("Socket error", HERE);
#ifdef IPV6_ENABLED
if (_version == V4)
#endif
_addr = _get_addr(host, port);
#ifdef IPV6_ENABLED
else
_addr6 = _get_addr6(host, port);
#endif
return s;
}

int Network::NetSocket::_bind | ( | int | port) |  |

protected

Bind a TCP server

Exceptions

  SocketError | when socket libc function return a negative value
  SetsockoptError | when setsockopt libc function return a negative value
  BindError | when bind libc function return a negative value
  Exception | if the selected protocole is incorrect (is you receive this exception, please submit a bug report)

Definition at line 128 of file netsocket.cc.

References _get_addr(), Network::Socket::_kind, Network::Socket::_version, HERE, Network::TCP, Network::UDP, and Network::V4.

{
int s, on;
if (_kind == TCP)
{
#ifdef IPV6_ENABLED
if (_version == V4)
#endif
s = socket(PF_INET, SOCK_STREAM, 0);
#ifdef IPV6_ENABLED
else
s = socket(PF_INET6, SOCK_STREAM, 0);
#endif
}
else if (_kind == UDP)
{
#ifdef IPV6_ENABLED
if (_version == V4)
#endif
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
#ifdef IPV6_ENABLED
else
s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
#endif
}
else
throw Exception("Unknown Protocole", HERE);
if (s < 0)
throw SocketError("Socket error", HERE);
on = 1;
if (_kind == TCP && setsockopt(s, SOL_SOCKET,
SO_REUSEADDR, (void *)&on,
sizeof (on)) == -1)
throw SetsockoptError("setsockopt error", HERE);
#ifdef IPV6_ENABLED
if (_version == V4)
{
#endif
struct sockaddr_in addr;
addr = _get_addr(port);
if (bind(s,(struct sockaddr*)&addr, (int)sizeof(addr)) == -1)
throw BindError("Bind error", HERE);
#ifdef IPV6_ENABLED
}
else
{
struct sockaddr_in6 addr6;
addr6 = _get_addr6(port);
if (bind(s,(struct sockaddr*)&addr6, (int)sizeof(addr6)) == -1)
throw BindError("Bind error", HERE);
}
#endif
return s;
}

void Network::NetSocket::_connect | ( | int | socket,
|  | int | port,
|  | const std::string & | host
| ) |  | const

protected

Connect to a hostname

Exceptions

  ConnectError | when connect libc function return a negative value.

Definition at line 186 of file netsocket.cc.

References _get_addr(), Network::Socket::_version, HERE, and Network::V4.

Referenced by Network::TcpSocket::connect().

{
#ifdef IPV6_ENABLED
if (_version == V4)
{
#endif
struct sockaddr_in addr;
addr = _get_addr(host, port);
if (connect(socket, (struct sockaddr *)&addr,
sizeof (addr)) < 0)
throw ConnectError("Unable to connect", HERE);
#ifdef IPV6_ENABLED
}
else
{
struct sockaddr_in6 addr6;
addr6 = _get_addr6(host, port);
if (connect(socket, (struct sockaddr *)&addr6,
sizeof (addr6)) < 0)
throw ConnectError("Unable to connect", HERE);
}
#endif
}

struct sockaddr_in Network::NetSocket::_get_addr | ( | const std::string & | host,
|  | int | port
| ) |  | const

protected

internal function (construct a sockaddr)

Exceptions

  HostnameError | when host is incorrect

Definition at line 30 of file netsocket.cc.

References HERE.

{
struct hostent *he;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
he = gethostbyname(host.c_str());
if (!he)
throw HostnameError("Unknown Hostname", HERE);
addr.sin_addr = *((struct in_addr *)he->h_addr);
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
return addr;
}

struct sockaddr_in Network::NetSocket::_get_addr | ( | int | port) |  | const

protected

internal function (construct a sockaddr)

Definition at line 61 of file netsocket.cc.

Referenced by _accept(), _bind(), _connect(), _write_str(), and _write_str_bin().

{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
return addr;
}

std::string Network::NetSocket::_get_ip | ( | int | port,
|  | int | socket
| ) |  | const

protected

Get Client Ip.

Definition at line 243 of file netsocket.cc.

Referenced by Network::TcpSocket::get_ip().

{
struct sockaddr_in addr;
#ifdef LIBSOCKET_WIN
int size;
#else
socklen_t size;
#endif
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
size = sizeof(addr);
getpeername(socket, (struct sockaddr *)&addr, &size);
return(std::string(inet_ntoa(addr.sin_addr)));
}

std::string Network::NetSocket::_read_line | ( | int | socket) |  |

protected virtual

Get a line from socket (when used with textual protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

Implements Network::Socket.

Definition at line 261 of file netsocket.cc.

References Network::Socket::_buffer, Network::Socket::_check_answer(), Network::Socket::_kind, Network::Socket::_set_timeout(), Network::Socket::_socket, Network::Socket::_state_timeout, Network::Socket::_tls, Network::Socket::_update_buffer(), HERE, and Network::UDP.

Referenced by read().

{
char chr[MAXPKTSIZE];
std::string str = "";
int res = 1, i;
std::pair<int, int> delim;
bool end = false;
if (socket < 0)
throw NoConnection("No Socket", HERE);
if (!_update_buffer(delim, i, str))
while (!end)
{
memset(chr, 0, MAXPKTSIZE);
if (_state_timeout)
_set_timeout(true, _socket, _state_timeout);
if (_kind == UDP)
#ifdef LIBSOCKET_WIN
res = recv(socket, chr, MAXPKTSIZE, 0);
#else
res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
#endif
else
#ifdef TLS
if (_tls)
res = gnutls_record_recv(_session, chr, MAXPKTSIZE);
else
#endif
res = recv(socket, chr, MAXPKTSIZE, 0);
if (_check_answer(res, str))
return str;
_buffer += std::string(chr, res);
if (_update_buffer(delim, i, str))
end = true;
}
_state_timeout = 0;
return str;
}

std::string Network::NetSocket::_read_line | ( | int | socket,
|  | int & | port,
|  | std::string & | host
| ) |  |

protected

Get a line from socket and store client hostname and port in port and host variable (when used with textual protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection
  GetpeernameError | when getpeername libc function return a negative value.

Definition at line 300 of file netsocket.cc.

References Network::Socket::_buffer, Network::Socket::_check_answer(), Network::Socket::_kind, Network::Socket::_set_timeout(), Network::Socket::_socket, Network::Socket::_state_timeout, Network::Socket::_tls, Network::Socket::_update_buffer(), Network::Socket::_version, HERE, Network::UDP, and Network::V4.

{
char chr[MAXPKTSIZE];
std::string str = "";
int res = 1, i;
std::pair<int, int> delim;
struct sockaddr_in addr;
#ifdef IPV6_ENABLED
struct sockaddr_in6 addr6;
#endif
#ifdef LIBSOCKET_WIN
int size;
#else
socklen_t size;
#endif
bool end = false;
#ifdef IPV6_ENABLED
if (V4 == _version)
#endif
size = sizeof(addr);
#ifdef IPV6_ENABLED
else
size = sizeof(addr6);
#endif
if (socket < 0)
throw NoConnection("No Socket", HERE);
if (!_update_buffer(delim, i, str))
while (!end)
{
if (_state_timeout)
_set_timeout(true, _socket, _state_timeout);
if (_kind == UDP)
{
#ifdef LIBSOCKET_WIN
int flags = 0;
#else
int flags = MSG_TRUNC;
#endif
#ifdef IPV6_ENABLED
if (V4 == _version)
#endif
res = recvfrom(socket, chr, MAXPKTSIZE, flags,
(struct sockaddr *) &addr, &size);
#ifdef IPV6_ENABLED
else
res = recvfrom(socket, chr, MAXPKTSIZE, flags,
(struct sockaddr *) &addr6, &size);
#endif
}
else
{
#ifdef TLS
if (_tls)
res = gnutls_record_recv(_session, chr, MAXPKTSIZE);
else
#endif
res = recvfrom(socket, chr, MAXPKTSIZE, 0, NULL, 0);
#ifdef IPV6_ENABLED
if (V4 == _version)
{
#endif
if (getpeername(socket, (struct sockaddr *) &addr, &size) < 0)
throw GetpeernameError("getpeername error", HERE);
#ifdef IPV6_ENABLED
}
else
if (getpeername(socket, (struct sockaddr *) &addr6, &size) < 0)
throw GetpeernameError("getpeername error", HERE);
#endif
}
if (_check_answer(res, str))
return str;
_buffer += std::string(chr, res);
if (_update_buffer(delim, i, str))
end = true;
}
#ifdef IPV6_ENABLED
if (V4 == _version)
{
#endif
host = std::string(inet_ntoa(addr.sin_addr));
port = ntohs(addr.sin_port);
#ifdef IPV6_ENABLED
}
else
{
char buf[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &addr6.sin6_addr, buf, INET6_ADDRSTRLEN) == 0)
throw InetntopError("Not a valid address", HERE);
host = std::string(buf);
port = ntohs(addr6.sin6_port);
}
#endif
_state_timeout = 0;
return str;
}

virtual std::string Network::NetSocket::_read_line_bin | ( | int | socket,
|  | int & | port,
|  | std::string & | host,
|  | unsigned int | pkg_size
| ) |  |

protected pure virtual

Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection
  GetpeernameError | when getpeername libc function return a negative value.

Implemented in Network::TcpSocket, and Network::UdpSocket.

Referenced by read(), and readn().

virtual std::string Network::NetSocket::_read_line_bin | ( | int | socket,
|  | unsigned int | size
| ) |  |

protected pure virtual

Get a line from socket (when used with binary protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

Implements Network::Socket.

Implemented in Network::TcpSocket, and Network::UdpSocket.

void Network::NetSocket::_write_str | ( | int | socket,
|  | const std::string & | str,
|  | const std::string & | host,
|  | int | port
| ) |  | const

protected

Write a string to a socket to a particular host (UDP) (when used with textual protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

Definition at line 400 of file netsocket.cc.

References Network::Socket::_addr, _get_addr(), Network::Socket::_tls, Network::Socket::_version, HERE, SENDTO_FLAGS, and Network::V4.

Referenced by writeto().

{
struct sockaddr_in addr;
#ifdef IPV6_ENABLED
struct sockaddr_in6 addr6;
#endif
int res = 1;
const char *buf = str.c_str();
unsigned int count = 0;
#ifdef IPV6_ENABLED
if (V4 == _version)
#endif
addr = _get_addr(host, port);
#ifdef IPV6_ENABLED
else
addr6 = _get_addr6(host, port);
#endif
if (socket < 0)
throw NoConnection("No Socket", HERE);
while (res && count < str.size())
{
#ifdef IPV6_ENABLED
if (V4 == _version)
#endif
#ifdef TLS
if (_tls)
res = gnutls_record_send(_session, buf + count, str.size() - count);
else
#endif
res = sendto(socket, buf + count,
str.size() - count, SENDTO_FLAGS,
(const struct sockaddr*)&addr, sizeof(_addr));
#ifdef IPV6_ENABLED
else
res = sendto(socket, buf + count,
str.size() - count, SENDTO_FLAGS,
(const struct sockaddr*)&addr6, sizeof(_addr6));
#endif
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
count += res;
}
}

void Network::NetSocket::_write_str_bin | ( | int | socket,
|  | const std::string & | str,
|  | const std::string & | host,
|  | int | port
| ) |  | const

protected

Write a string to a socket to a particular host (UDP) (when used with binary protocol)

Exceptions

  NoConnection | when there is no open socket
  ConnectionClosed | when there is no more connection.

Definition at line 446 of file netsocket.cc.

References Network::Socket::_addr, _get_addr(), Network::Socket::_tls, Network::Socket::_version, HERE, SENDTO_FLAGS, and Network::V4.

Referenced by writeto().

{
struct sockaddr_in addr;
#ifdef IPV6_ENABLED
struct sockaddr_in6 addr6;
#endif
int res = 1;
unsigned int count = 0;
#ifdef LIBSOCKET_WIN
char* buf = new char[str.size() + 2];
#else
char buf[str.size() + 2];
#endif
buf[0] = str.size() / 256;
buf[1] = str.size() % 256;
memcpy(buf + 2, str.c_str(), str.size());
#ifdef IPV6_ENABLED
if (V4 == _version)
#endif
addr = _get_addr(host, port);
#ifdef IPV6_ENABLED
else
addr6 = _get_addr6(host, port);
#endif
if (socket < 0)
throw NoConnection("No Socket", HERE);
while (res && count < str.size() + 2)
{
#ifdef IPV6_ENABLED
if (V4 == _version)
#endif
#ifdef TLS
if (_tls)
res = gnutls_record_send(_session, buf + count, str.size() + 2 - count);
else
#endif
res = sendto(socket, buf + count, str.size() + 2 - count,
SENDTO_FLAGS,
(const struct sockaddr*)&addr, sizeof(_addr));
#ifdef IPV6_ENABLED
else
res = sendto(socket, buf + count, str.size() + 2 - count,
SENDTO_FLAGS,
(const struct sockaddr*)&addr6, sizeof(_addr6));
#endif
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
count += res;
}
#ifdef LIBSOCKET_WIN
delete[] buf;
#endif
}

std::string Network::NetSocket::read | ( | ) |  |

virtual

function used by >> operator (read a string on current socket)

Implements Network::Socket.

Definition at line 533 of file netsocket.cc.

References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_socket, and Network::binary.

Referenced by Network::TcpSocket::_read_line_bin().

{
if (_proto_kind == binary)
return _read_line_bin(_socket, 0);
else
return _read_line(_socket);
}

std::string Network::NetSocket::read | ( | int | timeout) |  |

virtual

read a string with a timeout

Implements Network::Socket.

Definition at line 541 of file netsocket.cc.

References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_set_timeout(), Network::Socket::_socket, Network::Socket::_state_timeout, and Network::binary.

{
if (_proto_kind == binary)
{
_set_timeout(true, _socket, timeout);
return _read_line_bin(_socket, 0);
}
else
{
_state_timeout = timeout;
return _read_line(_socket);
}
}

std::string Network::NetSocket::read | ( | int & | port,
|  | std::string & | host
| ) |  |

Get a line from socket and store client hostname and port in port and host variable.

Definition at line 511 of file netsocket.cc.

References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_socket, and Network::binary.

{
if (_proto_kind == binary)
return _read_line_bin(_socket, port, host, 0);
else
return _read_line(_socket, port, host);
}

std::string Network::NetSocket::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)

Definition at line 519 of file netsocket.cc.

References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_set_timeout(), Network::Socket::_socket, Network::Socket::_state_timeout, and Network::binary.

{
if (_proto_kind == binary)
{
_set_timeout(true, _socket, timeout);
return _read_line_bin(_socket, port, host, 0);
}
else
{
_state_timeout = timeout;
return _read_line(_socket, port, host);
}
}

std::string Network::NetSocket::readn | ( | unsigned int | size) |  |

virtual

read a string from socket

Parameters

  size | represente the number of byte to read

Implements Network::Socket.

Definition at line 575 of file netsocket.cc.

References _read_line_bin(), and Network::Socket::_socket.

{
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, size);
}

std::string Network::NetSocket::readn | ( | int | timeout,
|  | unsigned int | size
| ) |  |

virtual

read a string with a timeout

Parameters

  size | represente the number of byte to read

Implements Network::Socket.

Definition at line 583 of file netsocket.cc.

References Network::Socket::_buffer, _read_line_bin(), Network::Socket::_set_timeout(), and Network::Socket::_socket.

{
if (!size || size > _buffer.size())
_set_timeout(true, _socket, timeout);
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, size);
}

std::string Network::NetSocket::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.

Parameters

  size | represente the number of byte to read

Definition at line 555 of file netsocket.cc.

References _read_line_bin(), and Network::Socket::_socket.

{
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, port, host, size);
}

std::string Network::NetSocket::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)

Parameters

  size | represente the number of byte to read

Definition at line 564 of file netsocket.cc.

References Network::Socket::_buffer, _read_line_bin(), Network::Socket::_set_timeout(), and Network::Socket::_socket.

{
if (!size || size > _buffer.size())
_set_timeout(true, _socket, timeout);
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, port, host, size);
}

void Network::NetSocket::writeto | ( | const std::string & | str,
|  | const std::string & | host,
|  | int | port
| ) |  |

virtual

function used to send a msg to a specific host (UDP)

Definition at line 502 of file netsocket.cc.

References Network::Socket::_proto_kind, Network::Socket::_socket, _write_str(), _write_str_bin(), and Network::binary.

{
if (_proto_kind == binary)
_write_str_bin(_socket, str, host, port);
else
_write_str(_socket, str, host, port);
}

## Member Data Documentation

int Network::NetSocket::_port

protected

Definition at line 161 of file netsocket.hh.

Referenced by Network::TcpSocket::accept(), Network::UdpSocket::connect(), Network::TcpSocket::connect(), and Network::TcpSocket::get_ip().

The documentation for this class was generated from the following files:

- netsocket.hh
- netsocket.cc