1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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).
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
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
)
57 msgin
.serial( counter
);
58 nlinfo( "Received PONG number %u", counter
);
62 CMessage
msgout ( "PING" );
63 msgout
.serial( counter
);
64 client
.send( msgout
);
65 nlinfo( "Sent PING number %u", counter
);
72 TCallbackItem CallbackArray
[] =
74 { "PONG", cbPong
} // when receiving a "PONG" message, call cbPong()
81 void main( int argc
, char **argv
)
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
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
);
98 while ( client
.connected() )
100 // Perform sends and receives, call callbacks
104 nlinfo( "Disconnected" );
106 catch ( Exception
&e
)
108 nlerror( "Error: %s", e
.what() );