From edb19ee4d81d14c8f8091e84725e3998dfa16b4a Mon Sep 17 00:00:00 2001 From: Sit Melai Date: Sat, 30 Mar 2019 22:40:35 +0100 Subject: [PATCH] Improvements of Samples for NelNet --- code/nel/samples/net/CMakeLists.txt | 2 +- .../net/class_transport/CMakeLists.txt | 2 +- .../samples/net/login_system/CMakeLists.txt | 2 +- .../nel/samples/net/net_layer3/CMakeLists.txt | 22 +++++++ code/nel/samples/net/net_layer3/client.cpp | 6 +- .../net/net_layer3/frontend_service.cpp | 26 +++++--- .../samples/net/net_layer3/ping_service.cpp | 62 ++++++++++++++++++- .../nel/samples/net/net_layer4/CMakeLists.txt | 22 +++++++ .../nel/samples/net/net_layer5/CMakeLists.txt | 22 +++++++ 9 files changed, 150 insertions(+), 16 deletions(-) create mode 100644 code/nel/samples/net/net_layer3/CMakeLists.txt create mode 100644 code/nel/samples/net/net_layer4/CMakeLists.txt create mode 100644 code/nel/samples/net/net_layer5/CMakeLists.txt diff --git a/code/nel/samples/net/CMakeLists.txt b/code/nel/samples/net/CMakeLists.txt index d0c4df49d..213e02be5 100644 --- a/code/nel/samples/net/CMakeLists.txt +++ b/code/nel/samples/net/CMakeLists.txt @@ -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) diff --git a/code/nel/samples/net/class_transport/CMakeLists.txt b/code/nel/samples/net/class_transport/CMakeLists.txt index 3561221d4..10084b051 100644 --- a/code/nel/samples/net/class_transport/CMakeLists.txt +++ b/code/nel/samples/net/class_transport/CMakeLists.txt @@ -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") diff --git a/code/nel/samples/net/login_system/CMakeLists.txt b/code/nel/samples/net/login_system/CMakeLists.txt index 97436fc90..0493c21cb 100644 --- a/code/nel/samples/net/login_system/CMakeLists.txt +++ b/code/nel/samples/net/login_system/CMakeLists.txt @@ -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") diff --git a/code/nel/samples/net/net_layer3/CMakeLists.txt b/code/nel/samples/net/net_layer3/CMakeLists.txt new file mode 100644 index 000000000..f170253cb --- /dev/null +++ b/code/nel/samples/net/net_layer3/CMakeLists.txt @@ -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) diff --git a/code/nel/samples/net/net_layer3/client.cpp b/code/nel/samples/net/net_layer3/client.cpp index 670f2becd..e06a4a7d0 100644 --- a/code/nel/samples/net/net_layer3/client.cpp +++ b/code/nel/samples/net/net_layer3/client.cpp @@ -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 diff --git a/code/nel/samples/net/net_layer3/frontend_service.cpp b/code/nel/samples/net/net_layer3/frontend_service.cpp index 2fbc3c2db..11527141e 100644 --- a/code/nel/samples/net/net_layer3/frontend_service.cpp +++ b/code/nel/samples/net/net_layer3/frontend_service.cpp @@ -37,6 +37,8 @@ #include "nel/net/callback_client.h" #include "nel/net/callback_server.h" using namespace NLNET; +using namespace NLMISC; + #include 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 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 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 vect( 400000 ); - msgout.serialCont( vect ); + // vector 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::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, "", "" ) diff --git a/code/nel/samples/net/net_layer3/ping_service.cpp b/code/nel/samples/net/net_layer3/ping_service.cpp index 8c762c922..ad3c68022 100644 --- a/code/nel/samples/net/net_layer3/ping_service.cpp +++ b/code/nel/samples/net/net_layer3/ping_service.cpp @@ -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 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, "", "" ) diff --git a/code/nel/samples/net/net_layer4/CMakeLists.txt b/code/nel/samples/net/net_layer4/CMakeLists.txt new file mode 100644 index 000000000..6fbde36e2 --- /dev/null +++ b/code/nel/samples/net/net_layer4/CMakeLists.txt @@ -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) diff --git a/code/nel/samples/net/net_layer5/CMakeLists.txt b/code/nel/samples/net/net_layer5/CMakeLists.txt new file mode 100644 index 000000000..1eeaacb62 --- /dev/null +++ b/code/nel/samples/net/net_layer5/CMakeLists.txt @@ -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)