Skip to content

Commit

Permalink
Upgrade the minimum C++ version from 14 to 17 + Modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChenX committed Dec 30, 2024
1 parent 7be1d12 commit 567f76d
Show file tree
Hide file tree
Showing 57 changed files with 1,144 additions and 1,390 deletions.
37 changes: 25 additions & 12 deletions turms-client-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ project(
LANGUAGES CXX
)

message(STATUS "TODO: Not implemented")

# ---- Options ----

#option(TURMS_CLIENT_BUILD_TESTS "Build Turms client tests" OFF)
#option(TURMS_CLIENT_BUILD_EXAMPLES "Build examples" OFF)

# ---- Top-level CMake config ----

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)

add_compile_definitions(
BOOST_THREAD_PROVIDES_FUTURE
BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
BOOST_THREAD_PROVIDES_FUTURE
BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
)

# ---- Dependencies ----

find_package(absl REQUIRED)
find_package(Protobuf REQUIRED)
find_package(Boost REQUIRED)
# Prefer config mode as config files are typically
# installed as part of the package, and the config mode is more reliable.
find_package(absl CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
find_package(Boost CONFIG REQUIRED COMPONENTS asio chrono core thread)

get_target_property(absl_INCLUDE_DIRS absl::base INTERFACE_INCLUDE_DIRECTORIES)

# ---- Source files ----

Expand All @@ -50,11 +52,22 @@ add_library(
${headers}
${sources}
)

if (NOT absl_INCLUDE_DIRS)
message(FATAL_ERROR "The variable absl_INCLUDE_DIRS is not set")
endif ()
if (NOT Protobuf_INCLUDE_DIRS)
message(FATAL_ERROR "The variable Protobuf_INCLUDE_DIRS is not set")
endif ()
if (NOT Boost_INCLUDE_DIRS)
message(FATAL_ERROR "The variable Boost_INCLUDE_DIRS is not set")
endif ()

target_include_directories(
${PROJECT_NAME}
PUBLIC
include
${absl_INCLUDE_DIR}
${Protobuf_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${absl_INCLUDE_DIRS}
${Protobuf_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@
#define TURMS_CLIENT_DRIVER_SERVICE_BASE_SERVICE_H

#include <boost/core/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/thread/future.hpp>
#include <optional>

#include "../state_store.h"

namespace turms {
namespace client {
namespace driver {
namespace service {
namespace turms::client::driver::service {

class BaseService : private boost::noncopyable {
public:
explicit BaseService(boost::asio::io_context& ioContext, StateStore& stateStore);
virtual ~BaseService() = default;

virtual auto close() -> boost::future<void> = 0;
virtual auto onDisconnected(const boost::optional<std::exception>& exception) -> void = 0;
virtual auto onDisconnected(const std::optional<std::exception>& exception) -> void = 0;

protected:
boost::asio::io_context& ioContext_;
StateStore& stateStore_;
};

} // namespace service
} // namespace driver
} // namespace client
} // namespace turms
} // namespace turms::client::driver::service

#endif // TURMS_CLIENT_DRIVER_SERVICE_BASE_SERVICE_H
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@

#include "base_service.h"
#include "turms/client/exception/response_exception.h"
#include "turms/client/model/response_status_code.h"
#include "turms/client/transport/tcp_client.h"

namespace turms {
namespace client {
namespace driver {
namespace service {
namespace turms::client::driver::service {

class ConnectionService : public BaseService,
private std::enable_shared_from_this<ConnectionService> {
public:
using MessageHandler = std::function<void(const std::vector<uint8_t>&)>;
using OnConnectedHandler = std::function<void()>;
using OnDisconnectedHandler = std::function<void(const boost::optional<std::exception>&)>;
using OnDisconnectedHandler = std::function<void(const std::optional<std::exception>&)>;

ConnectionService(boost::asio::io_context& ioContext,
StateStore& stateStore,
const boost::optional<std::string>& host,
const boost::optional<int>& port,
const boost::optional<int>& connectTimeoutMillis);
const std::optional<std::string>& host,
const std::optional<int>& port,
const std::optional<int>& connectTimeoutMillis);

auto resetStates() -> void;

Expand All @@ -43,32 +39,32 @@ class ConnectionService : public BaseService,

auto notifyOnConnectedListeners() -> void;

auto notifyOnDisconnectedListeners(const boost::optional<std::exception>& e) -> void;
auto notifyOnDisconnectedListeners(const std::optional<std::exception>& e) -> void;

auto notifyMessageListeners(const std::vector<uint8_t>& message) -> void;

auto completeDisconnectPromises(const boost::optional<std::exception>& e = boost::none) -> void;
auto completeDisconnectPromises(const std::optional<std::exception>& e = std::nullopt) -> void;

auto connect(const boost::optional<std::string>& host = boost::none,
const boost::optional<int>& port = boost::none,
const boost::optional<int>& connectTimeoutMillis = boost::none)
auto connect(const std::optional<std::string>& host = std::nullopt,
const std::optional<int>& port = std::nullopt,
const std::optional<int>& connectTimeoutMillis = std::nullopt)
-> boost::future<void>;

auto disconnect() -> boost::future<void>;
auto disconnect() const -> boost::future<void>;

auto onSocketOpened() -> void;

auto onSocketClosed(const boost::optional<std::exception>& e) -> void;
auto onSocketClosed(const std::optional<std::exception>& e) -> void;

auto close() -> boost::future<void> override;

auto onDisconnected(const boost::optional<std::exception>& exception) -> void override;
auto onDisconnected(const std::optional<std::exception>& exception) -> void override;

private:
std::string initialHost_;
int initialPort_;
int initialConnectTimeout_;
std::vector<boost::promise<boost::optional<std::exception>>> disconnectPromises_;
std::vector<boost::promise<std::optional<std::exception>>> disconnectPromises_;
std::vector<OnConnectedHandler> onConnectedListeners_;
std::vector<OnDisconnectedHandler> onDisconnectedListeners_;
std::vector<MessageHandler> messageListeners_;
Expand All @@ -85,9 +81,6 @@ class ConnectionService : public BaseService,
auto clear() -> void;
};

} // namespace service
} // namespace driver
} // namespace client
} // namespace turms
} // namespace turms::client::driver::service

#endif // TURMS_CLIENT_DRIVER_SERVICE_CONNECTION_SERVICE_H
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@
#define TURMS_CLIENT_DRIVER_SERVICE_HEARTBEAT_SERVICE_H

#include <boost/asio.hpp>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <stdexcept>

#include "base_service.h"
#include "turms/client/driver/state_store.h"
#include "turms/client/model/proto/notification/turms_notification.pb.h"
#include "turms/client/time/time_util.h"

namespace turms {
namespace client {
namespace driver {
namespace service {
namespace turms::client::driver::service {
class HeartbeatService : public BaseService,
private std::enable_shared_from_this<HeartbeatService> {
public:
HeartbeatService(boost::asio::io_context& ioContext,
StateStore& stateStore,
const boost::optional<int>& heartbeatIntervalMillis = boost::none);
const std::optional<int>& heartbeatIntervalMillis = std::nullopt);

auto isRunning() const -> bool;

auto start() -> void;

auto stop(const boost::optional<std::exception>& exception = boost::none) -> void;
auto stop(const std::optional<std::exception>& exception = std::nullopt) -> void;

auto send() -> boost::future<void>;

Expand All @@ -37,24 +30,21 @@ class HeartbeatService : public BaseService,

auto close() -> boost::future<void> override;

auto onDisconnected(const boost::optional<std::exception>& exception) -> void override;
auto onDisconnected(const std::optional<std::exception>& exception) -> void override;

private:
int heartbeatInterval_{0};
std::chrono::duration<int, std::milli> heartbeatTimerInterval_;
int64_t lastHeartbeatRequestDate_{0};
boost::optional<boost::asio::steady_timer> heartbeatTimer_;
std::optional<boost::asio::steady_timer> heartbeatTimer_;
std::vector<boost::promise<void>> heartbeatPromises_;

static const int64_t kHeartbeatFailureRequestId = -100LL;
static constexpr int64_t kHeartbeatFailureRequestId = -100LL;
static constexpr std::array<uint8_t, 1> kHeartbeat{0};

auto sendHeartbeatForever() -> void;
auto rejectHeartbeatRequests(const boost::optional<std::exception>& exception) -> void;
auto rejectHeartbeatRequests(const std::optional<std::exception>& exception) -> void;
};
} // namespace service
} // namespace driver
} // namespace client
} // namespace turms
} // namespace turms::client::driver::service

#endif // TURMS_CLIENT_DRIVER_SERVICE_HEARTBEAT_SERVICE_H
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@
#include "turms/client/driver/service/base_service.h"
#include "turms/client/driver/state_store.h"
#include "turms/client/exception/response_exception.h"
#include "turms/client/model/notification_util.h"
#include "turms/client/model/proto/notification/turms_notification.pb.h"
#include "turms/client/model/response_status_code.h"
#include "turms/client/random/random_util.h"

namespace turms {
namespace client {
namespace driver {
namespace service {
namespace turms::client::driver::service {

class ProtocolMessageService : public BaseService,
private std::enable_shared_from_this<ProtocolMessageService> {
Expand All @@ -29,8 +23,8 @@ class ProtocolMessageService : public BaseService,

ProtocolMessageService(boost::asio::io_context& ioContext,
StateStore& stateStore,
const boost::optional<int>& requestTimeout,
const boost::optional<int>& minRequestInterval);
const std::optional<int>& requestTimeout,
const std::optional<int>& minRequestInterval);

// Listeners

Expand All @@ -47,12 +41,12 @@ class ProtocolMessageService : public BaseService,

auto close() -> boost::future<void> override;

auto onDisconnected(const boost::optional<std::exception>& exception) -> void override;
auto onDisconnected(const std::optional<std::exception>& exception) -> void override;

private:
struct TurmsRequestContext {
boost::promise<TurmsNotification> promise;
boost::optional<boost::asio::steady_timer> timeoutTimer;
std::optional<boost::asio::steady_timer> timeoutTimer;
};

std::map<int64_t, TurmsRequestContext> idToRequest_{};
Expand All @@ -70,9 +64,6 @@ class ProtocolMessageService : public BaseService,
std::vector<std::function<void(const TurmsNotification&)>> notificationListeners_;
};

} // namespace service
} // namespace driver
} // namespace client
} // namespace turms
} // namespace turms::client::driver::service

#endif // TURMS_CLIENT_DRIVER_SERVICE_PROTOCOL_MESSAGE_SERVICE_H
14 changes: 5 additions & 9 deletions turms-client-cpp/include/turms/client/driver/state_store.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#ifndef TURMS_CLIENT_DRIVER_STATE_STORE_H
#define TURMS_CLIENT_DRIVER_STATE_STORE_H

#include <boost/optional.hpp>
#include <optional>
#include <string>

#include "turms/client/transport/tcp_client.h"

namespace turms {
namespace client {
namespace driver {
namespace turms::client::driver {

struct StateStore {
std::unique_ptr<transport::TcpClient> tcp;
bool isConnected;
bool isSessionOpen;
boost::optional<std::string> sessionId;
boost::optional<std::string> serverId;
std::optional<std::string> sessionId;
std::optional<std::string> serverId;
int64_t lastRequestDate;

auto reset() -> void;
};

} // namespace driver
} // namespace client
} // namespace turms
} // namespace turms::client::driver

#endif // TURMS_CLIENT_DRIVER_STATE_STORE_H
Loading

0 comments on commit 567f76d

Please sign in to comment.