Update lua versions
[ryzomcore.git] / nel / samples / net / udp / receive_task.cpp
blob43f7ba8059fee9213bcacfec3cd6dcbc2d0c0bef
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "receive_task.h"
23 #ifdef NL_OS_WINDOWS
24 # ifndef NL_COMP_MINGW
25 # define NOMINMAX
26 # endif
27 # include <windows.h>
28 #elif defined NL_OS_UNIX
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 #include <netdb.h>
36 #define WSAGetLastError() 0
38 #endif
41 using namespace NLMISC;
42 using namespace NLNET;
46 * TReceivedMessage
49 /// Constructor
50 TReceivedMessage::TReceivedMessage()
52 VAddrFrom.resize( sizeof(sockaddr_in) );
55 /// Return a vector containing the address info
56 void TReceivedMessage::addressToVector()
58 memcpy( &*VAddrFrom.begin(), AddrFrom.sockAddr(), sizeof(sockaddr_in) );
61 /// Set address with address info from specified vector
62 void TReceivedMessage::vectorToAddress()
64 AddrFrom.setSockAddr( (sockaddr_in*)&*VAddrFrom.begin() );
69 * Constructor
71 CReceiveTask::CReceiveTask( uint16 port, uint32 msgsize ) :
72 _DatagramLength( msgsize ),
73 _ReceivedMessage(),
74 _WriteQueue( "WriteQueue" ), // value unspecified
75 _ExitRequired( false )
77 // Socket
78 DataSock = new CUdpSock( false );
79 nlassert( DataSock );
81 DataSock->bind( port );
87 * Destructor
89 CReceiveTask::~CReceiveTask()
91 nlassert( DataSock != NULL );
92 delete DataSock;
93 DataSock = NULL;
99 * Run
101 void CReceiveTask::run()
103 uint maxrecvlength = _DatagramLength;
104 while ( ! _ExitRequired )
106 sint64 d;
109 // Receive into _ReceivedMessage
110 _DatagramLength = maxrecvlength;
111 _ReceivedMessage.resizeData( _DatagramLength );
112 _ReceivedMessage.setTypeEvent( TReceivedMessage::User );
113 DataSock->receivedFrom( _ReceivedMessage.userDataW(), _DatagramLength, _ReceivedMessage.AddrFrom );
114 d = CTime::getLocalTime ();
116 catch (const ESocket&)
118 // Remove the client corresponding to the address
119 _ReceivedMessage.setTypeEvent( TReceivedMessage::RemoveClient );
120 _DatagramLength = 0;
123 // Push into the write queue
124 _ReceivedMessage.addressToVector();
125 _ReceivedMessage.resizeData( _DatagramLength ); // _DatagramLength was modified by receivedFrom()
126 _ReceivedMessage.setDate ();
128 CSynchronized<CBufFIFO*>::CAccessor wq( &_WriteQueue );
129 wq.value()->push( _ReceivedMessage.data() );
130 wq.value()->push( _ReceivedMessage.VAddrFrom );
134 nlinfo( "Exiting from front-end receive task" );
139 * Set new write queue
141 void CReceiveTask::setWriteQueue( CBufFIFO *writequeue )
143 CSynchronized<CBufFIFO*>::CAccessor wq( &_WriteQueue );
144 wq.value() = writequeue;