#include <buf_sock.h>
Inheritance diagram for NLNET::CBufSock:
Definition at line 52 of file buf_sock.h.
Public Member Functions | |
uint64 | appId () const |
Returns the application identifier. | |
std::string | asString () const |
Returns a string with the characteristics of the object. | |
void | setAppId (uint64 id) |
Sets the application identifier. | |
virtual | ~CBufSock () |
Destructor. | |
Data Fields | |
std::string | AuthorizedCallback |
uint32 | ReceiveNextValue |
uint32 | SendNextValue |
Protected Member Functions | |
bool | advertiseDisconnection (CBufNetBase *bnb, TSockId sockid) |
bool | advertiseSystemEvent (CBufNetBase *bnb, TSockId sockid, bool &flag, bool condition, CBufNetBase::TEventType event) |
CBufSock (CTcpSock *sock=NULL) | |
Constructor. | |
void | connect (const CInetAddress &addr, bool nodelay, bool connectedstate) |
Connects to the specified addr; set connectedstate to true if no connection advertising is needed. | |
bool | connectedState () const |
Returns the "logically connected" state (changed when processing a connection/disconnection callback). | |
void | disconnect (bool connectedstate) |
Disconnects; set connectedstate to false if no disconnection advertising is needed. | |
bool | pushBuffer (const NLMISC::CMemStream &buffer) |
void | setConnectedState (bool connectedstate) |
Sets the "logically connected" state (changed when processing a connection/disconnection callback). | |
virtual std::string | typeStr () const |
Returns "CLT " (client). | |
Protected Attributes | |
bool | _KnowConnected |
NLMISC::CBufFIFO | SendFifo |
CTcpSock * | Sock |
Private Attributes | |
uint64 | _AppId |
bool | _ConnectedState |
NLMISC::TTime | _LastFlushTime |
NLMISC::CObjectVector< uint8 > | _ReadyToSendBuffer |
TBlockSize | _RTSBIndex |
sint32 | _TriggerSize |
NLMISC::TTime | _TriggerTime |
Friends | |
class | CBufClient |
class | CBufServer |
class | CCallbackClient |
class | CCallbackNetBase |
class | CCallbackServer |
class | CClientReceiveTask |
class | CServerReceiveTask |
|
Destructor.
Definition at line 81 of file buf_sock.cpp. References _AppId, _ConnectedState, _KnowConnected, _LastFlushTime, _ReadyToSendBuffer, _RTSBIndex, _TriggerSize, _TriggerTime, AuthorizedCallback, NLMISC::CObjectVector< uint8 >::clear(), nlassert, nlnettrace, and Sock.
00082 { 00083 nlassert (this != InvalidSockId); // invalid bufsock 00084 00085 nlnettrace( "CBufSock::~CBufSock" ); 00086 00087 delete Sock; // the socket disconnects automatically if needed 00088 00089 // destroy the structur to be sure that other people will not access to this anymore 00090 AuthorizedCallback = ""; 00091 Sock = NULL; 00092 _KnowConnected = false; 00093 _LastFlushTime = 0; 00094 _TriggerTime = 0; 00095 _TriggerSize = 0; 00096 _ReadyToSendBuffer.clear (); 00097 _RTSBIndex = 0; 00098 _AppId = 0; 00099 _ConnectedState = false; 00100 } |
|
Constructor.
Definition at line 52 of file buf_sock.cpp. References _LastFlushTime, nlnettrace, and Sock.
00052 : 00053 SendNextValue(0), 00054 ReceiveNextValue(0), 00055 Sock( sock ), 00056 _KnowConnected( false ), 00057 _LastFlushTime( 0 ), 00058 _TriggerTime( 20 ), 00059 _TriggerSize( -1 ), 00060 _RTSBIndex( 0 ), 00061 _AppId( 0 ), 00062 _ConnectedState( false ) 00063 { 00064 nlnettrace( "CBufSock::CBufSock" ); // don't define a global object 00065 00066 if ( Sock == NULL ) 00067 { 00068 Sock = new CTcpSock(); 00069 } 00070 00071 #ifdef NL_DEBUG 00072 _FlushTrigger = FTManual; 00073 #endif 00074 _LastFlushTime = CTime::getLocalTime(); 00075 } |
|
Pushes a disconnection message into bnb's receive queue, if it has not already been done (returns true in this case). You can either specify a sockid (for server) or InvalidSockId (for client) Definition at line 120 of file buf_sock.h. References _KnowConnected, advertiseSystemEvent(), nlassert, and NLNET::TSockId. Referenced by NLNET::CBufClient::send(), and NLNET::CBufClient::update().
00121 { 00122 #ifdef NL_DEBUG 00123 if ( sockid != InvalidSockId ) 00124 { 00125 nlassert( sockid == this ); 00126 } 00127 #endif 00128 return advertiseSystemEvent( bnb, sockid, _KnowConnected, true, CBufNetBase::Disconnection ); 00129 } |
|
Pushes a system message into bnb's receive queue, if the flags meets the condition, then resets the flag and returns true. You can either specify a sockid (for server) or InvalidSockId (for client). Definition at line 135 of file buf_sock.h. References asString(), buffer, condition, nlassert, nldebug, NLNET::CBufNetBase::pushMessageIntoReceiveQueue(), and NLNET::TSockId. Referenced by NLNET::CServerBufSock::advertiseConnection(), and advertiseDisconnection().
00137 { 00138 #ifdef NL_DEBUG 00139 if ( sockid != InvalidSockId ) 00140 { 00141 nlassert( sockid == this ); 00142 } 00143 #endif 00144 // Test flag 00145 if ( flag==condition ) 00146 { 00147 nldebug( "LNETL1: Pushing event to %s", asString().c_str() ); 00148 std::vector<uint8> buffer; 00149 if ( sockid == InvalidSockId ) 00150 { 00151 // Client: event type only 00152 buffer.resize( 1 ); 00153 buffer[0] = event; 00154 } 00155 else 00156 { 00157 // Server: sockid + event type 00158 buffer.resize( sizeof(TSockId) + 1 ); 00159 memcpy( &*buffer.begin(), &sockid, sizeof(TSockId) ); 00160 buffer[sizeof(TSockId)] = event; 00161 } 00162 // Push 00163 bnb->pushMessageIntoReceiveQueue( buffer ); 00164 00165 // Reset flag 00166 flag = !condition; 00167 return true; 00168 } 00169 else 00170 { 00171 return false; 00172 } 00173 } |
|
Returns the application identifier.
Definition at line 63 of file buf_sock.h. References _AppId, and uint64. Referenced by NLNET::CUnifiedNetwork::CUnifiedConnection::TConnection::getAppId().
00063 { return _AppId; } |
|
Returns a string with the characteristics of the object.
Definition at line 329 of file buf_sock.cpp. References NLNET::CSock::descriptor(), Sock, NLMISC::toString(), NLMISC::toStringPtr(), and typeStr(). Referenced by advertiseSystemEvent(), NLNET::CCallbackNetBase::authorizeOnly(), NLNET::CBufServer::dataAvailable(), flush(), NLNET::CCallbackNetBase::processOneMessage(), NLNET::CNonBlockingBufSock::receivePart(), NLNET::CServerReceiveTask::run(), NLNET::CListenTask::run(), NLNET::CNetManager::send(), and NLNET::CCallbackServer::sendAllMyAssociations().
00330 { 00331 // stringstream ss; 00332 string str; 00333 if (this == InvalidSockId) // tricky 00334 str = "<null>"; 00335 else 00336 { 00337 // if it crashs here, it means that the CBufSock was deleted and you try to access to the virtual table that is empty 00338 // because the object is destroyed. 00339 str += typeStr(); 00340 str += NLMISC::toStringPtr(this) + " (socket "; 00341 00342 if (Sock == NULL) 00343 str += "<null>"; 00344 else 00345 str += NLMISC::toString(Sock->descriptor()); 00346 00347 str += ")"; 00348 } 00349 return str; 00350 } |
|
Connects to the specified addr; set connectedstate to true if no connection advertising is needed.
Definition at line 295 of file buf_sock.cpp. References _ConnectedState, _KnowConnected, addr, NLNET::CTcpSock::connect(), NLNET::CSock::connected(), nlassert, ReceiveNextValue, SendNextValue, NLNET::CTcpSock::setNoDelay(), and Sock. Referenced by NLNET::CCallbackClient::connect(), and NLNET::CBufClient::connect().
00296 { 00297 nlassert (this != InvalidSockId); // invalid bufsock 00298 nlassert( ! Sock->connected() ); 00299 00300 SendNextValue = ReceiveNextValue = 0; 00301 00302 Sock->connect( addr ); 00303 _ConnectedState = connectedstate; 00304 _KnowConnected = connectedstate; 00305 if ( nodelay ) 00306 { 00307 Sock->setNoDelay( true ); 00308 } 00309 } |
|
Returns the "logically connected" state (changed when processing a connection/disconnection callback).
Definition at line 237 of file buf_sock.h. References _ConnectedState. Referenced by NLNET::CBufClient::connected(), NLNET::CCallbackClient::disconnect(), NLNET::CBufClient::disconnect(), and NLNET::CBufClient::~CBufClient().
00237 { return _ConnectedState; } |
|
Disconnects; set connectedstate to false if no disconnection advertising is needed.
Definition at line 315 of file buf_sock.cpp. References _ConnectedState, _KnowConnected, NLNET::CTcpSock::disconnect(), nlassert, ReceiveNextValue, SendNextValue, and Sock. Referenced by NLNET::CBufClient::disconnect().
00316 { 00317 nlassert (this != InvalidSockId); // invalid bufsock 00318 Sock->disconnect(); 00319 _ConnectedState = connectedstate; 00320 _KnowConnected = connectedstate; 00321 00322 SendNextValue = ReceiveNextValue = 0; 00323 } |
|
Forces to send all data pending in the send queue
Definition at line 137 of file buf_sock.cpp. References _ReadyToSendBuffer, _RTSBIndex, asString(), NLMISC::CObjectVector< uint8 >::clear(), NLMISC::CBufFIFO::empty(), NLMISC::CBufFIFO::front(), NLMISC::CObjectVector< uint8 >::getPtr(), len, nlassert, nlassertex, nldebug, NLMISC::CBufFIFO::pop(), res, NLMISC::CObjectVector< uint8 >::resize(), NLNET::CSock::send(), SendFifo, NLMISC::CObjectVector< uint8 >::size(), size, Sock, NLNET::TBlockSize, uint, uint32, and uint8. Referenced by NLNET::CBufServer::disconnect(), NLNET::CBufClient::disconnect(), NLNET::CBufServer::flush(), NLNET::CBufClient::flush(), and update().
00138 { 00139 nlassert (this != InvalidSockId); // invalid bufsock 00140 //nlnettrace( "CBufSock::flush" ); 00141 00142 // Copy data from the send queue to _ReadyToSendBuffer 00143 TBlockSize netlen; 00144 // vector<uint8> tmpbuffer; 00145 00146 // Process each element in the send queue 00147 while ( ! SendFifo.empty() ) 00148 { 00149 uint8 *tmpbuffer; 00150 uint32 size; 00151 SendFifo.front( tmpbuffer, size ); 00152 00153 // Compute the size and add it into the beginning of the buffer 00154 netlen = htonl( (TBlockSize)size ); 00155 uint32 oldBufferSize = _ReadyToSendBuffer.size(); 00156 _ReadyToSendBuffer.resize (oldBufferSize+sizeof(TBlockSize)+size); 00157 *(TBlockSize*)&(_ReadyToSendBuffer[oldBufferSize])=netlen; 00158 //nldebug( "O-%u %u+L%u (0x%x)", Sock->descriptor(), oldBufferSize, size, size ); 00159 00160 00161 // Append the temporary buffer to the global buffer 00162 CFastMem::memcpy (&_ReadyToSendBuffer[oldBufferSize+sizeof(TBlockSize)], tmpbuffer, size); 00163 SendFifo.pop(); 00164 } 00165 00166 // Actual sending of _ReadyToSendBuffer 00167 //if ( ! _ReadyToSendBuffer.empty() ) 00168 if ( _ReadyToSendBuffer.size() != 0 ) 00169 { 00170 // Send 00171 CSock::TSockResult res; 00172 TBlockSize len = _ReadyToSendBuffer.size() - _RTSBIndex; 00173 00174 res = Sock->send( _ReadyToSendBuffer.getPtr()+_RTSBIndex, len, false ); 00175 00176 if ( res == CSock::Ok ) 00177 { 00178 /* // Debug display 00179 switch ( _FlushTrigger ) 00180 { 00181 case FTTime : nldebug( "LNETL1: Time triggered flush for %s:", asString().c_str() ); break; 00182 case FTSize : nldebug( "LNETL1: Size triggered flush for %s:", asString().c_str() ); break; 00183 default: nldebug( "LNETL1: Manual flush for %s:", asString().c_str() ); 00184 } 00185 _FlushTrigger = FTManual; 00186 nldebug( "LNETL1: %s sent effectively a buffer (%d on %d B)", asString().c_str(), len, _ReadyToSendBuffer.size() ); 00187 */ 00188 00189 // TODO OPTIM remove the nldebug for speed 00190 //commented for optimisation nldebug( "LNETL1: %s sent effectively %u/%u bytes (pos %u wantedsend %u)", asString().c_str(), len, _ReadyToSendBuffer.size(), _RTSBIndex, realLen/*, stringFromVectorPart(_ReadyToSendBuffer,_RTSBIndex,len).c_str()*/ ); 00191 00192 if ( _RTSBIndex+len == _ReadyToSendBuffer.size() ) // for non-blocking mode 00193 { 00194 // If sending is ok, clear the global buffer 00195 //nldebug( "O-%u all %u bytes (%u to %u) sent", Sock->descriptor(), len, _RTSBIndex, _ReadyToSendBuffer.size() ); 00196 _ReadyToSendBuffer.clear(); 00197 _RTSBIndex = 0; 00198 } 00199 else 00200 { 00201 // Or clear only the data that was actually sent 00202 nlassertex( _RTSBIndex+len < _ReadyToSendBuffer.size(), ("index=%u len=%u size=%u", _RTSBIndex, len, _ReadyToSendBuffer.size()) ); 00203 //nldebug( "O-%u only %u B on %u (%u to %u) sent", Sock->descriptor(), len, _ReadyToSendBuffer.size()-_RTSBIndex, _RTSBIndex, _ReadyToSendBuffer.size() ); 00204 _RTSBIndex += len; 00205 if ( _ReadyToSendBuffer.size() > 20480 ) // if big, clear data already sent 00206 { 00207 uint nbcpy = _ReadyToSendBuffer.size() - _RTSBIndex; 00208 for (uint i = 0; i < nbcpy; i++) 00209 { 00210 _ReadyToSendBuffer[i] = _ReadyToSendBuffer[i+_RTSBIndex]; 00211 } 00212 _ReadyToSendBuffer.resize(nbcpy); 00213 //_ReadyToSendBuffer.erase( _ReadyToSendBuffer.begin(), _ReadyToSendBuffer.begin()+_RTSBIndex ); 00214 _RTSBIndex = 0; 00215 //nldebug( "O-%u Cleared data already sent, %u B remain", Sock->descriptor(), nbcpy ); 00216 } 00217 } 00218 } 00219 else 00220 { 00221 #ifdef NL_DEBUG 00222 // can happen in a normal behavior if, for example, the other side is not connected anymore 00223 nldebug( "LNETL1: %s failed to send effectively a buffer of %d bytes", asString().c_str(), _ReadyToSendBuffer.size() ); 00224 #endif 00225 return false; 00226 } 00227 } 00228 00229 return true; 00230 } |
|
Pushes a buffer to the send queue and update, or returns false if the socket is not physically connected the or an error occured during sending Definition at line 178 of file buf_sock.h. References buffer, NLNET::CSock::connected(), nlassert, nlwarning, NLMISC::CBufFIFO::push(), res, SendFifo, Sock, uint32, and update(). Referenced by NLNET::CBufServer::pushBufferToHost(), and NLNET::CBufClient::send().
00179 { 00180 nlassert (this != InvalidSockId); // invalid bufsock 00181 // nldebug( "LNETL1: Pushing buffer to %s", asString().c_str() ); 00182 00183 static uint32 biggerBufferSize = 64000; 00184 if (buffer.length() > biggerBufferSize) 00185 { 00186 biggerBufferSize = buffer.length(); 00187 nlwarning ("LNETL1: new record! bigger network message pushed (sent) is %u bytes", biggerBufferSize); 00188 } 00189 00190 if ( Sock->connected() ) 00191 { 00192 // Push into host's send queue 00193 SendFifo.push( buffer ); 00194 00195 // Update sending 00196 bool res = update (); 00197 return res; // not checking the result as in CBufServer::update() 00198 } 00199 return false; 00200 } |
|
Sets the application identifier.
Definition at line 60 of file buf_sock.h. References _AppId, and uint64. Referenced by NLNET::CUnifiedNetwork::addService(), NLNET::CPacsClient::connect(), and NLNET::CUnifiedNetwork::CUnifiedConnection::TConnection::setAppId().
00060 { _AppId = id; } |
|
Sets the "logically connected" state (changed when processing a connection/disconnection callback).
Definition at line 234 of file buf_sock.h. References _ConnectedState. Referenced by NLNET::CBufServer::dataAvailable(), and NLNET::CBufClient::dataAvailable().
00234 { _ConnectedState = connectedstate; } |
|
Sets the size flush trigger. When the size of the send queue reaches or exceeds this calue, all data in the send queue is automatically sent (-1 to disable this trigger ) Definition at line 104 of file buf_sock.h. References _TriggerSize, sint32, and size. Referenced by NLNET::CBufServer::setSizeFlushTrigger(), and NLNET::CBufClient::setSizeFlushTrigger().
00104 { _TriggerSize = size; } |
|
Sets the time flush trigger (in millisecond). When this time is elapsed, all data in the send queue is automatically sent (-1 to disable this trigger) Definition at line 236 of file buf_sock.cpp. References _LastFlushTime, _TriggerTime, nlassert, and sint32. Referenced by NLNET::CBufServer::setTimeFlushTrigger(), and NLNET::CBufClient::setTimeFlushTrigger().
00237 { 00238 nlassert (this != InvalidSockId); // invalid bufsock 00239 _TriggerTime = ms; 00240 _LastFlushTime = CTime::getLocalTime(); 00241 } |
|
Returns "CLT " (client).
Reimplemented in NLNET::CServerBufSock. Definition at line 115 of file buf_sock.h. Referenced by asString().
00115 { return "CLT "; } |
|
Update the network sending (call this method evenly). Returns false if an error occured.
Definition at line 247 of file buf_sock.cpp. References _LastFlushTime, _TriggerSize, _TriggerTime, flush(), nlassert, SendFifo, sint32, NLMISC::CBufFIFO::size(), and NLMISC::TTime. Referenced by pushBuffer(), and NLNET::CBufClient::update().
00248 { 00249 nlassert (this != InvalidSockId); // invalid bufsock 00250 // nlnettrace( "CBufSock::update-BEGIN" ); 00251 // Time trigger 00252 00253 if ( _TriggerTime != -1 ) 00254 { 00255 TTime now = CTime::getLocalTime(); 00256 if ( (sint32)(now-_LastFlushTime) >= _TriggerTime ) 00257 { 00258 #ifdef NL_DEBUG 00259 _FlushTrigger = FTTime; 00260 #endif 00261 if ( flush() ) 00262 { 00263 _LastFlushTime = now; 00264 // nlnettrace ( "CBufSock::update-END time 1" ); 00265 return true; 00266 } 00267 else 00268 { 00269 // nlnettrace ( "CBufSock::update-END time 0" ); 00270 return false; 00271 } 00272 } 00273 } 00274 // Size trigger 00275 if ( _TriggerSize != -1 ) 00276 { 00277 if ( (sint32)SendFifo.size() > _TriggerSize ) 00278 { 00279 #ifdef NL_DEBUG 00280 _FlushTrigger = FTSize; 00281 #endif 00282 // nlnettrace( "CBufSock::update-END size" ); 00283 return flush(); 00284 } 00285 } 00286 // nlnettrace( "CBufSock::update-END nosend" ); 00287 return true; 00288 } |
|
Reimplemented in NLNET::CNonBlockingBufSock. Definition at line 78 of file buf_sock.h. |
|
Reimplemented in NLNET::CServerBufSock. Definition at line 79 of file buf_sock.h. |
|
Definition at line 83 of file buf_sock.h. |
|
Definition at line 85 of file buf_sock.h. |
|
Definition at line 84 of file buf_sock.h. |
|
Reimplemented in NLNET::CNonBlockingBufSock. Definition at line 80 of file buf_sock.h. |
|
Reimplemented in NLNET::CServerBufSock. Definition at line 81 of file buf_sock.h. |
|
Definition at line 262 of file buf_sock.h. Referenced by appId(), setAppId(), and ~CBufSock(). |
|
Definition at line 265 of file buf_sock.h. Referenced by connect(), connectedState(), disconnect(), setConnectedState(), and ~CBufSock(). |
|
Definition at line 246 of file buf_sock.h. Referenced by advertiseDisconnection(), connect(), disconnect(), and ~CBufSock(). |
|
Definition at line 255 of file buf_sock.h. Referenced by CBufSock(), setTimeFlushTrigger(), update(), and ~CBufSock(). |
|
Definition at line 259 of file buf_sock.h. Referenced by flush(), and ~CBufSock(). |
|
Definition at line 260 of file buf_sock.h. Referenced by flush(), and ~CBufSock(). |
|
Definition at line 257 of file buf_sock.h. Referenced by setSizeFlushTrigger(), update(), and ~CBufSock(). |
|
Definition at line 256 of file buf_sock.h. Referenced by setTimeFlushTrigger(), update(), and ~CBufSock(). |
|
Little tricky but this string is used by Layer4 to know which callback is authorized. This is empty when all callback are authorized. Definition at line 70 of file buf_sock.h. Referenced by NLNET::CCallbackNetBase::authorizeOnly(), NLNET::CCallbackNetBase::processOneMessage(), and ~CBufSock(). |
|
Definition at line 74 of file buf_sock.h. Referenced by connect(), and disconnect(). |
|
Definition at line 240 of file buf_sock.h. Referenced by NLNET::CBufServer::displaySendQueueStat(), NLNET::CBufClient::displaySendQueueStat(), flush(), NLNET::CBufServer::getSendQueueSize(), NLNET::CBufClient::getSendQueueSize(), pushBuffer(), and update(). |
|
Definition at line 74 of file buf_sock.h. Referenced by connect(), disconnect(), and NLNET::CBufServer::send(). |
|