Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox.cc @ 95

Last change on this file since 95 was 95, checked in by anonymous, 17 years ago

first trys with network libraries

File size: 3.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 *   Author:
22 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "tnl.h"
29#include "tnlEventConnection.h"
30#include "tnlNetInterface.h"
31#include "tnlRPC.h"
32#include <stdio.h> 
33bool gQuit = false; // a flag used when the client wants to quit. using namespace TNL; // make sure we can simply use the TNL classes.
34
35class SimpleEventConnection : public EventConnection
36{
37    typedef EventConnection Parent;
38
39public:
40    // Let the network system know this is a valid network connection.
41    TNL_DECLARE_NETCONNECTION(SimpleEventConnection);
42
43    // declare the client to server message
44    TNL_DECLARE_RPC(rpcMessageClientToServer, (const char *theMessageString));
45
46    // declare the server to client message
47    TNL_DECLARE_RPC(rpcMessageServerToClient, (const char *theMessageString));
48};
49
50TNL_IMPLEMENT_NETCONNECTION(SimpleEventConnection, NetClassGroupGame, true);
51
52TNL_IMPLEMENT_RPC(SimpleEventConnection, rpcMessageClientToServer, (const char *messageString), NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirClientToServer, 0)
53{
54    // display the message the client sent
55    printf("Got message from client: %s\n", messageString);
56    // send a hello world back to the client.
57    rpcMessageServerToClient("Hello, World!");
58}
59
60TNL_IMPLEMENT_RPC(SimpleEventConnection, rpcMessageServerToClient, (const char *messageString), NetClassGroupGameMask, RPCGuaranteedOrdered, RPCDirServerToClient, 0)
61{
62    // display the message the server sent
63    printf("Got a message from server: %s\n", messageString);
64
65    // once the client has heard back from the server, it should quit.
66    gQuit = true;
67}
68
69int main(int argc, const char **argv)
70{
71    if(argc != 3)
72    {
73        printf("usage: simpletnltest <-server|-client> <connectAddress>");
74        return 1;
75    }
76    bool isClient = !strcmp(argv[1], "-client");
77
78    // convert the command-line address into TNL address form
79    Address cmdAddress(argv[2]);
80
81    RefPtr<NetInterface> theNetInterface;
82    if(isClient)
83    {
84        Address bindAddress(IPProtocol, Address::Any, 0);
85
86        // create a new NetInterface bound to any interface, any port (0)
87        theNetInterface = new NetInterface(bindAddress);
88       
89        // create a new SimpleEventConnection and tell it to connect to the
90        // server at cmdAddress.
91        SimpleEventConnection *newConnection = new SimpleEventConnection;
92        newConnection->connect(theNetInterface, cmdAddress);
93
94        // post an RPC, to be executed when the connection is established
95        newConnection->rpcMessageClientToServer("Hello??");
96    }
97    else
98    {
99        // create a server net interface, bound to the cmdAddress
100        theNetInterface = new NetInterface(cmdAddress);
101
102        // notify the NetInterface that it can allow connections
103        theNetInterface->setAllowsConnections(true);
104    }
105
106    // now just loop, processing incoming packets and sending outgoing packets
107    // until the global quit flag is set.
108    while(!gQuit)
109    {
110         theNetInterface->checkIncomingPackets();
111         theNetInterface->processConnections();
112         Platform::sleep(1);
113    }
114    return 0;
115}
Note: See TracBrowser for help on using the repository browser.