Home | nevrax.com |
|
buf_net_base.hGo 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_NET_BASE_H 00027 #define NL_BUF_NET_BASE_H 00028 00029 #include "nel/misc/types_nl.h" 00030 #include "nel/misc/mutex.h" 00031 #include "nel/misc/buf_fifo.h" 00032 #include "nel/misc/thread.h" 00033 #include "nel/misc/debug.h" 00034 #include "nel/misc/common.h" 00035 00036 namespace NLNET { 00037 00038 00039 class CBufSock; 00040 00042 typedef CBufSock *TSockId; 00043 00044 static const TSockId InvalidSockId = (TSockId) NULL; 00045 00047 typedef void (*TNetCallback) ( TSockId from, void *arg ); 00048 00050 typedef std::pair<TNetCallback,TSockId> TStoredNetCallback; 00051 00053 typedef NLMISC::CSynchronized<NLMISC::CBufFIFO> CSynchronizedFIFO; 00054 00056 typedef CSynchronizedFIFO::CAccessor CFifoAccessor; 00057 00059 typedef uint32 TBlockSize; 00060 00061 00073 class CBufNetBase 00074 { 00075 public: 00076 00078 enum TEventType { User = 'U', Connection = 'C', Disconnection = 'D' }; 00079 00081 virtual ~CBufNetBase() {}; 00082 00084 void setDisconnectionCallback( TNetCallback cb, void* arg ) { _DisconnectionCallback = cb; _DisconnectionCbArg = arg; } 00085 00087 uint32 getReceiveQueueSize() 00088 { 00089 CFifoAccessor syncfifo( &_RecvFifo ); 00090 return syncfifo.value().size(); 00091 } 00092 00100 void setMaxExpectedBlockSize( sint32 limit ) 00101 { 00102 if ( limit < 0 ) 00103 _MaxExpectedBlockSize = 0x7FFFFFF; 00104 else 00105 _MaxExpectedBlockSize = (uint32)limit; 00106 } 00107 00115 void setMaxSentBlockSize( sint32 limit ) 00116 { 00117 if ( limit < 0 ) 00118 _MaxSentBlockSize = 0x7FFFFFF; 00119 else 00120 _MaxSentBlockSize = (uint32)limit; 00121 } 00122 00124 uint32 maxExpectedBlockSize() const 00125 { 00126 return _MaxExpectedBlockSize; 00127 } 00128 00130 uint32 maxSentBlockSize() const 00131 { 00132 return _MaxSentBlockSize; 00133 } 00134 00135 protected: 00136 00137 friend class NLNET::CBufSock; 00138 00140 CBufNetBase(); 00141 00143 CSynchronizedFIFO& receiveQueue() { return _RecvFifo; } 00144 00146 TNetCallback disconnectionCallback() const { return _DisconnectionCallback; } 00147 00149 void* argOfDisconnectionCallback() const { return _DisconnectionCbArg; } 00150 00152 // TODO OPTIM never use this function 00153 void pushMessageIntoReceiveQueue( const std::vector<uint8>& buffer ) 00154 { 00155 //sint32 mbsize; 00156 { 00157 //nldebug( "BNB: Acquiring the receive queue... "); 00158 CFifoAccessor recvfifo( &_RecvFifo ); 00159 //nldebug( "BNB: Acquired, pushing the received buffer... "); 00160 recvfifo.value().push( buffer ); 00161 //nldebug( "BNB: Pushed, releasing the receive queue..." ); 00162 //mbsize = recvfifo.value().size() / 1048576; 00163 setDataAvailableFlag( true ); 00164 } 00165 //nldebug( "BNB: Released." ); 00166 //if ( mbsize > 1 ) 00167 //{ 00168 // nlwarning( "The receive queue size exceeds %d MB", mbsize ); 00169 //} 00170 } 00171 00172 void pushMessageIntoReceiveQueue( const uint8 *buffer, uint32 size) 00173 { 00174 //sint32 mbsize; 00175 { 00176 //nldebug( "BNB: Acquiring the receive queue... "); 00177 CFifoAccessor recvfifo( &_RecvFifo ); 00178 //nldebug( "BNB: Acquired, pushing the received buffer... "); 00179 recvfifo.value().push( buffer, size ); 00180 //nldebug( "BNB: Pushed, releasing the receive queue..." ); 00181 //mbsize = recvfifo.value().size() / 1048576; 00182 setDataAvailableFlag( true ); 00183 } 00184 //nldebug( "BNB: Released." ); 00185 /*if ( mbsize > 1 ) 00186 { 00187 nlwarning( "The receive queue size exceeds %d MB", mbsize ); 00188 }*/ 00189 } 00190 00192 void setDataAvailableFlag( bool da ) { _DataAvailable = da; } 00193 00195 volatile bool dataAvailableFlag() const { return _DataAvailable; } 00196 00197 private: 00198 00200 CSynchronizedFIFO _RecvFifo; 00201 00203 volatile bool _DataAvailable; 00204 00206 TNetCallback _DisconnectionCallback; 00207 00209 void* _DisconnectionCbArg; 00210 00212 uint32 _MaxExpectedBlockSize; 00213 00215 uint32 _MaxSentBlockSize; 00216 }; 00217 00218 00219 } // NLNET 00220 00221 00222 #endif // NL_BUF_NET_BASE_H 00223 00224 /* End of buf_net_base.h */ |