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);
60 operator SOCKET ()
const;
62 virtual erc open (
int type,
int domain = AF_INET,
int proto = 0);
83 unsigned int nread ()
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;
135 : handle{INVALID_SOCKET}
142 friend struct sock_initializer;
153#if !defined(SOCKBUF_BUFSIZ)
154#define SOCKBUF_BUFSIZ 1024
162 sockbuf (SOCKET soc = INVALID_SOCKET);
163 sockbuf (
int type,
int domain = AF_INET,
int proto = 0);
174 virtual int overflow (
int c = EOF)
override;
175 virtual int sync ()
override;
176 virtual std::streambuf*
setbuf (
char* buf, std::streamsize sz)
override;
177 virtual std::streamsize
showmanyc ()
override;
222 : strm (new
sockbuf (type, domain, proto)){};
232 return (
sockbuf*)std::ios::rdbuf ();
252 delete std::ios::rdbuf ();
268static struct sock_initializer
271 ~sock_initializer ();
284 return sl ? (HANDLE)sl->handle : INVALID_HANDLE_VALUE;
288inline sock::operator SOCKET ()
const
290 return sl ? sl->handle : INVALID_SOCKET;
296 return sl && sl->handle != INVALID_SOCKET;
302 if (!sl || sl->handle == INVALID_SOCKET)
306 if (ret == SOCKET_ERROR)
315 if (!sl || sl->handle == INVALID_SOCKET)
319 int len =
sizeof (sa);
329 if (!sl || sl->handle == INVALID_SOCKET)
347 return send (buf.c_str (), buf.size () * sizeof (T), msgf);
362 return sendto (sa, buf.c_str (), buf.size () * sizeof (T), msgf);
372 if (!sl || sl->handle == INVALID_SOCKET)
376 int optlen =
sizeof (int);
377 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&oldwtmo, &optlen);
379 setsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&wp_sec, optlen);
380 return oldwtmo / 1000;
386 if (!sl || sl->handle == INVALID_SOCKET)
390 int optlen =
sizeof (int);
391 getsockopt (sl->handle, SOL_SOCKET, SO_SNDTIMEO, (
char*)&oldwtmo, &optlen);
392 return oldwtmo / 1000;
402 if (!sl || sl->handle == INVALID_SOCKET)
406 int optlen =
sizeof (int);
407 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&oldrtmo, &optlen);
409 setsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&wp_sec, optlen);
410 return oldrtmo / 1000;
416 if (!sl || sl->handle == INVALID_SOCKET)
420 int optlen =
sizeof (int);
421 getsockopt (sl->handle, SOL_SOCKET, SO_RCVTIMEO, (
char*)&oldrtmo, &optlen);
422 return oldrtmo / 1000;
436 if (!sl || sl->handle == INVALID_SOCKET)
440 if (::getsockopt (sl->handle, level, op, (
char*)buf, &rlen) == SOCKET_ERROR)
454 if (!sl || sl->handle == INVALID_SOCKET)
457 if (::setsockopt (sl->handle, level, op, (
char*)buf, len) == SOCKET_ERROR)
469 getopt (SO_TYPE, &ty,
sizeof (ty));
485 getopt (SO_ERROR, &err,
sizeof (err));
493 getopt (SO_DEBUG, &old,
sizeof (old));
494 return (old != FALSE);
501 setopt (SO_DEBUG, &opt,
sizeof (opt));
508 getopt (SO_REUSEADDR, &old,
sizeof (old));
509 return (old != FALSE);
516 setopt (SO_REUSEADDR, &opt,
sizeof (opt));
523 getopt (SO_KEEPALIVE, &old,
sizeof (old));
524 return (old != FALSE);
531 setopt (SO_KEEPALIVE, &opt,
sizeof (opt));
538 getopt (SO_DONTROUTE, &old,
sizeof (old));
539 return (old != FALSE);
546 setopt (SO_DONTROUTE, &opt,
sizeof (opt));
553 getopt (SO_BROADCAST, &old,
sizeof (old));
554 return (old != FALSE);
566 setopt (SO_BROADCAST, &opt,
sizeof (opt));
576 getopt (SO_OOBINLINE, &old,
sizeof (old));
577 return (old != FALSE);
587 setopt (SO_OOBINLINE, &opt,
sizeof (opt));
594 getopt (SO_SNDBUF, &old,
sizeof (old));
601 setopt (SO_SNDBUF, &sz,
sizeof (sz));
608 getopt (SO_RCVBUF, &old,
sizeof (old));
615 setopt (SO_RCVBUF, &sz,
sizeof (sz));
625 if (!sl || sl->handle == INVALID_SOCKET)
628 unsigned long mode = on_off ? 0 : 1;
629 if (ioctlsocket (sl->handle, FIONBIO, &mode) == SOCKET_ERROR)
650 if (!sl || sl->handle == INVALID_SOCKET)
653 if (WSAEventSelect (sl->handle, (WSAEVENT)evt, mask) == SOCKET_ERROR)
666 if (!sl || sl->handle == INVALID_SOCKET)
669 WSANETWORKEVENTS netev;
670 if (WSAEnumNetworkEvents (sl->handle, NULL, &netev) == SOCKET_ERROR)
672 return netev.lNetworkEvents;
679 opt.l_onoff = on_off;
681 setopt (SO_LINGER, &opt,
sizeof (opt));
688 getopt (SO_LINGER, &opt,
sizeof (opt));
691 return (opt.l_onoff == 0);
697 int code = WSAGetLastError ();
712 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:206
generic_sockstream(const sockbuf &sb)
Create from an existing sockbuf.
Definition wsockstream.h:213
sockbuf * operator->()
Return the buffer used by this stream.
Definition wsockstream.h:235
generic_sockstream(int type, int domain=AF_INET, int proto=0)
Create a SOCK_STREAM or SOCK_DGRAM stream.
Definition wsockstream.h:221
generic_sockstream()
Default constructor.
Definition wsockstream.h:209
~generic_sockstream()
Create a SOCK_STREAM connected to a remote peer.
Definition wsockstream.h:250
sockbuf * rdbuf()
Return the buffer used by this stream.
Definition wsockstream.h:230
generic_sockstream(const sock &s)
Create from an existing socket.
Definition wsockstream.h:217
generic_sockstream(const inaddr &remote, int type=SOCK_STREAM)
Create a SOCK_STREAM connected to a remote peer.
Definition wsockstream.h:242
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:489
bool broadcast() const
Return "broadcast" option.
Definition wsockstream.h:550
int recvtimeout() const
Returns the send timeout value.
Definition wsockstream.h:414
bool reuseaddr() const
Return the "reuse address" flag.
Definition wsockstream.h:505
bool dontroute() const
Return status of "don't route" flag.
Definition wsockstream.h:535
long enumevents() const
Indicates which of the FD_XXX network events have occurred.
Definition wsockstream.h:664
checked< inaddr > peer() const
Retrieves the name of the peer to which the socket is connected.
Definition wsockstream.cpp:247
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:482
erc listen(int num=SOMAXCONN) const
Places the socket in a state in which it is listening for incoming connections.
Definition wsockstream.h:327
void linger(bool on_off, unsigned short seconds) const
Turn on or off linger mode and lingering timeout.
Definition wsockstream.h:676
erc bind() const
Associates a local address with the socket.
Definition wsockstream.cpp:278
bool operator==(const sock &other) const
Equality comparison operator.
Definition wsockstream.h:710
erc setopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Set a socket option.
Definition wsockstream.h:452
int recvbufsz() const
Return buffer size for receive operations.
Definition wsockstream.h:605
checked< inaddr > name() const
Return the local name for the object.
Definition wsockstream.cpp:232
virtual bool is_open() const
Check if socket is opened.
Definition wsockstream.h:294
erc connect(const inaddr &peer) const
Establishes a connection to specified peer.
Definition wsockstream.h:300
int getopt(int op, void *buf, int len, int level=SOL_SOCKET) const
Returns a socket option.
Definition wsockstream.h:434
void blocking(bool on_off)
Change blocking mode.
Definition wsockstream.h:623
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:451
int gettype() const
Return socket type (SOCK_DGRAM or SOCK_STREAM)
Definition wsockstream.h:466
bool debug() const
Return the debug flag.
Definition wsockstream.h:490
virtual erc close()
Close socket.
Definition wsockstream.cpp:204
bool oobinline() const
Return the status of the OOB_INLINE flag.
Definition wsockstream.h:573
unsigned int nread() const
Return number of characters waiting in socket's buffer.
Definition wsockstream.cpp:566
size_t recv(void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition wsockstream.cpp:351
sock()
Default constructor creates a closed socket.
Definition wsockstream.h:277
size_t send(const void *buf, size_t len, mflags msgf=mflags::none) const
Send data to the connected peer.
Definition wsockstream.cpp:415
bool operator!=(const sock &other) const
Inequality comparison operator.
Definition wsockstream.h:720
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:384
size_t recvfrom(sockaddr &sa, void *buf, size_t maxlen, mflags msgf=mflags::none) const
Receives data from socket.
Definition wsockstream.cpp:383
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:582
int sendbufsz() const
Return buffer size for send operations.
Definition wsockstream.h:591
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:544
HANDLE handle() const
Retrieve Windows socket handle.
Definition wsockstream.h:282
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:313
virtual ~sock()
Destructor.
Definition wsockstream.cpp:164
erc setevent(HANDLE evt, long mask) const
Associate an event object with this socket.
Definition wsockstream.h:648
static erc last_error()
Return an error code with the value returned by WSAGetLastError.
Definition wsockstream.h:695
bool is_writeready(int wp_sec, int wp_usec=0) const
Check if socket is "writable".
Definition wsockstream.cpp:520
bool keepalive() const
Return "keep alive" flag.
Definition wsockstream.h:520
sock & operator=(const sock &)
Assignment operator.
Definition wsockstream.cpp:131
Provide functions required by streambuf interface using an underlying socket.
Definition wsockstream.h:159
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:779
sockbuf(SOCKET soc=INVALID_SOCKET)
Build a sockbuf object from an existing socket.
Definition wsockstream.cpp:626
virtual ~sockbuf()
Destructor.
Definition wsockstream.cpp:672
virtual int sync() override
Return 0 if all chars flushed or -1 if error.
Definition wsockstream.cpp:691
virtual std::streamsize showmanyc() override
Return number of characters available in socket buffer.
Definition wsockstream.cpp:810
sockbuf & operator=(const sockbuf &)
Assignment operator.
Definition wsockstream.cpp:663
virtual std::streambuf * setbuf(char *buf, std::streamsize sz) override
Change buffering mode.
Definition wsockstream.cpp:713
virtual int underflow() override
underflow
Definition wsockstream.cpp:745
@ seconds
Degrees, minutes, seconds format (DD°MM'SS.sss")
Definition convert.h:247
Definition of erc and erfac classes.
Definition of inaddr class.
std::ostream & operator<<(std::ostream &strm, const mlib::inaddr &addr)
Serialize an address as '<hostname>:<port>'.
Definition inaddr.h:127
generic_sockstream< std::iostream > sockstream
Bidirectional socket stream.
Definition wsockstream.h:262
generic_sockstream< std::ostream > osockstream
Output socket stream.
Definition wsockstream.h:259
generic_sockstream< std::istream > isockstream
Input socket stream.
Definition wsockstream.h:256