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 ) ;
209 PPTConnection::readBuffer(
char *buffer,
const unsigned int buffer_size )
215 PPTConnection::readChunkHeader(
char *buffer,
int buffer_size )
217 char *temp_buffer = buffer ;
218 int totalBytesRead = 0 ;
222 int bytesRead = readBuffer( temp_buffer, buffer_size ) ;
223 BESDEBUG(
"ppt",
"PPTConnection::readChunkHeader - read "
224 << bytesRead <<
" bytes" << endl ) ;
229 if( bytesRead < buffer_size )
231 buffer_size = buffer_size - bytesRead ;
232 temp_buffer = temp_buffer + bytesRead ;
233 totalBytesRead += bytesRead ;
237 totalBytesRead += bytesRead ;
241 buffer[totalBytesRead] =
'\0' ;
242 return totalBytesRead ;
261 PPTConnection::receive( map<string,string> &extensions,
264 ostream *use_strm =
_out ;
270 BESDEBUG(
"ppt",
"PPTConnection::receive: buffer size = " << _inBuff_len
275 _inBuff =
new char[_inBuff_len+1] ;
281 int bytesRead = readChunkHeader( _inBuff, 8 ) ;
282 BESDEBUG(
"ppt",
"Reading header, read "
283 << bytesRead <<
" bytes" << endl ) ;
286 string err =
"Failed to read length and type of chunk" ;
291 lenbuffer[0] = _inBuff[0] ;
292 lenbuffer[1] = _inBuff[1] ;
293 lenbuffer[2] = _inBuff[2] ;
294 lenbuffer[3] = _inBuff[3] ;
295 lenbuffer[4] = _inBuff[4] ;
296 lenbuffer[5] = _inBuff[5] ;
297 lenbuffer[6] = _inBuff[6] ;
298 lenbuffer[7] =
'\0' ;
299 istringstream lenstrm( lenbuffer ) ;
300 unsigned long inlen = 0 ;
301 lenstrm >> hex >> setw(7) >> inlen ;
302 BESDEBUG(
"ppt",
"Reading header, chunk length = " << inlen << endl ) ;
303 BESDEBUG(
"ppt",
"Reading header, chunk type = " << _inBuff[7] << endl ) ;
305 if( _inBuff[7] ==
'x' )
307 ostringstream xstrm ;
308 receive( xstrm, inlen ) ;
311 else if( _inBuff[7] ==
'd' )
319 receive( *use_strm, inlen ) ;
323 string err = (string)
"type of data is " + _inBuff[7]
324 +
", should be x for extensions or d for data" ;
341 PPTConnection::receive( ostream &strm,
const int len )
343 BESDEBUG(
"ppt",
"PPTConnect::receive - len = " << len << endl ) ;
346 string err =
"buffer has not been initialized" ;
351 if( len > _inBuff_len )
353 to_read = _inBuff_len ;
355 BESDEBUG(
"ppt",
"PPTConnect::receive - to_read = " << to_read << endl ) ;
358 int bytesRead = readBuffer( _inBuff, to_read ) ;
361 string err =
"Failed to read data from socket" ;
364 BESDEBUG(
"ppt",
"PPTConnect::receive - bytesRead = "
365 << bytesRead << endl ) ;
368 _inBuff[bytesRead] =
'\0' ;
369 strm.write( _inBuff, bytesRead ) ;
374 if( bytesRead < len )
376 BESDEBUG(
"ppt",
"PPTConnect::receive - remaining = "
377 << (len - bytesRead) << endl ) ;
378 receive( strm, len - bytesRead ) ;
400 unsigned int index = 0 ;
404 string::size_type semi = xstr.find(
';', index ) ;
405 if( semi == string::npos )
407 string err =
"malformed extensions "
408 + xstr.substr( index, xstr.length() - index )
409 +
", missing semicolon" ;
412 string::size_type eq = xstr.find(
'=', index ) ;
413 if( eq == string::npos || eq > semi )
416 var = xstr.substr( index, semi-index ) ;
417 extensions[var] =
"" ;
419 else if( eq == semi-1 )
421 string err =
"malformed extensions "
422 + xstr.substr( index, xstr.length() - index )
423 +
", missing value after =" ;
428 var = xstr.substr( index, eq-index ) ;
429 val = xstr.substr( eq+1, semi-eq-1 ) ;
430 extensions[var] = val ;
433 if( index >= xstr.length() )
452 const int buffer_size )
457 struct pollfd arr[1] ;
462 for(
int j = 0; j < _timeout; j++ )
464 if( poll( arr, 1, 1000 ) < 0 )
466 string error(
"poll error" ) ;
469 if (errno == EINTR || errno == EAGAIN)
472 const char* error_info = strerror( errno ) ;
474 error +=
" " + (string)error_info ;
479 if (arr[0].revents==POLLIN)
481 return readBuffer( inBuff, buffer_size ) ;
485 cout <<
" " << j << flush ;
515 << (
void *)
this <<
")" << endl ;