10#if __has_include("defs.h")
14#include "safe_winsock.h"
48 explicit sock (SOCKET soc);
49 explicit sock (
int type,
int domain = AF_INET,
int proto = 0);
61 virtual erc open (
int type,
int domain = AF_INET,
int proto = 0);
82 unsigned int nread ()
const;
94 int getopt (
int op,
void* buf,
int len,
int level = SOL_SOCKET)
const;
95 erc setopt (
int op,
void* buf,
int len,
int level = SOL_SOCKET)
const;
100 void debug (
bool opt)
const;
134 : handle{INVALID_SOCKET}
141 friend struct sock_initializer;
152#if !defined(SOCKBUF_BUFSIZ)
153#define SOCKBUF_BUFSIZ 1024
161 sockbuf (SOCKET soc = INVALID_SOCKET);
162 sockbuf (
int type,
int domain = AF_INET,
int proto = 0);
173 virtual int overflow (
int c = EOF)
override;
174 virtual int sync ()
override;
175 virtual std::streambuf*
setbuf (
char* buf, std::streamsize sz)
override;
176 virtual std::streamsize
showmanyc ()
override;
221 : strm (new
sockbuf (type, domain, proto)){};
231 return (
sockbuf*)std::ios::rdbuf ();
251 delete std::ios::rdbuf ();
267static struct sock_initializer
270 ~sock_initializer ();
283 return sl ? (HANDLE)sl->handle : INVALID_HANDLE_VALUE;
289 return sl && sl->handle != INVALID_SOCKET;
295 if (!sl || sl->handle == INVALID_SOCKET)
299 if (ret == SOCKET_ERROR)
308 if (!sl || sl->handle == INVALID_SOCKET)
312 int len =
sizeof (sa);
322 if (!sl || sl->handle == INVALID_SOCKET)
340 return send (buf.c_str (), buf.size () * sizeof (T), msgf);
355 return sendto (sa, buf.c_str (), buf.size () * sizeof (T), msgf);
365 if (!sl || sl->handle == INVALID_SOCKET)
369 int optlen =
sizeof (int);
370 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&oldwtmo, &optlen);
372 setsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&wp_sec, optlen);
373 return oldwtmo / 1000;
379 if (!sl || sl->handle == INVALID_SOCKET)
383 int optlen =
sizeof (int);
384 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&oldwtmo, &optlen);
385 return oldwtmo / 1000;
395 if (!sl || sl->handle == INVALID_SOCKET)
399 int optlen =
sizeof (int);
400 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&oldrtmo, &optlen);
402 setsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&wp_sec, optlen);
403 return oldrtmo / 1000;
409 if (!sl || sl->handle == INVALID_SOCKET)
413 int optlen =
sizeof (int);
414 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&oldrtmo, &optlen);
415 return oldrtmo / 1000;
429 if (!sl || sl->handle == INVALID_SOCKET)
433 if (::getsockopt (sl->handle, level, op, (
char*)buf, &rlen) == SOCKET_ERROR)
447 if (!sl || sl->handle == INVALID_SOCKET)
450 if (::setsockopt (sl->handle, level, op, (
char*)buf, len) == SOCKET_ERROR)
462 getopt (SO_TYPE, &ty,
sizeof (ty));
478 getopt (SO_ERROR, &err,
sizeof (err));
486 getopt (SO_DEBUG, &old,
sizeof (old));
487 return (old != FALSE);
494 setopt (SO_DEBUG, &opt,
sizeof (opt));
501 getopt (SO_REUSEADDR, &old,
sizeof (old));
502 return (old != FALSE);
509 setopt (SO_REUSEADDR, &opt,
sizeof (opt));
516 getopt (SO_KEEPALIVE, &old,
sizeof (old));
517 return (old != FALSE);
524 setopt (SO_KEEPALIVE, &opt,
sizeof (opt));
531 getopt (SO_DONTROUTE, &old,
sizeof (old));
532 return (old != FALSE);
539 setopt (SO_DONTROUTE, &opt,
sizeof (opt));
546 getopt (SO_BROADCAST, &old,
sizeof (old));
547 return (old != FALSE);
559 setopt (SO_BROADCAST, &opt,
sizeof (opt));
569 getopt (SO_OOBINLINE, &old,
sizeof (old));
570 return (old != FALSE);
580 setopt (SO_OOBINLINE, &opt,
sizeof (opt));
587 getopt (SO_SNDBUF, &old,
sizeof (old));
594 setopt (SO_SNDBUF, &sz,
sizeof (sz));
601 getopt (SO_RCVBUF, &old,
sizeof (old));
608 setopt (SO_RCVBUF, &sz,
sizeof (sz));
618 if (!sl || sl->handle == INVALID_SOCKET)
621 unsigned long mode = on_off ? 0 : 1;
622 if (ioctlsocket (sl->handle, FIONBIO, &mode) == SOCKET_ERROR)
643 if (!sl || sl->handle == INVALID_SOCKET)
646 if (WSAEventSelect (sl->handle, (WSAEVENT)evt, mask) == SOCKET_ERROR)
659 if (!sl || sl->handle == INVALID_SOCKET)
662 WSANETWORKEVENTS netev;
663 if (WSAEnumNetworkEvents (sl->handle, NULL, &netev) == SOCKET_ERROR)
665 return netev.lNetworkEvents;
672 opt.l_onoff = on_off;
674 setopt (SO_LINGER, &opt,
sizeof (opt));
681 getopt (SO_LINGER, &opt,
sizeof (opt));
684 return (opt.l_onoff == 0);
690 int code = WSAGetLastError ();
705 return (sl == other.sl);
objects returned as a function result or thrown directly.
Definition errorcode.h:68
static erc success
The SUCCESS indicator.
Definition errorcode.h:116
void raise() const
Definition errorcode.h:598
An error facility routes a group of errors handled in a similar manner.
Definition errorcode.h:136
An IO stream using a sockbuf object as the underlying streambuf.
Definition wsockstream.h:205
generic_sockstream(const sockbuf &sb)
Create from an existing sockbuf.
Definition wsockstream.h:212
sockbuf * operator->()
Return the buffer used by this stream.
Definition wsockstream.h:234
generic_sockstream(int type, int domain=AF_INET, int proto=0)
Create a SOCK_STREAM or SOCK_DGRAM stream.
Definition wsockstream.h:220
generic_sockstream()
Default constructor.
Definition wsockstream.h:208
~generic_sockstream()
Create a SOCK_STREAM connected to a remote peer.
Definition wsockstream.h:249
sockbuf * rdbuf()
Return the buffer used by this stream.
Definition wsockstream.h:229
generic_sockstream(const sock &s)
Create from an existing socket.
Definition wsockstream.h:216
generic_sockstream(const inaddr &remote, int type=SOCK_STREAM)
Create a SOCK_STREAM connected to a remote peer.
Definition wsockstream.h:241
sockaddr wrapper
Definition inaddr.h:23
Encapsulation of a Windows socket.
Definition wsockstream.h:27
bool is_readready(int wp_sec, int wp_usec=0) const
Check if socket is "readable".
Definition wsockstream.cpp:493
bool broadcast() const
Return "broadcast" option.
Definition wsockstream.h:543
int recvtimeout() const
Returns the send timeout value.
Definition wsockstream.h:407
bool reuseaddr() const
Return the "reuse address" flag.
Definition wsockstream.h:498
bool dontroute() const
Return status of "don't route" flag.
Definition wsockstream.h:528
long enumevents() const
Indicates which of the FD_XXX network events have occurred.
Definition wsockstream.h:657
mflags
Flags for send/receive operations.
Definition wsockstream.h:39
@ dont_route
data should not be routed
Definition wsockstream.h:43
@ peek
don't remove data from the input queue
Definition wsockstream.h:42
@ none
no flags
Definition wsockstream.h:40
@ out_of_band
send out of band data
Definition wsockstream.h:41
@ wait_all
wait until buffer full or connection closed
Definition wsockstream.h:44
int clearerror() const
Return and clear the socket error flag.
Definition wsockstream.h:475
erc listen(int num=SOMAXCONN) const
Places the socket in a state in which it is listening for incoming connections.
Definition wsockstream.h:320
void linger(bool on_off, unsigned short seconds) const
Turn on or off linger mode and lingering timeout.
Definition wsockstream.h:669
erc bind() const
Associates a local address with the socket.
Definition wsockstream.cpp:282
bool operator==(const sock &other) const
Equality comparison operator.
Definition wsockstream.h:703
erc setopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Set a socket option.
Definition wsockstream.h:445
int recvbufsz() const
Return buffer size for receive operations.
Definition wsockstream.h:598
virtual bool is_open() const
Check if socket is opened.
Definition wsockstream.h:287
erc connect(const inaddr &peer) const
Establishes a connection to specified peer.
Definition wsockstream.h:293
int getopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Returns a socket option.
Definition wsockstream.h:427
void blocking(bool on_off)
Change blocking mode.
Definition wsockstream.h:616
erc name(inaddr &addr) const
Return the local name for the object.
Definition wsockstream.cpp:232
size_t sendto(const sockaddr &sa, const void *buf, size_t len, mflags msgf=mflags::none) const
Send data to a peer.
Definition wsockstream.cpp:455
int gettype() const
Return socket type (SOCK_DGRAM or SOCK_STREAM)
Definition wsockstream.h:459
bool debug() const
Return the debug flag.
Definition wsockstream.h:483
virtual erc close()
Close socket.
Definition wsockstream.cpp:204
bool oobinline() const
Return the status of the OOB_INLINE flag.
Definition wsockstream.h:566
unsigned int nread() const
Return number of characters waiting in socket's buffer.
Definition wsockstream.cpp:570
size_t recv(void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition wsockstream.cpp:355
sock()
Default constructor creates a closed socket.
Definition wsockstream.h:276
size_t send(const void *buf, size_t len, mflags msgf=mflags::none) const
Send data to the connected peer.
Definition wsockstream.cpp:419
bool operator!=(const sock &other) const
Inequality comparison operator.
Definition wsockstream.h:713
shuthow
operation blocked by shutdown function
Definition wsockstream.h:31
@ shut_readwrite
blocked for both
Definition wsockstream.h:34
@ shut_read
blocked for reading
Definition wsockstream.h:32
@ shut_write
blocked for writing
Definition wsockstream.h:33
int sendtimeout() const
Returns the send timeout value.
Definition wsockstream.h:377
size_t recvfrom(sockaddr &sa, void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition wsockstream.cpp:387
virtual erc open(int type, int domain=AF_INET, int proto=0)
Open the socket.
Definition wsockstream.cpp:183
virtual erc shutdown(shuthow sh) const
Disables sends or receives on socket.
Definition wsockstream.cpp:586
int sendbufsz() const
Return buffer size for send operations.
Definition wsockstream.h:584
bool is_exceptionpending(int wp_sec, int wp_usec=0) const
Check if socket has OOB data or any exceptional error conditions.
Definition wsockstream.cpp:548
HANDLE handle() const
Retrieve Windows socket handle.
Definition wsockstream.h:281
static errfac & Errors()
Return error facility used by all sock-derived classes.
Definition wsockstream.cpp:38
erc accept(sock &client, inaddr *sa=nullptr) const
Permits an incoming connection attempt on the socket.
Definition wsockstream.h:306
virtual ~sock()
Destructor.
Definition wsockstream.cpp:164
erc peer(inaddr &addr) const
Retrieves the name of the peer to which the socket is connected.
Definition wsockstream.cpp:249
erc setevent(HANDLE evt, long mask) const
Associate an event object with this socket.
Definition wsockstream.h:641
static erc last_error()
Return an error code with the value returned by WSAGetLastError.
Definition wsockstream.h:688
bool is_writeready(int wp_sec, int wp_usec=0) const
Check if socket is "writable".
Definition wsockstream.cpp:524
bool keepalive() const
Return "keep alive" flag.
Definition wsockstream.h:513
sock & operator=(const sock &)
Assignment operator.
Definition wsockstream.cpp:131
Provide functions required by streambuf interface using an underlying socket.
Definition wsockstream.h:158
virtual int overflow(int c=EOF) override
If c == EOF or buffer full, return sync(); otherwise insert c into the buffer and return c.
Definition wsockstream.cpp:783
virtual ~sockbuf()
Destructor.
Definition wsockstream.cpp:676
virtual int sync() override
Return 0 if all chars flushed or -1 if error.
Definition wsockstream.cpp:695
virtual std::streamsize showmanyc() override
Return number of characters available in socket buffer.
Definition wsockstream.cpp:814
sockbuf & operator=(const sockbuf &)
Assignment operator.
Definition wsockstream.cpp:667
virtual std::streambuf * setbuf(char *buf, std::streamsize sz) override
Change buffering mode.
Definition wsockstream.cpp:717
virtual int underflow() override
underflow
Definition wsockstream.cpp:749
Definition of erc and erfac classes.
@ seconds
Degrees, minutes, seconds format (DD°MM'SS.sss")
Definition convert.h:247
Definition of inaddr class.
std::ostream & operator<<(std::ostream &strm, const mlib::inaddr &addr)
Serialize an address as '<hostname>:<port>'.
Definition inaddr.h:122