2018-06-25 10:47:27 +00:00
|
|
|
#include <qse/si/TcpServer.hpp>
|
2018-07-01 13:57:37 +00:00
|
|
|
#include <qse/si/Mutex.hpp>
|
2018-06-25 10:47:27 +00:00
|
|
|
#include <qse/si/sio.h>
|
2018-06-27 11:18:20 +00:00
|
|
|
#include <qse/si/os.h>
|
2018-06-25 10:47:27 +00:00
|
|
|
#include <qse/cmn/mem.h>
|
2018-09-05 14:52:51 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2018-07-01 02:11:33 +00:00
|
|
|
#include <qse/cmn/HeapMmgr.hpp>
|
2018-07-20 08:48:33 +00:00
|
|
|
#include <qse/si/App.hpp>
|
2018-06-27 11:18:20 +00:00
|
|
|
|
2018-07-01 13:57:37 +00:00
|
|
|
|
2018-06-25 10:47:27 +00:00
|
|
|
#include <locale.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-09-05 14:52:51 +00:00
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-06-25 15:52:05 +00:00
|
|
|
class ClientHandler
|
|
|
|
{
|
|
|
|
public:
|
2018-07-01 13:57:37 +00:00
|
|
|
int operator() (QSE::TcpServer* server, QSE::TcpServer::Worker* worker)
|
2018-06-25 15:52:05 +00:00
|
|
|
{
|
2018-06-29 04:53:28 +00:00
|
|
|
qse_char_t addrbuf[128];
|
2018-06-28 14:07:35 +00:00
|
|
|
qse_uint8_t bb[256];
|
|
|
|
qse_ssize_t n;
|
|
|
|
|
2018-07-01 13:57:37 +00:00
|
|
|
worker->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
|
|
|
qse_printf (QSE_T("hello word..from %s -> wid %zu\n"), addrbuf, worker->getWid());
|
2018-06-29 04:53:28 +00:00
|
|
|
|
2018-06-28 14:07:35 +00:00
|
|
|
while (!server->isStopRequested())
|
|
|
|
{
|
2018-07-01 13:57:37 +00:00
|
|
|
if ((n = worker->socket.receive(bb, QSE_COUNTOF(bb))) <= 0)
|
2018-06-29 04:53:28 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-01 13:57:37 +00:00
|
|
|
worker->socket.send (bb, n);
|
2018-06-28 14:07:35 +00:00
|
|
|
}
|
2018-06-29 04:53:28 +00:00
|
|
|
|
2018-07-01 13:57:37 +00:00
|
|
|
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
|
|
|
|
2018-08-31 09:39:12 +00:00
|
|
|
#if defined(QSE_LANG_CPP11)
|
|
|
|
static QSE::TcpServerL<int(QSE::TcpServer::Worker*)>* g_server;
|
|
|
|
#else
|
2018-06-26 15:45:44 +00:00
|
|
|
static QSE::TcpServerF<ClientHandler>* g_server;
|
2018-06-27 08:53:51 +00:00
|
|
|
#endif
|
2018-06-26 15:45:44 +00:00
|
|
|
|
2018-07-01 13:57:37 +00:00
|
|
|
|
2018-08-31 09:39:12 +00:00
|
|
|
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:
|
2018-09-04 08:46:48 +00:00
|
|
|
qse_printf (QSE_T("requesting to stop server...app %p server %p\n"), this, &this->server);
|
2018-08-31 09:39:12 +00:00
|
|
|
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)
|
2018-09-05 14:52:51 +00:00
|
|
|
{
|
|
|
|
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;
|
2018-08-31 09:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QSE::TcpServerF<ClientHandler> server;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int test1()
|
|
|
|
{
|
|
|
|
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
|
|
|
|
MyApp app (&heap_mmgr);
|
2018-09-04 08:46:48 +00:00
|
|
|
|
2018-09-06 09:59:54 +00:00
|
|
|
//MyApp app2 (&heap_mmgr);
|
|
|
|
//MyApp app3 (&heap_mmgr);
|
|
|
|
//MyApp app4 (&heap_mmgr);
|
2018-09-04 08:46:48 +00:00
|
|
|
|
2018-09-05 14:52:51 +00:00
|
|
|
app.subscribeToSignal (SIGINT, true);
|
2018-09-04 08:46:48 +00:00
|
|
|
app.subscribeToSignal (SIGTERM, true);
|
|
|
|
|
2018-09-06 09:59:54 +00:00
|
|
|
//app4.subscribeToSignal (SIGINT, true);
|
|
|
|
//app3.subscribeToSignal (SIGINT, true);
|
|
|
|
//app2.subscribeToSignal (SIGINT, true);
|
2018-09-04 08:46:48 +00:00
|
|
|
|
2018-08-31 09:39:12 +00:00
|
|
|
int n = app.run();
|
2018-09-04 08:46:48 +00:00
|
|
|
app.subscribeToSignal (SIGTERM, false);
|
|
|
|
app.subscribeToSignal (SIGINT, false);
|
|
|
|
|
2018-09-06 09:59:54 +00:00
|
|
|
//app4.unsubscribeFromSignal (SIGINT);
|
|
|
|
//app3.unsubscribeFromSignal (SIGINT);
|
|
|
|
//app2.unsubscribeFromSignal (SIGINT);
|
2018-08-31 09:39:12 +00:00
|
|
|
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
|
|
|
{
|
2018-07-01 02:11:33 +00:00
|
|
|
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
|
|
|
|
|
2018-06-25 15:52:05 +00:00
|
|
|
#if defined(QSE_LANG_CPP11)
|
2018-07-01 13:57:37 +00:00
|
|
|
QSE::TcpServerL<int(QSE::TcpServer::Worker*)> server (
|
2018-06-28 04:08:43 +00:00
|
|
|
|
|
|
|
// workload by lambda
|
2018-07-01 13:57:37 +00:00
|
|
|
([&server](QSE::TcpServer::Worker* worker) {
|
2018-06-29 04:53:28 +00:00
|
|
|
qse_char_t addrbuf[128];
|
2018-06-27 11:18:20 +00:00
|
|
|
qse_uint8_t bb[256];
|
2018-06-28 09:43:34 +00:00
|
|
|
qse_ssize_t n;
|
2018-07-01 13:57:37 +00:00
|
|
|
|
|
|
|
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();
|
2018-07-01 07:33:56 +00:00
|
|
|
|
2018-06-29 16:48:20 +00:00
|
|
|
while (!server.isStopRequested())
|
2018-06-27 11:18:20 +00:00
|
|
|
{
|
2018-07-01 13:57:37 +00:00
|
|
|
if ((n = worker->socket.receive(bb, QSE_COUNTOF(bb))) <= 0)
|
2018-06-29 04:53:28 +00:00
|
|
|
{
|
2018-07-01 13:57:37 +00:00
|
|
|
g_prt_mutex.lock();
|
2018-06-29 04:53:28 +00:00
|
|
|
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
|
2018-07-01 13:57:37 +00:00
|
|
|
g_prt_mutex.unlock();
|
2018-06-29 04:53:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-07-01 13:57:37 +00:00
|
|
|
worker->socket.send (bb, n);
|
2018-06-27 11:18:20 +00:00
|
|
|
}
|
2018-07-01 07:59:06 +00:00
|
|
|
|
2018-07-01 13:57:37 +00:00
|
|
|
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;
|
2018-07-01 02:20:03 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
&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;
|
2018-06-27 11:18:20 +00:00
|
|
|
//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
|
|
|
|
2018-07-20 08:48:33 +00:00
|
|
|
static void handle_sigint (int sig)
|
2018-06-25 10:47:27 +00:00
|
|
|
{
|
2018-06-26 15:45:44 +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
|
|
|
|
{
|
2018-09-05 14:52:51 +00:00
|
|
|
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 ();
|
2018-07-20 08:48:33 +00:00
|
|
|
|
2018-08-31 09:39:12 +00:00
|
|
|
//QSE::App::setSignalHandler (SIGINT, handle_sigint);
|
2018-06-25 10:47:27 +00:00
|
|
|
test1();
|
2018-08-31 09:39:12 +00:00
|
|
|
//QSE::App::unsetSignalHandler (SIGINT);
|
2018-07-20 08:48:33 +00:00
|
|
|
|
2018-06-25 10:47:27 +00:00
|
|
|
qse_close_stdsios ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-31 09:39:12 +00:00
|
|
|
|
|
|
|
#endif ////////////////////////
|