Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7743 was 7739, checked in by dafrick, 14 years ago

Merging masterserver branch to new presentation branch.

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