1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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"
24 # ifndef NL_COMP_MINGW
28 #elif defined NL_OS_UNIX
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
36 #define WSAGetLastError() 0
41 using namespace NLMISC
;
42 using namespace NLNET
;
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() );
71 CReceiveTask::CReceiveTask( uint16 port
, uint32 msgsize
) :
72 _DatagramLength( msgsize
),
74 _WriteQueue( "WriteQueue" ), // value unspecified
75 _ExitRequired( false )
78 DataSock
= new CUdpSock( false );
81 DataSock
->bind( port
);
89 CReceiveTask::~CReceiveTask()
91 nlassert( DataSock
!= NULL
);
101 void CReceiveTask::run()
103 uint maxrecvlength
= _DatagramLength
;
104 while ( ! _ExitRequired
)
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
);
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
;