qse/qse/samples/si/tcpsvr01.cpp

243 lines
5.0 KiB
C++
Raw Normal View History

2018-06-25 10:47:27 +00:00
#include <qse/si/TcpServer.hpp>
#include <qse/si/Mutex.hpp>
2018-06-25 10:47:27 +00:00
#include <qse/si/sio.h>
#include <qse/si/os.h>
2018-06-25 10:47:27 +00:00
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/si/App.hpp>
2018-06-25 10:47:27 +00:00
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <unistd.h>
2018-06-25 15:52:05 +00:00
class ClientHandler
{
public:
int operator() (QSE::TcpServer* server, QSE::TcpServer::Worker* worker)
2018-06-25 15:52:05 +00:00
{
qse_char_t addrbuf[128];
qse_uint8_t bb[256];
qse_ssize_t n;
worker->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
qse_printf (QSE_T("hello word..from %s -> wid %zu\n"), addrbuf, worker->getWid());
while (!server->isStopRequested())
{
if ((n = worker->socket.receive(bb, QSE_COUNTOF(bb))) <= 0)
{
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
break;
}
worker->socket.send (bb, n);
}
qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, worker->getWid());
2018-06-25 15:52:05 +00:00
return 0;
}
};
2018-06-25 10:47:27 +00:00
#if defined(QSE_LANG_CPP11)
static QSE::TcpServerL<int(QSE::TcpServer::Worker*)>* g_server;
#else
static QSE::TcpServerF<ClientHandler>* g_server;
2018-06-27 08:53:51 +00:00
#endif
class MyApp: public QSE::App
{
public:
MyApp(QSE::Mmgr* mmgr): App(mmgr), server(mmgr) {}
void on_signal (int sig)
{
switch (sig)
{
case SIGINT:
case SIGTERM:
case SIGHUP:
qse_printf (QSE_T("requesting to stop server...app %p server %p\n"), this, &this->server);
this->server.stop();
break;
}
}
int run ()
{
2018-09-08 05:15:51 +00:00
QSE::App::Sigset signals;
signals.set (SIGINT);
signals.set (SIGHUP);
signals.set (SIGTERM);
signals.set (SIGUSR1);
signals.set (SIGUSR2);
if (this->guardProcess("myapp", signals) > 0)
{
qse_printf (QSE_T("Stareting workd\n"));
this->server.setThreadStackSize (256000);
return this->server.start (QSE_T("[::]:9998,0.0.0.0:9998"));
}
return -1;
}
protected:
QSE::TcpServerF<ClientHandler> server;
};
static int test1()
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
MyApp app (&heap_mmgr);
//MyApp app2 (&heap_mmgr);
//MyApp app3 (&heap_mmgr);
//MyApp app4 (&heap_mmgr);
app.subscribeToSignal (SIGINT, true);
app.subscribeToSignal (SIGTERM, true);
//app4.subscribeToSignal (SIGINT, true);
//app3.subscribeToSignal (SIGINT, true);
//app2.subscribeToSignal (SIGINT, true);
int n = app.run();
app.subscribeToSignal (SIGTERM, false);
app.subscribeToSignal (SIGINT, false);
//app4.unsubscribeFromSignal (SIGINT);
//app3.unsubscribeFromSignal (SIGINT);
//app2.unsubscribeFromSignal (SIGINT);
return n;
}
int main ()
{
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
#endif
qse_open_stdsios ();
test1();
qse_close_stdsios ();
return 0;
}
#if 0 ////////////////////////
2018-06-25 10:47:27 +00:00
static int test1 (void)
2018-06-25 15:52:05 +00:00
{
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
2018-06-25 15:52:05 +00:00
#if defined(QSE_LANG_CPP11)
QSE::TcpServerL<int(QSE::TcpServer::Worker*)> server (
// workload by lambda
([&server](QSE::TcpServer::Worker* worker) {
qse_char_t addrbuf[128];
qse_uint8_t bb[256];
qse_ssize_t n;
worker->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
g_prt_mutex.lock();
qse_printf (QSE_T("hello word..from %s -> wid %zu\n"), addrbuf, worker->getWid());
g_prt_mutex.unlock();
while (!server.isStopRequested())
{
if ((n = worker->socket.receive(bb, QSE_COUNTOF(bb))) <= 0)
{
g_prt_mutex.lock();
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
g_prt_mutex.unlock();
break;
}
worker->socket.send (bb, n);
}
g_prt_mutex.lock();
qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, worker->getWid());
g_prt_mutex.unlock();
2018-06-25 15:52:05 +00:00
return 0;
}),
&heap_mmgr
2018-06-27 08:53:51 +00:00
);
#else
2018-07-01 14:04:07 +00:00
QSE::TcpServerF<ClientHandler> server (&heap_mmgr);
2018-06-25 15:52:05 +00:00
#endif
2018-06-27 08:53:51 +00:00
server.setThreadStackSize (256000);
g_server = &server;
//server.start (QSE_T("0.0.0.0:9998"));
server.start (QSE_T("[::]:9998,0.0.0.0:9998"));
//server.start (QSE_T("[fe80::1c4:a90d:a0f0:d52%wlan0]:9998,0.0.0.0:9998"));
2018-06-27 08:53:51 +00:00
g_server = QSE_NULL;
2018-06-25 15:52:05 +00:00
return 0;
}
2018-06-25 10:47:27 +00:00
static void handle_sigint (int sig)
2018-06-25 10:47:27 +00:00
{
if (g_server) g_server->stop ();
2018-06-25 10:47:27 +00:00
}
int main ()
{
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
2018-06-25 10:47:27 +00:00
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
#endif
qse_open_stdsios ();
//QSE::App::setSignalHandler (SIGINT, handle_sigint);
2018-06-25 10:47:27 +00:00
test1();
//QSE::App::unsetSignalHandler (SIGINT);
2018-06-25 10:47:27 +00:00
qse_close_stdsios ();
return 0;
}
#endif ////////////////////////