Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added some output to udp sockets

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