Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / net / buf_net_base.cpp
blobeefc22cfc4c165bb447f7f112313516ad5cea9ed
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdnet.h"
19 #include "nel/net/buf_net_base.h"
21 using namespace NLMISC;
22 using namespace std;
25 namespace NLNET {
27 uint32 NbNetworkTask = 0;
29 // The value that will be used if setMaxExpectedBlockSize() is not called (or called with a negative argument)
30 uint32 CBufNetBase::DefaultMaxExpectedBlockSize = 1048576; // 10 M unless changed by a NeL variable
32 // The value that will be used if setMaxSentBlockSize() is not called (or called with a negative argument)
33 uint32 CBufNetBase::DefaultMaxSentBlockSize = 1048576; // 10 M unless changed by a NeL variable
36 /***************************************************************************************************
37 * User main thread
38 **************************************************************************************************/
42 * Constructor
44 #ifdef NL_OS_UNIX
45 CBufNetBase::CBufNetBase( bool isDataAvailablePipeSelfManaged ) :
46 #else
47 CBufNetBase::CBufNetBase() :
48 #endif
49 _RecvFifo("CBufNetBase::_RecvFifo"),
50 _DisconnectionCallback( NULL ),
51 _DisconnectionCbArg( NULL ),
52 _MaxExpectedBlockSize( DefaultMaxExpectedBlockSize ),
53 _MaxSentBlockSize( DefaultMaxSentBlockSize ),
54 _DataAvailable( false )
56 // Debug info for mutexes
57 #ifdef MUTEX_DEBUG
58 initAcquireTimeMap();
59 #endif
60 #if defined(NL_OS_UNIX)
61 _IsDataAvailablePipeSelfManaged = isDataAvailablePipeSelfManaged;
62 if ( _IsDataAvailablePipeSelfManaged )
64 if ( ::pipe( _DataAvailablePipeHandle ) != 0 )
65 nlwarning( "Unable to create D.A. pipe" );
67 #elif defined(NL_OS_WINDOWS)
68 _DataAvailableHandle = NULL;
69 #endif
74 * Destructor
76 CBufNetBase::~CBufNetBase()
78 #ifdef NL_OS_UNIX
79 if ( _IsDataAvailablePipeSelfManaged )
81 ::close( _DataAvailablePipeHandle[PipeRead] );
82 ::close( _DataAvailablePipeHandle[PipeWrite] );
84 #endif
89 * Push message into receive queue (mutexed)
90 * TODO OPTIM never use this function
92 void CBufNetBase::pushMessageIntoReceiveQueue( const std::vector<uint8>& buffer )
94 //sint32 mbsize;
96 //nldebug( "BNB: Acquiring the receive queue... ");
97 CFifoAccessor recvfifo( &_RecvFifo );
98 //nldebug( "BNB: Acquired, pushing the received buffer... ");
99 recvfifo.value().push( buffer );
100 //nldebug( "BNB: Pushed, releasing the receive queue..." );
101 //mbsize = recvfifo.value().size() / 1048576;
102 setDataAvailableFlag( true );
104 #if defined(NL_OS_UNIX)
105 // Wake-up main thread (outside the critical section of CFifoAccessor, to allow main thread to be
106 // read the fifo; if the main thread sees the Data Available flag is true but the pipe not written
107 // yet, it will block on read()).
108 uint8 b=0;
109 if ( write( _DataAvailablePipeHandle[PipeWrite], &b, 1 ) == -1 )
111 nlwarning( "LNETL1: Write pipe failed in pushMessageIntoReceiveQueue" );
113 //nldebug( "Pipe: 1 byte written (%p)", this );
114 #elif defined(NL_OS_WINDOWS)
115 if (_DataAvailableHandle)
116 SetEvent(_DataAvailableHandle);
117 #endif
118 //nldebug( "BNB: Released." );
119 //if ( mbsize > 1 )
121 // nlwarning( "The receive queue size exceeds %d MB", mbsize );
126 * Push message into receive queue (mutexed)
128 void CBufNetBase::pushMessageIntoReceiveQueue( const uint8 *buffer, uint32 size )
130 //sint32 mbsize;
132 //nldebug( "BNB: Acquiring the receive queue... ");
133 CFifoAccessor recvfifo( &_RecvFifo );
134 //nldebug( "BNB: Acquired, pushing the received buffer... ");
135 recvfifo.value().push( buffer, size );
136 //nldebug( "BNB: Pushed, releasing the receive queue..." );
137 //mbsize = recvfifo.value().size() / 1048576;
138 setDataAvailableFlag( true );
139 #if defined(NL_OS_UNIX)
140 // Wake-up main thread
141 uint8 b=0;
142 if ( write( _DataAvailablePipeHandle[PipeWrite], &b, 1 ) == -1 )
144 nlwarning( "LNETL1: Write pipe failed in pushMessageIntoReceiveQueue" );
146 nldebug( "Pipe: 1 byte written" );
147 #elif defined(NL_OS_WINDOWS)
148 if (_DataAvailableHandle)
149 SetEvent(_DataAvailableHandle);
150 #endif
152 //nldebug( "BNB: Released." );
153 /*if ( mbsize > 1 )
155 nlwarning( "The receive queue size exceeds %d MB", mbsize );
161 NLMISC_CATEGORISED_VARIABLE(nel, uint32, NbNetworkTask, "Number of server and client thread");
163 } // NLNET