1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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 #ifndef NL_FE_RECEIVE_TASK_H
20 #define NL_FE_RECEIVE_TASK_H
22 #include "nel/misc/types_nl.h"
23 #include "nel/misc/debug.h"
24 #include "nel/misc/thread.h"
25 #include "nel/misc/buf_fifo.h"
26 #include "nel/misc/mutex.h"
28 //#define MEASURE_RECEIVE_TASK
30 #include "nel/net/udp_sock.h"
35 const uint32 MsgHeaderSize
= 1;
39 * Placeholder for received messages
41 struct TReceivedMessage
43 /// Type of incoming events (see also NLNET::CBufNetBase::TEventType)
44 enum TEventType
{ User
= 'U', RemoveClient
= 'D' };
50 void resizeData( uint32 datasize
) { _Data
.resize( MsgHeaderSize
+ datasize
); }
52 /// Return a vector containing the address info
53 void addressToVector();
55 /// Set address with address info from specified vector
56 void vectorToAddress();
58 /// Set "disconnection" message for the current AddrFrom
59 void setTypeEvent( TEventType t
) { *_Data
.begin() = (uint8
)t
; }
61 /// Return the event type
62 TEventType
eventType() const { return (TEventType
)(*_Data
.begin()); }
64 /// Return a pointer to user data for writing
65 uint8
*userDataW() { return &*_Data
.begin() + MsgHeaderSize
; }
67 /// Return a pointer to user data for reading
68 const uint8
*userDataR() const { return &*_Data
.begin() + MsgHeaderSize
; }
70 /// Return the size of user data
71 uint32
userSize() { return (uint32
)_Data
.size() - MsgHeaderSize
; }
73 /// Return the data vector (event type header byte + user data)
74 std::vector
<uint8
>& data() { return _Data
; }
78 /// One byte for event type (header), followed by user data
79 std::vector
<uint8
> _Data
;
83 /// Address of sender as CInetAddress
84 NLNET::CInetAddress AddrFrom
;
86 /// Placeholder vector for address info
87 std::vector
<uint8
> VAddrFrom
;
92 * Front-end receive task
93 * \author Olivier Cado
94 * \author Nevrax France
97 class CFEReceiveTask
: public NLMISC::IRunnable
102 CFEReceiveTask( uint16 firstAcceptablePort
, uint16 lastAcceptablePort
, uint32 msgsize
);
110 /// Set new write queue (thread-safe because mutexed)
111 void setWriteQueue( NLMISC::CBufFIFO
*writequeue
);
113 /// Require exit (thread-safe because atomic assignment)
114 void requireExit() { _ExitRequired
= true; }
116 /// Return the number of rejected datagrams since the last call (thread-safe because atomic assignment)
117 uint
nbNewRejectedDatagrams() { uint nb
=_NbRejectedDatagrams
; _NbRejectedDatagrams
=0; return nb
; }
122 uint _DatagramLength
;
125 TReceivedMessage _ReceivedMessage
;
127 /// Write queue access
128 NLMISC::CSynchronized
<NLMISC::CBufFIFO
*> _WriteQueue
;
130 /// Number of datagrams not copied because too big
131 volatile uint _NbRejectedDatagrams
;
134 volatile bool _ExitRequired
;
138 /// External datagram socket
139 NLNET::CUdpSock
*DataSock
;
141 /// The date of the last UPD packet recevied
142 static volatile uint32 LastUDPPacketReceived
;
147 #endif // NL_FE_RECEIVE_TASK_H
149 /* End of fe_receive_task.h */