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/>.
17 #ifndef NL_RECEIVE_TASK_H
18 #define NL_RECEIVE_TASK_H
20 #include "nel/misc/types_nl.h"
21 #include "nel/misc/debug.h"
22 #include "nel/misc/thread.h"
23 #include "nel/misc/buf_fifo.h"
24 #include "nel/misc/mutex.h"
26 #include "nel/net/udp_sock.h"
31 const uint32 MsgHeaderSize
= 1+8; // 8 for the date
35 * Placeholder for received messages
37 struct TReceivedMessage
39 /// Type of incoming events (see also NLNET::CBufNetBase::TEventType)
40 enum TEventType
{ User
= 'U', RemoveClient
= 'D' };
46 void resizeData( uint32 datasize
) { _Data
.resize( MsgHeaderSize
+ datasize
); }
48 /// Return a vector containing the address info
49 void addressToVector();
51 /// Set address with address info from specified vector
52 void vectorToAddress();
54 /// Set "disconnection" message for the current AddrFrom
55 void setTypeEvent( TEventType t
) { *_Data
.begin() = (uint8
)t
; }
57 void setDate() { *(sint64
*)&(*(_Data
.begin()+1)) = NLMISC::CTime::getLocalTime(); }
59 sint64
getDate() { return *(sint64
*)&(*(_Data
.begin()+1)); }
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
;
93 * \author Olivier Cado
94 * \author Nevrax France
97 class CReceiveTask
: public NLMISC::IRunnable
102 CReceiveTask( uint16 port
, uint32 msgsize
);
110 /// Set new write queue
111 void setWriteQueue( NLMISC::CBufFIFO
*writequeue
);
114 void requireExit() { _ExitRequired
= true; }
119 uint _DatagramLength
;
122 TReceivedMessage _ReceivedMessage
;
124 /// Write queue access
125 NLMISC::CSynchronized
<NLMISC::CBufFIFO
*> _WriteQueue
;
128 volatile bool _ExitRequired
;
132 /// External datagram socket
133 NLNET::CUdpSock
*DataSock
;
138 #endif // NL_RECEIVE_TASK_H
140 /* End of receive_task.h */