10#if __has_include("defs.h")
14#include "safe_winsock.h"
21#if !defined(SOCKBUF_BUFSIZ)
23#define SOCKBUF_BUFSIZ 1024
72 :
sock(t, domain, proto)
89 , x_flags (sb.x_flags)
91 if (x_flags & flags::allocbuf)
100 std::basic_streambuf<T>::operator= (rhs);
101 x_flags = (x_flags & flags::allocbuf) | (rhs.x_flags & ~flags::allocbuf);
115 if (x_flags & flags::allocbuf)
117 delete[] std::basic_streambuf<T>::pbase ();
118 delete[] std::basic_streambuf<T>::eback ();
124 using int_type = std::char_traits<T>::int_type;
130 if (x_flags & flags::no_reads)
133 if (std::basic_streambuf<T>::gptr () < std::basic_streambuf<T>::egptr ())
134 return *(
unsigned char*)std::basic_streambuf<T>::gptr ();
136 if (std::basic_streambuf<T>::eback ())
139 size_t rval =
recv (std::basic_streambuf<T>::eback (), ibsize);
140 if (rval != (
size_t)EOF)
142 std::basic_streambuf<T>::setg (std::basic_streambuf<T>::eback (),
143 std::basic_streambuf<T>::eback (),
144 std::basic_streambuf<T>::eback () + rval);
145 return rval ? *(
unsigned char*)std::basic_streambuf<T>::gptr () : EOF;
153 size_t rval =
recv (&ch, 1);
154 if (rval == (
size_t)EOF || rval == 0)
166 if (x_flags & flags::no_writes)
175 if (std::basic_streambuf<T>::pbase ())
178 *std::basic_streambuf<T>::pptr () = (T)c;
179 std::basic_streambuf<T>::pbump (1);
184 unsigned char ch = (
unsigned char)c;
185 size_t rval =
send (&ch, 1);
186 if (rval == (
size_t)EOF || rval == 0)
195 if (std::basic_streambuf<T>::pptr () <= std::basic_streambuf<T>::pbase ())
197 if (!(x_flags & flags::no_writes))
199 size_t wlen = std::basic_streambuf<T>::pptr () - std::basic_streambuf<T>::pbase ();
200 size_t wval =
send (std::basic_streambuf<T>::pbase (), wlen);
201 std::basic_streambuf<T>::setp (std::basic_streambuf<T>::pbase (),
202 std::basic_streambuf<T>::epptr ());
205 TRACE (
"sockbuf::sync failed - wanted %d sent %d", wlen, wval);
216 std::basic_streambuf<T>*
setbuf (T* buf, std::streamsize sz)
override
221 if (x_flags & flags::allocbuf)
223 delete[] std::basic_streambuf<T>::eback ();
224 delete[] std::basic_streambuf<T>::pbase ();
226 std::basic_streambuf<T>::setp (NULL, NULL);
227 std::basic_streambuf<T>::setg (NULL, NULL, NULL);
228 x_flags &= ~flags::allocbuf;
233 x_flags |= flags::allocbuf;
234 T* ptr =
new T[(size_t)sz + 1];
235 std::basic_streambuf<T>::setp (ptr, ptr + sz);
236 ptr =
new T[(size_t)sz];
237 std::basic_streambuf<T>::setg (ptr, ptr + sz, ptr + sz);
241 std::basic_streambuf<T>::setp (buf,
void deactivate() const
Marks error code as inactive.
Definition errorcode.h:659
virtual bool is_open() const
Check if socket is opened.
Definition sock.h:183
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
type
socket types
Definition sock.h:23
@ shut_readwrite
blocked for both
Definition sock.h:34
virtual erc shutdown(shuthow sh) const
Disables sends or receives on socket.
Definition sock.cpp:579
virtual void close()
Close socket.
Definition sock.cpp:200
sock & operator=(const sock &)
Assignment operator.
Definition sock.cpp:129
std::basic_streambuf< T > * setbuf(T *buf, std::streamsize sz) override
Change buffering mode.
Definition sockbuf.h:216
~sockbuf()
Destructor.
Definition sockbuf.h:106
sockbuf< T > & operator=(const sockbuf< T > &rhs)
Definition sockbuf.h:97
int sync() override
Return 0 if all chars flushed or -1 if error.
Definition sockbuf.h:193
sockbuf()
Default constructor.
Definition sockbuf.h:63
int_type underflow() override
If c == EOF or buffer full, return sync(); otherwise insert c into the buffer and return c
Definition sockbuf.h:128
sockbuf(sock::type t, int domain, int proto)
Build a sockbuf object and the attached socket with the given parameters.
Definition sockbuf.h:71
sockbuf(const sock &s)
Build a sockbuf object from a sock base.
Definition sockbuf.h:79
sockbuf(const sockbuf< T > &sb)
Copy constructor.
Definition sockbuf.h:87
int_type overflow(int_type c=EOF) override
If c == EOF or buffer full, return sync(); otherwise insert c into the buffer and return c
Definition sockbuf.h:164
std::streamsize showmanyc() override
Return number of characters available in socket buffer.
Definition sockbuf.h:247
#define SOCKBUF_BUFSIZ
Default buffer size for socket streams.
Definition sockbuf.h:23
definition of TRACE macro