Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc @ 7630

Last change on this file since 7630 was 7630, checked in by smerkli, 14 years ago

initiated WANDiscovery code, now implementing…

File size: 5.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Sandro 'smerkli' Merkli
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include <cstdlib>
30#include <cstdio>
31#include <cstring>
32#include <enet/enet.h>
33#include "MasterServerComm.h"
34
35MasterServerComm::MasterServerComm()
36{
37  /* initialize Enet */
38  if (enet_initialize () != 0)
39  { fprintf (stderr, "An error occurred while initializing ENet.\n");
40    exit(EXIT_FAILURE);
41  }
42  atexit (enet_deinitialize);
43
44
45  /* initiate the client */
46  this->client = enet_host_create( NULL /* create a client host */,
47      1,
48      2, /* allow up 2 channels to be used, 0 and 1 */
49      0, 
50      0 ); 
51
52  /* see if it worked */
53  if (this->client == NULL)
54  { fprintf (stderr, 
55      "An error occurred while trying to create an ENet client host.\n");
56    exit (EXIT_FAILURE);
57  }
58}
59
60MasterServerComm::~MasterServerComm()
61{
62  enet_host_destroy(this->client);
63}
64
65int 
66MasterServerComm::connect( char *address, unsigned int port )
67{
68  /* Connect to address:port. */
69  enet_address_set_host( &this->address, address );
70  this->address.port = port;
71
72  /* Initiate the connection, allocating the two channels 0 and 1. */
73  this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
74
75  if (this->peer == NULL )
76  { fprintf( stderr, 
77        "No available peers for initiating an ENet connection.\n");
78    //exit (EXIT_FAILURE);
79    return -1;
80  }
81
82  /* Wait up to 2 seconds for the connection attempt to succeed. */
83  if (enet_host_service (this->client, &this->event, 2000) > 0 &&
84      this->event.type == ENET_EVENT_TYPE_CONNECT )
85    fprintf( stdout, "Connection to server succeeded." );
86  else
87  {
88    enet_peer_reset (this->peer);
89    fprintf( stdout, "Connection to %s failed.", address );
90    //exit(EXIT_FAILURE);
91    return -1;
92  }
93
94  return 0;
95}
96
97int 
98MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
99{ 
100  if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
101  { 
102    char *addrconv = NULL;
103    /* check what type of event it is and react accordingly */
104    switch (this->event.type)
105    { /* new connection, not supposed to happen. */
106      case ENET_EVENT_TYPE_CONNECT: break;
107
108      /* disconnect */
109      case ENET_EVENT_TYPE_DISCONNECT: 
110        /* ?? */ break;
111
112        /* incoming data */
113      case ENET_EVENT_TYPE_RECEIVE: 
114        addrconv = (char *) calloc( 50, 1 );
115        enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
116
117        /* DEBUG */
118        printf( "A packet of length %u containing %s was "
119          "received from %s on channel %u.\n",
120          this->event.packet->dataLength,
121          this->event.packet->data,
122          addrconv,
123          this->event.channelID );
124        /* END DEBUG */
125
126        /* call the supplied callback, if any. */
127        if( (*callback) != NULL )
128          (*callback)( addrconv, &(this->event) );
129
130        enet_packet_destroy( event.packet );
131        if( addrconv ) free( addrconv );
132        break;
133      default: break;
134    }
135  }
136 
137  return 0;
138}
139
140int 
141MasterServerComm::sendRequest( char *data )
142{
143  /* send the data to the friend */
144  /* Create a reliable packet of size 7 containing "packet\0" */
145  ENetPacket * packet = enet_packet_create( data, 
146      strlen( data ) + 1, 
147      ENET_PACKET_FLAG_RELIABLE);
148
149  /* Send the packet to the peer over channel id 0. */
150  enet_peer_send (this->peer, 0, packet);
151
152  /* One could just use enet_host_service() instead. */
153  enet_host_flush( this->client );
154  if( packet ) free( packet );
155}
156
157/* sample callback to output debugging info. */
158int callb( char *addr, ENetEvent *ev )
159{ 
160  printf( "A packet of length %u containing %s was "
161      "received from %s on channel %u.\n",
162      ev->packet->dataLength,
163      ev->packet->data,
164      addr,
165      ev->channelID );
166  return 0;
167}
168
169/* small testing implementation */
170int
171main( int argc, char *argv[] )
172{
173  /* setup object and connect */
174  MasterServerComm msc = MasterServerComm();
175  if( msc.connect( argv[1], 1234 ) )
176    exit(EXIT_FAILURE);
177 
178  /* send some data and poll for replies */
179  char *theinput = (char *)calloc( 100,1 );
180  while( true )
181  { 
182    fgets( theinput, 90, stdin );
183    if( !strncmp( theinput, "quit", 4 ) )
184      break;
185
186    msc.sendRequest( theinput );
187    msc.pollForReply( &callb );
188  }
189
190}
Note: See TracBrowser for help on using the repository browser.