# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

buf_sock.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
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 //#include <deque>
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         // debug features, we number all packet to be sure that they are all sent and received
00073         // \todo remove this debug feature when ok
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                 // Test flag
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                                 // 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         }
00174 
00178         bool pushBuffer( const NLMISC::CMemStream& buffer )
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         }
00201 
00202         /*bool pushBuffer( const std::vector<uint8>& buffer )
00203         {
00204                 nlassert (this != InvalidSockId);       // invalid bufsock
00205 //              nldebug( "LNETL1: Pushing buffer to %s", asString().c_str() );
00206 
00207                 static uint32 biggerBufferSize = 64000;
00208                 if (buffer.size() > biggerBufferSize)
00209                 {
00210                         biggerBufferSize = buffer.size();
00211                         nlwarning ("LNETL1: new record! bigger network message pushed (sent) is %u bytes", biggerBufferSize);
00212                 }
00213 
00214                 if ( Sock->connected() )
00215                 {
00216                         // Push into host's send queue
00217                         SendFifo.push( buffer );
00218 
00219                         // Update sending
00220                         bool res = update ();
00221                         return res; // not checking the result as in CBufServer::update()
00222                 }
00223                 return false;
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         // Send queue
00240         NLMISC::CBufFIFO        SendFifo;
00241 
00242         // Socket (pointer because it can be allocated by an accept())
00243         CTcpSock                        *Sock;
00244 
00245         // Prevents from pushing a connection/disconnection event twice
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; // updated only if time trigger is enabled (TriggerTime!=-1)
00256         NLMISC::TTime           _TriggerTime;
00257         sint32                          _TriggerSize;
00258 
00259         NLMISC::CObjectVector<uint8> _ReadyToSendBuffer;
00260         TBlockSize                      _RTSBIndex;
00261 
00262         uint64                          _AppId;
00263 
00264         // Connected state (from the user's point of view, i.e. changed when the connection/disconnection event is at the front of the receive queue)
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         // True if the length prefix has already been read
00320         bool                            _NowReadingBuffer;
00321 
00322         // Counts the number of bytes read for the current element (length prefix or buffer)
00323         TBlockSize                      _BytesRead;
00324 
00325         // Length of buffer to read
00326         TBlockSize                      _Length;
00327 
00328         // Buffer for nonblocking receives
00329         std::vector<uint8>      _ReceiveBuffer;
00330 
00331         // The task that "owns" the CServerBufSock object
00332         CServerReceiveTask      *_OwnerTask;
00333 
00334 };
00335 
00336 
00337 } // NLNET
00338 
00339 #endif // NL_BUF_SOCK_H
00340 
00341 /* End of buf_sock.h */