Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed some bugs in udpSockets. subprojects/network should work now

File size: 4.4 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
125/**
126 * disconnect from server
127 */
128void UdpSocket::disconnectServer( )
129{
130  SDLNet_UDP_Unbind( socket, -1 );
131  SDLNet_UDP_Close( socket );
132  socket = NULL;
133}
134
135/**
136 * send one packet to other host
137 * @param data pointer to data which will be sent
138 * @param length length of data
139 * @return true on success
140 */
141bool UdpSocket::writePacket( byte * data, int length )
142{
143  if ( serverSocket )
144  {
145    NetworkPacket networkPacket;
146    networkPacket.length = length;
147    networkPacket.data = data;
148    if ( !serverSocket->sendPacket( networkPacket, this->userId ) )
149    {
150      bOk = false;
151      return false;
152    }
153    else
154      return true;
155  }
156  else
157  {
158    assert( length <= packet->maxlen );
159   
160    memcpy( packet->data, data, length );
161    packet->len = length;
162   
163    if ( SDLNet_UDP_Send( socket, 1, packet) == 0 )
164    {
165      PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
166      bOk = false;
167      return false;
168    }
169   
170    return true;
171  }
172}
173
174/**
175 * recieve one packet from another host
176 * @param data pointer to buffer to copy data into
177 * @param maxLength maximal length of buffer
178 * @return less than 0 on error, number bytes read else
179 */
180int UdpSocket::readPacket( byte * data, int maxLength )
181{
182  if ( serverSocket )
183  {
184    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
185   
186    if ( networkPacket.length > 0 )
187    {
188      assert( maxLength > networkPacket.length );
189     
190      memcpy( data, networkPacket.data, networkPacket.length );
191    }
192   
193    if ( networkPacket.data )
194    {
195      free( networkPacket.data );
196      networkPacket.data = NULL;
197    }
198   
199    return networkPacket.length;
200  }
201  else
202  {
203    int numrecv = SDLNet_UDP_Recv( socket, packet);
204    if ( numrecv > 0) 
205    {
206      assert( packet->len <= maxLength );
207     
208      memcpy( data, packet->data, packet->len );
209      return packet->len;
210    }
211    else if ( numrecv < 0 )
212    {
213      PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
214      bOk = false;
215      return -1;
216    }
217    else
218    {
219      return 0;
220    }
221  }
222 
223  return 0;
224}
225
226
227
Note: See TracBrowser for help on using the repository browser.