Skip to content

Commit

Permalink
Update websocket server
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed Jun 13, 2024
1 parent 501420a commit b1740bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/socks5_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class Socks5Server {
} // namespace socks5

auto main() -> int {
signal(SIGPIPE, SIG_IGN);
signal(SIGPIPE, SIG_IGN); // uv_signal_start(signal, callback, signum)
socks5::Socks5Server proxy{"0.0.0.0", PORT};
proxy.run();
}
4 changes: 3 additions & 1 deletion uvio/codec/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class WebsocketCodec : public Codec<WebsocketCodec> {
auto decode(std::vector<char> &payload, Reader &reader)
-> Task<Result<void>> {
// https://github.com/python-websockets/websockets/blob/12.0/src/websockets/legacy/framing.py
std::array<char, 2> buf;
std::array<char, 2> buf{};
std::array<char, 4> mask_bits{};
std::array<unsigned char, 8> buf8{};
co_await reader.read_exact(buf);
bool fin = buf[0] & 0b100000000;
auto opcode = buf[0] & 0b00001111;

// TODO(x)
ASSERT(fin);
ASSERT(opcode == (int) Opcode::TEXT);

Expand Down
18 changes: 18 additions & 0 deletions uvio/net/websocket/websocket_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class WebsocketFramed {
co_return co_await websocket_codec_.Encode(frame, buffered_writer_);
}

// [[REMEMBER_CO_AWAIT]]
// auto send_ping(std::span<char> message = {}) -> Task<Result<void>> {
// WebsocketFrame frame{
// .opcode = Opcode::PING,
// .message = message,
// };
// co_return co_await websocket_codec_.Encode(frame, buffered_writer_);
// }
//
// [[REMEMBER_CO_AWAIT]]
// auto send_pong(std::span<char> message = {}) -> Task<Result<void>> {
// WebsocketFrame frame{
// .opcode = Opcode::PONG,
// .message = message,
// };
// co_return co_await websocket_codec_.Encode(frame, buffered_writer_);
// }

[[REMEMBER_CO_AWAIT]]
auto close() -> Task<Result<void>> {
WebsocketFrame frame{
Expand Down
4 changes: 4 additions & 0 deletions uvio/net/websocket/websocket_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class WebsocketServer {
co_await websocket_framed.send_response(resp);

if (websocket_handler_) {
// TODO(x): 创建两个协程 (参照:
// https://github.com/python-websockets/websockets/blob/12.0/src/websockets/legacy/protocol.py)
// data_transfer(): 处理数据传输
// keepalive_ping(): 处理ping/pong帧
co_await websocket_handler_(websocket_framed);
}
co_return;
Expand Down

0 comments on commit b1740bb

Please sign in to comment.