Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network5/src/libraries/network/MasterServerComm.cc @ 7773

Last change on this file since 7773 was 7759, checked in by scheusso, 14 years ago

a lot of changes:

  • some fixes (mostly gamestate:diff)
  • FunctionCall buffering (if Gamestate is not recent enough)
File size: 6.1 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  int MasterServerComm::initialize()
41  {
42    /* initialize Enet */
43    if (enet_initialize () != 0)
44    { COUT(1) << "An error occurred while initializing ENet.\n";
45      return 1;
46    }
47
48    /* initialize the event holder */
49    this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
50   
51    /* install atexit handler for enet */
52    atexit( enet_deinitialize );
53
54    /* initiate the client */
55    this->client = enet_host_create( NULL /* create a client host */,
56        1,
57        2, /* allow up 2 channels to be used, 0 and 1 */
58        0, 
59        0 ); 
60
61    /* see if it worked */
62    if (this->client == NULL)
63    { COUT(1) << "An error occurred while trying to create an ENet client host.\n";
64      return 1;
65    }
66
67    return 0;
68  }
69
70  MasterServerComm::~MasterServerComm()
71  {
72    /* destroy the enet facilities */
73    enet_host_destroy(this->client);
74  }
75
76  int MasterServerComm::connect( const char *address, unsigned int port )
77  {
78    /* Connect to address:port. */
79    enet_address_set_host( &this->address, address );
80    this->address.port = port;
81
82    /* Initiate the connection, allocating the two channels 0 and 1. */
83    this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
84
85    if (this->peer == NULL )
86    { COUT(2) << "ERROR: No available peers for initiating an ENet connection.\n";
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, 500) > 0 &&
92        this->event->type == ENET_EVENT_TYPE_CONNECT )
93      COUT(3) << "Connection to master server succeeded.\n";
94    else
95    {
96      enet_peer_reset (this->peer);
97      fprintf( stdout, "Connection to %s failed.", address );
98      COUT(2) << "ERROR: connection to " << address << " failed.\n";
99      return -1;
100    }
101
102    /* all fine */
103    return 0;
104  }
105
106  /* NOTE this is to be reimplemented soon to return
107   * a structure containing
108   * - addrconv
109   * - the event
110   * so we can also make callbacks from objects
111   */
112  int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ),
113    int delayms )
114  { 
115    /* see whether anything happened */
116    /* WORK MARK REMOVE THIS OUTPUT */
117    COUT(2) << "polling masterserver...\n";
118
119    /* address buffer */
120    char *addrconv = NULL;
121    int retval = 0;
122
123    /* enet_host_service returns 0 if no event occured */
124    /* just newly set below test to >0 from >= 0, to be tested */
125    if( enet_host_service( this->client, this->event, delayms ) > 0 )
126    { 
127      /* check what type of event it is and react accordingly */
128      switch (this->event->type)
129      { /* new connection, not supposed to happen. */
130        case ENET_EVENT_TYPE_CONNECT: break;
131
132        /* disconnect */
133        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
134
135        /* incoming data */
136        case ENET_EVENT_TYPE_RECEIVE: 
137          addrconv = (char *) calloc( 50, 1 );
138          if( !addrconv ) 
139          { COUT(2) << "MasterServerComm.cc: Could not allocate memory!\n";
140            break;
141          }
142
143          /* resolve IP */
144          enet_address_get_host_ip( &(this->event->peer->address), 
145            addrconv, 49 );
146
147          /* DEBUG */
148          COUT(3) << "MasterServer Debug: A packet of length " 
149            << this->event->packet->dataLength
150            << " containing " << this->event->packet->data
151            << " was received from " << addrconv
152            << " on channel " << 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          /* clean up */
160          enet_packet_destroy( event->packet );
161          if( addrconv ) 
162            free( addrconv );
163
164          break;
165        default: break;
166      }
167
168      /* event handled, return 0 */
169      return retval;
170    }
171
172    /* show that no event occured */
173    return 0;
174  }
175
176  int MasterServerComm::sendRequest( const char *data )
177  {
178    /* send the data to the friend */
179    /* Create a reliable packet of size 7 containing "packet\0" */
180    ENetPacket * packet = enet_packet_create( data, 
181        strlen( data ) + 1, 
182        ENET_PACKET_FLAG_RELIABLE);
183
184    /* Send the packet to the peer over channel id 0. */
185    enet_peer_send (this->peer, 0, packet);
186
187    /* One could just use enet_host_service() instead. */
188    enet_host_flush( this->client );
189   
190    /* free the packet */
191    enet_packet_destroy( packet );
192
193    /* all done. */
194    return 0;
195  }
196
197  int MasterServerComm::sendRequest( std::string data )
198  {
199    /* send the data to the friend */
200    /* Create a reliable packet of size 7 containing "packet\0" */
201    ENetPacket * packet = enet_packet_create( data.c_str(), 
202        data.length() + 1, 
203        ENET_PACKET_FLAG_RELIABLE);
204
205    /* Send the packet to the peer over channel id 0. */
206    enet_peer_send (this->peer, 0, packet);
207
208    /* One could just use enet_host_service() instead. */
209    enet_host_flush( this->client );
210    if( packet ) free( packet );
211
212    /* all done. */
213    return 0;
214  }
215
216}
Note: See TracBrowser for help on using the repository browser.