00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_BUF_SOCK_H
00027 #define NL_BUF_SOCK_H
00028
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/misc/hierarchical_timer.h"
00031
00032 #include "nel/net/buf_net_base.h"
00033 #include "nel/net/tcp_sock.h"
00034
00035
00036
00037 namespace NLNET {
00038
00039
00040 #define nlnettrace(__msg) //nldebug("LNETL1: %s",__msg);
00041
00042
00043 class CTcpSock;
00044
00045 class CBufNetBase;
00046
00047
00052 class CBufSock
00053 {
00054 public:
00055
00057 virtual ~CBufSock();
00058
00060 void setAppId( uint64 id ) { _AppId = id; }
00061
00063 uint64 appId() const { return _AppId; }
00064
00066 std::string asString() const;
00067
00070 std::string AuthorizedCallback;
00071
00072
00073
00074 uint32 SendNextValue, ReceiveNextValue;
00075
00076 protected:
00077
00078 friend class CBufClient;
00079 friend class CBufServer;
00080 friend class CClientReceiveTask;
00081 friend class CServerReceiveTask;
00082
00083 friend class CCallbackClient;
00084 friend class CCallbackServer;
00085 friend class CCallbackNetBase;
00086
00088 CBufSock( CTcpSock *sock=NULL );
00089
00091
00092
00094 bool update();
00095
00099 void setTimeFlushTrigger( sint32 ms );
00100
00104 void setSizeFlushTrigger( sint32 size ) { _TriggerSize = size; }
00105
00110 bool flush();
00111
00113
00115 virtual std::string typeStr() const { return "CLT "; }
00116
00120 bool advertiseDisconnection( CBufNetBase *bnb, TSockId sockid )
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 }
00130
00131
00135 bool advertiseSystemEvent(
00136 CBufNetBase *bnb, TSockId sockid, bool& flag, bool condition, CBufNetBase::TEventType event )
00137 {
00138 #ifdef NL_DEBUG
00139 if ( sockid != InvalidSockId )
00140 {
00141 nlassert( sockid == this );
00142 }
00143 #endif
00144
00145 if ( flag==condition )
00146 {
00147 nldebug( "Pushing event to %s", asString().c_str() );
00148 std::vector<uint8> buffer;
00149 if ( sockid == InvalidSockId )
00150 {
00151
00152 buffer.resize( 1 );
00153 buffer[0] = event;
00154 }
00155 else
00156 {
00157
00158 buffer.resize( sizeof(TSockId) + 1 );
00159 memcpy( &*buffer.begin(), &sockid, sizeof(TSockId) );
00160 buffer[sizeof(TSockId)] = event;
00161 }
00162
00163 bnb->pushMessageIntoReceiveQueue( buffer );
00164
00165
00166 flag = !condition;
00167 return true;
00168 }
00169 else
00170 {
00171 return false;
00172 }
00173 }
00174
00178 bool pushBuffer( const NLMISC::CMemStream& buffer )
00179 {
00180 nlassert (this != InvalidSockId);
00181
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
00193 SendFifo.push( buffer );
00194
00195
00196 bool res = update ();
00197 return res;
00198 }
00199 return false;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00228 void connect( const CInetAddress& addr, bool nodelay, bool connectedstate );
00229
00231 void disconnect( bool connectedstate );
00232
00234 void setConnectedState( bool connectedstate ) { _ConnectedState = connectedstate; }
00235
00237 bool connectedState() const { return _ConnectedState; }
00238
00239
00240 NLMISC::CBufFIFO SendFifo;
00241
00242
00243 CTcpSock *Sock;
00244
00245
00246 bool _KnowConnected;
00247
00248 private:
00249
00250 #ifdef NL_DEBUG
00251 enum TFlushTrigger { FTTime, FTSize, FTManual };
00252 TFlushTrigger _FlushTrigger;
00253 #endif
00254
00255 NLMISC::TTime _LastFlushTime;
00256 NLMISC::TTime _TriggerTime;
00257 sint32 _TriggerSize;
00258
00259 NLMISC::CObjectVector<uint8> _ReadyToSendBuffer;
00260 TBlockSize _RTSBIndex;
00261
00262 uint64 _AppId;
00263
00264
00265 bool _ConnectedState;
00266 };
00267
00268
00269 class CBufServer;
00270
00271
00276 class CServerBufSock : public CBufSock
00277 {
00278 protected:
00279
00280 friend class CBufServer;
00281 friend class CListenTask;
00282 friend class CServerReceiveTask;
00283
00287 CServerBufSock( CTcpSock *sock );
00288
00290 void setOwnerTask( CServerReceiveTask* owner ) { _OwnerTask = owner; }
00291
00293 CServerReceiveTask *ownerTask() { return _OwnerTask; }
00294
00298 bool receivePart();
00299
00301 const std::vector<uint8> receivedBuffer() const { nlnettrace( "CServerBufSock::receivedBuffer" ); return _ReceiveBuffer; }
00302
00306 bool advertiseConnection( CBufServer *bnb )
00307 {
00308 return advertiseSystemEvent( (CBufNetBase*)bnb, this, _KnowConnected, false, CBufNetBase::Connection );
00309 }
00310
00312 virtual std::string typeStr() const { return "SRV "; }
00313
00314 private:
00315
00317 bool _Advertised;
00318
00319
00320 bool _NowReadingBuffer;
00321
00322
00323 TBlockSize _BytesRead;
00324
00325
00326 TBlockSize _Length;
00327
00328
00329 std::vector<uint8> _ReceiveBuffer;
00330
00331
00332 CServerReceiveTask *_OwnerTask;
00333
00334 };
00335
00336
00337 }
00338
00339 #endif // NL_BUF_SOCK_H
00340
00341