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;
152struct sock_initializer
155 ~sock_initializer ();
172 return sl ? (HANDLE)(std::intptr_t)sl->handle : INVALID_HANDLE_VALUE;
177sock::operator SOCKET ()
const
179 return sl ? sl->handle : INVALID_SOCKET;
186 return sl && sl->handle != INVALID_SOCKET;
193 if (!sl || sl->handle == INVALID_SOCKET)
197 if (ret == INVALID_SOCKET)
214 if (!sl || sl->handle == INVALID_SOCKET)
218 socklen_t len =
sizeof (sa);
229 if (!sl || sl->handle == INVALID_SOCKET)
247 return send (buf.c_str (), buf.size () * sizeof (T), msgf);
262 return sendto (sa, buf.c_str (), buf.size () * sizeof (T), msgf);
272 assert (sl && sl->handle != INVALID_SOCKET);
275 socklen_t optlen =
sizeof (int);
276 int par = (int)tmo.count();
278 socklen_t optlen =
sizeof(timeval);
281 setsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&par, optlen);
288 assert (sl && sl->handle != INVALID_SOCKET);
291 socklen_t optlen =
sizeof (int);
293 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&tmo, &optlen);
294 return std::chrono::milliseconds(tmo);
296 socklen_t optlen =
sizeof(timeval);
298 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&tv, &optlen);
299 return std::chrono::duration_cast<std::chrono::milliseconds>(
to_chrono (tv));
310 assert (sl && sl->handle != INVALID_SOCKET);
312 socklen_t optlen =
sizeof (int);
313 int par = (int)tmo.count();
315 socklen_t optlen =
sizeof(timeval);
318 setsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&par, optlen);
325 assert (sl && sl->handle != INVALID_SOCKET);
327 socklen_t optlen =
sizeof (int);
329 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&tmo, &optlen);
330 return std::chrono::milliseconds (tmo);
332 socklen_t optlen =
sizeof(timeval);
334 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&tv, &optlen);
335 return std::chrono::duration_cast<std::chrono::milliseconds> (
to_chrono(tv));
351 if (!sl || sl->handle == INVALID_SOCKET)
354 socklen_t rlen = len;
355 if (::getsockopt (sl->handle, level, op, (
char*)buf, &rlen) == SOCKET_ERROR)
370 if (!sl || sl->handle == INVALID_SOCKET)
373 if (::setsockopt (sl->handle, level, op, (
char*)buf, len) == SOCKET_ERROR)
386 getopt (SO_TYPE, &ty,
sizeof (ty));
403 getopt (SO_ERROR, &err,
sizeof (err));
412 getopt (SO_DEBUG, &old,
sizeof (old));
421 setopt (SO_DEBUG, &opt,
sizeof (opt));
429 getopt (SO_REUSEADDR, &old,
sizeof (old));
438 setopt (SO_REUSEADDR, &opt,
sizeof (opt));
446 getopt (SO_KEEPALIVE, &old,
sizeof (old));
455 setopt (SO_KEEPALIVE, &opt,
sizeof (opt));
463 getopt (SO_DONTROUTE, &old,
sizeof (old));
472 setopt (SO_DONTROUTE, &opt,
sizeof (opt));
480 getopt (SO_BROADCAST, &old,
sizeof (old));
494 setopt (SO_BROADCAST, &opt,
sizeof (opt));
505 getopt (SO_OOBINLINE, &old,
sizeof (old));
517 setopt (SO_OOBINLINE, &opt,
sizeof (opt));
525 getopt (SO_SNDBUF, &old,
sizeof (old));
533 setopt (SO_SNDBUF, &sz,
sizeof (sz));
541 getopt (SO_RCVBUF, &old,
sizeof (old));
549 setopt (SO_RCVBUF, &sz,
sizeof (sz));
560 if (!sl || sl->handle == INVALID_SOCKET)
563 unsigned long mode = on_off ? 0 : 1;
564 if (ioctlsocket (sl->handle, FIONBIO, &mode) == SOCKET_ERROR)
585erc sock::setevent (HANDLE evt,
long mask)
const
587 if (!sl || sl->handle == INVALID_SOCKET)
590 if (WSAEventSelect (sl->handle, (WSAEVENT)evt, mask) == SOCKET_ERROR)
602long sock::enumevents ()
const
604 if (!sl || sl->handle == INVALID_SOCKET)
605 throw erc (WSAENOTSOCK,
Errors());
607 WSANETWORKEVENTS netev;
608 if (WSAEnumNetworkEvents (sl->handle, NULL, &netev) == SOCKET_ERROR)
610 return netev.lNetworkEvents;
619 opt.l_onoff = on_off;
621 setopt (SO_LINGER, &opt,
sizeof (opt));
629 getopt (SO_LINGER, &opt,
sizeof (opt));
632 return (opt.l_onoff == 0);
640 setopt (TCP_NODELAY, &value,
sizeof (
int), IPPROTO_TCP);
648 getopt (TCP_NODELAY, &value,
sizeof (
int), IPPROTO_TCP);
657 int code = WSAGetLastError ();
676 return (sl == other.sl);
701std::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:477
bool reuseaddr() const
Return the "reuse address" flag.
Definition sock.h:426
bool dontroute() const
Return status of "don't route" flag.
Definition sock.h:460
checked< inaddr > peer() const
Retrieves the name of the peer to which the socket is connected.
Definition sock.cpp:229
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:400
erc listen(int num=SOMAXCONN) const
Places the socket in a state in which it is listening for incoming connections.
Definition sock.h:227
void linger(bool on_off, unsigned short seconds) const
Turn on or off linger mode and lingering timeout.
Definition sock.h:616
erc bind() const
Associates a local address with the socket.
Definition sock.cpp:261
bool operator==(const sock &other) const
Equality comparison operator.
Definition sock.h:674
bool is_writeready(std::chrono::milliseconds tmo=std::chrono::milliseconds{0}) const
Check if socket is "writable".
Definition sock.cpp:505
erc setopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Set a socket option.
Definition sock.h:368
int recvbufsz() const
Return buffer size for receive operations.
Definition sock.h:538
checked< inaddr > name() const
Return the local name for the object.
Definition sock.cpp:214
virtual bool is_open() const
Check if socket is opened.
Definition sock.h:184
erc connect(const inaddr &peer) const
Establishes a connection to specified peer.
Definition sock.h:191
int getopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Returns a socket option.
Definition sock.h:349
void blocking(bool on_off)
Change blocking mode.
Definition sock.h:558
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:438
int gettype() const
Return socket type (SOCK_DGRAM or SOCK_STREAM)
Definition sock.h:383
std::chrono::milliseconds sendtimeout() const
Returns the send timeout value.
Definition sock.h:286
bool debug() const
Return the debug flag.
Definition sock.h:409
bool is_readready(std::chrono::milliseconds tmo=std::chrono::milliseconds{0}) const
Check if socket is "readable".
Definition sock.cpp:476
bool oobinline() const
Return the status of the OOB_INLINE flag.
Definition sock.h:502
unsigned int nread() const
Return number of characters waiting in socket's buffer.
Definition sock.cpp:549
size_t recv(void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition sock.cpp:338
sock()
Default constructor creates a closed socket.
Definition sock.h:164
size_t send(const void *buf, size_t len, mflags msgf=mflags::none) const
Send data to the connected peer.
Definition sock.cpp:402
bool operator!=(const sock &other) const
Inequality comparison operator.
Definition sock.h:685
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:370
bool connected() const
Check is socket is connected.
Definition sock.h:205
virtual erc shutdown(shuthow sh) const
Disables sends or receives on socket.
Definition sock.cpp:565
int sendbufsz() const
Return buffer size for send operations.
Definition sock.h:522
HANDLE handle() const
Retrieve Windows socket handle.
Definition sock.h:170
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:212
virtual ~sock()
Destructor.
Definition sock.cpp:159
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:528
static erc last_error()
Return an error code with the value returned by WSAGetLastError.
Definition sock.h:654
bool keepalive() const
Return "keep alive" flag.
Definition sock.h:443
virtual void close()
Close socket.
Definition sock.cpp:184
virtual erc open(type t, int domain=AF_INET, int proto=0)
Open the socket.
Definition sock.cpp:170
std::chrono::milliseconds recvtimeout() const
Returns the send timeout value.
Definition sock.h:323
bool nodelay() const
Return status of TCP_NODELAY option.
Definition sock.h:645
sock & operator=(const sock &)
Assignment operator.
Definition sock.cpp:126
@ seconds
Degrees, minutes, seconds format (DD°MM'SS.sss")
Definition convert.h:249
Definition of mlib::erc and mlib::errfac classes.
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.