Improvements of Samples for NelNet

This commit is contained in:
Sit Melai 2019-03-30 22:40:35 +01:00
parent 3fa6700408
commit edb19ee4d8
9 changed files with 150 additions and 16 deletions

View file

@ -9,7 +9,7 @@ ADD_SUBDIRECTORY(class_transport)
#ADD_SUBDIRECTORY(multi_shards)
#ADD_SUBDIRECTORY(net_layer3)
ADD_SUBDIRECTORY(net_layer3)
#ADD_SUBDIRECTORY(net_layer4)
#ADD_SUBDIRECTORY(net_layer5)
#ADD_SUBDIRECTORY(service)

View file

@ -2,7 +2,7 @@ ADD_EXECUTABLE(nl_sample_ct_ai_service WIN32 ai_service.cpp)
ADD_EXECUTABLE(nl_sample_ct_gd_service WIN32 gd_service.cpp)
ADD_DEFINITIONS(-DNL_CT_CFG="\\"${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_class_transport/\\"")
ADD_DEFINITIONS(-DNL_CT_CFG="${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_class_transport/")
TARGET_LINK_LIBRARIES(nl_sample_ct_ai_service nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_ct_ai_service "NeL, Samples, Net, Class Transport: AI Service")

View file

@ -2,7 +2,7 @@ ADD_EXECUTABLE(nl_sample_ls_client client.cpp)
ADD_EXECUTABLE(nl_sample_ls_fes WIN32 frontend_service.cpp)
ADD_DEFINITIONS(-DNL_LS_CFG="\\"${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_login_system/\\"")
ADD_DEFINITIONS(-DNL_LS_CFG="${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_login_system/")
TARGET_LINK_LIBRARIES(nl_sample_ls_client nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_ls_client "NeL, Samples, Net, Login Service: LS Client")

View file

@ -0,0 +1,22 @@
ADD_EXECUTABLE(nl_sample_net_layer3_client client.cpp)
ADD_EXECUTABLE(nl_sample_net_layer3_fes WIN32 frontend_service.cpp)
ADD_EXECUTABLE(nl_sample_net_layer3_ps WIN32 ping_service.cpp)
ADD_DEFINITIONS(-DNL_LS_CFG="${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_net_layer3/")
TARGET_LINK_LIBRARIES(nl_sample_net_layer3_client nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_net_layer3_client "NeL, Samples, Net, Net Layer 3: L3 Client")
NL_ADD_RUNTIME_FLAGS(nl_sample_net_layer3_client)
TARGET_LINK_LIBRARIES(nl_sample_net_layer3_fes nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_net_layer3_fes "NeL, Samples, Net, Net Layer3: L3 Frontend")
NL_ADD_RUNTIME_FLAGS(nl_sample_net_layer3_fes)
TARGET_LINK_LIBRARIES(nl_sample_net_layer3_ps nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_net_layer3_ps "NeL, Samples, Net, Net Layer3: L3 Ping Server")
NL_ADD_RUNTIME_FLAGS(nl_sample_net_layer3_ps)
INSTALL(TARGETS nl_sample_net_layer3_client nl_sample_net_layer3_fes nl_sample_net_layer3_ps RUNTIME DESTINATION ${NL_BIN_PREFIX} COMPONENT samplesnet)
INSTALL(FILES frontend_service.cfg ping_service.cfg DESTINATION ${NL_SHARE_PREFIX}/nl_sample_net_layer3 COMPONENT samplesnet)

View file

@ -78,20 +78,20 @@ TCallbackItem CallbackArray[] =
/*
* main
*/
void main( int argc, char **argv )
int main( int argc, char **argv )
{
try
{
// Initialize and connect to the front-end server at localhost:37000
CCallbackClient client;
client.addCallbackArray( CallbackArray, sizeof(CallbackArray)/sizeof(CallbackArray[0]) );
client.connect( CInetAddress( "localhost" , 37000 ) );
client.connect( CInetAddress( "localhost:37000" ) );
// Send a PING message
uint32 counter = 0;
CMessage msg( "PING" ); // create the message
msg.serial( counter ); // serialize the counter into the message
client.send( msg ); // put into the send queue
client.send( msg ); // put into the send queue
nlinfo( "Sent PING number %u", counter );
// Main loop

View file

@ -37,6 +37,8 @@
#include "nel/net/callback_client.h"
#include "nel/net/callback_server.h"
using namespace NLNET;
using namespace NLMISC;
#include <deque>
using namespace std;
@ -48,6 +50,10 @@ CCallbackClient *ToPingService;
// Temp storage (a queue because the connection to the ping service is reliable, the order is preserved)
deque<TSockId> ClientIds;
// THE SERVER
// Must be a pointer to control when to start listening socket and when stop it
CCallbackServer *Server;
/*
* Callback function called when receiving a "PING" message
@ -60,7 +66,7 @@ deque<TSockId> ClientIds;
* Input (expected message from a client): PING
* - uint32: ping counter
*
* Output (sent message to the ping server): PONG
* Output (sent message to the ping server): PING
* - uint32: ping counter
*/
void cbPing( CMessage& msgin, TSockId from, CCallbackNetBase& frontendserver )
@ -71,22 +77,24 @@ void cbPing( CMessage& msgin, TSockId from, CCallbackNetBase& frontendserver )
msgin.serial( counter );
ClientIds.push_back( from ); // store client sockid
printf("Received PING");
// Output
CMessage msgout( "PING" );
msgout.serial( counter );
vector<uint8> vect( 400000 );
msgout.serialCont( vect );
// vector<uint8> vect( 400000 );
// msgout.serialCont( vect );
ToPingService->send( msgout );
nlinfo( "Received PING number %u from %s", counter, frontendserver.hostAddress(from).asString().c_str() );
}
/*
* Disconnection callback, called when a client disconnects
*/
void discCallback( TSockId from, void *p )
{
nldebug("cliend disconnected");
// Remove all occurences of from in the queue
deque<TSockId>::iterator iq;
for ( iq=ClientIds.begin(); iq!=ClientIds.end(); )
@ -129,10 +137,9 @@ void cbPong( CMessage& msgin, TSockId from, CCallbackNetBase& clientofthepingser
ClientIds.pop_front();
// Output: send the reply to the client
CCallbackServer *server = IService::getInstance()->getServer();
CMessage msgout( "PONG" );
msgout.serial( counter );
server->send( msgout, clientfrom );
Server->send( msgout, clientfrom );
nlinfo( "Sent PONG number %u to %s", counter, clientfrom->asString().c_str() );
}
@ -176,8 +183,11 @@ public:
nlerror( "Ping Service not available" );
}
Server = new CCallbackServer();
Server->init(37000);
// Disconnection callback for the clients
IService::getServer()->setDisconnectionCallback( discCallback, NULL );
Server->setDisconnectionCallback( discCallback, NULL );
Server->addCallbackArray(CallbackArray, 1);
}
/*
@ -203,4 +213,4 @@ public:
* Declare a service with the class CFrontEndService, the names "FS" (short) and "frontend_service" (long).
* The port is set to 37000 and the main callback array is CallbackArray.
*/
NLNET_OLD_SERVICE_MAIN( CFrontEndService, "FS", "frontend_service", 37000, CallbackArray, "", "" )
NLNET_SERVICE_MAIN( CFrontEndService, "FS", "frontend_service", 0, EmptyCallbackArray, "", "" )

View file

@ -29,7 +29,33 @@
// We're building a server, using the NeL Service framework
#include "nel/net/service.h"
using namespace NLNET;
using namespace NLMISC;
using namespace std;
// Must be a pointer to control when to start listening socket and when stop it
CCallbackServer *Server;
vector<TSockId> Clients;
// MESSAGES
// ***************************************************************************
void clientWantsToConnect ( TSockId from, void *arg )
{
// Called when a client wants to connect
Clients.push_back (from);
}
// ***************************************************************************
void clientWantsToDisconnect ( TSockId from, void *arg )
{
// Called when a client wants to disconnect
for (uint i = 0; i < Clients.size(); ++i)
if (Clients[i] == from)
{
Clients.erase(Clients.begin()+i);
return;
}
}
/*
* Callback function called when receiving a "PING" message
@ -70,11 +96,43 @@ TCallbackItem CallbackArray[] =
};
// We use IService directly, no need to inherit from it
// SERVICE
// ***************************************************************************
class CPingService : public IService
{
public:
void init ()
{
// Init the server on port 3333
Server = new CCallbackServer();
Server->init (3333);
Server->setConnectionCallback (clientWantsToConnect, NULL);
Server->setDisconnectionCallback (clientWantsToDisconnect, NULL);
Server->addCallbackArray (CallbackArray, 1);
}
bool update ()
{
// this function is called every "loop". you return true if you want
// to continue or return false if you want to exit the service.
// the loop is called evenly (by default, at least one time per second).
Server->update();
return true;
}
void release ()
{
// Must delete the server here
delete Server;
}
};
/*
* Declare a service with the class IService, the names "PS" (short) and "ping_service" (long).
* The port is automatically allocated (0) and the main callback array is CallbackArray.
*/
NLNET_OLD_SERVICE_MAIN( IService, "PS", "ping_service", 0, CallbackArray, "", "" )
NLNET_SERVICE_MAIN( CPingService, "PS", "ping_service", 0, EmptyCallbackArray, "", "" )

View file

@ -0,0 +1,22 @@
ADD_EXECUTABLE(nl_sample_net_layer4_client client.cpp)
ADD_EXECUTABLE(nl_sample_net_layer4_fes WIN32 frontend_service.cpp)
ADD_EXECUTABLE(nl_sample_net_layer4_ps WIN32 ping_service.cpp)
ADD_DEFINITIONS(-DNL_LS_CFG="${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_net_layer4/")
TARGET_LINK_LIBRARIES(nl_sample_net_layer4_client nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_net_layer4_client "NeL, Samples, Net, Net Layer 4: L4 Client")
NL_ADD_RUNTIME_FLAGS(nl_sample_net_layer4_client)
TARGET_LINK_LIBRARIES(nl_sample_net_layer4_fes nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_net_layer4_fes "NeL, Samples, Net, Net Layer4: L4 Frontend")
NL_ADD_RUNTIME_FLAGS(nl_sample_net_layer4_fes)
TARGET_LINK_LIBRARIES(nl_sample_net_layer4_ps nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_net_layer4_ps "NeL, Samples, Net, Net Layer4: L4 Ping Server")
NL_ADD_RUNTIME_FLAGS(nl_sample_net_layer4_ps)
INSTALL(TARGETS nl_sample_net_layer4_client nl_sample_net_layer4_fes nl_sample_net_layer4_ps RUNTIME DESTINATION ${NL_BIN_PREFIX} COMPONENT samplesnet)
INSTALL(FILES frontend_service.cfg ping_service.cfg DESTINATION ${NL_SHARE_PREFIX}/nl_sample_net_layer4 COMPONENT samplesnet)

View file

@ -0,0 +1,22 @@
ADD_EXECUTABLE(nl_sample_nel_layer4_client client.cpp)
ADD_EXECUTABLE(nl_sample_nel_layer4_fes WIN32 frontend_service.cpp)
ADD_EXECUTABLE(nl_sample_nel_layer4_ps WIN32 ping_service.cpp)
ADD_DEFINITIONS(-DNL_LS_CFG="${NL_SHARE_ABSOLUTE_PREFIX}/nl_sample_net_layer4/")
TARGET_LINK_LIBRARIES(nl_sample_nel_layer4_client nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_nel_layer4 "NeL, Samples, Net, Net Layer 4: L4 Client")
NL_ADD_RUNTIME_FLAGS(nl_sample_nel_layer4)
TARGET_LINK_LIBRARIES(nl_sample_nel_layer4_fes nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_nel_layer4_fes "NeL, Samples, Net, Net Layer4: L4 Frontend")
NL_ADD_RUNTIME_FLAGS(nl_sample_nel_layer4_fes)
TARGET_LINK_LIBRARIES(nl_sample_nel_layer4_ps nelmisc nelnet)
NL_DEFAULT_PROPS(nl_sample_nel_layer4_ps "NeL, Samples, Net, Net Layer4: L4 Ping Server")
NL_ADD_RUNTIME_FLAGS(nl_sample_nel_layer4_ps)
INSTALL(TARGETS nl_sample_nel_layer4_client nl_sample_nel_layer4_fes nl_sample_nel_layer4_ps RUNTIME DESTINATION ${NL_BIN_PREFIX} COMPONENT samplesnet)
INSTALL(FILES frontend_service.cfg ping_service.cfg DESTINATION ${NL_SHARE_PREFIX}/nl_sample_nel_layer4 COMPONENT samplesnet)