Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/libraries/network/MasterServerComm.cc @ 7748

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

Removed some things and fixed some bugs

File size: 7.0 KB
RevLine 
[7589]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
[7611]29#include "MasterServerComm.h"
[7589]30
[7631]31namespace orxonox
[7589]32{
[7631]33 
34  MasterServerComm::MasterServerComm()
35  { /* nothing anymore, everything's been outsourced to
36     * the initialize method to facilitate debugging
37     */
38  } 
[7589]39
40
[7631]41  int MasterServerComm::initialize()
42  {
43    /* initialize Enet */
44    if (enet_initialize () != 0)
45    { COUT(1) << "An error occurred while initializing ENet.\n";
46      return 1;
47    }
[7684]48
49    /* initialize the event holder */
50    this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
[7631]51   
52    /* install atexit handler for enet */
53    atexit( enet_deinitialize );
[7589]54
[7631]55
56    /* initiate the client */
57    this->client = enet_host_create( NULL /* create a client host */,
58        1,
59        2, /* allow up 2 channels to be used, 0 and 1 */
60        0, 
61        0 ); 
62
63    /* see if it worked */
64    if (this->client == NULL)
65    { COUT(1) << "An error occurred while trying to create an ENet client host.\n";
66      return 1;
67    }
[7632]68
69    return 0;
[7589]70  }
71
[7631]72  MasterServerComm::~MasterServerComm()
73  {
74    /* destroy the enet facilities */
75    enet_host_destroy(this->client);
76  }
[7589]77
[7725]78  int MasterServerComm::connect( const char *address, unsigned int port )
[7631]79  {
80    /* Connect to address:port. */
81    enet_address_set_host( &this->address, address );
82    this->address.port = port;
[7589]83
[7631]84    /* Initiate the connection, allocating the two channels 0 and 1. */
85    this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
[7589]86
[7631]87    if (this->peer == NULL )
[7662]88    { COUT(2) << "ERROR: No available peers for initiating an ENet connection.\n";
[7630]89    return -1;
[7631]90    }
[7589]91
[7631]92    /* Wait up to 2 seconds for the connection attempt to succeed. */
[7684]93    if (enet_host_service (this->client, this->event, 2000) > 0 &&
94        this->event->type == ENET_EVENT_TYPE_CONNECT )
[7745]95      COUT(3) << "Connection to master server succeeded.\n";
[7631]96    else
97    {
98      enet_peer_reset (this->peer);
99      fprintf( stdout, "Connection to %s failed.", address );
[7745]100      COUT(2) << "ERROR: connection to " << address << " failed.\n";
[7631]101      return -1;
102    }
103
104    return 0;
[7611]105  }
[7589]106
[7631]107  int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
[7611]108  { 
[7631]109    /* see whether anything happened */
[7668]110    /* WORK MARK REMOVE THIS OUTPUT */
[7692]111    COUT(2) << "polling masterserver...\n";
[7668]112
[7692]113    /* address buffer */
114    char *addrconv = NULL;
115    int retval = 0;
116
[7672]117    /* enet_host_service returns 0 if no event occured */
118    /* just newly set below test to >0 from >= 0, to be tested */
[7684]119    if( enet_host_service( this->client, this->event, 1000 ) > 0 )
[7631]120    { 
121      /* check what type of event it is and react accordingly */
[7684]122      switch (this->event->type)
[7631]123      { /* new connection, not supposed to happen. */
124        case ENET_EVENT_TYPE_CONNECT: break;
[7611]125
[7631]126        /* disconnect */
127        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
128
[7611]129        /* incoming data */
[7631]130        case ENET_EVENT_TYPE_RECEIVE: 
131          addrconv = (char *) calloc( 50, 1 );
[7692]132          if( !addrconv ) 
133          { COUT(2) << "MasterServerComm.cc: Could not allocate memory!\n";
134            break;
135          }
136
137          /* resolve IP */
[7684]138          enet_address_get_host_ip( &(this->event->peer->address), 
139            addrconv, 49 );
[7611]140
[7631]141          /* DEBUG */
[7725]142          COUT(3) << "A packet of length " << this->event->packet->dataLength
143            << " containing " << this->event->packet->data
144            << " was received from " << addrconv
145            << " on channel " << this->event->channelID;
146          //printf( "A packet of length %u containing %s was "
147            //"received from %s on channel %u.\n",
148            //this->event->packet->dataLength,
149            //this->event->packet->data,
150            //addrconv,
151            //this->event->channelID );
[7631]152          /* END DEBUG */
[7611]153
[7631]154          /* call the supplied callback, if any. */
155          if( (*callback) != NULL )
[7684]156            retval = (*callback)( addrconv, (this->event) );
[7611]157
[7684]158          enet_packet_destroy( event->packet );
[7631]159          if( addrconv ) free( addrconv );
160          break;
161        default: break;
162      }
163
164      /* event handled, return 0 */
[7650]165      return retval;
[7589]166    }
[7631]167
168    /* show that no event occured */
[7668]169    return 0;
[7589]170  }
[7611]171
[7725]172  int MasterServerComm::sendRequest( const char *data )
[7631]173  {
174    /* send the data to the friend */
175    /* Create a reliable packet of size 7 containing "packet\0" */
176    ENetPacket * packet = enet_packet_create( data, 
177        strlen( data ) + 1, 
178        ENET_PACKET_FLAG_RELIABLE);
[7611]179
[7631]180    /* Send the packet to the peer over channel id 0. */
181    enet_peer_send (this->peer, 0, packet);
[7611]182
[7631]183    /* One could just use enet_host_service() instead. */
184    enet_host_flush( this->client );
[7668]185   
[7692]186    /* free the packet */
187    enet_packet_destroy( packet );
[7632]188
189    /* all done. */
190    return 0;
[7631]191  }
[7611]192
[7650]193  int MasterServerComm::sendRequest( std::string data )
194  {
195    /* send the data to the friend */
196    /* Create a reliable packet of size 7 containing "packet\0" */
197    ENetPacket * packet = enet_packet_create( data.c_str(), 
198        data.length() + 1, 
199        ENET_PACKET_FLAG_RELIABLE);
200
201    /* Send the packet to the peer over channel id 0. */
202    enet_peer_send (this->peer, 0, packet);
203
204    /* One could just use enet_host_service() instead. */
205    enet_host_flush( this->client );
206    if( packet ) free( packet );
207
208    /* all done. */
209    return 0;
210  }
211
[7611]212}
213
[7631]214
215/* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */
216/* not needed anymore, only here for testing purposes */
217/*
218//[> sample callback to output debugging info. <]
219//int callb( char *addr, ENetEvent *ev )
220//{
221  //printf( "A packet of length %u containing %s was "
222      //"received from %s on channel %u.\n",
223      //ev->packet->dataLength,
224      //ev->packet->data,
225      //addr,
226      //ev->channelID );
227  //return 0;
228//}
229
230//[> small testing implementation <]
231//int
232//main( int argc, char *argv[] )
233//{
234  //[> setup object and connect <]
235  //MasterServerComm msc = MasterServerComm();
236  //if( msc.connect( argv[1], 1234 ) )
237    //exit(EXIT_FAILURE);
[7611]238 
[7631]239  //[> send some data and poll for replies <]
240  //char *theinput = (char *)calloc( 100,1 );
241  //while( true )
242  //{
243    //fgets( theinput, 90, stdin );
244    //if( !strncmp( theinput, "quit", 4 ) )
245      //break;
[7611]246
[7631]247    //msc.sendRequest( theinput );
248    //msc.pollForReply( &callb );
249  //}
[7589]250
[7631]251//}
252*/
Note: See TracBrowser for help on using the repository browser.