Skip to content

Commit

Permalink
Fix the code to work
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Oct 20, 2024
1 parent 41589e9 commit 7bf2987
Show file tree
Hide file tree
Showing 25 changed files with 1,703 additions and 286 deletions.
1 change: 0 additions & 1 deletion moxt/cclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define MOXT_CCLIENT_HPP

#include "common.hpp"
#include "moxt/httpx/clientpool.hpp"
#include "moxt/httpx/httpbase.hpp"
#include "moxt/httpx/httpclient.hpp"
#include <photon/common/identity-pool.h>
Expand Down
35 changes: 18 additions & 17 deletions moxt/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,29 @@
#undef min
#endif

// #define USE_FMTLOG 1
#define USE_SPDLOG 1
#define USE_FMTLOG 1
// #define USE_SPDLOG 1

#if defined(USE_FMTLOG)
#define FMTLOG_ACTIVE_LEVEL FMTLOG_LEVEL_DBG
#include <fmtlog/fmtlog.h>
#elif defined(USE_SPDLOG)

#define SPDLOG_LEVEL_NAMES \
{ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "" }

#include <spdlog/spdlog.h>
#define logd spdlog::debug
#define logi spdlog::info
#define logw spdlog::warn
#define loge spdlog::error
#else
#include <quill/Quill.h>
#define logd(fmt, ...) QUILL_LOG_DEBUG(quill::get_logger(), fmt, ##__VA_ARGS__)
#define logi(fmt, ...) QUILL_LOG_INFO(quill::get_logger(), fmt, ##__VA_ARGS__)
#define logw(fmt, ...) \
QUILL_LOG_WARNING(quill::get_logger(), fmt, ##__VA_ARGS__)
#define loge(fmt, ...) QUILL_LOG_ERROR(quill::get_logger(), fmt, ##__VA_ARGS__)
// #define SPDLOG_LEVEL_NAMES \
// { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "" }

// #include <spdlog/spdlog.h>
// #define logd spdlog::debug
// #define logi spdlog::info
// #define logw spdlog::warn
// #define loge spdlog::error
// #else
// #include <quill/Quill.h>
// #define logd(fmt, ...) QUILL_LOG_DEBUG(quill::get_logger(), fmt, ##__VA_ARGS__)
// #define logi(fmt, ...) QUILL_LOG_INFO(quill::get_logger(), fmt, ##__VA_ARGS__)
// #define logw(fmt, ...) \
// QUILL_LOG_WARNING(quill::get_logger(), fmt, ##__VA_ARGS__)
// #define loge(fmt, ...) QUILL_LOG_ERROR(quill::get_logger(), fmt, ##__VA_ARGS__)
#endif

// #define logd spdlog::debug
Expand Down
144 changes: 0 additions & 144 deletions moxt/httpx/clientpool.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions moxt/httpx/clientpool.hpp

This file was deleted.

38 changes: 27 additions & 11 deletions moxt/httpx/websocket.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "websocket.hpp"

void on_connect_null(WebSocket *ws) {}
void on_connected_null(WebSocket *ws) {}
void on_BeforeReconnectNull(WebSocket *ws) {}
void on_heartbeat_null(WebSocket *ws) {}
void on_message_null(WebSocket *ws, const char *data, size_t len) {
logd("on_message_null: {}", std::string_view(data, len));
Expand All @@ -12,7 +13,8 @@ WebSocket::WebSocket(std::string host, std::string port, std::string path,
_ioContext(ioContext), _resolver(asio::make_strand(_ioContext)),
_sslContext(asio::ssl::context::tlsv12_client),
_reconnectTimer(_ioContext), m_HeartbeatTimer(_ioContext),
m_HeartbeatInterval(), on_connect_callback_(on_connect_null),
m_HeartbeatInterval(), on_connected_callback_(on_connected_null),
beforeReconnectCallback_(on_BeforeReconnectNull),
on_heartbeat_callback_(on_heartbeat_null),
on_message_callback_(on_message_null) {
// logd("WebSocket::WebSocket");
Expand Down Expand Up @@ -49,8 +51,8 @@ void WebSocket::OnResolve(beast::error_code ec,
logd("WebSocket::OnResolve");

if (ec) {
loge("Can't resolve gateway URL '{}': {} ({})", host,
ec.message(), ec.value());
loge("Can't resolve gateway URL '{}': {} ({})", host, ec.message(),
ec.value());
Disconnect(true);
return;
}
Expand Down Expand Up @@ -129,10 +131,11 @@ void WebSocket::OnSslHandshake(beast::error_code ec) {

// set a decorator to change the User-Agent of the handshake
_websocket->set_option(beast::websocket::stream_base::decorator(
[](beast::websocket::request_type &req) {
req.set(beast::http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" xt-connector");
[this](beast::websocket::request_type &req) {
// req.set(beast::http::field::user_agent,
// std::string(BOOST_BEAST_VERSION_STRING) +
// " xt-connector");
this->SetHeaders(req);
}));

_websocket->async_handshake(
Expand All @@ -158,14 +161,22 @@ void WebSocket::OnHandshake(beast::error_code ec) {
Read();

// 触发连接回调
// if (on_connect_callback_ != nullptr) {
on_connect_callback_(this);
// }
on_connected_callback_(this);

m_HeartbeatInterval = std::chrono::seconds(10);
DoHeartbeat({});
}

void WebSocket::SetHeaders(beast::websocket::request_type &req) {
req.set(beast::http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) + "echo");
// 添加Headers
for (const auto &header : currentHeaders_.headers) {
printf("header: %s %s\n", header.first.c_str(), header.second.c_str());
req.set(header.first, header.second);
}
}

void WebSocket::Disconnect(bool reconnect /*= false*/) {
logd("WebSocket::Disconnect");

Expand Down Expand Up @@ -215,6 +226,11 @@ void WebSocket::OnReconnect(beast::error_code ec) {
}

++_reconnectCount;

// 在重连之前调用回调
// printf("beforeReconnectCallback_ %p\n", this);
beforeReconnectCallback_(this);

Connect();
}

Expand Down
36 changes: 29 additions & 7 deletions moxt/httpx/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "moxt/common.hpp"
#include "moxt/httpx/httpbase.hpp"
#include "moxt/std23/function_ref.h"
#include <functional>
#include <std23/function_ref.h>

// 参考以下代码实现:
namespace asio = boost::asio;
Expand All @@ -21,7 +21,16 @@ class WebSocket;

class WebSocket;

typedef std23::function_ref<void(WebSocket *ws)> OnConnectCallback;
struct WebSocketHeaders {
std::map<std::string, std::string> headers;

void set(const std::string &key, const std::string &value) {
headers[key] = value;
}
};

typedef std23::function_ref<void(WebSocket *ws)> OnConnectedCallback;
typedef std23::function_ref<void(WebSocket *ws)> BeforeReconnectCallback;
typedef std23::function_ref<void(WebSocket *ws)> OnHeartbeatCallback;
typedef std23::function_ref<void(WebSocket *ws, const char *data, size_t len)>
OnMessageCallback;
Expand Down Expand Up @@ -54,7 +63,9 @@ class WebSocket {
asio::steady_timer m_HeartbeatTimer;
std::chrono::steady_clock::duration m_HeartbeatInterval;

OnConnectCallback on_connect_callback_;
WebSocketHeaders currentHeaders_; // 当前的 headers
BeforeReconnectCallback beforeReconnectCallback_; // 重连前的回调
OnConnectedCallback on_connected_callback_;
OnMessageCallback on_message_callback_;
OnHeartbeatCallback on_heartbeat_callback_;

Expand All @@ -65,6 +76,7 @@ class WebSocket {
asio::ip::tcp::resolver::results_type::endpoint_type ep);
void OnSslHandshake(beast::error_code ec);
void OnHandshake(beast::error_code ec);
void SetHeaders(beast::websocket::request_type &req);

void OnClose(beast::error_code ec);
void OnReconnect(beast::error_code ec);
Expand All @@ -82,18 +94,28 @@ class WebSocket {
void Connect();
void Disconnect(bool reconnect = false);
void Write(std::string const &data);

// 设置Headers
void SetHeaders(const std::map<std::string, std::string> &headers) {
currentHeaders_.headers = headers;
}

// 设置连接回调函数
void set_on_connect(OnConnectCallback callback) {
on_connect_callback_ = callback;
void SetOnConnected(OnConnectedCallback callback) {
on_connected_callback_ = callback;
}

void SetBeforeReconnectCallback(BeforeReconnectCallback callback) {
beforeReconnectCallback_ = callback;
}

// 设置心跳回调函数
void set_on_heartbeat(OnHeartbeatCallback callback) {
void SetOnHeartbeat(OnHeartbeatCallback callback) {
on_heartbeat_callback_ = callback;
}

// 设置消息回调函数
void set_on_message(OnMessageCallback callback) {
void SetOnMessage(OnMessageCallback callback) {
on_message_callback_ = callback;
}
};
Loading

0 comments on commit 7bf2987

Please sign in to comment.