Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / samples / net / udp / receive_task.h
blob212fb93fc7c3ab3ed40952e1a34b8bb27ee64527
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 #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"
28 #include <vector>
31 const uint32 MsgHeaderSize = 1+8; // 8 for the date
34 /**
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' };
42 /// Constructor
43 TReceivedMessage();
45 /// Resize data
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; }
76 private:
78 /// One byte for event type (header), followed by user data
79 std::vector<uint8> _Data;
81 public:
83 /// Address of sender as CInetAddress
84 NLNET::CInetAddress AddrFrom;
86 /// Placeholder vector for address info
87 std::vector<uint8> VAddrFrom;
91 /**
92 * receive task
93 * \author Olivier Cado
94 * \author Nevrax France
95 * \date 2001
97 class CReceiveTask : public NLMISC::IRunnable
99 public:
101 /// Constructor
102 CReceiveTask( uint16 port, uint32 msgsize );
104 /// Destructor
105 ~CReceiveTask();
107 /// Run
108 virtual void run();
110 /// Set new write queue
111 void setWriteQueue( NLMISC::CBufFIFO *writequeue );
113 /// Require exit
114 void requireExit() { _ExitRequired = true; }
116 private:
118 /// Datagram length
119 uint _DatagramLength;
121 /// Received message
122 TReceivedMessage _ReceivedMessage;
124 /// Write queue access
125 NLMISC::CSynchronized<NLMISC::CBufFIFO*> _WriteQueue;
127 /// Exit required
128 volatile bool _ExitRequired;
130 public:
132 /// External datagram socket
133 NLNET::CUdpSock *DataSock;
138 #endif // NL_RECEIVE_TASK_H
140 /* End of receive_task.h */