libassa  3.5.1
Streambuf.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Streambuf.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1999 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 //------------------------------------------------------------------------------
13 // Created: 12/02/99
14 //------------------------------------------------------------------------------
15 
16 #include "assa/Streambuf.h"
17 #include "assa/MemDump.h"
18 
19 using namespace ASSA;
20 
21 void
23 dump () const
24 {
25 #ifdef LOG_PACKET
26  trace_with_mask("io_ptrs::dump",STRMBUF);
27  int len;
28 
29  DL((STRMBUF,"---Ptr------:---Val---\n"));
30  DL((STRMBUF,"m_read_base.: 0x%x\n", (u_long)m_read_base));
31  DL((STRMBUF,"m_read_ptr..: 0x%x\n", (u_long)m_read_ptr ));
32  DL((STRMBUF,"m_read_end..: 0x%x\n", (u_long)m_read_end ));
33 
34  if (m_read_ptr && (len = m_read_end - m_read_ptr) > 0) {
35  MemDump get_area (m_read_ptr, len);
36  DL((STRMBUF,"\n%s\n", get_area.getMemDump ()));
37  }
38 
39  DL((STRMBUF,"m_write_base: 0x%x\n", (u_long)m_write_base));
40  DL((STRMBUF,"m_write_ptr.: 0x%x\n", (u_long)m_write_ptr ));
41  DL((STRMBUF,"m_write_end.: 0x%x\n", (u_long)m_write_end ));
42 
43  if (m_write_base && (len = m_write_ptr - m_write_base) > 0) {
44  MemDump put_area (m_write_base, len);
45  DL((STRMBUF,"%s\n", put_area.getMemDump ()));
46  }
47 
48  DL((STRMBUF,"m_buf_base..: 0x%x\n", (u_long)m_buf_base ));
49  DL((STRMBUF,"m_buf_end...: 0x%x\n", (u_long)m_buf_end ));
50  DL((STRMBUF,"------------:---------\n");
51 
52 #endif
53 }
54 
55 int
57 snextc ()
58 {
59  trace_with_mask("Streambuf::snextc",STRMBUFTRACE);
60 
61  if (m_read_ptr >= m_read_end && underflow () == EOF) {
62  return EOF;
63  }
64  return m_read_ptr++, sgetc ();
65 }
66 
67 void
69 setg (char* gbeg_, char* gnext_, char* gend_)
70 {
71  trace_with_mask("Streambuf::setg",STRMBUFTRACE);
72 
73  m_read_base = gbeg_;
74  m_read_ptr = gnext_;
75  m_read_end = gend_;
76 }
77 
78 void
80 setb (char* b_, char* eb_, int del_)
81 {
82  trace_with_mask("Streambuf::setb",STRMBUFTRACE);
83 
84  if (m_buf_base && !(m_flags & USER_BUF))
85  delete m_buf_base;
86 
87  m_buf_base = b_;
88  m_buf_end = eb_;
89 
90  if (del_)
91  m_flags &= ~ USER_BUF; // clear bit
92  else
93  m_flags |= USER_BUF; // set bit
94 
95  dump ();
96 }
97 
98 Streambuf*
100 setbuf (char* p_, int len_)
101 {
102  trace_with_mask("Streambuf::setb",STRMBUFTRACE);
103 
104  if (sync () == EOF) // Flush out all pending bytes before
105  return NULL; // resetting buffer. Also, first time around,
106  // calling sync() suppose to set put area
107  // pointers.
108 
109  if (p_ == NULL || len_ == 0) {
110  DL((STRMBUF,"Unbuffered IO set.\n"));
111  unbuffered (1);
112  // We do it from doalloc instead - vlg
113  // setb (m_shortbuf, m_shortbuf+1, 0);
114  }
115  else {
116  DL((STRMBUF,"Buffered IO set.\n"));
117  unbuffered (0);
118  setb (p_, p_ + len_, 0);
119  }
120  setp (0, 0);
121  setg (0, 0, 0);
122 
123  return this;
124 }
125 
126 int
128 xsgetn (char* data_, int len_)
129 {
130  trace_with_mask("Streambuf::xsgetn",STRMBUFTRACE);
131 
132  /*
133  Get area is empty and nothing is on the socket.
134  */
135  int count = m_read_end - m_read_ptr; // Bytes in Get area
136 
137  if (count == 0 && underflow () == EOF) {
138  DL((STRMBUFTRACE,"returning %d. count: %d\n", EOF));
139  return EOF;
140  }
141  count = m_read_end - m_read_ptr; // Adjusted bytes in Get area
142 
143  DL((STRMBUFTRACE,"Adjusted bytes in Get Area: %d\n",count));
144 
145  if (count > len_) {
146  count = len_;
147  }
148 
149  if (count <= 0) {
150  count = 0; // Peer closed connection
151  }
152  else if (count > 20) {
153  memcpy (data_, m_read_ptr, count);
154  m_read_ptr += count;
155  }
156  else {
157  char* s = data_;
158  char* p = m_read_ptr;
159  int i = count;
160  while (i-- > 0) {
161  *s++ = *p++;
162  }
163  m_read_ptr = p;
164  }
165  DL((STRMBUFTRACE,"Transferred %d bytes to user-space buffer\n", count));
166 
167  return (count);
168 }
169 
170 int
172 uflow ()
173 {
174  trace_with_mask("Streambuf::uflow",STRMBUFTRACE);
175 
176  if (underflow () == EOF)
177  return EOF;
178  dump ();
179  return *(unsigned char *) m_read_ptr++;
180 }
181 
182 int
184 xsputn (const char* data_, int len_)
185 {
186  trace_with_mask("Streambuf::xsputn",STRMBUFTRACE);
187 
188  const char* s = data_;
189  int more = len_;
190  if (more <= 0) {
191  return 0;
192  }
193 
194  for (;;) {
195  int count = m_write_end - m_write_ptr; // Space available
196 
197  if (count > 0) {
198 
199  if (count > more) // Enough buffer space
200  count = more;
201 
202  if (count > 20) {
203  memcpy (m_write_ptr, s, count);
204  s += count;
205  m_write_ptr += count;
206  }
207  else if (count <= 0) {
208  count = 0;
209  }
210  else {
211  char* p = m_write_ptr;
212  int i;
213 
214  for (i=count; --i >= 0;) {
215  *p++ = *s++;
216  }
217  m_write_ptr = p;
218  }
219  more -= count;
220  } // if (count>0)
221 
222  if (more == 0 || overflow ((unsigned char) *s++) == EOF) {
223  break;
224  }
225  more--;
226 
227  } // for (;;)
228 
229  return (len_ - more);
230 }
231 
232 
233 int
235 {
236  trace_with_mask("Streambuf::doallocate",STRMBUFTRACE);
237 
238  char* buf;
239  buf = new char [1024];
240  if (buf == NULL) {
241  return EOF;
242  }
243  setb (buf, buf+1024, 1);
244 
245  return 1;
246 }
247 
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
unsigned long u_long
Definition: Logger_Impl.h:41
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
Streambuf class is based on Standard C++ iostream streambuf class.
const char * getMemDump() const
Obtain a pointer to the dump image (null-terminated char string).
Definition: MemDump.h:94
Streambuf class.
Definition: Streambuf.h:91
virtual int underflow()
This function is called to supply characters for input (from some source) when the get area is empty,...
Definition: Streambuf.h:598
void setp(char *pbeg_, char *pend_)
Set put area pointers.
Definition: Streambuf.h:588
virtual Streambuf * setbuf(char *p_, int len_)
Performs an operation that is defined separately for each class derived from Streambuf.
Definition: Streambuf.cpp:100
virtual int overflow(int c=EOF)
This function is called to consume characters (flush them to output), typically when the put area is ...
Definition: Streambuf.h:607
int unbuffered()
Definition: Streambuf.h:408
virtual int doallocate()
This function is called by allocate when unbuffered() is zero and base() is zero.
Definition: Streambuf.cpp:234
virtual int xsgetn(char *b_, int len_)
Assigns up to len_ characters to successive elements of the array whose first element is designated b...
Definition: Streambuf.cpp:128
virtual int uflow()
Reads the characters from the input sequence, if possible, and moves the stream position past it,...
Definition: Streambuf.cpp:172
void setb(char *b_, char *eb_, int del_)
Establish the reserve area (buffer).
Definition: Streambuf.cpp:80
virtual int sync()
This function synchronizes the streambuf with its actual stream of characters.
Definition: Streambuf.h:523
int sgetc()
This function returns the character after the get pointer, or EOF if the get pointer is at the end of...
Definition: Streambuf.h:549
void setg(char *gbeg_, char *gnext_, char *gend_)
Set get area pointers.
Definition: Streambuf.cpp:69
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).
Definition: Streambuf.cpp:184
int snextc()
This function moves the get pointer forward one position, then returns the character after the get ...
Definition: Streambuf.cpp:57
char * m_write_base
Definition: Streambuf.h:37
char * m_write_ptr
Definition: Streambuf.h:38
char * m_write_end
Definition: Streambuf.h:39
char * m_read_ptr
Definition: Streambuf.h:34
char * m_read_end
Definition: Streambuf.h:35
char * m_buf_base
Definition: Streambuf.h:41
char * m_read_base
Definition: Streambuf.h:33
char * m_buf_end
Definition: Streambuf.h:42
void dump() const
Definition: Streambuf.cpp:23
Definition: Acceptor.h:40
@ STRMBUFTRACE
Extended Streambuf & friends messages
Definition: LogMask.h:46
@ STRMBUF
Class Streambuf & friends messages
Definition: LogMask.h:45