libsocket
1.5
|
This class represent an abstract socket connection (udp | tcp server | tcp client) More...
#include <netsocket.hh>
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... | |||
![]() | |||
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="") | ||
| |||
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)
| |||||||||
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. More... | |||||||||
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)
| |||||||||
![]() | |||||||||
void | _close (int socket) const | ||||||||
Close a connnection
| |||||||||
void | _listen (int socket) const | ||||||||
Listen on port
| |||||||||
void | _write_str (int socket, const std::string &str) const | ||||||||
Write a string to a socket (when used with textual protocol)
| |||||||||
void | _write_str_bin (int socket, const std::string &str) const | ||||||||
Write a string to a socket (when used with binary protocol)
| |||||||||
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 |
![]() | |
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 |
This class represent an abstract socket connection (udp | tcp server | tcp client)
Definition at line 33 of file netsocket.hh.
|
inline |
Definition at line 36 of file netsocket.hh.
|
inline |
Definition at line 40 of file netsocket.hh.
|
inlinevirtual |
Definition at line 45 of file netsocket.hh.
|
protected |
Wait for a client
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().
|
protected |
Bind a UDP server
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().
|
protected |
Bind a TCP server
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.
|
protected |
Connect to a hostname
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().
|
protected |
internal function (construct a sockaddr)
HostnameError | when host is incorrect |
Definition at line 30 of file netsocket.cc.
References HERE.
|
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().
|
protected |
Get Client Ip.
Definition at line 243 of file netsocket.cc.
Referenced by Network::TcpSocket::get_ip().
|
protectedvirtual |
Get a line from socket (when used with textual protocol)
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().
|
protected |
Get a line from socket and store client hostname and port in port and host variable (when used with textual protocol)
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.
|
protectedpure virtual |
Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol)
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.
|
protectedpure virtual |
Get a line from socket (when used with binary protocol)
NoConnection | when there is no open socket |
ConnectionClosed | when there is no more connection. |
Implements Network::Socket.
Implemented in Network::TcpSocket, and Network::UdpSocket.
|
protected |
Write a string to a socket to a particular host (UDP) (when used with textual protocol)
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().
|
protected |
Write a string to a socket to a particular host (UDP) (when used with binary protocol)
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().
|
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().
|
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.
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.
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.
|
virtual |
read a string from socket
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.
|
virtual |
read a string with a timeout
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.
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.
size | represente the number of byte to read |
Definition at line 555 of file netsocket.cc.
References _read_line_bin(), and Network::Socket::_socket.
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)
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.
|
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.
|
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().