Update lua versions
[ryzomcore.git] / nel / samples / net / net_layer3 / client.cpp
blob670f2becdce59dc0a81574eaffa793800bdf9a6d
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Layer 3 example, client.
21 * This client connects to a front-end server at localhost:37000.
22 * It sends pings and expects pongs (ping replies).
26 // We're using NeL
27 #include "nel/misc/types_nl.h"
28 #include "nel/misc/debug.h"
29 using namespace NLMISC;
31 // We're using CInetAddress, CMessage and CCallbackClient (because we're building a client)
32 #include "nel/net/inet_address.h"
33 #include "nel/net/message.h"
34 #include "nel/net/callback_client.h"
35 using namespace NLNET;
39 * Callback function called when receiving a "PONG" message
41 * Arguments:
42 * - msgin: the incoming message
43 * - from: the "sockid" of the sender (usually useless for a CCallbackClient)
44 * - client: the CCallbackNetBase object (which really is a CCallbackClient object, for a client)
46 * Input (expected message): PONG
47 * - uint32: ping counter
49 * Output (sent message): PING
50 * - uint32: new ping counter
52 void cbPong( CMessage& msgin, TSockId from, CCallbackNetBase& client )
54 uint32 counter;
56 // Input
57 msgin.serial( counter );
58 nlinfo( "Received PONG number %u", counter );
59 counter++;
61 // Output
62 CMessage msgout ( "PING" );
63 msgout.serial( counter );
64 client.send( msgout );
65 nlinfo( "Sent PING number %u", counter );
70 * Callback array
72 TCallbackItem CallbackArray[] =
74 { "PONG", cbPong } // when receiving a "PONG" message, call cbPong()
79 * main
81 void main( int argc, char **argv )
83 try
85 // Initialize and connect to the front-end server at localhost:37000
86 CCallbackClient client;
87 client.addCallbackArray( CallbackArray, sizeof(CallbackArray)/sizeof(CallbackArray[0]) );
88 client.connect( CInetAddress( "localhost" , 37000 ) );
90 // Send a PING message
91 uint32 counter = 0;
92 CMessage msg( "PING" ); // create the message
93 msg.serial( counter ); // serialize the counter into the message
94 client.send( msg ); // put into the send queue
95 nlinfo( "Sent PING number %u", counter );
97 // Main loop
98 while ( client.connected() )
100 // Perform sends and receives, call callbacks
101 client.update();
104 nlinfo( "Disconnected" );
106 catch ( Exception &e )
108 nlerror( "Error: %s", e.what() );