Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7650 was 7650, checked in by smerkli, 13 years ago

Minimum target achieved for today, servers can log on to master server, clients can get server list. To be debugged.

File size: 6.3 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    /* install atexit handler for enet */
50    atexit( enet_deinitialize );
51
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    }
65
66    return 0;
67  }
68
69  MasterServerComm::~MasterServerComm()
70  {
71    /* destroy the enet facilities */
72    enet_host_destroy(this->client);
73  }
74
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;
80
81    /* Initiate the connection, allocating the two channels 0 and 1. */
82    this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
83
84    if (this->peer == NULL )
85    { fprintf( stderr, 
86        "No available peers for initiating an ENet connection.\n");
87    //exit (EXIT_FAILURE);
88    return -1;
89    }
90
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;
104  }
105
106  int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
107  { 
108    /* see whether anything happened */
109    if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
110    { 
111      /* address buffer */
112      char *addrconv = NULL;
113      int retval = 0;
114
115      /* check what type of event it is and react accordingly */
116      switch (this->event.type)
117      { /* new connection, not supposed to happen. */
118        case ENET_EVENT_TYPE_CONNECT: break;
119
120        /* disconnect */
121        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
122
123        /* incoming data */
124        case ENET_EVENT_TYPE_RECEIVE: 
125          addrconv = (char *) calloc( 50, 1 );
126          enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
127
128          /* DEBUG */
129          printf( "A packet of length %u containing %s was "
130            "received from %s on channel %u.\n",
131            this->event.packet->dataLength,
132            this->event.packet->data,
133            addrconv,
134            this->event.channelID );
135          /* END DEBUG */
136
137          /* call the supplied callback, if any. */
138          if( (*callback) != NULL )
139            retval = (*callback)( addrconv, &(this->event) );
140
141          enet_packet_destroy( event.packet );
142          if( addrconv ) free( addrconv );
143          break;
144        default: break;
145      }
146
147      /* event handled, return 0 */
148      return retval;
149    }
150
151    /* show that no event occured */
152    return 1;
153  }
154
155  int MasterServerComm::sendRequest( char *data )
156  {
157    /* send the data to the friend */
158    /* Create a reliable packet of size 7 containing "packet\0" */
159    ENetPacket * packet = enet_packet_create( data, 
160        strlen( data ) + 1, 
161        ENET_PACKET_FLAG_RELIABLE);
162
163    /* Send the packet to the peer over channel id 0. */
164    enet_peer_send (this->peer, 0, packet);
165
166    /* One could just use enet_host_service() instead. */
167    enet_host_flush( this->client );
168    if( packet ) free( packet );
169
170    /* all done. */
171    return 0;
172  }
173
174  int MasterServerComm::sendRequest( std::string data )
175  {
176    /* send the data to the friend */
177    /* Create a reliable packet of size 7 containing "packet\0" */
178    ENetPacket * packet = enet_packet_create( data.c_str(), 
179        data.length() + 1, 
180        ENET_PACKET_FLAG_RELIABLE);
181
182    /* Send the packet to the peer over channel id 0. */
183    enet_peer_send (this->peer, 0, packet);
184
185    /* One could just use enet_host_service() instead. */
186    enet_host_flush( this->client );
187    if( packet ) free( packet );
188
189    /* all done. */
190    return 0;
191  }
192
193}
194
195
196/* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */
197/* not needed anymore, only here for testing purposes */
198/*
199//[> sample callback to output debugging info. <]
200//int callb( char *addr, ENetEvent *ev )
201//{
202  //printf( "A packet of length %u containing %s was "
203      //"received from %s on channel %u.\n",
204      //ev->packet->dataLength,
205      //ev->packet->data,
206      //addr,
207      //ev->channelID );
208  //return 0;
209//}
210
211//[> small testing implementation <]
212//int
213//main( int argc, char *argv[] )
214//{
215  //[> setup object and connect <]
216  //MasterServerComm msc = MasterServerComm();
217  //if( msc.connect( argv[1], 1234 ) )
218    //exit(EXIT_FAILURE);
219 
220  //[> send some data and poll for replies <]
221  //char *theinput = (char *)calloc( 100,1 );
222  //while( true )
223  //{
224    //fgets( theinput, 90, stdin );
225    //if( !strncmp( theinput, "quit", 4 ) )
226      //break;
227
228    //msc.sendRequest( theinput );
229    //msc.pollForReply( &callb );
230  //}
231
232//}
233*/
Note: See TracBrowser for help on using the repository browser.