45 using std::ostringstream ;
46 using std::istringstream ;
102 map<string,string> &extensions )
104 if( !buffer.empty() )
106 sendChunk( buffer, extensions ) ;
109 map<string,string> no_extensions ;
110 sendChunk(
"", no_extensions ) ;
114 sendChunk(
"", extensions ) ;
123 map<string,string> extensions ;
125 send(
"", extensions ) ;
137 PPTConnection::sendChunk(
const string &buffer, map<string,string> &extensions )
140 if( extensions.size() )
144 strm << hex << setw( 7 ) << setfill(
'0' ) << buffer.length() <<
"d" ;
145 if( !buffer.empty() )
149 string toSend = strm.str() ;
161 if( extensions.size() )
163 ostringstream estrm ;
164 map<string,string>::const_iterator i = extensions.begin() ;
165 map<string,string>::const_iterator ie = extensions.end() ;
166 for( ; i != ie; i++ )
168 estrm << (*i).first ;
169 string value = (*i).second ;
172 estrm <<
"=" << value ;
176 string xstr = estrm.str() ;
177 strm << hex << setw( 7 ) << setfill(
'0' ) << xstr.length() <<
"x" << xstr ;
178 string toSend = strm.str() ;
192 BESDEBUG(
"ppt",
"PPTConnection::send - sending " << buffer << endl ) ;
204 PPTConnection::readBuffer(
char *buffer,
const unsigned int buffer_size )
210 PPTConnection::readChunkHeader(
char *buffer,
unsigned int buffer_size )
212 char *temp_buffer = buffer ;
213 int totalBytesRead = 0 ;
217 int bytesRead = readBuffer( temp_buffer, buffer_size ) ;
218 BESDEBUG(
"ppt",
"PPTConnection::readChunkHeader - read " 219 << bytesRead <<
" bytes" << endl ) ;
224 if( bytesRead < buffer_size )
226 buffer_size = buffer_size - bytesRead ;
227 temp_buffer = temp_buffer + bytesRead ;
228 totalBytesRead += bytesRead ;
232 totalBytesRead += bytesRead ;
236 buffer[totalBytesRead] =
'\0' ;
237 return totalBytesRead ;
256 PPTConnection::receive( map<string,string> &extensions,
259 ostream *use_strm =
_out ;
265 BESDEBUG(
"ppt",
"PPTConnection::receive: buffer size = " << _inBuff_len
270 _inBuff =
new char[_inBuff_len+1] ;
276 int bytesRead = readChunkHeader( _inBuff, 8 ) ;
277 BESDEBUG(
"ppt",
"Reading header, read " 278 << bytesRead <<
" bytes" << endl ) ;
281 string err =
"Failed to read length and type of chunk" ;
286 lenbuffer[0] = _inBuff[0] ;
287 lenbuffer[1] = _inBuff[1] ;
288 lenbuffer[2] = _inBuff[2] ;
289 lenbuffer[3] = _inBuff[3] ;
290 lenbuffer[4] = _inBuff[4] ;
291 lenbuffer[5] = _inBuff[5] ;
292 lenbuffer[6] = _inBuff[6] ;
293 lenbuffer[7] =
'\0' ;
294 istringstream lenstrm( lenbuffer ) ;
295 unsigned long inlen = 0 ;
296 lenstrm >> hex >> setw(7) >> inlen ;
297 BESDEBUG(
"ppt",
"Reading header, chunk length = " << inlen << endl ) ;
298 BESDEBUG(
"ppt",
"Reading header, chunk type = " << _inBuff[7] << endl ) ;
300 if( _inBuff[7] ==
'x' )
302 ostringstream xstrm ;
303 receive( xstrm, inlen ) ;
306 else if( _inBuff[7] ==
'd' )
314 receive( *use_strm, inlen ) ;
318 string err = (string)
"type of data is " + _inBuff[7]
319 +
", should be x for extensions or d for data" ;
336 PPTConnection::receive( ostream &strm,
const unsigned int len )
338 BESDEBUG(
"ppt",
"PPTConnect::receive - len = " << len << endl ) ;
341 string err =
"buffer has not been initialized" ;
345 unsigned int to_read = len ;
346 if( len > _inBuff_len )
348 to_read = _inBuff_len ;
350 BESDEBUG(
"ppt",
"PPTConnect::receive - to_read = " << to_read << endl ) ;
353 int bytesRead = readBuffer( _inBuff, to_read ) ;
356 string err =
"Failed to read data from socket" ;
359 BESDEBUG(
"ppt",
"PPTConnect::receive - bytesRead = " 360 << bytesRead << endl ) ;
363 _inBuff[bytesRead] =
'\0' ;
364 strm.write( _inBuff, bytesRead ) ;
369 if( bytesRead < len )
371 BESDEBUG(
"ppt",
"PPTConnect::receive - remaining = " 372 << (len - bytesRead) << endl ) ;
373 receive( strm, len - bytesRead ) ;
399 string::size_type semi = xstr.find(
';', index ) ;
400 if( semi == string::npos )
402 string err =
"malformed extensions " 403 + xstr.substr( index, xstr.length() - index )
404 +
", missing semicolon" ;
407 string::size_type eq = xstr.find(
'=', index ) ;
408 if( eq == string::npos || eq > semi )
411 var = xstr.substr( index, semi-index ) ;
412 extensions[var] =
"" ;
414 else if( eq == semi-1 )
416 string err =
"malformed extensions " 417 + xstr.substr( index, xstr.length() - index )
418 +
", missing value after =" ;
423 var = xstr.substr( index, eq-index ) ;
424 val = xstr.substr( eq+1, semi-eq-1 ) ;
425 extensions[var] = val ;
428 if( index >= xstr.length() )
447 const unsigned int buffer_size )
452 struct pollfd arr[1] ;
457 for(
int j = 0; j < _timeout; j++ )
459 if( poll( arr, 1, 1000 ) < 0 )
461 string error(
"poll error" ) ;
462 const char* error_info = strerror( errno ) ;
464 error +=
" " + (string)error_info ;
469 if (arr[0].revents==POLLIN)
471 return readBuffer( inBuff, buffer_size ) ;
475 cout <<
" " << j << flush ;
505 << (
void *)
this <<
")" << endl ;
virtual int readBufferNonBlocking(char *inBuff, const unsigned int buff_size)
read a buffer of data from the socket without blocking
exception thrown if inernal error encountered
virtual unsigned int getRecvBufferSize()=0
#define PPT_CHUNK_HEADER_SPACE
virtual void dump(ostream &strm) const
dumps information about this object
virtual void dump(ostream &strm) const
dumps information about this object
virtual void sendExit()
Send the exit token as an extension.
virtual void read_extensions(map< string, string > &extensions, const string &xstr)
the string passed are extensions, read them and store the name/value pairs into the passed map ...
static ostream & LMarg(ostream &strm)
virtual void send(const string &str, int start, int end)
virtual void sendExtensions(map< string, string > &extensions)
send the specified extensions
virtual unsigned int getSendChunkSize()
static string PPT_EXIT_NOW
virtual int receive(char *inBuff, const int inSize)
virtual unsigned int getRecvChunkSize()
virtual int getSocketDescriptor()
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
virtual Socket * getSocket()
virtual void send(const string &buffer)
sends the buffer to the socket
virtual unsigned int getSendBufferSize()=0