Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/udp_server_socket.cc @ 8076

Last change on this file since 8076 was 7954, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk.

File size: 6.0 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer:
14*/
15
16#include "udp_server_socket.h"
17
18
19
20/**
21 * constructor
22 * @param port port to listen on
23 */
24UdpServerSocket::UdpServerSocket( int port ) : ServerSocket( port )
25{
26  packet = SDLNet_AllocPacket( UDP_PACKET_SIZE );
27 
28  if ( !packet )
29  {
30    PRINTF(1)("SDLNet_AllocPacket: %s\n", SDLNet_GetError());
31   
32    assert( false );
33    bOk = false;
34  }
35 
36  memset( packet->data, 0, UDP_PACKET_SIZE );
37 
38  listen( port );
39}
40
41/**
42 * default destructor
43 */
44UdpServerSocket::~UdpServerSocket( )
45{
46  for ( int i = 0; i < packetBuffer.size(); i++ )
47    removeUserPackets( i );
48 
49  if ( packet )
50    SDLNet_FreePacket( packet );
51 
52  if ( socket )
53    SDLNet_UDP_Close( socket );
54}
55
56/**
57 * tell udpServerSocket to recieve packets on port
58 * @param port port
59 * @return true on success
60 */
61bool UdpServerSocket::listen( unsigned int port )
62{
63  socket = SDLNet_UDP_Open( port );
64 
65  PRINTF(0)("listening on port: %d\n", port);
66 
67  if ( !socket )
68  {
69   
70    bOk = false;
71    return false;
72  }
73 
74  return true;
75}
76
77/**
78 * get newly connected socket. note
79 * @return new socket or NULL if no new socket exists
80 */
81NetworkSocket * UdpServerSocket::getNewSocket( void )
82{
83  NetworkSocket * result = NULL;
84 
85  if ( newSocketList.size() > 0 )
86  {
87    result = newSocketList.front();
88 
89    newSocketList.pop_front();
90  }
91 
92  return result;
93}
94
95/**
96 * stop listening on server
97 */
98void UdpServerSocket::close( )
99{
100 
101  for ( int i = 0; i < packetBuffer.size(); i++ )
102    removeUserPackets( i );
103 
104  packetBuffer.clear();
105  userList.clear();
106 
107  SDLNet_UDP_Close( socket );
108  socket = NULL;
109}
110
111/**
112 * clean up users buffer
113 * @param userId users userid
114 */
115void UdpServerSocket::removeUserPackets( int userId )
116{
117  if ( userId >= packetBuffer.size() )
118    return;
119 
120  for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
121  {
122    if ( it->data )
123    {
124      free( it->data );
125      it->data = NULL;
126    }
127  }
128 
129  packetBuffer[userId].clear();
130}
131
132/**
133 * get next packet for user
134 * @param userId user id
135 * @return recieved packet or packet with length 0 if no packet available
136 */
137NetworkPacket UdpServerSocket::getPacket( int userId )
138{
139  NetworkPacket res;
140  res.data = NULL;
141  res.length = 0;
142 
143  if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
144  {
145    res.data = packetBuffer[userId].front().data;
146    res.length = packetBuffer[userId].front().length;
147    packetBuffer[userId].pop_front();
148  }
149 
150  return res;
151}
152
153/**
154 * get number of packets recieved for user
155 * @param userId user id
156 * @return number of packets in buffer
157 */
158int UdpServerSocket::getPacketCount( int userId )
159{
160  if ( userId >= packetBuffer.size() )
161    return -1;
162 
163  return packetBuffer[userId].size();
164}
165
166/**
167 * will set user state
168 * @param userId users id
169 * @param ip users host / port
170 */
171void UdpServerSocket::initUser( int userId, IPaddress ip )
172{
173  int channel = SDLNet_UDP_Bind( socket, userId, &ip );
174  if( channel != userId ) 
175  {
176    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
177    assert(false);
178    return;
179  }
180 
181  if ( userId < packetBuffer.size() )
182    removeUserPackets( userId );
183 
184  if ( packetBuffer.size() <= userId )
185    packetBuffer.resize( userId + 1 );
186 
187  if ( userList.size() <= userId )
188    userList.resize( userId + 1 );
189 
190  userList[ userId ] = ip;
191}
192
193/**
194 * remove user from list
195 * @param userId user id
196 */
197void UdpServerSocket::removeUser( int userId )
198{
199  removeUserPackets( userId );
200 
201  if ( userId >= userList.size() )
202    return;
203 
204  userList[userId].host = 0;
205  userList[userId].port = 0;
206 
207  SDLNet_UDP_Unbind( socket, userId );
208}
209
210/**
211 * send one packet to client associated to userId
212 * @param networkPacket packet to send
213 * @param userId users id
214 * @return true on success
215 */
216bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
217{
218  assert( networkPacket.length <= UDP_PACKET_SIZE );
219 
220  memcpy( packet->data, networkPacket.data, networkPacket.length );
221  packet->len = networkPacket.length;
222  packet->channel = -1;
223 
224  if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
225  {
226    PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
227    return false;
228  }
229 
230  return true;
231}
232
233/**
234 * do periodically things
235 */
236void UdpServerSocket::update( )
237{
238  int res;
239  int newConn = 0;
240 
241  for ( res = SDLNet_UDP_Recv( socket, packet ); res == 1; res = SDLNet_UDP_Recv( socket, packet ) )
242  {
243    int userId;
244    bool isNewConnection = false;
245   
246    for ( userId =0; userId < userList.size(); userId++ )
247      if ( userList[userId].host == packet->address.host && userList[userId].port == packet->address.port )
248        break;
249   
250    if ( userId >= userList.size() )
251    {
252     
253      newConn++;
254      isNewConnection = true;
255     
256      if ( newConn > MAX_NEW_CONNECTIONS )
257      {
258        PRINTF(2)("Too many new connections. Dropping packet\n");
259        continue;
260      }
261     
262      for ( userId =0; userId < userList.size(); userId++ )
263        if ( userList[userId].host == 0 && userList[userId].port == 0 )
264          break;
265     
266      initUser( userId, packet->address );
267      UdpSocket * sock = new UdpSocket( this, packet->address, userId );
268      newSocketList.push_back( sock );
269      PRINTF(0)("NEW CONNECTION %x\n", packet->address.host );
270    }
271   
272    //add new packet to packetbuffer
273
274    NetworkPacket networkPacket;
275    networkPacket.length = packet->len;
276    if ( packet->len != 0 )
277    {
278      networkPacket.data = (byte*)malloc( packet->len );
279      assert( networkPacket.data );
280    }
281    else
282    {
283      if ( isNewConnection )
284        continue;
285     
286      networkPacket.data = NULL;
287    }
288    memcpy( networkPacket.data, packet->data, packet->len );
289    packetBuffer[userId].push_back( networkPacket );
290
291  }
292 
293  assert( res == 0 );
294}
Note: See TracBrowser for help on using the repository browser.