12 #include <netinet/in.h> 13 #include <sys/socket.h> 24 #include <boost/uuid/uuid.hpp> 25 #include <boost/uuid/uuid_io.hpp> 28 #define INVALID_SOCKET -1 66 smart_socket(
int _domain,
int _type,
int _protocol,
bool _Block =
false)
68 m_Socket = ::socket(_domain, _type, _protocol);
83 return static_cast<int>(m_Socket);
88 return m_Socket = _Val;
102 int opts = fcntl(m_Socket, F_GETFL);
107 opts = _val ? (opts | O_NONBLOCK) : (opts & ~O_NONBLOCK);
109 return fcntl(m_Socket, F_SETFL, opts);
126 return ::shutdown(m_Socket, _How);
132 throw std::runtime_error(
"Socket is invalid");
135 FD_SET(m_Socket, &readset);
139 timeout.tv_sec = m_SecTimeOut;
140 timeout.tv_usec = m_USecTimeOut;
143 int retval = ::select(m_Socket + 1, &readset, NULL, NULL, &timeout);
145 throw std::runtime_error(
"Server's socket got error while calling \"select\"");
149 return FD_ISSET(m_Socket, &readset);
159 throw std::runtime_error(
"The given buffer pointer is NULL.");
161 const ssize_t bytes_read = ::recv(_Socket, &(*_Buf)[0], _Buf->capacity(), 0);
169 if (ECONNRESET == errno || ENOTCONN == errno)
182 template <
typename _T>
183 smart_socket&
operator>>(smart_socket& _Socket,
_T* _Buf);
194 throw std::runtime_error(
"The given buffer pointer is NULL.");
196 const ssize_t bytes_read = ::recv(_Socket, &(*_Buf)[0], _Buf->capacity(), 0);
199 _Buf->resize(bytes_read);
205 if (ECONNRESET == errno || ENOTCONN == errno)
210 _Buf->resize(bytes_read);
218 inline int sendall(
int s,
const unsigned char*
const buf,
int len,
int flags)
226 n = ::send(s, buf + total, len - total, flags);
231 if (EAGAIN == errno || EWOULDBLOCK == errno)
253 template <
typename _T>
254 smart_socket&
operator<<(smart_socket& _Socket,
_T& _Buf);
265 sendall(_Socket, &_Buf[0], _Buf.size(), 0);
276 copy(_Str2Send.begin(), _Str2Send.end(), back_inserter(buf));
287 throw std::invalid_argument(
"smart_socket::receive_string: Parameter is NULL");
291 *_Str2Receive = std::string(reinterpret_cast<char*>(&buf[0]), buf.size());
301 _Addr.erase(remove(_Addr.begin(), _Addr.end(),
'.'), _Addr.end());
303 return (_Addr.end() == std::find_if(_Addr.begin(), _Addr.end(), std::not_fn(
IsDigit())));
310 inline void host2ip(
const std::string& _Host, std::string* _IP)
321 hostent* he = gethostbyname(_Host.c_str());
325 *_IP = inet_ntoa(*(reinterpret_cast<in_addr*>(he->h_addr)));
332 inline void ip2host(
const std::string& _IP, std::string* _Host)
344 inet_aton(_IP.c_str(), &addr);
345 hostent* he = gethostbyaddr(&addr,
sizeof(addr), AF_INET);
363 void Bind(
unsigned short _nPort,
const std::string* _Addr = NULL)
369 addr.sin_family = AF_INET;
370 addr.sin_port = htons(_nPort);
372 addr.sin_addr.s_addr = htonl(INADDR_ANY);
374 inet_aton(_Addr->c_str(), &addr.sin_addr);
376 if (::bind(
m_Socket, reinterpret_cast<struct sockaddr*>(&addr),
sizeof(addr)) < 0)
382 if (::listen(
m_Socket, _Backlog) < 0)
388 return ::accept(
m_Socket, NULL, NULL);
419 void connect(
unsigned short _nPort,
const std::string& _Addr)
422 throw std::runtime_error(
426 addr.sin_family = AF_INET;
427 addr.sin_port = htons(_nPort);
430 inet_aton(ip.c_str(), &addr.sin_addr);
461 socklen_t size =
sizeof(sockaddr);
462 return (getsockname(_socket, reinterpret_cast<sockaddr*>(_addr), &size) == -1) ?
false :
true;
474 socklen_t size =
sizeof(sockaddr);
475 return (getpeername(_socket, reinterpret_cast<sockaddr*>(_addr), &size) == -1) ?
false :
true;
484 template <
class _Type>
493 if (!_Type()(_Socket, &addr))
497 ip2host(inet_ntoa(addr.sin_addr), &host);
499 std::stringstream ss;
500 ss << host <<
":" << ntohs(addr.sin_port);
523 std::string strSocket;
525 std::string strSocketPeer;
530 std::ostringstream ss;
533 ss << _strMsg <<
"\n";
535 ss <<
"Error on Socket<" << strSocket <<
">";
537 if (!strSocketPeer.empty())
539 ss <<
"and peer <" << strSocketPeer <<
">";
553 for (
int i = _Min; i <= _Max; ++i)
590 return (((uint64_t)htonl(val)) << 32) + htonl(val >> 32);
595 return (((uint64_t)ntohl(val)) << 32) + ntohl(val >> 32);
601 template <
typename T>
607 return ntohs(_value);
613 return ntohl(_value);
622 template <
typename T>
628 return htons(_value);
634 return htonl(_value);
648 inline void writeall(
int _handle,
const std::string& _msg)
653 std::vector<char> buf;
654 buf.reserve(_msg.size());
655 copy(_msg.begin(), _msg.end(), back_inserter(buf));
657 const size_t len = buf.size();
661 if ((n = write(_handle, &buf[total], len - total)) < 0)
smart_socket m_Socket
Definition: INet.h:450
Definition: MiscUtils.h:219
int get_free_port(int _Min, int _Max)
The function checks and returns a free port from the given range of the ports.
Definition: INet.h:550
_socket2string< SSocketPeer2String_Trait > peer2string
Socket-peer to string representation.
Definition: INet.h:515
T normalizeWrite(T _value)
uint64_t ntohll(uint64_t val)
Definition: INet.h:593
int is_read_ready(size_t m_SecTimeOut, size_t m_USecTimeOut=0)
This function indicates that socket is ready to be read (for non-blocking sockets)
Definition: INet.h:129
void setNonBlock(bool _val=true)
Definition: INet.h:440
CSocketServer()
Definition: INet.h:359
smart_socket & operator<<(smart_socket &_Socket, _T &_Buf)
This is a stream operator which helps to send data to the given socket.
Socket_t detach()
Definition: INet.h:444
A Trait class for _socket2string template. This class operates on a local side of the socket.
Definition: INet.h:457
CSocketClient()
Definition: INet.h:414
#define INVALID_SOCKET
this macro indicates an invalid status of the socket
Definition: INet.h:28
void ip2host(const std::string &_IP, std::string *_Host)
ip2host converts a given IP address to host name.
Definition: INet.h:332
int operator=(const int &_Val)
Definition: INet.h:85
void send_string(smart_socket &_Socket, const std::string &_Str2Send)
A helper function, which sends a string to the given socket.
Definition: INet.h:273
bool operator()(Socket_t _socket, sockaddr_in *_addr) const
Definition: INet.h:459
_socket2string< SSocket2String_Trait > socket2string
Socket to string representation.
Definition: INet.h:509
smart_socket()
Definition: INet.h:58
std::string socket_error_string(Socket_t _socket, const char *_strMsg=NULL)
The function returns socket's error string.
Definition: INet.h:521
void host2ip(const std::string &_Host, std::string *_IP)
host2ip converts a given host name to IP address.
Definition: INet.h:310
void connect(unsigned short _nPort, const std::string &_Addr)
Definition: INet.h:419
T normalizeRead(T _value)
Socket_t get()
Definition: INet.h:96
A Trait class for _socket2string template. This class operates a peer of the socket.
Definition: INet.h:470
int sendall(int s, const unsigned char *const buf, int len, int flags)
A helper function, which insures that whole buffer was send.
Definition: INet.h:218
int Socket_t
A basic socket type.
Definition: INet.h:44
~smart_socket()
Definition: INet.h:73
smart_socket & operator>>(smart_socket &_Socket, _T *_Buf)
This is a stream operator which helps to receive data from the given socket.
uint16_t normalizeRead< uint16_t >(uint16_t _value)
Definition: INet.h:605
Socket_t Accept()
Definition: INet.h:386
std::vector< unsigned char > BYTEVector_t
An STL vector of bytes.
Definition: def.h:124
smart_socket(int _domain, int _type, int _protocol, bool _Block=false)
Definition: INet.h:66
Socket_t detach()
Definition: INet.h:90
void close()
Definition: INet.h:111
uint64_t normalizeRead< uint64_t >(uint64_t _value)
Definition: INet.h:617
bool is_ip_address(std::string _Addr)
This function checks whether _Addr is an IP address or not.
Definition: INet.h:298
void setNonBlock(bool _val=true)
Definition: INet.h:394
uint64_t htonll(uint64_t val)
Definition: INet.h:588
bool operator()(Socket_t _socket, sockaddr_in *_addr) const
Definition: INet.h:472
int errno2str(std::string *_msg)
Retrieves a string, which represent the last error.
Definition: ErrorCode.h:49
void Bind(unsigned short _nPort, const std::string *_Addr=NULL)
Definition: INet.h:363
Socket_t detach()
Definition: INet.h:398
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:82
The system_error exception class retrieves a string, which represent the last error.
Definition: ErrorCode.h:76
bool is_valid() const
Definition: INet.h:120
A wrapper for a basic Socket.
Definition: INet.h:54
Socket_t getSocket()
Definition: INet.h:390
void receive_string(smart_socket &_Socket, std::string *_Str2Receive, size_t _BufSize)
A helper function, which receives a string from the given socket.
Definition: INet.h:284
uint32_t normalizeWrite< uint32_t >(uint32_t _value)
Definition: INet.h:632
smart_socket(int _Socket)
Definition: INet.h:62
int shutdown(int _How=SHUT_RDWR)
Definition: INet.h:124
void writeall(int _handle, const std::string &_msg)
A helper function, which insures that whole buffer was written.
Definition: INet.h:648
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:28
uint16_t normalizeWrite< uint16_t >(uint16_t _value)
Definition: INet.h:626
CSocketClient implements a simple socket client.
Definition: INet.h:411
void Listen(int _Backlog)
Definition: INet.h:380
_socket2string(Socket_t _Socket, std::string *_Str)
Definition: INet.h:487
uint32_t normalizeRead< uint32_t >(uint32_t _value)
Definition: INet.h:611
size_t read_from_socket(smart_socket &_Socket, BYTEVector_t *_Buf)
Definition: INet.h:156
Socket_t getSocket()
Definition: INet.h:436
uint64_t normalizeWrite< uint64_t >(uint64_t _value)
Definition: INet.h:638
A template class, which makes a string representation of the socket.
Definition: INet.h:485
int set_nonblock(bool _val=true)
Definition: INet.h:100
smart_socket m_Socket
Definition: INet.h:404
CSocketServer implements a simple socket server.
Definition: INet.h:356
Definition: BoostHelper.h:14