libassa  3.5.1
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
ASSA::Socketbuf Class Reference

#include <Socketbuf.h>

Inheritance diagram for ASSA::Socketbuf:
ASSA::Streambuf ASSA::io_ptrs

Public Member Functions

 Socketbuf (Socket *s_)
 
virtual ~Socketbuf ()
 
- Public Member Functions inherited from ASSA::Streambuf
virtual ~Streambuf ()
 
Streambufpubsetbuf (char *s_, int n_)
 Set buffer. More...
 
int pubsync ()
 
int in_avail ()
 This function returns the number of characters
immediately available in the get area. More...
 
int snextc ()
 This function moves the get pointer forward one
position, then returns the character after the get pointer's new position. More...
 
int sbumpc ()
 This function should probably have been called `‘sgetc’'. More...
 
int sgetc ()
 This function returns the character after the get pointer, or EOF if the get pointer is at the end of the sequence. More...
 
int sgetn (char *b_, int len_)
 This function gets the next len_ characters following the get pointer, copying them to the char array pointed to by b_; it advances the get pointer past the last character fetched. More...
 
int sputc (char c_)
 This function stores c just after the put pointer, and advances the pointer one position, possibly extending the sequence. More...
 
int sputn (char *b_, int len_)
 From the location pointed to by ptr, stores exactly len characters after the put pointer, advancing the put pointer just past the last character. More...
 
void unbuffered (int i_)
 If i_ is non-zero, then all IO operations are buffered. More...
 
int unbuffered ()
 
- Public Member Functions inherited from ASSA::io_ptrs
 io_ptrs ()
 
void dump () const
 

Protected Member Functions

virtual int sync ()
 This function synchronizes the streambuf with its actual stream of characters. More...
 
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. More...
 
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. More...
 
virtual int showmanyc ()
 The morphemes of showmanyc are "es-how-many-see",
not "show-man-ic". More...
 
virtual int doallocate ()
 This function is called by allocate when unbuffered() is zero and base() is zero. More...
 
- Protected Member Functions inherited from ASSA::Streambuf
 Streambuf ()
 The default constructor is protected for class Streambuf to asssure that only objects for classes derived from this class may be constructed. More...
 
 Streambuf (const Streambuf &)
 
Streambufoperator= (const Streambuf &)
 
char * base () const
 Returns the lowest possible value for gptr() - the beginning of the get area. More...
 
char * gptr () const
 Returns a pointer to the beginning of the get area, and thus to the next character to be fetched (if there are any). More...
 
char * egptr () const
 Returns a pointer just past the end of the get area, the maximum possible value for gptr(). More...
 
void setg (char *gbeg_, char *gnext_, char *gend_)
 Set get area pointers. More...
 
char * pbase () const
 Returns a pointer to the beginning fo the space available for the put area, the lowest possible value for pptr(). More...
 
char * pptr () const
 Returns a pointer to the beginning of the put area, and thus to the location of the next character that is stored (if possible). More...
 
char * epptr () const
 Returns a pointer just past the end of the put area, the maximum possible value for pptr(). More...
 
void setp (char *pbeg_, char *pend_)
 Set put area pointers. More...
 
void pbump (int n_)
 Advances the next pointer for the output sequence by n_. More...
 
void setb (char *b_, char *eb_, int del_)
 Establish the reserve area (buffer). More...
 
void init ()
 
virtual Streambufsetbuf (char *p_, int len_)
 Performs an operation that is defined separately for each class derived from Streambuf. More...
 
virtual int xsgetn (char *b_, int len_)
 Assigns up to len_ characters to successive elements of the array whose first element is designated by b_. More...
 
virtual int uflow ()
 Reads the characters from the input sequence, if possible, and moves the stream position past it, as follows: More...
 
virtual int xsputn (const char *b_, int len_)
 Writes up to len_ characters to the output sequence as if by repeated calls to sputc (c). More...
 

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

Socketm_s
 Reference to the Socket stream. More...
 

Additional Inherited Members

- Public Types inherited from ASSA::io_ptrs
enum  { USER_BUF = 1 , UNBUFFERED = 2 , EOF_SEEN = 4 , ERR_SEEN = 8 }
 
- Public Attributes inherited from ASSA::io_ptrs
char * m_read_base
 
char * m_read_ptr
 
char * m_read_end
 
char * m_write_base
 
char * m_write_ptr
 
char * m_write_end
 
char * m_buf_base
 
char * m_buf_end
 
int m_flags
 
char m_shortbuf [1]
 
- Static Public Attributes inherited from ASSA::Streambuf
static const int MAXTCPFRAMESZ = 65536
 Size of the internal input/output buffer. More...
 

Detailed Description

Definition at line 29 of file Socketbuf.h.

Constructor & Destructor Documentation

◆ Socketbuf()

Socketbuf::Socketbuf ( Socket s_)

Definition at line 24 of file Socketbuf.cpp.

26  : m_s (s_)
27 {
28  trace_with_mask("Socketbuf::Socketbuf",STRMBUFTRACE);
29  // By default, I am doing buffering IO
30  unbuffered (0);
31 }
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Socket * m_s
Reference to the Socket stream.
Definition: Socketbuf.h:51
int unbuffered()
Definition: Streambuf.h:408
@ STRMBUFTRACE
Extended Streambuf & friends messages
Definition: LogMask.h:46

References ASSA::STRMBUFTRACE, trace_with_mask, and ASSA::Streambuf::unbuffered().

◆ ~Socketbuf()

Socketbuf::~Socketbuf ( )
virtual

Definition at line 58 of file Socketbuf.cpp.

60 {
61  trace_with_mask("Socketbuf::~Socketbuf",STRMBUFTRACE);
62  overflow (EOF); // flush put area
63 }
virtual int overflow(int c_=EOF)
This function is called to consume characters (flush them to output), typically when the put area is ...
Definition: Socketbuf.cpp:162

References overflow(), ASSA::STRMBUFTRACE, and trace_with_mask.

Member Function Documentation

◆ doallocate()

int Socketbuf::doallocate ( )
protectedvirtual

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.

227 {
228  trace_with_mask("Socketbuf::doallocate",STRMBUFTRACE);
229 
230  // Get area comes first and matches entier buffer area.
231  // Put area comes right after it. They are two equally-sized
232  // separate buffers.
233  //
234  // ------------ -
235  // | | ^
236  // | get area | | buffer area
237  // | | v
238  // ------------ -
239  // | put area |
240  // | |
241  // ------------
242  //
243  // Return 1 on allocation and 0 if there is no need
244 
245  if (m_buf_base)
246  return 0;
247 
248  if ( ! (m_flags & UNBUFFERED) ) {
249  DL((STRMBUF,"Buffered IO - allocating %d bytes\n",
250  2 * MAXTCPFRAMESZ));
251  char* buf = new char [2 * MAXTCPFRAMESZ];
252 
253  setg (buf, buf + MAXTCPFRAMESZ, buf + MAXTCPFRAMESZ);
254  setb (buf, buf + MAXTCPFRAMESZ, 1);
255 
256  buf += MAXTCPFRAMESZ;
257  setp (buf, buf+MAXTCPFRAMESZ);
258  }
259  else {
260  DL((STRMBUF,"Unbuffered IO - same 1 byte array\n"));
261  setb (m_shortbuf, m_shortbuf+1, 0);
262 
263  // read_base is pointing to the begining, and
264  // read_end to one past end of the buffer.
265  // Because buffer is empty, read_ptr should point
266  // to the end (same as read_end). This way, calling
267  // routines will detect that get area needs data from sync.
268  //
269  // +- read_base +- read_ptr
270  // | |- read_end
271  // | |+----- one past end-of-block
272  // v vv
273  // +--------------+-+
274  // | get area | |
275  // +--------------+-+
277 
278  // write_base & write_ptr are pointing to the begining,
279  // and write_end to one past end of the buffer.
280  // Because buffer is empty, read_ptr should point
281  // to the beginning (same as write_base). This way, calling
282  // routines will detect that put area needs data from sync.
283  //
284  // +- write_base +- write_end points one past
285  // |- write_ptr | end-of-block
286  // v v
287  // +--------------+-+
288  // | put area | |
289  // +--------------+-+
290 
292  }
293  dump ();
294  return 1;
295 }
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
void setp(char *pbeg_, char *pend_)
Set put area pointers.
Definition: Streambuf.h:588
static const int MAXTCPFRAMESZ
Size of the internal input/output buffer.
Definition: Streambuf.h:106
void setb(char *b_, char *eb_, int del_)
Establish the reserve area (buffer).
Definition: Streambuf.cpp:80
void setg(char *gbeg_, char *gnext_, char *gend_)
Set get area pointers.
Definition: Streambuf.cpp:69
char m_shortbuf[1]
Definition: Streambuf.h:47
char * m_buf_base
Definition: Streambuf.h:41
void dump() const
Definition: Streambuf.cpp:23
@ STRMBUF
Class Streambuf & friends messages
Definition: LogMask.h:45

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().

◆ flush_output()

int Socketbuf::flush_output ( )
private

Definition at line 191 of file Socketbuf.cpp.

193 {
194  trace_with_mask("Socketbuf::flush_output",STRMBUFTRACE);
195 
196  if (pptr () <= pbase ()) { // Nothing to flush
197  return 0;
198  }
199 
200  int requested;
201  int xmitted;
202 
203  requested = pptr () - pbase ();
204 
205  if ((xmitted = sys_write (pbase (), requested)) < 0) {
206  return EOF;
207  }
208 
209  if (unbuffered ()) {
210  setp (pbase (), epptr ());
211  return 0;
212  }
213 
214  requested -= xmitted;
215  setp (pbase (), pbase () + MAXTCPFRAMESZ);
216  pbump (requested);
217 
218  if (requested > 0) {
219  ::memmove (pbase (), pbase () + xmitted, requested);
220  }
221 
222  return 0;
223 }
int sys_write(char *b_, int len_)
Definition: Socketbuf.cpp:86
char * pptr() const
Returns a pointer to the beginning of the put area, and thus to the location of the next character th...
Definition: Streambuf.h:499
char * epptr() const
Returns a pointer just past the end of the put area, the maximum possible value for pptr().
Definition: Streambuf.h:507
char * pbase() const
Returns a pointer to the beginning fo the space available for the put area, the lowest possible value...
Definition: Streambuf.h:491
void pbump(int n_)
Advances the next pointer for the output sequence by n_.
Definition: Streambuf.h:515

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().

◆ overflow()

int Socketbuf::overflow ( int  c = EOF)
protectedvirtual

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 161 of file Socketbuf.cpp.

163 {
164  trace_with_mask("Socketbuf::overflow",STRMBUFTRACE);
165 
166  // If c == EOF, return flush_output()
167  // Otherwise, insert c into the buffer
168 
169  if (c_ == EOF)
170  return flush_output ();
171 
172  if (pbase () == 0 && doallocate () == EOF)
173  return EOF;
174 
175  if (pptr () >= epptr() && flush_output () == EOF)
176  return EOF;
177 
178  xput_char (c_);
179 
180  dump ();
181 
182  if ((unbuffered () || pptr () >= epptr ()) && flush_output () == EOF)
183  return EOF;
184 
185  dump ();
186 
187  return c_;
188 }
virtual int doallocate()
This function is called by allocate when unbuffered() is zero and base() is zero.
Definition: Socketbuf.cpp:226
void xput_char(char c_)
Definition: Socketbuf.cpp:51

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().

◆ showmanyc()

int Socketbuf::showmanyc ( )
protectedvirtual

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 42 of file Socketbuf.cpp.

44 {
45  trace_with_mask("Socketbuf::showmanyc",STRMBUFTRACE);
46  return m_s->getBytesAvail ();
47 }
int getBytesAvail(void) const
Return number of bytes available in socket receive buffer.
Definition: Socket.cpp:48

References ASSA::Socket::getBytesAvail(), m_s, ASSA::STRMBUFTRACE, and trace_with_mask.

◆ sync()

int Socketbuf::sync ( )
protectedvirtual

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 34 of file Socketbuf.cpp.

36 {
37  trace_with_mask("Socketbuf::sync",STRMBUFTRACE);
38  return flush_output ();
39 }

References flush_output(), ASSA::STRMBUFTRACE, and trace_with_mask.

◆ sys_read()

int Socketbuf::sys_read ( char *  b_,
int  len_ 
)
private

Definition at line 66 of file Socketbuf.cpp.

68 {
69  trace_with_mask("Socketbuf::sys_read",STRMBUFTRACE);
70 
71  int ret = ::recv (m_s->getHandler (), b_, len_, 0);
72 
73  DL((STRMBUFTRACE,"Tried to read %d bytes from fd=%d\n",
74  len_, m_s->getHandler ()));
75  DL((STRMBUFTRACE,"::recv() returned %d\n", ret));
76 
77  if (ret == -1) {
78  DL((STRMBUFTRACE,"::recv() error: %d (%s)\n",
79  errno, strerror (get_errno ())));
80  }
81  return (ret);
82 }
virtual handler_t getHandler() const =0
Get file descriptor.
int get_errno()
Fetch error number in a portable way.
Definition: Logger_Impl.h:115

References DL, ASSA::get_errno(), ASSA::Socket::getHandler(), m_s, ASSA::STRMBUFTRACE, and trace_with_mask.

Referenced by underflow().

◆ sys_write()

int Socketbuf::sys_write ( char *  b_,
int  len_ 
)
private

Definition at line 85 of file Socketbuf.cpp.

87 {
88  trace_with_mask("Socketbuf::sys_write",STRMBUFTRACE);
89 
90  int ret = ::send (m_s->getHandler (), b_, len_, 0);
91 
92  DL((STRMBUFTRACE,"Tried to write %d bytes to fd=%d\n",
93  len_, m_s->getHandler ()));
94  DL((STRMBUFTRACE,"::send() returned %d\n", ret));
95 
96  if (ret == -1) {
97  DL((STRMBUFTRACE,"::send() error: %d\n",errno));
98  }
99 
100  return (ret);
101 }

References DL, ASSA::Socket::getHandler(), m_s, ASSA::STRMBUFTRACE, and trace_with_mask.

Referenced by flush_output().

◆ underflow()

int Socketbuf::underflow ( )
protectedvirtual

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 104 of file Socketbuf.cpp.

106 {
107  /*
108  The important thing to note is that this function
109  returns:
110  a) pointer to the first character in buffer available.
111  b) EOF if sys_read () failed.
112 
113  In case of peer closing its connection, a) will be true.
114  Caller can always find out number of bytes in buffer
115  and determine if peer indeed closed connection.
116  */
117  trace_with_mask("Socketbuf::underflow",STRMBUFTRACE);
118 
119  if (gptr () < egptr ()) // The get area is not empty,
120  {
121  return *(unsigned char*) gptr (); // return 1st character
122  }
123 
124  if (base () == 0 && // If buffer isn't established,
125  doallocate () == EOF) // allocate buffer (both buff & unbuff IO)
126  {
127  return EOF;
128  }
129 
130  int bufsz = unbuffered () ? 1 : MAXTCPFRAMESZ;
131 
132  /*
133  Read as much as I can up to the allocated buffer size.
134  EOF = (-1).
135  */
136  int rval = sys_read (base (), bufsz);
137 
138  DL((STRMBUF,"Socketbuf::sys_read() returned %d bytes\n", rval));
139 
140  if (rval == EOF)
141  {
142  if (get_errno () != EWOULDBLOCK) {
143  m_flags |= EOF_SEEN;
144  }
145  return EOF;
146  }
147 
148  DL((STRMBUF,"Having read %d bytes from socket\n",rval));
149  MemDump::dump_to_log (STRMBUF, "Data received:", base (), rval);
150 
151  // Set get area pointers according to the data just read
152 
153  setg (base (), base (), base () + rval);
154 
155  dump ();
156 
157  return (*(unsigned char*) gptr ()); // Return front character
158 }
static void dump_to_log(unsigned long mask_, const char *info_, const char *msg_, int len_)
Write hex/ascii dump of a memory region to log file.
Definition: MemDump.cpp:111
int sys_read(char *b_, int len_)
Definition: Socketbuf.cpp:67
char * base() const
Returns the lowest possible value for gptr() - the beginning of the get area.
Definition: Streambuf.h:464
char * egptr() const
Returns a pointer just past the end of the get area, the maximum possible value for gptr().
Definition: Streambuf.h:482
char * gptr() const
Returns a pointer to the beginning of the get area, and thus to the next character to be fetched (if ...
Definition: Streambuf.h:473

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().

◆ xput_char()

void Socketbuf::xput_char ( char  c_)
private

Definition at line 50 of file Socketbuf.cpp.

52 {
53  trace_with_mask("Socketbuf::xput_char",STRMBUFTRACE);
54  *pptr() = c_;
55  pbump (1);
56 }

References ASSA::Streambuf::pbump(), ASSA::Streambuf::pptr(), ASSA::STRMBUFTRACE, and trace_with_mask.

Referenced by overflow().

Member Data Documentation

◆ m_s

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().


The documentation for this class was generated from the following files: