Go to the documentation of this file.
31 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
32 # define LIBSOCKET_WIN
38 # include <sys/types.h>
39 # include <sys/time.h>
43 # define SENDTO_FLAGS 0
45 # if defined(__APPLE__) && defined(__MACH__)
46 # define SENDTO_FLAGS 0
48 # define SENDTO_FLAGS MSG_NOSIGNAL
50 # include <sys/socket.h>
51 # include <netinet/in.h>
52 # include <arpa/inet.h>
58 # include <gnutls/gnutls.h>
93 static const int MAXPKTSIZE = 65507;
95 static const char DEFAULT_DELIM =
'\0';
110 void write(
const std::string& str);
119 void add_delim(
const std::string& delim);
121 void del_delim(
const std::string& delim);
129 unsigned size = 1024,
130 const std::string &certfile =
"",
131 const std::string &keyfile =
"",
132 const std::string &trustfile =
"",
133 const std::string &crlfile =
"");
140 virtual std::string
read() = 0;
142 virtual std::string
read(
int timeout) = 0;
145 virtual std::string
readn(
unsigned int size) = 0;
148 virtual std::string
readn(
int timeout,
unsigned int size) = 0;
154 void _close(
int socket)
const;
158 void _listen(
int socket)
const;
162 virtual std::string
_read_line(
int socket) = 0;
166 virtual std::string
_read_line_bin(
int socket,
unsigned int size) = 0;
170 void _write_str(
int socket,
const std::string& str)
const;
180 void _set_timeout(
bool enable,
int socket,
int timeout);
182 std::pair<int, int>
_find_delim(
const std::string& str,
int start)
const;
185 bool _update_buffer(std::pair<int, int> &delim,
int &i, std::string &str);
197 struct sockaddr_in6 _addr6;
205 gnutls_session _session;
206 gnutls_certificate_credentials _x509_cred;
enum Network::e_gnutls_kind GnuTLSKind
void _listen(int socket) const
Listen on port.
void enable_tls()
Enable TLS on socket.
Network namespace represent all networks connection.
void init_tls(GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="")
std::list< std::string > _delim
virtual std::string readn(unsigned int size)=0
read a string from socket
void _set_timeout(bool enable, int socket, int timeout)
set a timeout on a socket
void _write_str_bin(int socket, const std::string &str) const
Write a string to a socket (when used with binary protocol)
void _write_str(int socket, const std::string &str) const
Write a string to a socket (when used with textual protocol)
virtual std::string read()=0
function used by >> operator (read a string on current socket)
void write(const std::string &str)
function used by << operator (write a string on current socket)
virtual std::string _read_line(int socket)=0
Get a line from socket (when used with textual protocol)
enum Network::e_pkind PROTO_KIND
void allow_empty_lines()
, if set, empty lines will be returned in text procols (if not, they are skipped)
bool _check_answer(int res, std::string &str)
return the content of the buffer is there is
bool _update_buffer(std::pair< int, int > &delim, int &i, std::string &str)
look delimiter and remove delimiter at begining of buffer if needed
bool connected() const
return true when socket is connected
enum Network::e_version SOCKET_VERSION
std::pair< int, int > _find_delim(const std::string &str, int start) const
This class represent an abstract socket connection (udp | tcp server | tcp client)
virtual std::string _read_line_bin(int socket, unsigned int size)=0
Get a line from socket (when used with binary protocol)
int get_socket()
get socket (fd) warning: be very carefull with this method
void add_delim(const std::string &delim)
set the delimitor for the text mode
Socket & operator<<(Socket &s, const std::string &str)
write a string on current socket
enum Network::e_kind SOCKET_KIND
Socket & operator>>(Socket &s, std::string &str)
read a string on current socket
void _close(int socket) const
Close a connnection.
Socket(SOCKET_KIND kind, SOCKET_VERSION version=V4)
void del_delim(const std::string &delim)
delete this delimitor for the socket