Skip to content

Commit 7bf2987

Browse files
committed
Fix the code to work
1 parent 41589e9 commit 7bf2987

25 files changed

+1703
-286
lines changed

moxt/cclient.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define MOXT_CCLIENT_HPP
33

44
#include "common.hpp"
5-
#include "moxt/httpx/clientpool.hpp"
65
#include "moxt/httpx/httpbase.hpp"
76
#include "moxt/httpx/httpclient.hpp"
87
#include <photon/common/identity-pool.h>

moxt/common.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,29 @@
2525
#undef min
2626
#endif
2727

28-
// #define USE_FMTLOG 1
29-
#define USE_SPDLOG 1
28+
#define USE_FMTLOG 1
29+
// #define USE_SPDLOG 1
3030

3131
#if defined(USE_FMTLOG)
32+
#define FMTLOG_ACTIVE_LEVEL FMTLOG_LEVEL_DBG
3233
#include <fmtlog/fmtlog.h>
3334
#elif defined(USE_SPDLOG)
3435

35-
#define SPDLOG_LEVEL_NAMES \
36-
{ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "" }
37-
38-
#include <spdlog/spdlog.h>
39-
#define logd spdlog::debug
40-
#define logi spdlog::info
41-
#define logw spdlog::warn
42-
#define loge spdlog::error
43-
#else
44-
#include <quill/Quill.h>
45-
#define logd(fmt, ...) QUILL_LOG_DEBUG(quill::get_logger(), fmt, ##__VA_ARGS__)
46-
#define logi(fmt, ...) QUILL_LOG_INFO(quill::get_logger(), fmt, ##__VA_ARGS__)
47-
#define logw(fmt, ...) \
48-
QUILL_LOG_WARNING(quill::get_logger(), fmt, ##__VA_ARGS__)
49-
#define loge(fmt, ...) QUILL_LOG_ERROR(quill::get_logger(), fmt, ##__VA_ARGS__)
36+
// #define SPDLOG_LEVEL_NAMES \
37+
// { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "" }
38+
39+
// #include <spdlog/spdlog.h>
40+
// #define logd spdlog::debug
41+
// #define logi spdlog::info
42+
// #define logw spdlog::warn
43+
// #define loge spdlog::error
44+
// #else
45+
// #include <quill/Quill.h>
46+
// #define logd(fmt, ...) QUILL_LOG_DEBUG(quill::get_logger(), fmt, ##__VA_ARGS__)
47+
// #define logi(fmt, ...) QUILL_LOG_INFO(quill::get_logger(), fmt, ##__VA_ARGS__)
48+
// #define logw(fmt, ...) \
49+
// QUILL_LOG_WARNING(quill::get_logger(), fmt, ##__VA_ARGS__)
50+
// #define loge(fmt, ...) QUILL_LOG_ERROR(quill::get_logger(), fmt, ##__VA_ARGS__)
5051
#endif
5152

5253
// #define logd spdlog::debug

moxt/httpx/clientpool.cpp

Lines changed: 0 additions & 144 deletions
This file was deleted.

moxt/httpx/clientpool.hpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

moxt/httpx/websocket.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "websocket.hpp"
22

3-
void on_connect_null(WebSocket *ws) {}
3+
void on_connected_null(WebSocket *ws) {}
4+
void on_BeforeReconnectNull(WebSocket *ws) {}
45
void on_heartbeat_null(WebSocket *ws) {}
56
void on_message_null(WebSocket *ws, const char *data, size_t len) {
67
logd("on_message_null: {}", std::string_view(data, len));
@@ -12,7 +13,8 @@ WebSocket::WebSocket(std::string host, std::string port, std::string path,
1213
_ioContext(ioContext), _resolver(asio::make_strand(_ioContext)),
1314
_sslContext(asio::ssl::context::tlsv12_client),
1415
_reconnectTimer(_ioContext), m_HeartbeatTimer(_ioContext),
15-
m_HeartbeatInterval(), on_connect_callback_(on_connect_null),
16+
m_HeartbeatInterval(), on_connected_callback_(on_connected_null),
17+
beforeReconnectCallback_(on_BeforeReconnectNull),
1618
on_heartbeat_callback_(on_heartbeat_null),
1719
on_message_callback_(on_message_null) {
1820
// logd("WebSocket::WebSocket");
@@ -49,8 +51,8 @@ void WebSocket::OnResolve(beast::error_code ec,
4951
logd("WebSocket::OnResolve");
5052

5153
if (ec) {
52-
loge("Can't resolve gateway URL '{}': {} ({})", host,
53-
ec.message(), ec.value());
54+
loge("Can't resolve gateway URL '{}': {} ({})", host, ec.message(),
55+
ec.value());
5456
Disconnect(true);
5557
return;
5658
}
@@ -129,10 +131,11 @@ void WebSocket::OnSslHandshake(beast::error_code ec) {
129131

130132
// set a decorator to change the User-Agent of the handshake
131133
_websocket->set_option(beast::websocket::stream_base::decorator(
132-
[](beast::websocket::request_type &req) {
133-
req.set(beast::http::field::user_agent,
134-
std::string(BOOST_BEAST_VERSION_STRING) +
135-
" xt-connector");
134+
[this](beast::websocket::request_type &req) {
135+
// req.set(beast::http::field::user_agent,
136+
// std::string(BOOST_BEAST_VERSION_STRING) +
137+
// " xt-connector");
138+
this->SetHeaders(req);
136139
}));
137140

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

160163
// 触发连接回调
161-
// if (on_connect_callback_ != nullptr) {
162-
on_connect_callback_(this);
163-
// }
164+
on_connected_callback_(this);
164165

165166
m_HeartbeatInterval = std::chrono::seconds(10);
166167
DoHeartbeat({});
167168
}
168169

170+
void WebSocket::SetHeaders(beast::websocket::request_type &req) {
171+
req.set(beast::http::field::user_agent,
172+
std::string(BOOST_BEAST_VERSION_STRING) + "echo");
173+
// 添加Headers
174+
for (const auto &header : currentHeaders_.headers) {
175+
printf("header: %s %s\n", header.first.c_str(), header.second.c_str());
176+
req.set(header.first, header.second);
177+
}
178+
}
179+
169180
void WebSocket::Disconnect(bool reconnect /*= false*/) {
170181
logd("WebSocket::Disconnect");
171182

@@ -215,6 +226,11 @@ void WebSocket::OnReconnect(beast::error_code ec) {
215226
}
216227

217228
++_reconnectCount;
229+
230+
// 在重连之前调用回调
231+
// printf("beforeReconnectCallback_ %p\n", this);
232+
beforeReconnectCallback_(this);
233+
218234
Connect();
219235
}
220236

moxt/httpx/websocket.hpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#include "moxt/common.hpp"
44
#include "moxt/httpx/httpbase.hpp"
5+
#include "moxt/std23/function_ref.h"
56
#include <functional>
6-
#include <std23/function_ref.h>
77

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

2222
class WebSocket;
2323

24-
typedef std23::function_ref<void(WebSocket *ws)> OnConnectCallback;
24+
struct WebSocketHeaders {
25+
std::map<std::string, std::string> headers;
26+
27+
void set(const std::string &key, const std::string &value) {
28+
headers[key] = value;
29+
}
30+
};
31+
32+
typedef std23::function_ref<void(WebSocket *ws)> OnConnectedCallback;
33+
typedef std23::function_ref<void(WebSocket *ws)> BeforeReconnectCallback;
2534
typedef std23::function_ref<void(WebSocket *ws)> OnHeartbeatCallback;
2635
typedef std23::function_ref<void(WebSocket *ws, const char *data, size_t len)>
2736
OnMessageCallback;
@@ -54,7 +63,9 @@ class WebSocket {
5463
asio::steady_timer m_HeartbeatTimer;
5564
std::chrono::steady_clock::duration m_HeartbeatInterval;
5665

57-
OnConnectCallback on_connect_callback_;
66+
WebSocketHeaders currentHeaders_; // 当前的 headers
67+
BeforeReconnectCallback beforeReconnectCallback_; // 重连前的回调
68+
OnConnectedCallback on_connected_callback_;
5869
OnMessageCallback on_message_callback_;
5970
OnHeartbeatCallback on_heartbeat_callback_;
6071

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

6981
void OnClose(beast::error_code ec);
7082
void OnReconnect(beast::error_code ec);
@@ -82,18 +94,28 @@ class WebSocket {
8294
void Connect();
8395
void Disconnect(bool reconnect = false);
8496
void Write(std::string const &data);
97+
98+
// 设置Headers
99+
void SetHeaders(const std::map<std::string, std::string> &headers) {
100+
currentHeaders_.headers = headers;
101+
}
102+
85103
// 设置连接回调函数
86-
void set_on_connect(OnConnectCallback callback) {
87-
on_connect_callback_ = callback;
104+
void SetOnConnected(OnConnectedCallback callback) {
105+
on_connected_callback_ = callback;
106+
}
107+
108+
void SetBeforeReconnectCallback(BeforeReconnectCallback callback) {
109+
beforeReconnectCallback_ = callback;
88110
}
89111

90112
// 设置心跳回调函数
91-
void set_on_heartbeat(OnHeartbeatCallback callback) {
113+
void SetOnHeartbeat(OnHeartbeatCallback callback) {
92114
on_heartbeat_callback_ = callback;
93115
}
94116

95117
// 设置消息回调函数
96-
void set_on_message(OnMessageCallback callback) {
118+
void SetOnMessage(OnMessageCallback callback) {
97119
on_message_callback_ = callback;
98120
}
99121
};

0 commit comments

Comments
 (0)