Skip to content

Commit

Permalink
Fix service status assignment and checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dvsku committed May 30, 2024
1 parent 7201a48 commit 00da942
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
14 changes: 7 additions & 7 deletions include/libnetwrk/net/core/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ namespace libnetwrk {
Connect to service.
*/
bool connect(const std::string& host, const uint16_t port) {
if (m_context.status != service_status::stopped)
if (m_context.status != to_underlying(service_status::stopped))
return false;

m_context.status = service_status::starting;
m_context.status = to_underlying(service_status::starting);

bool connected = connect_impl(host, port);

if (connected) {
if (m_context.cb_connect)
m_context.cb_connect(m_comp_connection.connection);

m_context.status = service_status::started;
m_context.status = to_underlying(service_status::started);
}
else {
m_context.status = service_status::stopped;
m_context.status = to_underlying(service_status::stopped);
}

return connected;
Expand All @@ -79,18 +79,18 @@ namespace libnetwrk {
Disconnect the client and clean up.
*/
void disconnect() {
if (m_context.status != service_status::started)
if (m_context.status != to_underlying(service_status::started))
return;

m_context.status = service_status::stopping;
m_context.status = to_underlying(service_status::stopping);
this->teardown();

if (m_context.cb_disconnect)
m_context.cb_disconnect();

LIBNETWRK_INFO(m_context.name, "Disconnected.");

m_context.status = service_status::stopped;
m_context.status = to_underlying(service_status::stopped);
}

void send(message_t& message, libnetwrk::send_flags flags = libnetwrk::send_flags::none) {
Expand Down
14 changes: 7 additions & 7 deletions include/libnetwrk/net/core/service/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,35 @@ namespace libnetwrk {
}

bool start(const std::string& host, const uint16_t port) {
if (m_context.status != service_status::stopped)
if (m_context.status != to_underlying(service_status::stopped))
return false;

m_context.status = service_status::starting;
m_context.status = to_underlying(service_status::starting);

bool started = start_impl(host, port);

if (started) {
if (m_context.cb_start)
m_context.cb_start();

m_context.status = service_status::started;
m_context.status = to_underlying(service_status::started);
}
else {
m_context.status = service_status::stopped;
m_context.status = to_underlying(service_status::stopped);
}

return started;
}

void stop() {
if (m_context.status != service_status::started)
if (m_context.status != to_underlying(service_status::started))
return;

m_context.status = service_status::stopping;
m_context.status = to_underlying(service_status::stopping);

this->teardown();

m_context.status = service_status::stopped;
m_context.status = to_underlying(service_status::stopped);

if (m_context.cb_stop)
m_context.cb_stop();
Expand Down
4 changes: 2 additions & 2 deletions include/libnetwrk/net/core/shared/shared_comp_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ namespace libnetwrk {
}

void process_messages_loop() {
while (m_context.status == service_status::started) {
while (m_context.status == to_underlying(service_status::started)) {
bool wait = false;

{
Expand All @@ -231,7 +231,7 @@ namespace libnetwrk {
m_cv.wait(lock);
}

if (m_context.status != service_status::started)
if (m_context.status != to_underlying(service_status::started))
break;

invoke_processing_callbacks();
Expand Down
4 changes: 2 additions & 2 deletions include/libnetwrk/net/core/shared/shared_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace libnetwrk {

public:
std::string name = "";
std::atomic_uint8_t status = libnetwrk::service_status::stopped;
std::atomic_uint8_t status = to_underlying(libnetwrk::service_status::stopped);
io_context_t io_context;

cb_message_t cb_message;
Expand All @@ -50,7 +50,7 @@ namespace libnetwrk {

public:
bool is_running() const {
return status == service_status::started;
return status == to_underlying(service_status::started);
}

void start_io_context() {
Expand Down
2 changes: 1 addition & 1 deletion include/libnetwrk/net/tcp/tcp_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace libnetwrk::tcp {
: base_t(name) {}

virtual ~tcp_client() {
this->m_context.status = service_status::stopping;
this->m_context.status = to_underlying(service_status::stopping);
this->teardown();
};

Expand Down
2 changes: 1 addition & 1 deletion include/libnetwrk/net/tcp/tcp_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace libnetwrk::tcp {
: base_t(name), m_acceptor(this->m_context.io_context) {};

virtual ~tcp_service() {
this->m_context.status = service_status::stopping;
this->m_context.status = to_underlying(service_status::stopping);
this->teardown();
}

Expand Down

0 comments on commit 00da942

Please sign in to comment.