#include <ClientServerUtils.h>
Public Types | |
| enum | { MaxChunkSize = MAXLINE } |
Public Member Functions | |
| SocketInterface (int portNum, int timeOutSeconds=10000) | |
| void | setTimeOut (int timeOutSeconds) |
| void | getAtLeast (int numBytes) |
| template<typename T> | |
| void | sendStruct (T *ptr) |
| void | copy (char *target, int numBytes) |
| template<typename T> | |
| void | receiveStruct (T *ptr) |
| void | checkSocketEmpty (void) |
| void | sendSequence (const WordSequence &seq) |
| void | receiveSequence (WordSequence &seq) |
| void | sendString (const string &s) |
| void | sendChars (const char *pChar, int numChars) |
| void | receiveString (string &s) |
| void | receiveChars (vector< char > &v) |
Protected Attributes | |
| int | portNum_ |
| int | bytesRead_ |
| int | bytesSent_ |
| timeval | timeOut_ |
| char | buffer_ [MAXLINE] |
Definition at line 225 of file ClientServerUtils.h.
| anonymous enum |
| SocketInterface::SocketInterface | ( | int | portNum, | |
| int | timeOutSeconds = 10000 | |||
| ) | [inline] |
Definition at line 228 of file ClientServerUtils.h.
References timeOut_.
00228 : 00229 portNum_( portNum ), bytesRead_(0), bytesSent_(0) 00230 { 00231 timeOut_.tv_sec = timeOutSeconds; 00232 timeOut_.tv_usec = 0; 00233 }
| void SocketInterface::setTimeOut | ( | int | timeOutSeconds | ) | [inline] |
Definition at line 236 of file ClientServerUtils.h.
References timeOut_.
Referenced by sendQuery().
00236 { 00237 timeOut_.tv_sec = timeOutSeconds; 00238 }
Here is the caller graph for this function:

| void SocketInterface::getAtLeast | ( | int | numBytes | ) |
Definition at line 483 of file ClientServerUtils.cpp.
References buffer_, MaxChunkSize, portNum_, and timeOut_.
Referenced by copy().
00484 { 00485 00486 // cout << " getAtLeast " << numBytes << " bytes\n"; 00487 00488 int numRead(0); 00489 int totalNumRead(0); 00490 int numTries(0); 00491 fd_set fds; 00492 FD_ZERO(&fds); 00493 FD_SET(portNum_, &fds); 00494 00495 while(1) 00496 { 00497 // bit of a cheat using numRead twice here ... 00498 numRead=select(portNum_+1, &fds, NULL, NULL, &timeOut_ ); 00499 if (numRead==0) 00500 { 00501 throw NetworkException 00502 ("Timed out getting data from socket: " + (string)strerror(errno) ); 00503 } // ~if 00504 else if (numRead<0) 00505 { 00506 throw NetworkException 00507 ("Select error getting data from socket: " + (string)strerror(errno) ); 00508 } // ~else if 00509 00510 // numRead=read(portNum_,(void*)&buffer_[0],MaxChunkSize); 00511 numRead=recv(portNum_,(void*)&buffer_[0],MaxChunkSize,0); 00512 if (numRead==0) 00513 { 00514 if (++numTries==100) 00515 throw NetworkException 00516 ("Reading zero bytes from socket, probable peer disconnect: " 00517 + (string)strerror(errno) ); 00518 } 00519 if ( numRead < 0 ) 00520 { 00521 if (errno==EINTR) continue; 00522 else 00523 { 00524 // throw NetworkException("getAtLeast error"); 00525 throw NetworkException 00526 ( (string)"getAtLeast error: " + (string)strerror(errno) ); 00527 } // ~else 00528 } // ~if 00529 // cout << " got " << numRead << " so far...\n"; 00530 insert(end(),&buffer_[0],&buffer_[numRead]); 00531 totalNumRead +=numRead; 00532 if (totalNumRead>=numBytes) break; 00533 00534 } // ~while 00535 00536 // cout << " total got " << totalNumRead << endl; 00537 00538 }
Here is the caller graph for this function:

| void SocketInterface::sendStruct | ( | T * | ptr | ) | [inline] |
Definition at line 242 of file ClientServerUtils.h.
References bytesSent_, portNum_, and Writen().
Referenced by processQuery(), MatchTaskServer::sendMatches(), sendQuery(), and sendSequence().
00243 { 00244 bytesSent_+= sizeof(T); 00245 Writen(portNum_, (void*) ptr, sizeof(T) ); 00246 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::copy | ( | char * | target, | |
| int | numBytes | |||
| ) |
Definition at line 472 of file ClientServerUtils.cpp.
References getAtLeast().
Referenced by receiveSequence(), and receiveStruct().
00473 { 00474 // cout << "copy - requested " << numBytes << ", got " << size() << endl; 00475 if (size()<numBytes) getAtLeast(numBytes-size()); 00476 00477 for( int i(0) ; i < numBytes ; ++ i ) 00478 { *target++ = front(); pop_front(); } 00479 00480 // cout << "final size " << size() << endl; 00481 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::receiveStruct | ( | T * | ptr | ) | [inline] |
Definition at line 250 of file ClientServerUtils.h.
References copy().
Referenced by processQuery(), receiveChars(), receiveSequence(), receiveString(), and sendQuery().
00251 { 00252 copy( (char*)ptr, sizeof(T)); 00253 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::checkSocketEmpty | ( | void | ) |
Definition at line 437 of file ClientServerUtils.cpp.
References buffer_, MAXLINE, and portNum_.
Referenced by processQuery(), and sendQuery().
00438 { 00439 00440 // Linux and other Unixes doesn't have MSG_NONBLOCK as an argument to recv. Workaround by DJC. 00441 00442 00443 // Is the socket blocking at the moment? 00444 bool blocking; 00445 00446 int flags=fcntl(portNum_,F_GETFL,0); 00447 int newflags=flags; 00448 00449 if (flags==-1) 00450 throw NetworkException("Problem reading status (fcntl) of socket"); 00451 00452 00453 // Make it non-blocking 00454 newflags|=O_NONBLOCK; 00455 fcntl(portNum_,F_SETFL,newflags); 00456 00457 // See if we can read data 00458 int n(recv( portNum_, buffer_, MAXLINE, MSG_PEEK)); 00459 00460 // Put the socket back to how it was before (blocking or non-blocking) 00461 fcntl(portNum_,F_SETFL,flags); 00462 00463 //perror("error status = "); 00464 if (errno!=EWOULDBLOCK) 00465 { 00466 // cout << "There are " << n << " bytes waiting to be read.\n"; 00467 if (n>0) throw NetworkException("Unexpected data at socket!"); 00468 } 00469 00470 }
Here is the caller graph for this function:

| void SocketInterface::sendSequence | ( | const WordSequence & | seq | ) |
Definition at line 350 of file ClientServerUtils.cpp.
References SequenceHeader::basesInLast, WordSequence::getNumBasesInLast(), portNum_, sendStruct(), SequenceHeader::size, and Writen().
Referenced by sendQuery().
00351 { 00352 SequenceHeader sinfo; 00353 sinfo.size = seq.end() - seq.begin(); 00354 sinfo.basesInLast = seq.getNumBasesInLast(); 00355 00356 sendStruct(&sinfo); 00357 Writen(portNum_, (void*)&(*seq.begin()), sinfo.size*sizeof(Word)); 00358 00359 // bytesSent_+=sinfo.size*sizeof(Word)+sizeof(sinfo); 00360 // cout << "@sent seq " << sinfo.size*sizeof(Word)+sizeof(sinfo) 00361 // << " bytes, " << bytesSent_ << "total.\n"; 00362 00363 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::receiveSequence | ( | WordSequence & | seq | ) |
Definition at line 366 of file ClientServerUtils.cpp.
References SequenceHeader::basesInLast, bytesRead_, copy(), receiveStruct(), WordSequence::setNumBasesInLast(), and SequenceHeader::size.
Referenced by processQuery().
00367 { 00368 SequenceHeader sinfo; 00369 receiveStruct(&sinfo); 00370 // cout << "receiveSeq: " << sinfo.size << " " << sinfo.basesInLast << endl; 00371 seq.resize(sinfo.size); 00372 00373 copy((char*)&(*seq.begin()), sinfo.size*sizeof(Word)); 00374 seq.setNumBasesInLast(sinfo.basesInLast); 00375 00376 bytesRead_+=sinfo.size*sizeof(Word)+sizeof(sinfo); 00377 // cout << "@read seq " << sinfo.size*sizeof(Word)+sizeof(sinfo) 00378 // << " bytes, " << bytesRead_ << "total.\n"; 00379 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::sendString | ( | const string & | s | ) |
Definition at line 381 of file ClientServerUtils.cpp.
References bytesSent_, portNum_, and Writen().
Referenced by MatchTaskServer::sendMatches().
00382 { 00383 Writen(portNum_, (void*)(s.c_str()), s.size()+1); 00384 bytesSent_+=s.size()+1; 00385 // cout << "@send string " << s.size()+1 << " bytes, " << bytesSent_ 00386 // << "total.\n"; 00387 // +1 cos we need to send the \0 at the end of the string 00388 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::sendChars | ( | const char * | pChar, | |
| int | numChars | |||
| ) |
Definition at line 390 of file ClientServerUtils.cpp.
References bytesSent_, portNum_, and Writen().
Referenced by MatchTaskServer::sendMatches().
00391 { 00392 static const char c('\0'); 00393 Writen(portNum_, (void*)pChar, numChars); 00394 Writen(portNum_, (void*)&c, 1); 00395 bytesSent_+=numChars+1; 00396 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::receiveString | ( | string & | s | ) |
Definition at line 399 of file ClientServerUtils.cpp.
References receiveStruct().
Referenced by sendQuery().
00400 { 00401 s=""; 00402 char c; 00403 while(1) 00404 { 00405 receiveStruct(&c); 00406 if((c=='\0')||(c=='\n')) return; 00407 s+=c; 00408 } 00409 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketInterface::receiveChars | ( | vector< char > & | v | ) |
Definition at line 411 of file ClientServerUtils.cpp.
References receiveStruct().
00412 { 00413 v.clear(); 00414 char c; 00415 while(1) 00416 { 00417 receiveStruct(&c); 00418 if((c=='\0')||(c=='\n')) return; 00419 v.push_back(c); 00420 } 00421 }
Here is the call graph for this function:

int SocketInterface::portNum_ [protected] |
Definition at line 268 of file ClientServerUtils.h.
Referenced by checkSocketEmpty(), getAtLeast(), sendChars(), sendSequence(), sendString(), and sendStruct().
int SocketInterface::bytesRead_ [protected] |
int SocketInterface::bytesSent_ [protected] |
Definition at line 270 of file ClientServerUtils.h.
Referenced by sendChars(), sendString(), and sendStruct().
timeval SocketInterface::timeOut_ [protected] |
Definition at line 272 of file ClientServerUtils.h.
Referenced by getAtLeast(), setTimeOut(), and SocketInterface().
char SocketInterface::buffer_[MAXLINE] [protected] |
Definition at line 273 of file ClientServerUtils.h.
Referenced by checkSocketEmpty(), and getAtLeast().
1.5.2