rename QSE::TcpServer::Client to QSE::TcpServer::Worker.
added code to generate unique worker id
This commit is contained in:
parent
3d5d9aebfb
commit
797518df18
@ -72,20 +72,20 @@ public:
|
|||||||
}
|
}
|
||||||
void setMaxConnections (qse_size_t mc) QSE_CPP_NOEXCEPT
|
void setMaxConnections (qse_size_t mc) QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
// don't disconnect client connections
|
// don't disconnect worker connections
|
||||||
// establised before maxConn is set.
|
// establised before maxConn is set.
|
||||||
// 0 means there's no restriction over
|
// 0 means there's no restriction over
|
||||||
// the number of connection.
|
// the number of connection.
|
||||||
this->max_connections = mc;
|
this->max_connections = mc;
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_size_t getClientCount () const QSE_CPP_NOEXCEPT
|
qse_size_t getWorkerCount () const QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
return this->client_list[Client::LIVE].getSize();
|
return this->worker_list[Worker::LIVE].getSize();
|
||||||
}
|
}
|
||||||
qse_size_t getConnectionCount () const QSE_CPP_NOEXCEPT
|
qse_size_t getConnectionCount () const QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
return this->client_list[Client::LIVE].getSize();
|
return this->worker_list[Worker::LIVE].getSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_size_t getThreadStackSize () const QSE_CPP_NOEXCEPT
|
qse_size_t getThreadStackSize () const QSE_CPP_NOEXCEPT
|
||||||
@ -109,7 +109,8 @@ protected:
|
|||||||
Listener* next_listener;
|
Listener* next_listener;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Client: public QSE::Thread
|
public:
|
||||||
|
class Worker: public QSE::Thread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
friend class TcpServer;
|
friend class TcpServer;
|
||||||
@ -119,7 +120,7 @@ protected:
|
|||||||
DEAD = 0,
|
DEAD = 0,
|
||||||
LIVE = 1
|
LIVE = 1
|
||||||
};
|
};
|
||||||
Client (Listener* listener) QSE_CPP_NOEXCEPT: listener(listener), prev_client(QSE_NULL), next_client(QSE_NULL), claimed(false) {}
|
Worker (Listener* listener) QSE_CPP_NOEXCEPT: listener(listener), prev_worker(QSE_NULL), next_worker(QSE_NULL), claimed(false), wid(wid_map_t::WID_INVALID) {}
|
||||||
|
|
||||||
int main ();
|
int main ();
|
||||||
int stop () QSE_CPP_NOEXCEPT;
|
int stop () QSE_CPP_NOEXCEPT;
|
||||||
@ -131,17 +132,45 @@ protected:
|
|||||||
const TcpServer* getServer() const QSE_CPP_NOEXCEPT { return this->listener->server; }
|
const TcpServer* getServer() const QSE_CPP_NOEXCEPT { return this->listener->server; }
|
||||||
|
|
||||||
|
|
||||||
Client* getNextClient() { return this->next_client; }
|
Worker* getNextWorker() { return this->next_worker; }
|
||||||
Client* getPrevClient() { return this->prev_client; }
|
Worker* getPrevWorker() { return this->prev_worker; }
|
||||||
|
|
||||||
|
qse_size_t getWid() const { return this->wid; }
|
||||||
|
|
||||||
Listener* listener;
|
Listener* listener;
|
||||||
Client* prev_client;
|
Worker* prev_worker;
|
||||||
Client* next_client;
|
Worker* next_worker;
|
||||||
bool claimed;
|
bool claimed;
|
||||||
|
qse_size_t wid;
|
||||||
|
|
||||||
QSE::Socket socket;
|
QSE::Socket socket;
|
||||||
QSE::SocketAddress address;
|
QSE::SocketAddress address;
|
||||||
QSE::SpinLock csspl; // spin lock for client stop
|
QSE::SpinLock csspl; // spin lock for worker stop
|
||||||
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
struct wid_map_data_t
|
||||||
|
{
|
||||||
|
int used;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
Worker* worker;
|
||||||
|
qse_size_t next;
|
||||||
|
} u;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wid_map_t
|
||||||
|
{
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
WID_INVALID = (qse_size_t)-1
|
||||||
|
};
|
||||||
|
|
||||||
|
wid_map_t(): ptr(QSE_NULL), capa(0), free_first(WID_INVALID), free_last(WID_INVALID) {}
|
||||||
|
wid_map_data_t* ptr;
|
||||||
|
qse_size_t capa;
|
||||||
|
qse_size_t free_first;
|
||||||
|
qse_size_t free_last;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ListenerList
|
struct ListenerList
|
||||||
@ -162,58 +191,59 @@ protected:
|
|||||||
qse_size_t count;
|
qse_size_t count;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ClientList
|
struct WorkerList
|
||||||
{
|
{
|
||||||
ClientList() QSE_CPP_NOEXCEPT: head(QSE_NULL), tail(QSE_NULL), count(0) {}
|
WorkerList() QSE_CPP_NOEXCEPT: head(QSE_NULL), tail(QSE_NULL), count(0) {}
|
||||||
|
|
||||||
qse_size_t getSize() const { return this->count; }
|
qse_size_t getSize() const { return this->count; }
|
||||||
Client* getHead() { return this->head; }
|
Worker* getHead() { return this->head; }
|
||||||
Client* getTail() { return this->tail; }
|
Worker* getTail() { return this->tail; }
|
||||||
|
|
||||||
void append (Client* client)
|
void append (Worker* worker)
|
||||||
{
|
{
|
||||||
client->next_client = QSE_NULL;
|
worker->next_worker = QSE_NULL;
|
||||||
if (this->count == 0)
|
if (this->count == 0)
|
||||||
{
|
{
|
||||||
this->head = this->tail = client;
|
this->head = this->tail = worker;
|
||||||
client->prev_client = QSE_NULL;
|
worker->prev_worker = QSE_NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
client->prev_client = this->tail;
|
worker->prev_worker = this->tail;
|
||||||
this->tail->next_client = client;
|
this->tail->next_worker = worker;
|
||||||
this->tail = client;
|
this->tail = worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->count++;
|
this->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove (Client* client)
|
void remove (Worker* worker)
|
||||||
{
|
{
|
||||||
if (client->next_client)
|
if (worker->next_worker)
|
||||||
client->next_client->prev_client = client->prev_client;
|
worker->next_worker->prev_worker = worker->prev_worker;
|
||||||
else
|
else
|
||||||
this->tail = client->prev_client;
|
this->tail = worker->prev_worker;
|
||||||
|
|
||||||
if (client->prev_client)
|
if (worker->prev_worker)
|
||||||
client->prev_client->next_client = client->next_client;
|
worker->prev_worker->next_worker = worker->next_worker;
|
||||||
else
|
else
|
||||||
this->head = client->next_client;
|
this->head = worker->next_worker;
|
||||||
|
|
||||||
client->prev_client = QSE_NULL;
|
worker->prev_worker = QSE_NULL;
|
||||||
client->next_client = QSE_NULL;
|
worker->next_worker = QSE_NULL;
|
||||||
this->count--;
|
this->count--;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Client* head;
|
Worker* head;
|
||||||
Client* tail;
|
Worker* tail;
|
||||||
qse_size_t count;
|
qse_size_t count;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
wid_map_t wid_map; // worker's id map
|
||||||
ListenerList listener_list;
|
ListenerList listener_list;
|
||||||
ClientList client_list[2];
|
WorkerList worker_list[2];
|
||||||
QSE::SpinLock client_list_spl;
|
QSE::SpinLock worker_list_spl;
|
||||||
|
|
||||||
ErrorCode errcode;
|
ErrorCode errcode;
|
||||||
bool stop_requested;
|
bool stop_requested;
|
||||||
@ -221,15 +251,21 @@ protected:
|
|||||||
qse_size_t max_connections;
|
qse_size_t max_connections;
|
||||||
qse_size_t thread_stack_size;
|
qse_size_t thread_stack_size;
|
||||||
|
|
||||||
friend class TcpServer::Client;
|
friend class TcpServer::Worker;
|
||||||
virtual int handle_client (Socket* sock, SocketAddress* addr) = 0;
|
virtual int handle_worker (Worker* worker) = 0;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void delete_all_clients (Client::State state) QSE_CPP_NOEXCEPT;
|
void delete_all_workers (Worker::State state) QSE_CPP_NOEXCEPT;
|
||||||
|
|
||||||
int setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT;
|
int setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT;
|
||||||
void free_all_listeners () QSE_CPP_NOEXCEPT;
|
void free_all_listeners () QSE_CPP_NOEXCEPT;
|
||||||
|
|
||||||
|
int prepare_to_acquire_wid () QSE_CPP_NOEXCEPT;
|
||||||
|
void acquire_wid (Worker* worker) QSE_CPP_NOEXCEPT;
|
||||||
|
void release_wid (Worker* worker) QSE_CPP_NOEXCEPT;
|
||||||
|
void free_wid_map () QSE_CPP_NOEXCEPT;
|
||||||
|
|
||||||
static void dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QSE_CPP_NOEXCEPT;
|
static void dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QSE_CPP_NOEXCEPT;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -248,9 +284,9 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
F __lfunc;
|
F __lfunc;
|
||||||
|
|
||||||
int handle_client (Socket* sock, SocketAddress* addr)
|
int handle_worker (Worker* worker)
|
||||||
{
|
{
|
||||||
return this->__lfunc(this, sock, addr);
|
return this->__lfunc(this, worker);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -277,7 +313,7 @@ public:
|
|||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// upon failure, i set this->__lfunc to null.
|
// upon failure, i set this->__lfunc to null.
|
||||||
// this->handle_client() will return failure for this.
|
// this->handle_worker() will return failure for this.
|
||||||
this->__lfunc = nullptr;
|
this->__lfunc = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -288,7 +324,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
int setClientHandler (T&& f) QSE_CPP_NOEXCEPT
|
int setWorkerHandler (T&& f) QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
Callable* lf;
|
Callable* lf;
|
||||||
|
|
||||||
@ -330,7 +366,7 @@ protected:
|
|||||||
|
|
||||||
Callable* __lfunc;
|
Callable* __lfunc;
|
||||||
|
|
||||||
int handle_client (Socket* sock, SocketAddress* addr)
|
int handle_worker (Worker* worker)
|
||||||
{
|
{
|
||||||
if (!this->__lfunc)
|
if (!this->__lfunc)
|
||||||
{
|
{
|
||||||
@ -338,7 +374,7 @@ protected:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this->__lfunc->invoke(sock, addr);
|
return this->__lfunc->invoke(worker);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -33,23 +33,25 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#define WID_MAP_ALIGN 128
|
||||||
|
#define WID_MAX (wid_map_t::WID_INVALID - 1)
|
||||||
|
|
||||||
QSE_BEGIN_NAMESPACE(QSE)
|
QSE_BEGIN_NAMESPACE(QSE)
|
||||||
|
|
||||||
#include "../cmn/syserr.h"
|
#include "../cmn/syserr.h"
|
||||||
IMPLEMENT_SYSERR_TO_ERRNUM (TcpServer::ErrorCode, TcpServer::)
|
IMPLEMENT_SYSERR_TO_ERRNUM (TcpServer::ErrorCode, TcpServer::)
|
||||||
|
|
||||||
int TcpServer::Client::main ()
|
int TcpServer::Worker::main ()
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
// blockAllSignals is called inside run because
|
// blockAllSignals is called inside run because
|
||||||
// Client is instantiated in the TcpServer thread.
|
// Worker is instantiated in the TcpServer thread.
|
||||||
// so if it is called in the constructor of Client,
|
// so if it is called in the constructor of Worker,
|
||||||
// it would just block signals to the TcpProxy thread.
|
// it would just block signals to the TcpProxy thread.
|
||||||
this->blockAllSignals (); // don't care about the result.
|
this->blockAllSignals (); // don't care about the result.
|
||||||
|
|
||||||
try { n = this->listener->server->handle_client(&this->socket, &this->address); }
|
try { n = this->listener->server->handle_worker(this); }
|
||||||
catch (...) { n = -1; }
|
catch (...) { n = -1; }
|
||||||
|
|
||||||
this->csspl.lock ();
|
this->csspl.lock ();
|
||||||
@ -58,27 +60,27 @@ int TcpServer::Client::main ()
|
|||||||
|
|
||||||
TcpServer* server = this->getServer();
|
TcpServer* server = this->getServer();
|
||||||
|
|
||||||
server->client_list_spl.lock ();
|
server->worker_list_spl.lock ();
|
||||||
if (!this->claimed)
|
if (!this->claimed)
|
||||||
{
|
{
|
||||||
server->client_list[Client::LIVE].remove (this);
|
server->worker_list[Worker::LIVE].remove (this);
|
||||||
server->client_list[Client::DEAD].append (this);
|
server->worker_list[Worker::DEAD].append (this);
|
||||||
}
|
}
|
||||||
server->client_list_spl.unlock ();
|
server->worker_list_spl.unlock ();
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TcpServer::Client::stop () QSE_CPP_NOEXCEPT
|
int TcpServer::Worker::stop () QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
// the receiver will be notified of the end of
|
// the receiver will be notified of the end of
|
||||||
// the connection by the socket's closing.
|
// the connection by the socket's closing.
|
||||||
// therefore, handle_client() must return
|
// therefore, handle_worker() must return
|
||||||
// when it detects the end of the connection.
|
// when it detects the end of the connection.
|
||||||
//
|
//
|
||||||
// TODO: must think of a better way to do this
|
// TODO: must think of a better way to do this
|
||||||
// as it might not be thread-safe.
|
// as it might not be thread-safe.
|
||||||
// but it is still ok because Client::stop()
|
// but it is still ok because Worker::stop()
|
||||||
// is rarely called.
|
// is rarely called.
|
||||||
this->csspl.lock ();
|
this->csspl.lock ();
|
||||||
this->socket.shutdown ();
|
this->socket.shutdown ();
|
||||||
@ -99,8 +101,8 @@ TcpServer::TcpServer (Mmgr* mmgr) QSE_CPP_NOEXCEPT:
|
|||||||
TcpServer::~TcpServer () QSE_CPP_NOEXCEPT
|
TcpServer::~TcpServer () QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
// QSE_ASSERT (this->server_serving == false);
|
// QSE_ASSERT (this->server_serving == false);
|
||||||
this->delete_all_clients (Client::LIVE);
|
this->delete_all_workers (Worker::LIVE);
|
||||||
this->delete_all_clients (Client::DEAD);
|
this->delete_all_workers (Worker::DEAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
|
void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
|
||||||
@ -151,6 +153,7 @@ struct mux_xtn_t
|
|||||||
TcpServer* server;
|
TcpServer* server;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QSE_CPP_NOEXCEPT
|
void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
mux_xtn_t* mux_xtn = (mux_xtn_t*)qse_mux_getxtn(mux);
|
mux_xtn_t* mux_xtn = (mux_xtn_t*)qse_mux_getxtn(mux);
|
||||||
@ -158,7 +161,7 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
|
|||||||
|
|
||||||
if (mux_xtn->first_time)
|
if (mux_xtn->first_time)
|
||||||
{
|
{
|
||||||
server->delete_all_clients(Client::DEAD);
|
server->delete_all_workers(Worker::DEAD);
|
||||||
mux_xtn->first_time = false;
|
mux_xtn->first_time = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,37 +178,35 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
|
|||||||
/* the reset should be the listener's socket */
|
/* the reset should be the listener's socket */
|
||||||
Listener* lsck = (Listener*)evt->data;
|
Listener* lsck = (Listener*)evt->data;
|
||||||
|
|
||||||
if (server->max_connections > 0 && server->max_connections <= server->client_list[Client::LIVE].getSize())
|
if (server->max_connections > 0 && server->max_connections <= server->worker_list[Worker::LIVE].getSize())
|
||||||
{
|
{
|
||||||
// too many connections. accept the connection and close it.
|
// too many connections. accept the connection and close it.
|
||||||
|
|
||||||
Socket s;
|
|
||||||
SocketAddress sa;
|
|
||||||
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
|
|
||||||
// TODO: logging.
|
// TODO: logging.
|
||||||
return;
|
goto accept_and_drop;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client* client;
|
Worker* worker;
|
||||||
|
|
||||||
// allocating the client object before accept is
|
// allocating the worker object before accept is
|
||||||
// a bit awkward. but socket.accept() can be passed
|
// a bit awkward. but socket.accept() can be passed
|
||||||
// the socket field inside the client object.
|
// the socket field inside the worker object.
|
||||||
try { client = new(server->getMmgr()) Client(lsck); }
|
try { worker = new(server->getMmgr()) Worker(lsck); }
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// memory alloc failed. accept the connection and close it.
|
// memory alloc failed. accept the connection and close it.
|
||||||
|
|
||||||
Socket s;
|
|
||||||
SocketAddress sa;
|
|
||||||
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
|
|
||||||
// TODO: logging.
|
// TODO: logging.
|
||||||
return;
|
goto accept_and_drop;
|
||||||
|
}
|
||||||
|
if (server->wid_map.free_first == wid_map_t::WID_INVALID && server->prepare_to_acquire_wid() <= -1)
|
||||||
|
{
|
||||||
|
QSE_CPP_DELETE_WITH_MMGR (worker, Worker, server->getMmgr());
|
||||||
|
// TODO: logging
|
||||||
|
goto accept_and_drop;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lsck->accept(&client->socket, &client->address, Socket::T_CLOEXEC) <= -1)
|
if (lsck->accept(&worker->socket, &worker->address, Socket::T_CLOEXEC) <= -1)
|
||||||
{
|
{
|
||||||
QSE_CPP_DELETE_WITH_MMGR (client, Client, server->getMmgr());
|
QSE_CPP_DELETE_WITH_MMGR (worker, Worker, server->getMmgr());
|
||||||
|
|
||||||
if (server->isStopRequested()) return; /* normal termination requested */
|
if (server->isStopRequested()) return; /* normal termination requested */
|
||||||
|
|
||||||
@ -217,26 +218,38 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
server->client_list_spl.lock ();
|
server->worker_list_spl.lock ();
|
||||||
server->client_list[Client::LIVE].append (client);
|
server->worker_list[Worker::LIVE].append (worker);
|
||||||
server->client_list_spl.unlock ();
|
server->worker_list_spl.unlock ();
|
||||||
|
|
||||||
client->setStackSize (server->thread_stack_size);
|
server->acquire_wid (worker);
|
||||||
|
worker->setStackSize (server->thread_stack_size);
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
if (client->start(Thread::DETACHED) <= -1)
|
if (worker->start(Thread::DETACHED) <= -1)
|
||||||
#else
|
#else
|
||||||
if (client->start(0) <= -1)
|
if (worker->start(0) <= -1)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
// TODO: logging.
|
// TODO: logging.
|
||||||
|
|
||||||
server->client_list_spl.lock ();
|
server->worker_list_spl.lock ();
|
||||||
server->client_list[Client::LIVE].remove (client);
|
server->worker_list[Worker::LIVE].remove (worker);
|
||||||
server->client_list_spl.unlock ();
|
server->worker_list_spl.unlock ();
|
||||||
QSE_CPP_DELETE_WITH_MMGR (client, Client, server->getMmgr());
|
|
||||||
|
server->release_wid (worker);
|
||||||
|
QSE_CPP_DELETE_WITH_MMGR (worker, Worker, server->getMmgr());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
accept_and_drop:
|
||||||
|
Socket s;
|
||||||
|
SocketAddress sa;
|
||||||
|
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
|
||||||
|
// TODO: logging.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int TcpServer::setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
|
int TcpServer::setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
|
||||||
@ -399,18 +412,19 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->delete_all_clients (Client::LIVE);
|
this->delete_all_workers (Worker::LIVE);
|
||||||
this->delete_all_clients (Client::DEAD);
|
this->delete_all_workers (Worker::DEAD);
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
this->delete_all_clients (Client::LIVE);
|
this->delete_all_workers (Worker::LIVE);
|
||||||
this->delete_all_clients (Client::DEAD);
|
this->delete_all_workers (Worker::DEAD);
|
||||||
|
|
||||||
this->setErrorCode (E_EEXCEPT);
|
this->setErrorCode (E_EEXCEPT);
|
||||||
this->server_serving = false;
|
this->server_serving = false;
|
||||||
this->setStopRequested (false);
|
this->setStopRequested (false);
|
||||||
this->free_all_listeners ();
|
this->free_all_listeners ();
|
||||||
|
this->free_wid_map ();
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -418,6 +432,7 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
|
|||||||
this->server_serving = false;
|
this->server_serving = false;
|
||||||
this->setStopRequested (false);
|
this->setStopRequested (false);
|
||||||
this->free_all_listeners ();
|
this->free_all_listeners ();
|
||||||
|
this->free_wid_map ();
|
||||||
|
|
||||||
return xret;
|
return xret;
|
||||||
}
|
}
|
||||||
@ -437,25 +452,118 @@ int TcpServer::stop () QSE_CPP_NOEXCEPT
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpServer::delete_all_clients (Client::State state) QSE_CPP_NOEXCEPT
|
void TcpServer::delete_all_workers (Worker::State state) QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
Client* np;
|
Worker* worker;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
this->client_list_spl.lock();
|
this->worker_list_spl.lock();
|
||||||
np = this->client_list[state].getHead();
|
worker = this->worker_list[state].getHead();
|
||||||
if (np)
|
if (worker)
|
||||||
{
|
{
|
||||||
this->client_list[state].remove (np);
|
this->worker_list[state].remove (worker);
|
||||||
np->claimed = true;
|
worker->claimed = true;
|
||||||
}
|
}
|
||||||
this->client_list_spl.unlock();
|
this->worker_list_spl.unlock();
|
||||||
if (!np) break;
|
if (!worker) break;
|
||||||
|
|
||||||
np->stop();
|
worker->stop();
|
||||||
np->join ();
|
worker->join ();
|
||||||
QSE_CPP_DELETE_WITH_MMGR (np, Client, this->getMmgr()); // delete np
|
|
||||||
|
this->release_wid (worker);
|
||||||
|
QSE_CPP_DELETE_WITH_MMGR (worker, Worker, this->getMmgr()); // delete worker
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int TcpServer::prepare_to_acquire_wid () QSE_CPP_NOEXCEPT
|
||||||
|
{
|
||||||
|
qse_size_t new_capa;
|
||||||
|
qse_size_t i, j;
|
||||||
|
wid_map_data_t* tmp;
|
||||||
|
|
||||||
|
QSE_ASSERT (this->wid_map.free_first == wid_map_t::WID_INVALID);
|
||||||
|
QSE_ASSERT (this->wid_map.free_last == wid_map_t::WID_INVALID);
|
||||||
|
|
||||||
|
new_capa = QSE_ALIGNTO_POW2(this->wid_map.capa + 1, WID_MAP_ALIGN);
|
||||||
|
if (new_capa > WID_MAX)
|
||||||
|
{
|
||||||
|
if (this->wid_map.capa >= WID_MAX)
|
||||||
|
{
|
||||||
|
this->setErrorCode (E_ENOMEM); // TODO: proper error code
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_capa = WID_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = (wid_map_data_t*)QSE_MMGR_REALLOC(this->getMmgr(), this->wid_map.ptr, QSE_SIZEOF(*tmp) * new_capa);
|
||||||
|
if (!tmp)
|
||||||
|
{
|
||||||
|
this->setErrorCode (E_ENOMEM);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->wid_map.free_first = this->wid_map.capa;
|
||||||
|
for (i = this->wid_map.capa, j = this->wid_map.capa + 1; j < new_capa; i++, j++)
|
||||||
|
{
|
||||||
|
tmp[i].used = 0;
|
||||||
|
tmp[i].u.next = j;
|
||||||
|
}
|
||||||
|
tmp[i].used = 0;
|
||||||
|
tmp[i].u.next = wid_map_t::WID_INVALID;
|
||||||
|
this->wid_map.free_last = i;
|
||||||
|
|
||||||
|
this->wid_map.ptr = tmp;
|
||||||
|
this->wid_map.capa = new_capa;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpServer::acquire_wid (Worker* worker) QSE_CPP_NOEXCEPT
|
||||||
|
{
|
||||||
|
qse_size_t wid;
|
||||||
|
|
||||||
|
wid = this->wid_map.free_first;
|
||||||
|
worker->wid = wid;
|
||||||
|
|
||||||
|
this->wid_map.free_first = this->wid_map.ptr[wid].u.next;
|
||||||
|
if (this->wid_map.free_first == wid_map_t::WID_INVALID) this->wid_map.free_last = wid_map_t::WID_INVALID;
|
||||||
|
|
||||||
|
this->wid_map.ptr[wid].used = 1;
|
||||||
|
this->wid_map.ptr[wid].u.worker = worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpServer::release_wid (Worker* worker) QSE_CPP_NOEXCEPT
|
||||||
|
{
|
||||||
|
qse_size_t wid;
|
||||||
|
|
||||||
|
wid = worker->wid;
|
||||||
|
QSE_ASSERT (wid < this->wid_map.capa && wid != wid_map_t::WID_INVALID);
|
||||||
|
|
||||||
|
this->wid_map.ptr[wid].used = 0;
|
||||||
|
this->wid_map.ptr[wid].u.next = wid_map_t::WID_INVALID;
|
||||||
|
if (this->wid_map.free_last == wid_map_t::WID_INVALID)
|
||||||
|
{
|
||||||
|
QSE_ASSERT (this->wid_map.free_first <= wid_map_t::WID_INVALID);
|
||||||
|
this->wid_map.free_first = wid;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->wid_map.ptr[this->wid_map.free_last].u.next = wid;
|
||||||
|
}
|
||||||
|
this->wid_map.free_last = wid;
|
||||||
|
worker->wid = wid_map_t::WID_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpServer::free_wid_map () QSE_CPP_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (this->wid_map.ptr)
|
||||||
|
{
|
||||||
|
QSE_MMGR_FREE (this->getMmgr(), this->wid_map.ptr);
|
||||||
|
this->wid_map.capa = 0;
|
||||||
|
this->wid_map.free_first = wid_map_t::WID_INVALID;
|
||||||
|
this->wid_map.free_last = wid_map_t::WID_INVALID;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
#include <qse/si/TcpServer.hpp>
|
#include <qse/si/TcpServer.hpp>
|
||||||
#include <qse/si/mtx.h>
|
#include <qse/si/Mutex.hpp>
|
||||||
#include <qse/si/sio.h>
|
#include <qse/si/sio.h>
|
||||||
#include <qse/si/os.h>
|
#include <qse/si/os.h>
|
||||||
#include <qse/cmn/mem.h>
|
#include <qse/cmn/mem.h>
|
||||||
#include <qse/cmn/HeapMmgr.hpp>
|
#include <qse/cmn/HeapMmgr.hpp>
|
||||||
|
|
||||||
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
@ -14,34 +15,41 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
QSE::Mutex g_prt_mutex;
|
||||||
|
|
||||||
#if defined(QSE_LANG_CPP11)
|
#if defined(QSE_LANG_CPP11)
|
||||||
QSE::TcpServerL<int(QSE::Socket*,QSE::SocketAddress*)>* g_server;
|
QSE::TcpServerL<int(QSE::TcpServer::Worker*)>* g_server;
|
||||||
#else
|
#else
|
||||||
|
|
||||||
class ClientHandler
|
class ClientHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
int operator() (QSE::TcpServer* server, QSE::Socket* clisock, QSE::SocketAddress* cliaddr)
|
int operator() (QSE::TcpServer* server, QSE::TcpServer::Worker* worker)
|
||||||
{
|
{
|
||||||
qse_char_t addrbuf[128];
|
qse_char_t addrbuf[128];
|
||||||
qse_uint8_t bb[256];
|
qse_uint8_t bb[256];
|
||||||
qse_ssize_t n;
|
qse_ssize_t n;
|
||||||
|
|
||||||
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
worker->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
||||||
qse_printf (QSE_T("hello word..from %s\n"), 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())
|
while (!server->isStopRequested())
|
||||||
{
|
{
|
||||||
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
|
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);
|
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
|
||||||
|
g_prt_mutex.unlock();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
clisock->send (bb, n);
|
worker->socket.send (bb, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_printf (QSE_T("byte to %s\n"), addrbuf);
|
g_prt_mutex.lock();
|
||||||
|
qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, worker->getWid());
|
||||||
|
g_prt_mutex.unlock();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -49,33 +57,40 @@ public:
|
|||||||
static QSE::TcpServerF<ClientHandler>* g_server;
|
static QSE::TcpServerF<ClientHandler>* g_server;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int test1 (void)
|
static int test1 (void)
|
||||||
{
|
{
|
||||||
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
|
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
|
||||||
|
|
||||||
#if defined(QSE_LANG_CPP11)
|
#if defined(QSE_LANG_CPP11)
|
||||||
QSE::TcpServerL<int(QSE::Socket*,QSE::SocketAddress*)> server (
|
QSE::TcpServerL<int(QSE::TcpServer::Worker*)> server (
|
||||||
|
|
||||||
// workload by lambda
|
// workload by lambda
|
||||||
([&server](QSE::Socket* clisock, QSE::SocketAddress* cliaddr) {
|
([&server](QSE::TcpServer::Worker* worker) {
|
||||||
qse_char_t addrbuf[128];
|
qse_char_t addrbuf[128];
|
||||||
qse_uint8_t bb[256];
|
qse_uint8_t bb[256];
|
||||||
qse_ssize_t n;
|
qse_ssize_t n;
|
||||||
|
|
||||||
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
worker->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
||||||
qse_printf (QSE_T("hello word..from %s\n"), 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())
|
while (!server.isStopRequested())
|
||||||
{
|
{
|
||||||
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
|
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);
|
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
|
||||||
|
g_prt_mutex.unlock();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
clisock->send (bb, n);
|
worker->socket.send (bb, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_printf (QSE_T("byte to %s\n"), addrbuf);
|
g_prt_mutex.lock();
|
||||||
|
qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, worker->getWid());
|
||||||
|
g_prt_mutex.unlock();
|
||||||
return 0;
|
return 0;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@ -83,7 +98,7 @@ static int test1 (void)
|
|||||||
|
|
||||||
);
|
);
|
||||||
#else
|
#else
|
||||||
QSE::TcpServerF<ClientHandler> server (&heap_mmgr);
|
QSE::TcpServerF<ClientHandler> server /*(&heap_mmgr)*/;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
server.setThreadStackSize (256000);
|
server.setThreadStackSize (256000);
|
||||||
|
Loading…
Reference in New Issue
Block a user