Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

just added some notes so I understand my own stuff next week.

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