1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
19 #include "nel/net/buf_net_base.h"
21 using namespace NLMISC
;
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 /***************************************************************************************************
38 **************************************************************************************************/
45 CBufNetBase::CBufNetBase( bool isDataAvailablePipeSelfManaged
) :
47 CBufNetBase::CBufNetBase() :
49 _RecvFifo("CBufNetBase::_RecvFifo"),
50 _DisconnectionCallback( NULL
),
51 _DisconnectionCbArg( NULL
),
52 _MaxExpectedBlockSize( DefaultMaxExpectedBlockSize
),
53 _MaxSentBlockSize( DefaultMaxSentBlockSize
),
54 _DataAvailable( false )
56 // Debug info for mutexes
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
;
76 CBufNetBase::~CBufNetBase()
79 if ( _IsDataAvailablePipeSelfManaged
)
81 ::close( _DataAvailablePipeHandle
[PipeRead
] );
82 ::close( _DataAvailablePipeHandle
[PipeWrite
] );
89 * Push message into receive queue (mutexed)
90 * TODO OPTIM never use this function
92 void CBufNetBase::pushMessageIntoReceiveQueue( const std::vector
<uint8
>& buffer
)
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()).
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
);
118 //nldebug( "BNB: Released." );
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
)
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
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
);
152 //nldebug( "BNB: Released." );
155 nlwarning( "The receive queue size exceeds %d MB", mbsize );
161 NLMISC_CATEGORISED_VARIABLE(nel
, uint32
, NbNetworkTask
, "Number of server and client thread");