Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7668 was 7668, checked in by dafrick, 13 years ago

fix this…

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