aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/salticidae/conn.h20
-rw-r--r--include/salticidae/netaddr.h1
-rw-r--r--include/salticidae/network.h10
3 files changed, 18 insertions, 13 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index 665e1d5..48902d4 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -260,7 +260,7 @@ class ConnPool {
/* related to workers */
size_t nworker;
salticidae::BoxObj<Worker[]> workers;
- bool worker_running;
+ int system_state;
void accept_client(int, int);
conn_t add_conn(const conn_t &conn);
@@ -344,7 +344,7 @@ class ConnPool {
queue_capacity(config._queue_capacity),
listen_fd(-1),
nworker(config._nworker),
- worker_running(false) {
+ system_state(0) {
workers = new Worker[nworker];
user_tcall = new ThreadCall(ec);
disp_ec = workers[0].get_ec();
@@ -381,16 +381,16 @@ class ConnPool {
ConnPool(ConnPool &&) = delete;
void start() {
- if (worker_running) return;
+ if (system_state) return;
SALTICIDAE_LOG_INFO("starting all threads...");
for (size_t i = 0; i < nworker; i++)
workers[i].start();
- worker_running = true;
+ system_state = 1;
}
void stop_workers() {
- if (!worker_running) return;
- worker_running = false;
+ if (system_state != 1) return;
+ system_state = 2;
SALTICIDAE_LOG_INFO("stopping all threads...");
/* stop the dispatcher */
workers[0].stop();
@@ -401,10 +401,6 @@ class ConnPool {
/* join all worker threads */
for (size_t i = 1; i < nworker; i++)
workers[i].get_handle().join();
- }
-
- void stop() {
- stop_workers();
for (auto it: pool)
{
conn_t conn = it.second;
@@ -412,6 +408,10 @@ class ConnPool {
conn->self_ref = nullptr;
::close(conn->fd);
}
+ }
+
+ void stop() {
+ stop_workers();
if (listen_fd != -1)
{
close(listen_fd);
diff --git a/include/salticidae/netaddr.h b/include/salticidae/netaddr.h
index bff4cb7..e4e8c6c 100644
--- a/include/salticidae/netaddr.h
+++ b/include/salticidae/netaddr.h
@@ -141,6 +141,7 @@ void netaddr_free(const netaddr_t *self);
netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port);
netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port);
netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr);
+netaddr_t *netaddr_copy(const netaddr_t *self);
bool netaddr_is_eq(const netaddr_t *a, const netaddr_t *b);
bool netaddr_is_null(const netaddr_t *self);
uint32_t netaddr_get_ip(const netaddr_t *self);
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index b269acc..6fef98a 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -144,7 +144,7 @@ class MsgNetwork: public ConnPool {
}
};
- virtual ~MsgNetwork() { stop_workers(); }
+ virtual ~MsgNetwork() { stop(); }
MsgNetwork(const EventContext &ec, const Config &config):
ConnPool(ec, config) {
@@ -203,6 +203,7 @@ class MsgNetwork: public ConnPool {
inline void _send_msg(Msg &&msg, const conn_t &conn);
inline void _send_msg_dispatcher(const Msg &msg, const conn_t &conn);
+ void stop() { stop_workers(); }
using ConnPool::listen;
conn_t connect(const NetAddr &addr) {
return static_pointer_cast<Conn>(ConnPool::connect(addr));
@@ -437,7 +438,7 @@ class PeerNetwork: public MsgNetwork<OpcodeType> {
this->reg_handler(generic_bind(&PeerNetwork::msg_pong, this, _1, _2));
}
- ~PeerNetwork() { this->stop_workers(); }
+ virtual ~PeerNetwork() { this->stop(); }
void add_peer(const NetAddr &paddr);
void del_peer(const NetAddr &paddr);
@@ -969,6 +970,7 @@ msgnetwork_conn_t *msgnetwork_conn_copy(const msgnetwork_conn_t *self);
void msgnetwork_conn_free(const msgnetwork_conn_t *self);
void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *err);
void msgnetwork_start(msgnetwork_t *self);
+void msgnetwork_stop(msgnetwork_t *self);
void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn);
typedef void (*msgnetwork_msg_callback_t)(const msg_t *, const msgnetwork_conn_t *, void *userdata);
@@ -983,7 +985,7 @@ void msgnetwork_reg_error_handler(msgnetwork_t *self, msgnetwork_error_callback_
msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn);
msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn);
-netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn);
+const netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn);
// PeerNetwork
@@ -1002,12 +1004,14 @@ void peernetwork_del_peer(peernetwork_t *self, const netaddr_t *paddr);
bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr);
const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self, const netaddr_t *paddr, SalticidaeCError *cerror);
msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self);
+peernetwork_t *msgnetwork_as_peernetwork_unsafe(msgnetwork_t *self);
msgnetwork_conn_t *msgnetwork_conn_new_from_peernetwork_conn(const peernetwork_conn_t *conn);
peernetwork_conn_t *peernetwork_conn_copy(const peernetwork_conn_t *self);
void peernetwork_conn_free(const peernetwork_conn_t *self);
void peernetwork_send_msg_by_move(peernetwork_t *self, msg_t * _moved_msg, const netaddr_t *paddr);
void peernetwork_multicast_msg_by_move(peernetwork_t *self, msg_t *_moved_msg, const netaddr_array_t *paddrs);
void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *err);
+void peernetwork_stop(peernetwork_t *self);
#ifdef __cplusplus
}