Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/udp_socket.cc @ 8284

Last change on this file since 8284 was 8228, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk with command: svn merge branches/network trunk -r8150:HEAD

File size: 4.9 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_socket.h"
17#include "udp_server_socket.h"
18
19
20void UdpSocket::init( )
21{
22  //TODO setClassId
23  this->serverSocket = NULL;
24  this->socket = NULL;
25  this->packet = NULL;
26}
27
28
29/**
30 * constructor - connects to give host
31 * @param host host
32 * @param port port
33 */
34UdpSocket::UdpSocket( std::string host, int port )
35{
36  init();
37  this->packet = SDLNet_AllocPacket( UDP_PACKET_SIZE );
38 
39  assert( this->packet );
40 
41  memset( packet->data, 0, UDP_PACKET_SIZE );
42  PRINTF(0)("PACKET DATA: %x\n", packet->data);
43 
44  this->connectToServer( host, port );
45}
46
47/**
48 * default constructor. use this one if you want to call connecttoServer
49 */
50UdpSocket::UdpSocket( )
51{
52  this->init();
53  this->packet = SDLNet_AllocPacket( UDP_PACKET_SIZE );
54 
55  if ( !packet )
56  {
57    PRINTF(1)("SDLNet_AllocPacket: %s\n", SDLNet_GetError());
58   
59    assert( false );
60    bOk = false;
61  }
62 
63}
64
65/**
66 * constructor. used by UdpServerSocket
67 * @param serverSocket pointer to serverSocket
68 * @param ip client's ip address
69 * @param userId userid used by serverSocket
70 */
71UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId )
72{
73  this->init();
74  this->serverSocket = serverSocket;
75  this->userId = userId;
76}
77
78/**
79 * destructor
80 */
81UdpSocket::~UdpSocket( )
82{
83  this->disconnectServer();
84 
85  if ( serverSocket )
86    serverSocket->removeUser( userId );
87
88  if ( this->packet )
89    SDLNet_FreePacket( this->packet );
90 
91  if ( socket )
92    SDLNet_UDP_Close( socket );
93}
94
95/**
96 * connect to server
97 * @param host host name
98 * @param port port number
99 */
100void UdpSocket::connectToServer( std::string host, int port )
101{
102  assert( serverSocket == NULL );
103 
104  IPaddress ip;
105 
106  PRINTF(0)("connect to server %s on port %d\n", host.c_str(), port);
107 
108  if ( SDLNet_ResolveHost( &ip, host.c_str(), port ) != 0 )
109  {
110    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError() );
111    bOk = false;
112    return;
113  }
114 
115  socket = SDLNet_UDP_Open(0);
116  if ( !socket )
117  {
118    PRINTF(1)("SDLNet_UDP_Open: %s\n", SDLNet_GetError() );
119    bOk = false;
120    return;
121  }
122 
123  int channel = SDLNet_UDP_Bind(socket, 1, &ip);
124  if ( channel == -1 ) 
125  {
126    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
127    bOk = false;
128    return;
129  }
130}
131
132/**
133 * disconnect from server
134 */
135void UdpSocket::disconnectServer( )
136{
137  PRINTF(0)("disconnect\n");
138  writePacket( NULL, 0 );
139  SDLNet_UDP_Unbind( socket, -1 );
140  SDLNet_UDP_Close( socket );
141  bOk = false;
142  socket = NULL;
143}
144
145/**
146 * send one packet to other host
147 * @param data pointer to data which will be sent
148 * @param length length of data
149 * @return true on success
150 */
151bool UdpSocket::writePacket( byte * data, int length )
152{
153  if ( serverSocket )
154  {
155    NetworkPacket networkPacket;
156    networkPacket.length = length;
157    networkPacket.data = data;
158    if ( !serverSocket->sendPacket( networkPacket, this->userId ) )
159    {
160      bOk = false;
161      return false;
162    }
163    else
164      return true;
165  }
166  else
167  {
168    assert( length <= packet->maxlen );
169   
170    memcpy( packet->data, data, length );
171    packet->len = length;
172   
173    if ( socket && SDLNet_UDP_Send( socket, 1, packet) == 0 )
174    {
175      PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
176      bOk = false;
177      return false;
178    }
179   
180    return true;
181  }
182}
183
184/**
185 * recieve one packet from another host
186 * @param data pointer to buffer to copy data into
187 * @param maxLength maximal length of buffer
188 * @return less than 0 on error, number bytes read else
189 */
190int UdpSocket::readPacket( byte * data, int maxLength )
191{
192  assert( maxLength <= UDP_PACKET_SIZE );
193 
194  if ( serverSocket )
195  {
196    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
197   
198    if ( networkPacket.length == -1 )
199    {
200      this->disconnectServer();
201      return 0;
202    }
203   
204    if ( networkPacket.length > 0 )
205    {
206      assert( maxLength > networkPacket.length );
207     
208      memcpy( data, networkPacket.data, networkPacket.length );
209    }
210   
211    if ( networkPacket.data )
212    {
213      free( networkPacket.data );
214      networkPacket.data = NULL;
215    }
216   
217    return networkPacket.length;
218  }
219  else
220  {
221    int numrecv = SDLNet_UDP_Recv( socket, packet);
222    if ( numrecv > 0) 
223    {
224      assert( packet->len <= maxLength );
225     
226      if ( packet->len == 0 )
227      {
228        this->disconnectServer();
229        return 0;
230      }
231     
232      memcpy( data, packet->data, packet->len );
233      return packet->len;
234    }
235    else if ( numrecv < 0 )
236    {
237      PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
238      bOk = false;
239      return -1;
240    }
241    else
242    {
243      return 0;
244    }
245  }
246 
247  return 0;
248}
249
250
251
Note: See TracBrowser for help on using the repository browser.