Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8774 was 8623, checked in by bensch, 18 years ago

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8230:HEAD https://svn.orxonox.net/orxonox/branches/network .
conflicts resolved in favour of the network branche (conflicts were in network)

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