3#if __has_include("defs.h")
7#include "safe_winsock.h"
48 explicit sock (SOCKET soc);
49 explicit sock (
type t,
int domain = AF_INET,
int proto = 0);
59 operator SOCKET ()
const;
61 virtual erc open (
type t,
int domain = AF_INET,
int proto = 0);
62 virtual void close ();
75 void sendtimeout (std::chrono::milliseconds tmo)
const;
77 void recvtimeout (std::chrono::milliseconds tmo)
const;
79 bool is_readready (std::chrono::milliseconds tmo = std::chrono::milliseconds{0})
const;
80 bool is_writeready (std::chrono::milliseconds tmo = std::chrono::milliseconds{0})
const;
81 bool is_exceptionpending (std::chrono::milliseconds tmo = std::chrono::milliseconds{0})
const;
82 unsigned int nread ()
const;
84 erc
bind (
const inaddr&)
const;
87 erc
connect (
const inaddr&
peer, std::chrono::milliseconds tmo)
const;
89 erc
listen (
int num = SOMAXCONN)
const;
90 erc
accept (
sock& client, inaddr* sa =
nullptr)
const;
91 erc
accept (
sock& client, std::chrono::milliseconds tmo, inaddr* sa =
nullptr)
const;
92 checked<inaddr>
name ()
const;
93 checked<inaddr>
peer ()
const;
95 int getopt (
int op,
void* buf,
int len,
int level = SOL_SOCKET)
const;
96 erc
setopt (
int op,
void* buf,
int len,
int level = SOL_SOCKET)
const;
101 void debug (
bool opt)
const;
118 erc setevent (HANDLE evt,
long mask)
const;
119 long enumevents ()
const;
121 void linger (
bool on_off,
unsigned short seconds)
const;
122 bool linger (
unsigned short* seconds = 0)
const;
130 static void Errors (errfac& facitlity);
139 : handle{INVALID_SOCKET}
146 friend struct sock_initializer;
152static struct sock_initializer
155 ~sock_initializer ();
171 return sl ? (HANDLE)(std::intptr_t)sl->handle : INVALID_HANDLE_VALUE;
176sock::operator SOCKET ()
const
178 return sl ? sl->handle : INVALID_SOCKET;
185 return sl && sl->handle != INVALID_SOCKET;
192 if (!sl || sl->handle == INVALID_SOCKET)
196 if (ret == INVALID_SOCKET)
213 if (!sl || sl->handle == INVALID_SOCKET)
217 socklen_t len =
sizeof (sa);
228 if (!sl || sl->handle == INVALID_SOCKET)
246 return send (buf.c_str (), buf.size () * sizeof (T), msgf);
261 return sendto (sa, buf.c_str (), buf.size () * sizeof (T), msgf);
271 assert (sl && sl->handle != INVALID_SOCKET);
274 socklen_t optlen =
sizeof (int);
275 int par = (int)tmo.count();
277 socklen_t optlen =
sizeof(timeval);
280 setsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&par, optlen);
287 assert (sl && sl->handle != INVALID_SOCKET);
290 socklen_t optlen =
sizeof (int);
292 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&tmo, &optlen);
293 return std::chrono::milliseconds(tmo);
295 socklen_t optlen =
sizeof(timeval);
297 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&tv, &optlen);
298 return std::chrono::duration_cast<std::chrono::milliseconds>(
to_chrono (tv));
309 assert (sl && sl->handle != INVALID_SOCKET);
311 socklen_t optlen =
sizeof (int);
312 int par = (int)tmo.count();
314 socklen_t optlen =
sizeof(timeval);
317 setsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&par, optlen);
324 assert (sl && sl->handle != INVALID_SOCKET);
326 socklen_t optlen =
sizeof (int);
328 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&tmo, &optlen);
329 return std::chrono::milliseconds (tmo);
331 socklen_t optlen =
sizeof(timeval);
333 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&tv, &optlen);
334 return std::chrono::duration_cast<std::chrono::milliseconds> (
to_chrono(tv));
350 if (!sl || sl->handle == INVALID_SOCKET)
353 socklen_t rlen = len;
354 if (::getsockopt (sl->handle, level, op, (
char*)buf, &rlen) == SOCKET_ERROR)
369 if (!sl || sl->handle == INVALID_SOCKET)
372 if (::setsockopt (sl->handle, level, op, (
char*)buf, len) == SOCKET_ERROR)
385 getopt (SO_TYPE, &ty,
sizeof (ty));
402 getopt (SO_ERROR, &err,
sizeof (err));
411 getopt (SO_DEBUG, &old,
sizeof (old));
420 setopt (SO_DEBUG, &opt,
sizeof (opt));
428 getopt (SO_REUSEADDR, &old,
sizeof (old));
437 setopt (SO_REUSEADDR, &opt,
sizeof (opt));
445 getopt (SO_KEEPALIVE, &old,
sizeof (old));
454 setopt (SO_KEEPALIVE, &opt,
sizeof (opt));
462 getopt (SO_DONTROUTE, &old,
sizeof (old));
471 setopt (SO_DONTROUTE, &opt,
sizeof (opt));
479 getopt (SO_BROADCAST, &old,
sizeof (old));
493 setopt (SO_BROADCAST, &opt,
sizeof (opt));
504 getopt (SO_OOBINLINE, &old,
sizeof (old));
516 setopt (SO_OOBINLINE, &opt,
sizeof (opt));
524 getopt (SO_SNDBUF, &old,
sizeof (old));
532 setopt (SO_SNDBUF, &sz,
sizeof (sz));
540 getopt (SO_RCVBUF, &old,
sizeof (old));
548 setopt (SO_RCVBUF, &sz,
sizeof (sz));
559 if (!sl || sl->handle == INVALID_SOCKET)
562 unsigned long mode = on_off ? 0 : 1;
563 if (ioctlsocket (sl->handle, FIONBIO, &mode) == SOCKET_ERROR)
584erc sock::setevent (HANDLE evt,
long mask)
const
586 if (!sl || sl->handle == INVALID_SOCKET)
589 if (WSAEventSelect (sl->handle, (WSAEVENT)evt, mask) == SOCKET_ERROR)
601long sock::enumevents ()
const
603 if (!sl || sl->handle == INVALID_SOCKET)
604 throw erc (WSAENOTSOCK,
Errors());
606 WSANETWORKEVENTS netev;
607 if (WSAEnumNetworkEvents (sl->handle, NULL, &netev) == SOCKET_ERROR)
609 return netev.lNetworkEvents;
618 opt.l_onoff = on_off;
620 setopt (SO_LINGER, &opt,
sizeof (opt));
628 getopt (SO_LINGER, &opt,
sizeof (opt));
631 return (opt.l_onoff == 0);
639 setopt (TCP_NODELAY, &value,
sizeof (
int), IPPROTO_TCP);
647 getopt (TCP_NODELAY, &value,
sizeof (
int), IPPROTO_TCP);
656 int code = WSAGetLastError ();
675 return (sl == other.sl);
700std::ostream& operator<< (std::ostream& strm,
const mlib::sock& s)
objects returned as a function result or thrown directly.
Definition errorcode.h:73
static erc success
The SUCCESS indicator.
Definition errorcode.h:121
void raise() const
Definition errorcode.h:603
We keep a reference counter associated with each sock object because it is more expensive to duplicat...
Definition sock.h:19
bool broadcast() const
Return "broadcast" option.
Definition sock.h:476
bool reuseaddr() const
Return the "reuse address" flag.
Definition sock.h:425
bool dontroute() const
Return status of "don't route" flag.
Definition sock.h:459
checked< inaddr > peer() const
Retrieves the name of the peer to which the socket is connected.
Definition sock.cpp:244
mflags
Flags for send/receive operations.
Definition sock.h:39
@ dont_route
data should not be routed
Definition sock.h:43
@ peek
don't remove data from the input queue
Definition sock.h:42
@ none
no flags
Definition sock.h:40
@ out_of_band
send out of band data
Definition sock.h:41
@ wait_all
wait until buffer full or connection closed
Definition sock.h:44
int clearerror() const
Return and clear the socket error flag.
Definition sock.h:399
erc listen(int num=SOMAXCONN) const
Places the socket in a state in which it is listening for incoming connections.
Definition sock.h:226
void linger(bool on_off, unsigned short seconds) const
Turn on or off linger mode and lingering timeout.
Definition sock.h:615
erc bind() const
Associates a local address with the socket.
Definition sock.cpp:275
bool operator==(const sock &other) const
Equality comparison operator.
Definition sock.h:673
bool is_writeready(std::chrono::milliseconds tmo=std::chrono::milliseconds{0}) const
Check if socket is "writable".
Definition sock.cpp:519
erc setopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Set a socket option.
Definition sock.h:367
int recvbufsz() const
Return buffer size for receive operations.
Definition sock.h:537
checked< inaddr > name() const
Return the local name for the object.
Definition sock.cpp:229
virtual bool is_open() const
Check if socket is opened.
Definition sock.h:183
erc connect(const inaddr &peer) const
Establishes a connection to specified peer.
Definition sock.h:190
int getopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Returns a socket option.
Definition sock.h:348
void blocking(bool on_off)
Change blocking mode.
Definition sock.h:557
size_t sendto(const sockaddr &sa, const void *buf, size_t len, mflags msgf=mflags::none) const
Send data to a peer.
Definition sock.cpp:452
int gettype() const
Return socket type (SOCK_DGRAM or SOCK_STREAM)
Definition sock.h:382
std::chrono::milliseconds sendtimeout() const
Returns the send timeout value.
Definition sock.h:285
bool debug() const
Return the debug flag.
Definition sock.h:408
bool is_readready(std::chrono::milliseconds tmo=std::chrono::milliseconds{0}) const
Check if socket is "readable".
Definition sock.cpp:490
bool oobinline() const
Return the status of the OOB_INLINE flag.
Definition sock.h:501
unsigned int nread() const
Return number of characters waiting in socket's buffer.
Definition sock.cpp:563
size_t recv(void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition sock.cpp:352
sock()
Default constructor creates a closed socket.
Definition sock.h:163
size_t send(const void *buf, size_t len, mflags msgf=mflags::none) const
Send data to the connected peer.
Definition sock.cpp:416
bool operator!=(const sock &other) const
Inequality comparison operator.
Definition sock.h:684
type
socket types
Definition sock.h:23
shuthow
operation blocked by shutdown function
Definition sock.h:31
@ shut_readwrite
blocked for both
Definition sock.h:34
@ shut_read
blocked for reading
Definition sock.h:32
@ shut_write
blocked for writing
Definition sock.h:33
size_t recvfrom(sockaddr &sa, void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition sock.cpp:384
bool connected() const
Check is socket is connected.
Definition sock.h:204
virtual erc shutdown(shuthow sh) const
Disables sends or receives on socket.
Definition sock.cpp:579
int sendbufsz() const
Return buffer size for send operations.
Definition sock.h:521
HANDLE handle() const
Retrieve Windows socket handle.
Definition sock.h:169
static errfac & Errors()
Return error facility used by all sock-derived classes.
Definition sock.cpp:36
erc accept(sock &client, inaddr *sa=nullptr) const
Permits an incoming connection attempt on the socket.
Definition sock.h:211
virtual ~sock()
Destructor.
Definition sock.cpp:162
bool is_exceptionpending(std::chrono::milliseconds tmo=std::chrono::milliseconds{0}) const
Check if socket has OOB data or any exceptional error conditions.
Definition sock.cpp:542
static erc last_error()
Return an error code with the value returned by WSAGetLastError.
Definition sock.h:653
bool keepalive() const
Return "keep alive" flag.
Definition sock.h:442
virtual void close()
Close socket.
Definition sock.cpp:200
virtual erc open(type t, int domain=AF_INET, int proto=0)
Open the socket.
Definition sock.cpp:185
std::chrono::milliseconds recvtimeout() const
Returns the send timeout value.
Definition sock.h:322
bool nodelay() const
Return status of TCP_NODELAY option.
Definition sock.h:644
sock & operator=(const sock &)
Assignment operator.
Definition sock.cpp:129
@ seconds
Degrees, minutes, seconds format (DD°MM'SS.sss")
Definition convert.h:249
Definition of mlib::erc and mlib::errfac classes.
sock::mflags operator|(sock::mflags f1, sock::mflags f2)
Bitwise OR operator for send message flags.
Definition sock.h:691
std::chrono::microseconds to_chrono(const timeval &tv)
Conversion to a chrono duration in microseconds.
Definition tvops.h:200
timeval from_chrono(const T &dur)
Conversion from a chrono duration.
Definition tvops.h:190
Definition of mlib::inaddr class.
Operations on timeval structure.