diff --git a/reference/MtRpcClient/MtRpcClient.cpp b/reference/MtRpcClient/MtRpcClient.cpp new file mode 100644 index 000000000..36ef17502 --- /dev/null +++ b/reference/MtRpcClient/MtRpcClient.cpp @@ -0,0 +1,34 @@ +#include +#include + +using namespace Upp; + +void GetClientList(int i) +{ + INTERLOCKED { LOG("=>> GetClientList(" << i << ")"); } + + ValueArray result; + XmlRpcRequest call("127.0.0.1:1234"); + + if(call("GetClientList") >> result) + { + INTERLOCKED { LOG("Message received " << i); } + } + else + { + INTERLOCKED { LOG("Error: " << call.GetError()); } + } + + INTERLOCKED { LOG("<<= GetClientList(" << i << ")"); } +} + +CONSOLE_APP_MAIN +{ + StdLogSetup(LOG_COUT|LOG_FILE); + CoWork co; + for(int i = 0; i < 20; i++) + { + co & callback1(GetClientList, i); + } + +} diff --git a/reference/MtRpcClient/MtRpcClient.upp b/reference/MtRpcClient/MtRpcClient.upp new file mode 100644 index 000000000..53882c18f --- /dev/null +++ b/reference/MtRpcClient/MtRpcClient.upp @@ -0,0 +1,12 @@ +description "Multi thread rpc client\377"; + +uses + Core, + Core/Rpc; + +file + MtRpcClient.cpp; + +mainconfig + "" = "MT"; + diff --git a/reference/MtRpcClient/init b/reference/MtRpcClient/init new file mode 100644 index 000000000..cac0a13c1 --- /dev/null +++ b/reference/MtRpcClient/init @@ -0,0 +1,5 @@ +#ifndef _MtRpcClient_icpp_init_stub +#define _MtRpcClient_icpp_init_stub +#include "Core/init" +#include "Core/Rpc/init" +#endif diff --git a/reference/MtRpcServer/MtRpcServer.cpp b/reference/MtRpcServer/MtRpcServer.cpp new file mode 100644 index 000000000..1e0a3a23f --- /dev/null +++ b/reference/MtRpcServer/MtRpcServer.cpp @@ -0,0 +1,127 @@ +#include +#include +#ifdef PLATFORM_WIN32 +#include +#endif + +using namespace Upp; + +RPC_METHOD(GetClientList) +{ + INTERLOCKED { LOG("GetClientList()"); } + + ValueArray clients; + for(int i = 0; i < 1000; i++) + { + ValueMap client; + + client.Add("firstName", Format("First Name %d", i)); + client.Add("lastName", Format("Last Name %d", i)); + client.Add("birthday", GetSysDate()); + + clients.Add(client); + } + + Sleep(1000); + + rpc << clients; + INTERLOCKED { LOG("Done."); } +} + +class MtRpcServer +{ +private: + + TcpSocket rpc; + int threadPoolSize; + int port; + bool quit; + + static MtRpcServer *server; + + void Process(int n) + { + INTERLOCKED { LOG("Process " << n << " started"); } + Mutex accept_mutex; + + for(;;) + { + TcpSocket http; + accept_mutex.Enter(); + if(quit) + { + accept_mutex.Leave(); + break; + } + + bool b = http.Accept(rpc); + accept_mutex.Leave(); + if(quit) + break; + + if(b) + { + INTERLOCKED { LOG("Request accepted " << n); } + RpcPerform(http, NULL); + } + else + { + INTERLOCKED { LOG("Request rejected " << n); } + break; + } + } + } + + #ifdef PLATFORM_WIN32 + static BOOL WINAPI CtrlCHandlerRoutine(__in DWORD dwCtrlType) + { + LOG("Ctrl+C handler"); + server->quit = true; + for(int i = 0; i < server->threadPoolSize; i++) + { + TcpSocket h; + h.Connect("127.0.0.1", server->port); + h.Put("quit"); + } + return TRUE; + } + #endif + +public: + + MtRpcServer(int port = 1234, int poolSize = 8) + { + server = this; + #ifdef PLATFORM_WIN32 + SetConsoleCtrlHandler(CtrlCHandlerRoutine, true); + #endif + this->threadPoolSize = poolSize; + this->port = port; + this->quit = false; + } + + void Run() + { + rpc.Timeout(Null); + if(!rpc.Listen(port, 5)) + Exit(1); + + for(int i = 0; i < threadPoolSize; i++) + Thread::Start(callback1(this, &MtRpcServer::Process, i)); + + while(Thread::GetCount()) + Sleep(100); + } +}; + +MtRpcServer *MtRpcServer::server = NULL; + + +CONSOLE_APP_MAIN +{ + StdLogSetup(LOG_COUT|LOG_FILE); + LOG("MtRpcServer.."); + LogRpcRequests(); + MtRpcServer server; + server.Run(); +} diff --git a/reference/MtRpcServer/MtRpcServer.upp b/reference/MtRpcServer/MtRpcServer.upp new file mode 100644 index 000000000..1ca10bf21 --- /dev/null +++ b/reference/MtRpcServer/MtRpcServer.upp @@ -0,0 +1,12 @@ +description "Multi thread rpc server\377"; + +uses + Core, + Core/Rpc; + +file + MtRpcServer.cpp; + +mainconfig + "" = "MT"; + diff --git a/reference/MtRpcServer/init b/reference/MtRpcServer/init new file mode 100644 index 000000000..59e0eae0a --- /dev/null +++ b/reference/MtRpcServer/init @@ -0,0 +1,5 @@ +#ifndef _MtRpcServer_icpp_init_stub +#define _MtRpcServer_icpp_init_stub +#include "Core/init" +#include "Core/Rpc/init" +#endif