Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the network branche back to trunk.

File size: 4.7 KB
RevLine 
[7540]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{
[7541]22  //TODO setClassId
[7540]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 
[7613]39  assert( this->packet );
40 
41  memset( packet->data, 0, UDP_PACKET_SIZE );
42  PRINTF(0)("PACKET DATA: %x\n", packet->data);
43 
[7540]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;
[7556]75  this->userId = userId;
[7540]76}
77
78/**
79 * destructor
80 */
[7556]81UdpSocket::~UdpSocket( )
[7540]82{
83  if ( serverSocket )
84    serverSocket->removeUser( userId );
85 
86  disconnectServer();
[7556]87 
88  if ( this->packet )
89    SDLNet_FreePacket( this->packet );
90 
91  if ( socket )
92    SDLNet_UDP_Close( socket );
[7540]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 
[7570]106  PRINTF(0)("connect to server %s on port %d\n", host.c_str(), port);
107 
[7540]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 
[7553]123  int channel = SDLNet_UDP_Bind(socket, 1, &ip);
[7540]124  if ( channel == -1 ) 
125  {
[7872]126    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
[7540]127    bOk = false;
128    return;
129  }
[7565]130 
131  writePacket( NULL, 0 );
[7540]132}
133
134/**
135 * disconnect from server
136 */
137void UdpSocket::disconnectServer( )
138{
139  SDLNet_UDP_Unbind( socket, -1 );
140  SDLNet_UDP_Close( socket );
[7565]141  bOk = false;
[7540]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 ( 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{
[7565]192  assert( maxLength <= UDP_PACKET_SIZE );
193 
[7540]194  if ( serverSocket )
195  {
196    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
197   
198    if ( networkPacket.length > 0 )
199    {
200      assert( maxLength > networkPacket.length );
201     
202      memcpy( data, networkPacket.data, networkPacket.length );
203    }
204   
205    if ( networkPacket.data )
206    {
[7556]207      free( networkPacket.data );
[7540]208      networkPacket.data = NULL;
209    }
210   
211    return networkPacket.length;
212  }
213  else
214  {
215    int numrecv = SDLNet_UDP_Recv( socket, packet);
216    if ( numrecv > 0) 
217    {
218      assert( packet->len <= maxLength );
219     
220      memcpy( data, packet->data, packet->len );
221      return packet->len;
222    }
223    else if ( numrecv < 0 )
224    {
225      PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
226      bOk = false;
227      return -1;
228    }
229    else
230    {
231      return 0;
232    }
233  }
234 
235  return 0;
236}
237
238
239
Note: See TracBrowser for help on using the repository browser.