libassa 3.5.0
|
#include <Socketbuf.h>
Public Member Functions | |
Socketbuf (Socket *s_) | |
virtual | ~Socketbuf () |
Protected Member Functions | |
virtual int | sync () |
This function synchronizes the streambuf with its actual stream of characters. | |
virtual int | underflow () |
This function is called to supply characters for input (from some source) when the get area is empty, although it may be called at other times. | |
virtual int | overflow (int c_=EOF) |
This function is called to consume characters (flush them to output), typically when the put area is full and an attempt is made to store another character. | |
virtual int | showmanyc () |
The morphemes of showmanyc are "es-how-many-see", not "show-man-ic". | |
virtual int | doallocate () |
This function is called by allocate when unbuffered() is zero and base() is zero. | |
Private Member Functions | |
int | flush_output () |
int | sys_read (char *b_, int len_) |
int | sys_write (char *b_, int len_) |
void | xput_char (char c_) |
Private Attributes | |
Socket * | m_s |
Reference to the Socket stream. |
Definition at line 29 of file Socketbuf.h.
Socketbuf::Socketbuf | ( | Socket * | s_ | ) |
Definition at line 25 of file Socketbuf.cpp.
References ASSA::STRMBUFTRACE, trace_with_mask, and ASSA::Streambuf::unbuffered().
: m_s (s_) { trace_with_mask("Socketbuf::Socketbuf",STRMBUFTRACE); // By default, I am doing buffering IO unbuffered (0); }
Socketbuf::~Socketbuf | ( | ) | [virtual] |
Definition at line 59 of file Socketbuf.cpp.
References overflow(), ASSA::STRMBUFTRACE, and trace_with_mask.
{ trace_with_mask("Socketbuf::~Socketbuf",STRMBUFTRACE); overflow (EOF); // flush put area }
int Socketbuf::doallocate | ( | ) | [protected, virtual] |
This function is called by allocate when unbuffered() is zero and base() is zero.
It attempts to make a buffer of suitable size available. On success it must call setb to establish the reserve area, then return a value greater than zero. On failure it returns EOF. The default behavior is to allocate a buffer using new.
Reimplemented from ASSA::Streambuf.
Definition at line 226 of file Socketbuf.cpp.
References DL, ASSA::io_ptrs::dump(), ASSA::io_ptrs::m_buf_base, ASSA::io_ptrs::m_flags, ASSA::io_ptrs::m_shortbuf, ASSA::Streambuf::MAXTCPFRAMESZ, ASSA::Streambuf::setb(), ASSA::Streambuf::setg(), ASSA::Streambuf::setp(), ASSA::STRMBUF, ASSA::STRMBUFTRACE, trace_with_mask, and ASSA::io_ptrs::UNBUFFERED.
Referenced by overflow(), and underflow().
{ trace_with_mask("Socketbuf::doallocate",STRMBUFTRACE); // Get area comes first and matches entier buffer area. // Put area comes right after it. They are two equally-sized // separate buffers. // // ------------ - // | | ^ // | get area | | buffer area // | | v // ------------ - // | put area | // | | // ------------ // // Return 1 on allocation and 0 if there is no need if (m_buf_base) return 0; if ( ! (m_flags & UNBUFFERED) ) { DL((STRMBUF,"Buffered IO - allocating %d bytes\n", 2 * MAXTCPFRAMESZ)); char* buf = new char [2 * MAXTCPFRAMESZ]; setg (buf, buf + MAXTCPFRAMESZ, buf + MAXTCPFRAMESZ); setb (buf, buf + MAXTCPFRAMESZ, 1); buf += MAXTCPFRAMESZ; setp (buf, buf+MAXTCPFRAMESZ); } else { DL((STRMBUF,"Unbuffered IO - same 1 byte array\n")); setb (m_shortbuf, m_shortbuf+1, 0); // read_base is pointing to the begining, and // read_end to one past end of the buffer. // Because buffer is empty, read_ptr should point // to the end (same as read_end). This way, calling // routines will detect that get area needs data from sync. // // +- read_base +- read_ptr // | |- read_end // | |+----- one past end-of-block // v vv // +--------------+-+ // | get area | | // +--------------+-+ setg (m_shortbuf, m_shortbuf+1, m_shortbuf+1); // write_base & write_ptr are pointing to the begining, // and write_end to one past end of the buffer. // Because buffer is empty, read_ptr should point // to the beginning (same as write_base). This way, calling // routines will detect that put area needs data from sync. // // +- write_base +- write_end points one past // |- write_ptr | end-of-block // v v // +--------------+-+ // | put area | | // +--------------+-+ setp (m_shortbuf, m_shortbuf+1); } dump (); return 1; }
int Socketbuf::flush_output | ( | ) | [private] |
Definition at line 192 of file Socketbuf.cpp.
References ASSA::Streambuf::epptr(), ASSA::Streambuf::MAXTCPFRAMESZ, ASSA::Streambuf::pbase(), ASSA::Streambuf::pbump(), ASSA::Streambuf::pptr(), ASSA::Streambuf::setp(), ASSA::STRMBUFTRACE, sys_write(), trace_with_mask, and ASSA::Streambuf::unbuffered().
Referenced by overflow(), and sync().
{ trace_with_mask("Socketbuf::flush_output",STRMBUFTRACE); if (pptr () <= pbase ()) { // Nothing to flush return 0; } int requested; int xmitted; requested = pptr () - pbase (); if ((xmitted = sys_write (pbase (), requested)) < 0) { return EOF; } if (unbuffered ()) { setp (pbase (), epptr ()); return 0; } requested -= xmitted; setp (pbase (), pbase () + MAXTCPFRAMESZ); pbump (requested); if (requested > 0) { ::memmove (pbase (), pbase () + xmitted, requested); } return 0; }
int Socketbuf::overflow | ( | int | c = EOF | ) | [protected, virtual] |
This function is called to consume characters (flush them to output), typically when the put area is full and an attempt is made to store another character.
If c is not EOF, overflow must either store or consume the character, following those already in the put area. It returns EOF on error, any other value on success. The default behavior of the base class version is undefined, so each derived class must define its own overflow. The normal action for a derived class version is to consume the characters in the put area (those between pbase() and pptr()), call setp() to set up a new put area, then store c (using sputc()) if it is not EOF.
Reimplemented from ASSA::Streambuf.
Definition at line 162 of file Socketbuf.cpp.
References doallocate(), ASSA::io_ptrs::dump(), ASSA::Streambuf::epptr(), flush_output(), ASSA::Streambuf::pbase(), ASSA::Streambuf::pptr(), ASSA::STRMBUFTRACE, trace_with_mask, ASSA::Streambuf::unbuffered(), and xput_char().
Referenced by ~Socketbuf().
{ trace_with_mask("Socketbuf::overflow",STRMBUFTRACE); // If c == EOF, return flush_output() // Otherwise, insert c into the buffer if (c_ == EOF) return flush_output (); if (pbase () == 0 && doallocate () == EOF) return EOF; if (pptr () >= epptr() && flush_output () == EOF) return EOF; xput_char (c_); dump (); if ((unbuffered () || pptr () >= epptr ()) && flush_output () == EOF) return EOF; dump (); return c_; }
int Socketbuf::showmanyc | ( | ) | [protected, virtual] |
The morphemes of showmanyc are "es-how-many-see", not "show-man-ic".
Return an estimate of the number of characters available in the sequence, or -1. If it returns a positive value, then successive calls to underflow() will not return EOF until at least that number of characters have been supplied. If showmanyc() returns -1, then calls to underflow() or uflow() will fail. The intention is not only that these calls will not return EOF, but that they will return ``immediately.''
Reimplemented from ASSA::Streambuf.
Definition at line 43 of file Socketbuf.cpp.
References ASSA::Socket::getBytesAvail(), m_s, ASSA::STRMBUFTRACE, and trace_with_mask.
{ trace_with_mask("Socketbuf::showmanyc",STRMBUFTRACE); return m_s->getBytesAvail (); }
int Socketbuf::sync | ( | ) | [protected, virtual] |
This function synchronizes the streambuf with its actual stream of characters.
The derived class version should flush any characters in the put area to their final destination, and if possible give back any characters in the input buffer to their source. It should return EOF on any error, zero on success. The default behavior of the base class version is to return zero if there are no pending input or output characters (in_avail() and out_waiting() are both zero), and return EOF otherwise.
Reimplemented from ASSA::Streambuf.
Definition at line 35 of file Socketbuf.cpp.
References flush_output(), ASSA::STRMBUFTRACE, and trace_with_mask.
{ trace_with_mask("Socketbuf::sync",STRMBUFTRACE); return flush_output (); }
int Socketbuf::sys_read | ( | char * | b_, |
int | len_ | ||
) | [private] |
Definition at line 67 of file Socketbuf.cpp.
References DL, ASSA::get_errno(), ASSA::Socket::getHandler(), m_s, ASSA::STRMBUFTRACE, and trace_with_mask.
Referenced by underflow().
{ trace_with_mask("Socketbuf::sys_read",STRMBUFTRACE); int ret = ::recv (m_s->getHandler (), b_, len_, 0); DL((STRMBUFTRACE,"Tried to read %d bytes from fd=%d\n", len_, m_s->getHandler ())); DL((STRMBUFTRACE,"::recv() returned %d\n", ret)); if (ret == -1) { DL((STRMBUFTRACE,"::recv() error: %d (%s)\n", errno, strerror (get_errno ()))); } return (ret); }
int Socketbuf::sys_write | ( | char * | b_, |
int | len_ | ||
) | [private] |
Definition at line 86 of file Socketbuf.cpp.
References DL, ASSA::Socket::getHandler(), m_s, ASSA::STRMBUFTRACE, and trace_with_mask.
Referenced by flush_output().
{ trace_with_mask("Socketbuf::sys_write",STRMBUFTRACE); int ret = ::send (m_s->getHandler (), b_, len_, 0); DL((STRMBUFTRACE,"Tried to write %d bytes to fd=%d\n", len_, m_s->getHandler ())); DL((STRMBUFTRACE,"::send() returned %d\n", ret)); if (ret == -1) { DL((STRMBUFTRACE,"::send() error: %d\n",errno)); } return (ret); }
int Socketbuf::underflow | ( | ) | [protected, virtual] |
This function is called to supply characters for input (from some source) when the get area is empty, although it may be called at other times.
If the get area is not empty, it should just return the first character (without advancing the get pointer). If the get area is empty, it should establish a new get area, aquire new input, and return the first character, if any. If no input characters are available, it should leave an empty get area and return EOF. The default behavior of the base class version is undefined, so each derived class must define its own underflow.
Reimplemented from ASSA::Streambuf.
Definition at line 105 of file Socketbuf.cpp.
References ASSA::Streambuf::base(), DL, doallocate(), ASSA::io_ptrs::dump(), ASSA::MemDump::dump_to_log(), ASSA::Streambuf::egptr(), ASSA::io_ptrs::EOF_SEEN, ASSA::get_errno(), ASSA::Streambuf::gptr(), ASSA::io_ptrs::m_flags, ASSA::Streambuf::MAXTCPFRAMESZ, ASSA::Streambuf::setg(), ASSA::STRMBUF, ASSA::STRMBUFTRACE, sys_read(), trace_with_mask, and ASSA::Streambuf::unbuffered().
{ /* The important thing to note is that this function returns: a) pointer to the first character in buffer available. b) EOF if sys_read () failed. In case of peer closing its connection, a) will be true. Caller can always find out number of bytes in buffer and determine if peer indeed closed connection. */ trace_with_mask("Socketbuf::underflow",STRMBUFTRACE); if (gptr () < egptr ()) // The get area is not empty, { return *(unsigned char*) gptr (); // return 1st character } if (base () == 0 && // If buffer isn't established, doallocate () == EOF) // allocate buffer (both buff & unbuff IO) { return EOF; } int bufsz = unbuffered () ? 1 : MAXTCPFRAMESZ; /* Read as much as I can up to the allocated buffer size. EOF = (-1). */ int rval = sys_read (base (), bufsz); DL((STRMBUF,"Socketbuf::sys_read() returned %d bytes\n", rval)); if (rval == EOF) { if (get_errno () != EWOULDBLOCK) { m_flags |= EOF_SEEN; } return EOF; } DL((STRMBUF,"Having read %d bytes from socket\n",rval)); MemDump::dump_to_log (STRMBUF, "Data received:", base (), rval); // Set get area pointers according to the data just read setg (base (), base (), base () + rval); dump (); return (*(unsigned char*) gptr ()); // Return front character }
void Socketbuf::xput_char | ( | char | c_ | ) | [private] |
Definition at line 51 of file Socketbuf.cpp.
References ASSA::Streambuf::pbump(), ASSA::Streambuf::pptr(), ASSA::STRMBUFTRACE, and trace_with_mask.
Referenced by overflow().
{ trace_with_mask("Socketbuf::xput_char",STRMBUFTRACE); *pptr() = c_; pbump (1); }
Socket* ASSA::Socketbuf::m_s [private] |
Reference to the Socket stream.
Definition at line 51 of file Socketbuf.h.
Referenced by showmanyc(), sys_read(), and sys_write().