12 #include <netinet/in.h> 13 #include <sys/socket.h> 24 #pragma clang diagnostic push 25 #pragma clang diagnostic ignored "-Wdeprecated-register" 26 #include <boost/uuid/uuid.hpp> 27 #include <boost/uuid/uuid_io.hpp> 28 #pragma clang diagnostic pop 31 #define INVALID_SOCKET -1 69 smart_socket(
int _domain,
int _type,
int _protocol,
bool _Block =
false)
71 m_Socket = ::socket(_domain, _type, _protocol);
86 return static_cast<int>(m_Socket);
91 return m_Socket = _Val;
105 int opts = fcntl(m_Socket, F_GETFL);
110 opts = _val ? (opts | O_NONBLOCK) : (opts & ~O_NONBLOCK);
112 return fcntl(m_Socket, F_SETFL, opts);
129 return ::shutdown(m_Socket, _How);
135 throw std::runtime_error(
"Socket is invalid");
138 FD_SET(m_Socket, &readset);
142 timeout.tv_sec = m_SecTimeOut;
143 timeout.tv_usec = m_USecTimeOut;
146 int retval = ::select(m_Socket + 1, &readset, NULL, NULL, &timeout);
148 throw std::runtime_error(
"Server's socket got error while calling \"select\"");
152 return FD_ISSET(m_Socket, &readset);
162 throw std::runtime_error(
"The given buffer pointer is NULL.");
164 const ssize_t bytes_read = ::recv(_Socket, &(*_Buf)[0], _Buf->capacity(), 0);
172 if (ECONNRESET == errno || ENOTCONN == errno)
185 template <
typename _T>
186 smart_socket&
operator>>(smart_socket& _Socket,
_T* _Buf);
197 throw std::runtime_error(
"The given buffer pointer is NULL.");
199 const ssize_t bytes_read = ::recv(_Socket, &(*_Buf)[0], _Buf->capacity(), 0);
202 _Buf->resize(bytes_read);
208 if (ECONNRESET == errno || ENOTCONN == errno)
213 _Buf->resize(bytes_read);
221 inline int sendall(
int s,
const unsigned char*
const buf,
int len,
int flags)
229 n = ::send(s, buf + total, len - total, flags);
234 if (EAGAIN == errno || EWOULDBLOCK == errno)
256 template <
typename _T>
257 smart_socket&
operator<<(smart_socket& _Socket,
_T& _Buf);
268 sendall(_Socket, &_Buf[0], _Buf.size(), 0);
279 copy(_Str2Send.begin(), _Str2Send.end(), back_inserter(buf));
290 throw std::invalid_argument(
"smart_socket::receive_string: Parameter is NULL");
294 *_Str2Receive = std::string(reinterpret_cast<char*>(&buf[0]), buf.size());
304 _Addr.erase(remove(_Addr.begin(), _Addr.end(),
'.'), _Addr.end());
306 return (_Addr.end() == std::find_if(_Addr.begin(), _Addr.end(), std::not1(
IsDigit())));
313 inline void host2ip(
const std::string& _Host, std::string* _IP)
324 hostent* he = gethostbyname(_Host.c_str());
328 *_IP = inet_ntoa(*(reinterpret_cast<in_addr*>(he->h_addr)));
335 inline void ip2host(
const std::string& _IP, std::string* _Host)
347 inet_aton(_IP.c_str(), &addr);
348 hostent* he = gethostbyaddr(&addr,
sizeof(addr), AF_INET);
366 void Bind(
unsigned short _nPort,
const std::string* _Addr = NULL)
372 addr.sin_family = AF_INET;
373 addr.sin_port = htons(_nPort);
375 addr.sin_addr.s_addr = htonl(INADDR_ANY);
377 inet_aton(_Addr->c_str(), &addr.sin_addr);
379 if (::bind(
m_Socket, reinterpret_cast<struct sockaddr*>(&addr),
sizeof(addr)) < 0)
385 if (::listen(
m_Socket, _Backlog) < 0)
391 return ::accept(
m_Socket, NULL, NULL);
422 void connect(
unsigned short _nPort,
const std::string& _Addr)
425 throw std::runtime_error(
429 addr.sin_family = AF_INET;
430 addr.sin_port = htons(_nPort);
433 inet_aton(ip.c_str(), &addr.sin_addr);
464 socklen_t size =
sizeof(sockaddr);
465 return (getsockname(_socket, reinterpret_cast<sockaddr*>(_addr), &size) == -1) ?
false :
true;
477 socklen_t size =
sizeof(sockaddr);
478 return (getpeername(_socket, reinterpret_cast<sockaddr*>(_addr), &size) == -1) ?
false :
true;
487 template <
class _Type>
496 if (!_Type()(_Socket, &addr))
500 ip2host(inet_ntoa(addr.sin_addr), &host);
502 std::stringstream ss;
503 ss << host <<
":" << ntohs(addr.sin_port);
526 std::string strSocket;
528 std::string strSocketPeer;
533 std::ostringstream ss;
536 ss << _strMsg <<
"\n";
538 ss <<
"Error on Socket<" << strSocket <<
">";
540 if (!strSocketPeer.empty())
542 ss <<
"and peer <" << strSocketPeer <<
">";
556 for (
int i = _Min; i <= _Max; ++i)
593 return (((uint64_t)htonl(val)) << 32) + htonl(val >> 32);
598 return (((uint64_t)ntohl(val)) << 32) + ntohl(val >> 32);
604 template <
typename T>
610 return ntohs(_value);
616 return ntohl(_value);
625 template <
typename T>
631 return htons(_value);
637 return htonl(_value);
651 inline void writeall(
int _handle,
const std::string& _msg)
656 std::vector<char> buf;
657 buf.reserve(_msg.size());
658 copy(_msg.begin(), _msg.end(), back_inserter(buf));
660 const size_t len = buf.size();
664 if ((n = write(_handle, &buf[total], len - total)) < 0)
bool operator()(Socket_t _socket, sockaddr_in *_addr) const
Definition: INet.h:462
A Trait class for _socket2string template. This class operates on a local side of the socket.
Definition: INet.h:460
_socket2string< SSocket2String_Trait > socket2string
Socket to string representation.
Definition: INet.h:512
void connect(unsigned short _nPort, const std::string &_Addr)
Definition: INet.h:422
uint64_t normalizeRead< uint64_t >(uint64_t _value)
Definition: INet.h:620
void Bind(unsigned short _nPort, const std::string *_Addr=NULL)
Definition: INet.h:366
#define INVALID_SOCKET
this macro indicates an invalid status of the socket
Definition: INet.h:31
uint64_t normalizeWrite< uint64_t >(uint64_t _value)
Definition: INet.h:641
size_t read_from_socket(smart_socket &_Socket, BYTEVector_t *_Buf)
Definition: INet.h:159
T normalizeRead(T _value)
CSocketClient()
Definition: INet.h:417
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:553
int set_nonblock(bool _val=true)
Definition: INet.h:103
Socket_t getSocket()
Definition: INet.h:439
The system_error exception class retrieves a string, which represent the last error.
Definition: ErrorCode.h:77
void writeall(int _handle, const std::string &_msg)
A helper function, which insures that whole buffer was written.
Definition: INet.h:651
uint32_t normalizeRead< uint32_t >(uint32_t _value)
Definition: INet.h:614
Socket_t detach()
Definition: INet.h:401
Socket_t get()
Definition: INet.h:99
void Listen(int _Backlog)
Definition: INet.h:383
bool is_ip_address(std::string _Addr)
This function checks whether _Addr is an IP address or not.
Definition: INet.h:301
CSocketServer()
Definition: INet.h:362
smart_socket & operator<<(smart_socket &_Socket, _T &_Buf)
This is a stream operator which helps to send data to the given socket.
int operator=(const int &_Val)
Definition: INet.h:88
uint64_t ntohll(uint64_t val)
Definition: INet.h:596
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:28
A Trait class for _socket2string template. This class operates a peer of the socket.
Definition: INet.h:473
T normalizeWrite(T _value)
void close()
Definition: INet.h:114
bool is_valid() const
Definition: INet.h:123
Socket_t detach()
Definition: INet.h:447
A wrapper for a basic Socket.
Definition: INet.h:57
uint16_t normalizeWrite< uint16_t >(uint16_t _value)
Definition: INet.h:629
uint32_t normalizeWrite< uint32_t >(uint32_t _value)
Definition: INet.h:635
std::string socket_error_string(Socket_t _socket, const char *_strMsg=NULL)
The function returns socket's error string.
Definition: INet.h:524
smart_socket m_Socket
Definition: INet.h:407
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
int Socket_t
A basic socket type.
Definition: INet.h:47
smart_socket & operator>>(smart_socket &_Socket, _T *_Buf)
This is a stream operator which helps to receive data from the given socket.
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:221
smart_socket()
Definition: INet.h:61
smart_socket(int _Socket)
Definition: INet.h:65
_socket2string< SSocketPeer2String_Trait > peer2string
Socket-peer to string representation.
Definition: INet.h:518
_socket2string(Socket_t _Socket, std::string *_Str)
Definition: INet.h:490
Socket_t Accept()
Definition: INet.h:389
int errno2str(std::string *_msg)
Retrieves a string, which represent the last error.
Definition: ErrorCode.h:50
Socket_t getSocket()
Definition: INet.h:393
smart_socket m_Socket
Definition: INet.h:453
CSocketServer implements a simple socket server.
Definition: INet.h:359
Definition: MiscUtils.h:219
void host2ip(const std::string &_Host, std::string *_IP)
host2ip converts a given host name to IP address.
Definition: INet.h:313
A template class, which makes a string representation of the socket.
Definition: INet.h:488
Socket_t detach()
Definition: INet.h:93
~smart_socket()
Definition: INet.h:76
uint16_t normalizeRead< uint16_t >(uint16_t _value)
Definition: INet.h:608
std::vector< unsigned char > BYTEVector_t
An STL vector of bytes.
Definition: def.h:127
void setNonBlock(bool _val=true)
Definition: INet.h:397
void send_string(smart_socket &_Socket, const std::string &_Str2Send)
A helper function, which sends a string to the given socket.
Definition: INet.h:276
CSocketClient implements a simple socket client.
Definition: INet.h:414
void ip2host(const std::string &_IP, std::string *_Host)
ip2host converts a given IP address to host name.
Definition: INet.h:335
void setNonBlock(bool _val=true)
Definition: INet.h:443
smart_socket(int _domain, int _type, int _protocol, bool _Block=false)
Definition: INet.h:69
uint64_t htonll(uint64_t val)
Definition: INet.h:591
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:287
int shutdown(int _How=SHUT_RDWR)
Definition: INet.h:127
bool operator()(Socket_t _socket, sockaddr_in *_addr) const
Definition: INet.h:475
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:132
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21