Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/libraries/network/MasterServerComm.cc @ 7633

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

oops, forgot to rename the new lua function.

File size: 5.7 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    }
48   
49    /* install atexit handler for enet */
50    atexit( enet_deinitialize );
[7589]51
[7631]52
53    /* initiate the client */
54    this->client = enet_host_create( NULL /* create a client host */,
55        1,
56        2, /* allow up 2 channels to be used, 0 and 1 */
57        0, 
58        0 ); 
59
60    /* see if it worked */
61    if (this->client == NULL)
62    { COUT(1) << "An error occurred while trying to create an ENet client host.\n";
63      return 1;
64    }
[7632]65
66    return 0;
[7589]67  }
68
[7631]69  MasterServerComm::~MasterServerComm()
70  {
71    /* destroy the enet facilities */
72    enet_host_destroy(this->client);
73  }
[7589]74
[7631]75  int MasterServerComm::connect( char *address, unsigned int port )
76  {
77    /* Connect to address:port. */
78    enet_address_set_host( &this->address, address );
79    this->address.port = port;
[7589]80
[7631]81    /* Initiate the connection, allocating the two channels 0 and 1. */
82    this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
[7589]83
[7631]84    if (this->peer == NULL )
85    { fprintf( stderr, 
[7589]86        "No available peers for initiating an ENet connection.\n");
[7630]87    //exit (EXIT_FAILURE);
88    return -1;
[7631]89    }
[7589]90
[7631]91    /* Wait up to 2 seconds for the connection attempt to succeed. */
92    if (enet_host_service (this->client, &this->event, 2000) > 0 &&
93        this->event.type == ENET_EVENT_TYPE_CONNECT )
94      fprintf( stdout, "Connection to server succeeded." );
95    else
96    {
97      enet_peer_reset (this->peer);
98      fprintf( stdout, "Connection to %s failed.", address );
99      //exit(EXIT_FAILURE);
100      return -1;
101    }
102
103    return 0;
[7611]104  }
[7589]105
[7631]106  int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
[7611]107  { 
[7631]108    /* see whether anything happened */
109    if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
110    { 
111      /* address buffer */
112      char *addrconv = NULL;
[7589]113
[7631]114      /* check what type of event it is and react accordingly */
115      switch (this->event.type)
116      { /* new connection, not supposed to happen. */
117        case ENET_EVENT_TYPE_CONNECT: break;
[7611]118
[7631]119        /* disconnect */
120        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
121
[7611]122        /* incoming data */
[7631]123        case ENET_EVENT_TYPE_RECEIVE: 
124          addrconv = (char *) calloc( 50, 1 );
125          enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
[7611]126
[7631]127          /* DEBUG */
128          printf( "A packet of length %u containing %s was "
129            "received from %s on channel %u.\n",
130            this->event.packet->dataLength,
131            this->event.packet->data,
132            addrconv,
133            this->event.channelID );
134          /* END DEBUG */
[7611]135
[7631]136          /* call the supplied callback, if any. */
137          if( (*callback) != NULL )
138            (*callback)( addrconv, &(this->event) );
[7611]139
[7631]140          enet_packet_destroy( event.packet );
141          if( addrconv ) free( addrconv );
142          break;
143        default: break;
144      }
145
146      /* event handled, return 0 */
147      return 0;
[7589]148    }
[7631]149
150    /* show that no event occured */
151    return 1;
[7589]152  }
[7611]153
[7631]154  int MasterServerComm::sendRequest( char *data )
155  {
156    /* send the data to the friend */
157    /* Create a reliable packet of size 7 containing "packet\0" */
158    ENetPacket * packet = enet_packet_create( data, 
159        strlen( data ) + 1, 
160        ENET_PACKET_FLAG_RELIABLE);
[7611]161
[7631]162    /* Send the packet to the peer over channel id 0. */
163    enet_peer_send (this->peer, 0, packet);
[7611]164
[7631]165    /* One could just use enet_host_service() instead. */
166    enet_host_flush( this->client );
167    if( packet ) free( packet );
[7632]168
169    /* all done. */
170    return 0;
[7631]171  }
[7611]172
173}
174
[7631]175
176/* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */
177/* not needed anymore, only here for testing purposes */
178/*
179//[> sample callback to output debugging info. <]
180//int callb( char *addr, ENetEvent *ev )
181//{
182  //printf( "A packet of length %u containing %s was "
183      //"received from %s on channel %u.\n",
184      //ev->packet->dataLength,
185      //ev->packet->data,
186      //addr,
187      //ev->channelID );
188  //return 0;
189//}
190
191//[> small testing implementation <]
192//int
193//main( int argc, char *argv[] )
194//{
195  //[> setup object and connect <]
196  //MasterServerComm msc = MasterServerComm();
197  //if( msc.connect( argv[1], 1234 ) )
198    //exit(EXIT_FAILURE);
[7611]199 
[7631]200  //[> send some data and poll for replies <]
201  //char *theinput = (char *)calloc( 100,1 );
202  //while( true )
203  //{
204    //fgets( theinput, 90, stdin );
205    //if( !strncmp( theinput, "quit", 4 ) )
206      //break;
[7611]207
[7631]208    //msc.sendRequest( theinput );
209    //msc.pollForReply( &callb );
210  //}
[7589]211
[7631]212//}
213*/
Note: See TracBrowser for help on using the repository browser.