Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7631 was 7631, checked in by smerkli, 14 years ago

added comments, lua function (to be tested) and various implementation bits

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