Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/udp_socket.cc @ 7565

Last change on this file since 7565 was 7565, checked in by rennerc, 18 years ago

reimplemented NetworkStream

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