├── .gitignore ├── LICENSE ├── README.md ├── example ├── build.sh ├── client.cc └── server.cc └── websocket.h /.gitignore: -------------------------------------------------------------------------------- 1 | server 2 | client 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Meng Rao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # websocket 2 | A single header c++ websocket client/server lib for linux that implements [rfc6455](https://tools.ietf.org/html/rfc6455). 3 | 4 | ## Server class `WSServer` 5 | As this lib uses template callback functions, user need to define 3 event handlers in his own class: `onWSConnect()`, `onWSClose()` and `onWSMsg()`: 6 | ```c++ 7 | // called when a new websocket connection is about to open 8 | // optional: origin, protocol, extensions will be nullptr if not exist in the request headers 9 | // optional: fill resp_protocol[resp_protocol_size] to add protocol to response headers 10 | // optional: fill resp_extensions[resp_extensions_size] to add extensions to response headers 11 | // return true if accept this new connection 12 | bool onWSConnect(WSConnection& conn, const char* request_uri, const char* host, const char* origin, const char* protocol, 13 | const char* extensions, char* resp_protocol, uint32_t resp_protocol_size, char* resp_extensions, 14 | uint32_t resp_extensions_size) { 15 | ... 16 | } 17 | 18 | // called when a websocket connection is closed 19 | // status_code 1005 means no status code in the close msg 20 | // status_code 1006 means not a clean close(tcp connection closed without a close msg) 21 | void onWSClose(WSConnection& conn, uint16_t status_code, const char* reason) { 22 | ... 23 | } 24 | 25 | // onWSMsg is used if RecvSegment == false(by default), called when a whole msg is received 26 | void onWSMsg(WSConnection& conn, uint8_t opcode, const uint8_t* payload, uint32_t pl_len) { 27 | ... 28 | } 29 | ``` 30 | 31 | To get the server running, user calls `init()` once to start the server and `poll()` repetitively to trigger events defined above: 32 | ```c++ 33 | // newconn_timeout: new tcp connection max inactive time in milliseconds, 0 means no limit 34 | // openconn_timeout: open ws connection max inactive time in milliseconds, 0 means no limit 35 | // if failed, call getLastError() for the reason 36 | bool init(const char* server_ip, uint16_t server_port, uint64_t newconn_timeout = 0, uint64_t openconn_timeout = 0); 37 | 38 | // non-blocking 39 | void poll(EventHandler* handler); 40 | ``` 41 | 42 | ## Connection class `WSConnection` 43 | User can't create `WSConnection` objects himself, but only get `WSConnection` references from event handler parameters. However user is allowed to save the reference as long as the connection is not closed. `WSConnection` exposes below functions to use: 44 | ```c++ 45 | // get remote network address 46 | bool getPeername(struct sockaddr_in& addr); 47 | 48 | // if not closed 49 | bool isConnected(); 50 | 51 | // send a msg or a segment 52 | // if sending a msg of multiple segments, only set fin to true for the last one 53 | void send(uint8_t opcode, const uint8_t* payload, uint32_t pl_len, bool fin = true); 54 | 55 | // clean close the connection with optional status_code and reason 56 | // status_code 1005 means don't include status_code in close msg 57 | void close(uint16_t status_code = 1005, const char* reason = ""); 58 | ``` 59 | It also allows to attach user-defined data structure to a `WSConnection` for user to operate on: 60 | ```c++ 61 | ConnUserData user_data; 62 | ``` 63 | 64 | ## Client class `WSClient` 65 | `WSClient` is actually a subclass of `WSConnection` with one additional connect function `wsConnect` which is blocking: 66 | ```c++ 67 | // timeout: connect timeout in milliseconds, 0 means no limit 68 | // if failed, call getLastError() for the reason 69 | bool wsConnect(uint64_t timeout, const char* server_ip, uint16_t server_port, const char* request_uri, 70 | const char* host, const char* origin = nullptr, const char* protocol = nullptr, 71 | const char* extensions = nullptr, char* resp_protocol = nullptr, uint32_t resp_protocol_size = 0, 72 | char* resp_extensions = nullptr, uint32_t resp_extensions_size = 0) 73 | ``` 74 | And similar to `WSServer`, user need to define event handler function `onWSClose()` and `onWSMsg()`(but not `onWSConnect()`), and call the non-blocking `poll()` to trigger events. 75 | 76 | ## Configurations and Limitations 77 | Most of the server and client configurations are defined as class template parameters as below: 78 | ```c++ 79 | // EventHandler: user defined type defining the required event handler functions 80 | // ConnUserData: user defined type attached to a WSConnection as member name `user_data` 81 | // RecvSegment: switch to use segment handler function `onWSSegment` instead of `onWSMsg` 82 | // RecvBufSize: msg/segment receive buffer size, too long msgs will cause connection being closed with status code 1009 83 | // MaxConns: for WSServer only, max number of active connections 84 | template 85 | ``` 86 | Note that if `RecvSegment` is set to true, `onWSSegment()` will be called in replace of `onWSMsg()` with 2 additional parameters: 87 | ```c++ 88 | // pl_start_idx: index in the whole msg for the 1st byte of payload 89 | // fin: whether it's the last segment 90 | void onWSSegment(WSConnection& conn, uint8_t opcode, const uint8_t* payload, uint32_t pl_len, uint32_t pl_start_idx, bool fin); 91 | ``` 92 | And if the c++ standard is older than c++17, user need to define both event handlers even if only one will be called(c++17 introduces `if constexpr` which allows only one function to be defined). 93 | 94 | One implementation issue for client: client is not fully conformant to rfc6455 in that handshaking key and msg masking key are of constant values instead of random ones, for the purpose of efficiency and simplicity. 95 | 96 | One performance consideration for server: as the library is using a simple busy-polling model instead of things like `epoll`, the number of active connections should be limited, as the default value of `MaxConns` implies. 97 | 98 | Thread safety: this lib is not thread safe. 99 | 100 | TLS support: this lib does not support TLS. 101 | 102 | ## Examples 103 | Example codes implement a [admincmd](https://github.com/MengRao/admincmd) client and server on top of websocket layer, allowing the author to add a web client and reusing all of the existing admin commands provided by the c++ server. 104 | -------------------------------------------------------------------------------- /example/build.sh: -------------------------------------------------------------------------------- 1 | g++ -O3 -std=c++11 server.cc -o server -pthread 2 | g++ -O3 -std=c++11 client.cc -o client -pthread 3 | -------------------------------------------------------------------------------- /example/client.cc: -------------------------------------------------------------------------------- 1 | #include "../websocket.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | class Client 9 | { 10 | public: 11 | using WSClient = websocket::WSClient; 12 | using WSConn = WSClient::Connection; 13 | 14 | void run() { 15 | if (!wsclient.wsConnect(3000, "127.0.0.1", 1234, "/", "127.0.0.1:1234")) { 16 | std::cout << "wsclient connect failed: " << wsclient.getLastError() << std::endl; 17 | return; 18 | } 19 | running = true; 20 | ws_thr = std::thread([this]() { 21 | while (running.load(std::memory_order_relaxed) && wsclient.isConnected()) { 22 | { 23 | std::lock_guard lck(mtx); 24 | wsclient.poll(this); 25 | } 26 | std::this_thread::yield(); 27 | } 28 | }); 29 | 30 | std::cout << "Client running..." << std::endl; 31 | std::string line; 32 | while (running.load(std::memory_order_relaxed) && wsclient.isConnected() && std::getline(std::cin, line)) { 33 | std::lock_guard lck(mtx); 34 | wsclient.send(websocket::OPCODE_TEXT, (const uint8_t*)line.data(), line.size()); 35 | } 36 | stop(); 37 | 38 | ws_thr.join(); 39 | std::cout << "Client stopped..." << std::endl; 40 | } 41 | 42 | void stop() { running = false; } 43 | 44 | void onWSClose(WSConn& conn, uint16_t status_code, const char* reason) { 45 | std::cout << "ws close, status_code: " << status_code << ", reason: " << reason << std::endl; 46 | } 47 | 48 | void onWSMsg(WSConn& conn, uint8_t opcode, const uint8_t* payload, uint32_t pl_len) { 49 | if (opcode == websocket::OPCODE_PING) { 50 | conn.send(websocket::OPCODE_PONG, payload, pl_len); 51 | return; 52 | } 53 | if (opcode != websocket::OPCODE_TEXT) { 54 | std::cout << "got none text msg, opcode: " << (int)opcode << std::endl; 55 | return; 56 | } 57 | std::cout.write((const char*)payload, pl_len); 58 | std::cout << std::endl; 59 | } 60 | 61 | // no need to define onWSSegment if using c++17 62 | void onWSSegment(WSConn& conn, uint8_t opcode, const uint8_t* payload, uint32_t pl_len, uint32_t pl_start_idx, 63 | bool fin) { 64 | std::cout << "error: onWSSegment should not be called" << std::endl; 65 | } 66 | 67 | private: 68 | WSClient wsclient; 69 | std::mutex mtx; 70 | std::thread ws_thr; 71 | std::string admincmd_help; 72 | std::atomic running; 73 | }; 74 | 75 | Client client; 76 | 77 | void my_handler(int s) { 78 | client.stop(); 79 | } 80 | 81 | int main(int argc, char** argv) { 82 | struct sigaction sigIntHandler; 83 | 84 | sigIntHandler.sa_handler = my_handler; 85 | sigemptyset(&sigIntHandler.sa_mask); 86 | sigIntHandler.sa_flags = 0; 87 | 88 | sigaction(SIGINT, &sigIntHandler, NULL); 89 | 90 | client.run(); 91 | } 92 | 93 | -------------------------------------------------------------------------------- /example/server.cc: -------------------------------------------------------------------------------- 1 | #include "../websocket.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | class Server 8 | { 9 | public: 10 | struct CMDConnData 11 | { 12 | bool login; 13 | }; 14 | using WSServer = websocket::WSServer; 15 | using WSConn = WSServer::Connection; 16 | 17 | void run() { 18 | if (!wsserver.init("0.0.0.0", 1234)) { 19 | std::cout << "wsserver init failed: " << wsserver.getLastError() << std::endl; 20 | return; 21 | } 22 | admincmd_help = "Server help:\n" 23 | "login password\n" 24 | "echo str\n" 25 | "stop\n"; 26 | 27 | running = true; 28 | ws_thr = std::thread([this]() { 29 | while (running.load(std::memory_order_relaxed)) { 30 | wsserver.poll(this); 31 | std::this_thread::yield(); 32 | } 33 | }); 34 | 35 | std::cout << "Server running..." << std::endl; 36 | ws_thr.join(); 37 | std::cout << "Server stopped..." << std::endl; 38 | } 39 | 40 | void stop() { running = false; } 41 | 42 | // called when a new websocket connection is about to open 43 | // optional: origin, protocol, extensions will be nullptr if not exist in the request headers 44 | // optional: fill resp_protocol[resp_protocol_size] to add protocol to response headers 45 | // optional: fill resp_extensions[resp_extensions_size] to add extensions to response headers 46 | // return true if accept this new connection 47 | bool onWSConnect(WSConn& conn, const char* request_uri, const char* host, const char* origin, const char* protocol, 48 | const char* extensions, char* resp_protocol, uint32_t resp_protocol_size, char* resp_extensions, 49 | uint32_t resp_extensions_size) { 50 | struct sockaddr_in addr; 51 | conn.getPeername(addr); 52 | std::cout << "ws connection from: " << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << std::endl; 53 | std::cout << "request_uri: " << request_uri << std::endl; 54 | std::cout << "host: " << host << std::endl; 55 | if (origin) { 56 | std::cout << "origin: " << origin << std::endl; 57 | } 58 | if (protocol) { 59 | std::cout << "protocol: " << protocol << std::endl; 60 | } 61 | if (extensions) { 62 | std::cout << "extensions: " << extensions << std::endl; 63 | } 64 | return true; 65 | } 66 | 67 | // called when a websocket connection is closed 68 | // status_code 1005 means no status code in the close msg 69 | // status_code 1006 means not a clean close(tcp connection closed without a close msg) 70 | void onWSClose(WSConn& conn, uint16_t status_code, const char* reason) { 71 | std::cout << "ws close, status_code: " << status_code << ", reason: " << reason << std::endl; 72 | } 73 | 74 | // onWSMsg is used if RecvSegment == false(by default), called when a whole msg is received 75 | void onWSMsg(WSConn& conn, uint8_t opcode, const uint8_t* payload, uint32_t pl_len) { 76 | if (opcode == websocket::OPCODE_PING) { 77 | conn.send(websocket::OPCODE_PONG, payload, pl_len); 78 | return; 79 | } 80 | if (opcode != websocket::OPCODE_TEXT) { 81 | conn.close(1003, "not text msg"); 82 | return; 83 | } 84 | const char* data = (const char*)payload; 85 | const char* data_end = data + pl_len; 86 | char buf[4096] = {0}; 87 | const char* argv[4096]; 88 | char* out = buf + 1; 89 | int argc = 0; 90 | bool in_quote = false; 91 | bool single_quote = false; 92 | while (data < data_end) { 93 | char ch = *data++; 94 | if (!in_quote) { 95 | if (ch == ' ') *out++ = 0; 96 | else { 97 | if (*(out - 1) == 0) argv[argc++] = out; 98 | if (ch == '\'') 99 | in_quote = single_quote = true; 100 | else if (ch == '"') 101 | in_quote = true; 102 | else if (ch == '\\') 103 | *out++ = *data++; 104 | else 105 | *out++ = ch; 106 | } 107 | } 108 | else { 109 | if (single_quote) { 110 | if (ch == '\'') 111 | in_quote = single_quote = false; 112 | else 113 | *out++ = ch; 114 | } 115 | else { 116 | if (ch == '"') 117 | in_quote = false; 118 | else if (ch == '\\' && (*data == '\\' || *data == '"')) 119 | *out++ = *data++; 120 | else 121 | *out++ = ch; 122 | } 123 | } 124 | } 125 | if (argc) { 126 | *out = 0; 127 | std::string resp = onCMD(conn.user_data, argc, argv); 128 | if (resp.size()) conn.send(websocket::OPCODE_TEXT, (const uint8_t*)resp.data(), resp.size()); 129 | } 130 | } 131 | 132 | // onWSSegment is used if RecvSegment == true, called when a segment is received 133 | // pl_start_idx: index in the whole msg for the 1st byte of payload 134 | // fin: whether it's the last segment 135 | void onWSSegment(WSConn& conn, uint8_t opcode, const uint8_t* payload, uint32_t pl_len, uint32_t pl_start_idx, 136 | bool fin) { 137 | std::cout << "error: onWSSegment should not be called" << std::endl; 138 | } 139 | 140 | private: 141 | std::string onCMD(CMDConnData& conn, int argc, const char** argv) { 142 | std::string resp; 143 | if (!strcmp(argv[0], "help")) { 144 | resp = admincmd_help; 145 | } 146 | else if (!strcmp(argv[0], "login")) { 147 | if (argc < 2 || strcmp(argv[1], "123456")) { 148 | resp = "wrong password"; 149 | } 150 | else { 151 | conn.login = true; 152 | resp = "login success"; 153 | } 154 | } 155 | else if (!conn.login) { 156 | resp = "must login first"; 157 | } 158 | else if (!strcmp(argv[0], "echo")) { 159 | if (argc >= 2) resp = std::string(argv[1]); 160 | } 161 | else if (!strcmp(argv[0], "stop")) { 162 | stop(); 163 | } 164 | else { 165 | resp = "invalid cmd, check help"; 166 | } 167 | 168 | return resp; 169 | } 170 | 171 | private: 172 | WSServer wsserver; 173 | std::thread ws_thr; 174 | std::string admincmd_help; 175 | std::atomic running; 176 | }; 177 | 178 | Server server; 179 | 180 | void my_handler(int s) { 181 | server.stop(); 182 | } 183 | 184 | int main(int argc, char** argv) { 185 | struct sigaction sigIntHandler; 186 | 187 | sigIntHandler.sa_handler = my_handler; 188 | sigemptyset(&sigIntHandler.sa_mask); 189 | sigIntHandler.sa_flags = 0; 190 | 191 | sigaction(SIGINT, &sigIntHandler, NULL); 192 | 193 | server.run(); 194 | } 195 | -------------------------------------------------------------------------------- /websocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License 3 | 4 | Copyright (c) 2020 Meng Rao 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | #pragma once 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | namespace websocket { 36 | 37 | template 38 | class SocketTcpConnection 39 | { 40 | public: 41 | ~SocketTcpConnection() { close("destruct"); } 42 | 43 | const char* getLastError() { return last_error_; }; 44 | 45 | bool isConnected() { return fd_ >= 0; } 46 | 47 | bool connect(const char* server_ip, uint16_t server_port) { 48 | int fd = socket(AF_INET, SOCK_STREAM, 0); 49 | if (fd < 0) { 50 | saveError("socket error", true); 51 | return false; 52 | } 53 | struct sockaddr_in server_addr; 54 | server_addr.sin_family = AF_INET; 55 | inet_pton(AF_INET, server_ip, &(server_addr.sin_addr)); 56 | server_addr.sin_port = htons(server_port); 57 | bzero(&(server_addr.sin_zero), 8); 58 | if (::connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { 59 | saveError("connect error", true); 60 | ::close(fd); 61 | return false; 62 | } 63 | return open(fd); 64 | } 65 | 66 | bool getPeername(struct sockaddr_in& addr) { 67 | socklen_t addr_len = sizeof(addr); 68 | return ::getpeername(fd_, (struct sockaddr*)&addr, &addr_len) == 0; 69 | } 70 | 71 | void close(const char* reason, bool check_errno = false) { 72 | if (fd_ >= 0) { 73 | saveError(reason, check_errno); 74 | ::close(fd_); 75 | fd_ = -1; 76 | } 77 | } 78 | 79 | bool write(const uint8_t* data, uint32_t size, bool more = false) { 80 | int flags = MSG_NOSIGNAL; 81 | if (more) flags |= MSG_MORE; 82 | do { 83 | int sent = ::send(fd_, data, size, flags); 84 | if (sent < 0) { 85 | if (errno != EAGAIN) { 86 | close("send error", true); 87 | return false; 88 | } 89 | continue; 90 | } 91 | data += sent; 92 | size -= sent; 93 | } while (size != 0); 94 | return true; 95 | } 96 | 97 | template 98 | bool read(Handler handler) { 99 | int ret = ::read(fd_, recvbuf_ + tail_, RecvBufSize - tail_); 100 | if (ret <= 0) { 101 | if (ret < 0 && errno == EAGAIN) return false; 102 | if (ret < 0) { 103 | close("read error", true); 104 | } 105 | else { 106 | close("remote close"); 107 | } 108 | return false; 109 | } 110 | tail_ += ret; 111 | 112 | uint32_t remaining = handler(recvbuf_ + head_, tail_ - head_); 113 | if (remaining == 0) { 114 | head_ = tail_ = 0; 115 | } 116 | else { 117 | head_ = tail_ - remaining; 118 | if (head_ >= RecvBufSize / 2) { 119 | memcpy(recvbuf_, recvbuf_ + head_, remaining); 120 | head_ = 0; 121 | tail_ = remaining; 122 | } 123 | else if (tail_ == RecvBufSize) { 124 | close("recv buf full"); 125 | } 126 | } 127 | return true; 128 | } 129 | 130 | protected: 131 | template 132 | friend class SocketTcpServer; 133 | 134 | bool open(int fd) { 135 | fd_ = fd; 136 | head_ = tail_ = 0; 137 | 138 | int flags = fcntl(fd_, F_GETFL, 0); 139 | if (fcntl(fd_, F_SETFL, flags | O_NONBLOCK) < 0) { 140 | close("fcntl O_NONBLOCK error", true); 141 | return false; 142 | } 143 | 144 | int yes = 1; 145 | if (setsockopt(fd_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) { 146 | close("setsockopt TCP_NODELAY error", true); 147 | return false; 148 | } 149 | 150 | return true; 151 | } 152 | 153 | void saveError(const char* msg, bool check_errno) { 154 | snprintf(last_error_, sizeof(last_error_), "%s %s", msg, check_errno ? (const char*)strerror(errno) : ""); 155 | } 156 | 157 | int fd_ = -1; 158 | uint32_t head_; 159 | uint32_t tail_; 160 | char recvbuf_[RecvBufSize]; 161 | char last_error_[64] = ""; 162 | }; 163 | 164 | template 165 | class SocketTcpServer 166 | { 167 | public: 168 | using TcpConnection = SocketTcpConnection; 169 | 170 | bool init(const char* interface, const char* server_ip, uint16_t server_port) { 171 | listenfd_ = socket(AF_INET, SOCK_STREAM, 0); 172 | if (listenfd_ < 0) { 173 | saveError("socket error"); 174 | return false; 175 | } 176 | 177 | int flags = fcntl(listenfd_, F_GETFL, 0); 178 | if (fcntl(listenfd_, F_SETFL, flags | O_NONBLOCK) < 0) { 179 | close("fcntl O_NONBLOCK error"); 180 | return false; 181 | } 182 | 183 | int yes = 1; 184 | if (setsockopt(listenfd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { 185 | close("setsockopt SO_REUSEADDR error"); 186 | return false; 187 | } 188 | 189 | struct sockaddr_in local_addr; 190 | local_addr.sin_family = AF_INET; 191 | inet_pton(AF_INET, server_ip, &(local_addr.sin_addr)); 192 | local_addr.sin_port = htons(server_port); 193 | bzero(&(local_addr.sin_zero), 8); 194 | if (bind(listenfd_, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) { 195 | close("bind error"); 196 | return false; 197 | } 198 | if (listen(listenfd_, 5) < 0) { 199 | close("listen error"); 200 | return false; 201 | } 202 | 203 | return true; 204 | }; 205 | 206 | void close(const char* reason) { 207 | if (listenfd_ >= 0) { 208 | saveError(reason); 209 | ::close(listenfd_); 210 | listenfd_ = -1; 211 | } 212 | } 213 | 214 | const char* getLastError() { return last_error_; }; 215 | 216 | ~SocketTcpServer() { close("destruct"); } 217 | 218 | bool accept2(TcpConnection& conn) { 219 | struct sockaddr_in clientaddr; 220 | socklen_t addr_len = sizeof(clientaddr); 221 | int fd = ::accept(listenfd_, (struct sockaddr*)&(clientaddr), &addr_len); 222 | if (fd < 0) { 223 | return false; 224 | } 225 | if (!conn.open(fd)) { 226 | return false; 227 | } 228 | return true; 229 | } 230 | 231 | private: 232 | void saveError(const char* msg) { snprintf(last_error_, sizeof(last_error_), "%s %s", msg, strerror(errno)); } 233 | 234 | int listenfd_ = -1; 235 | char last_error_[64] = ""; 236 | }; 237 | 238 | inline uint64_t getns() { 239 | timespec ts; 240 | ::clock_gettime(CLOCK_REALTIME, &ts); 241 | return ts.tv_sec * 1000000000 + ts.tv_nsec; 242 | } 243 | 244 | static const uint8_t OPCODE_CONT = 0; 245 | static const uint8_t OPCODE_TEXT = 1; 246 | static const uint8_t OPCODE_BINARY = 2; 247 | static const uint8_t OPCODE_CLOSE = 8; 248 | static const uint8_t OPCODE_PING = 9; 249 | static const uint8_t OPCODE_PONG = 10; 250 | 251 | template 252 | class WSConnection 253 | { 254 | public: 255 | ConnUserData user_data; 256 | 257 | // get remote network address 258 | bool getPeername(struct sockaddr_in& addr) { return conn.getPeername(addr); } 259 | 260 | bool isConnected() { return conn.isConnected(); } 261 | 262 | // if sending a msg of multiple segments, only set fin to true for the last one 263 | void send(uint8_t opcode, const uint8_t* payload, uint32_t pl_len, bool fin = true) { 264 | uint8_t h[14]; 265 | uint32_t h_len = 2; 266 | if (opcode >> 3) // if control 267 | fin = true; 268 | else { 269 | if (!send_fin) opcode = OPCODE_CONT; 270 | send_fin = fin; 271 | } 272 | h[0] = (opcode & 15) | ((uint8_t)fin << 7); 273 | h[1] = (uint8_t)SendMask << 7; 274 | if (pl_len < 126) { 275 | h[1] |= (uint8_t)pl_len; 276 | } 277 | else if (pl_len < 65536) { 278 | h[1] |= 126; 279 | *(uint16_t*)(h + 2) = htobe16(pl_len); 280 | h_len += 2; 281 | } 282 | else { 283 | h[1] |= 127; 284 | *(uint64_t*)(h + 2) = htobe64(pl_len); 285 | h_len += 8; 286 | } 287 | if (SendMask) { // for efficency and simplicity masking-key is always set to 0 288 | *(uint32_t*)(h + h_len) = 0; 289 | h_len += 4; 290 | } 291 | conn.write(h, h_len, true); 292 | conn.write(payload, pl_len, false); 293 | } 294 | 295 | // clean close the connection with optional status_code and reason 296 | void close(uint16_t status_code = 1005, const char* reason = "") { 297 | *(uint16_t*)close_reason = htobe16(status_code); 298 | uint32_t reason_len = snprintf((char*)close_reason + 2, sizeof(close_reason) - 2, "%s", reason); 299 | if (status_code != 1005) { 300 | send(OPCODE_CLOSE, close_reason, 2 + reason_len); 301 | } 302 | else 303 | send(OPCODE_CLOSE, nullptr, 0); 304 | conn.close("clean close"); 305 | } 306 | 307 | protected: 308 | template 309 | friend class WSServer; 310 | 311 | void init(uint64_t expire) { 312 | open = false; 313 | send_fin = true; 314 | *(uint16_t*)close_reason = htobe16(1006); 315 | close_reason[2] = 0; 316 | frame_size = 0; 317 | expire_time = expire; 318 | } 319 | 320 | uint32_t handleWSMsg(EventHandler* handler, uint8_t* data, uint32_t size) { 321 | // we might read a little more bytes beyond size, which is okey 322 | const uint8_t* data_end = data + size; 323 | uint8_t opcode = data[0] & 15; 324 | bool beg = opcode != OPCODE_CONT, fin = data[0] >> 7; //, control = opcode >> 3; 325 | bool mask = data[1] >> 7; 326 | uint8_t mask_key[4]; 327 | uint64_t pl_len = data[1] & 127; 328 | data += 2; 329 | if (pl_len == 126) { 330 | pl_len = be16toh(*(uint16_t*)data); 331 | data += 2; 332 | } 333 | else if (pl_len == 127) { 334 | pl_len = be64toh(*(uint64_t*)data) & ~(1ULL << 63); 335 | data += 8; 336 | } 337 | if (mask) { 338 | *(uint32_t*)mask_key = *(uint32_t*)data; 339 | data += 4; 340 | } 341 | if (data_end - data < (int64_t)pl_len) { 342 | if (size + (data + pl_len - data_end) > RecvBufSize) close(1009); 343 | return size; 344 | } 345 | if (mask) { 346 | for (uint64_t i = 0; i < pl_len; i++) data[i] ^= mask_key[i & 3]; 347 | } 348 | if (RecvSegment || (beg && fin)) { 349 | if (opcode == OPCODE_CLOSE) { 350 | uint16_t status_code = 1005; 351 | char reason[128] = {0}; 352 | if (pl_len >= 2) { 353 | status_code = be16toh(*(uint16_t*)data); 354 | uint64_t reason_len = std::min(sizeof(reason) - 1, pl_len - 2); 355 | memcpy(reason, data + 2, reason_len); 356 | reason[reason_len] = 0; 357 | } 358 | close(status_code, reason); 359 | } 360 | else { 361 | #if __cplusplus >= 201703L 362 | if constexpr (RecvSegment) { 363 | #else 364 | if (RecvSegment) { 365 | #endif 366 | if (beg) recv_opcode = opcode; 367 | handler->onWSSegment(*this, recv_opcode, data, pl_len, frame_size, fin); 368 | if (fin) 369 | frame_size = 0; 370 | else 371 | frame_size += pl_len; 372 | } 373 | else 374 | handler->onWSMsg(*this, opcode, data, pl_len); 375 | } 376 | } 377 | #if __cplusplus >= 201703L 378 | else if constexpr (!RecvSegment) { 379 | #else 380 | else { 381 | #endif 382 | if (frame_size + pl_len > RecvBufSize) 383 | close(1009); 384 | else { 385 | memcpy(frame + frame_size, data, pl_len); 386 | frame_size += pl_len; 387 | if (beg) recv_opcode = opcode; 388 | if (fin) { 389 | handler->onWSMsg(*this, recv_opcode, frame, frame_size); 390 | frame_size = 0; 391 | } 392 | } 393 | } 394 | return data_end - (data + pl_len); 395 | } 396 | 397 | void handleWSClose(EventHandler* handler) { 398 | uint16_t status_code = be16toh(*(uint16_t*)close_reason); 399 | const char* reason = (const char*)close_reason + 2; 400 | if (status_code == 1006) reason = conn.getLastError(); 401 | handler->onWSClose(*this, status_code, reason); 402 | } 403 | 404 | bool open; 405 | bool send_fin; 406 | uint8_t recv_opcode; 407 | uint32_t frame_size; 408 | uint64_t expire_time; 409 | uint8_t frame[RecvSegment ? 0 : RecvBufSize]; 410 | typename SocketTcpServer::TcpConnection conn; 411 | uint8_t close_reason[128]; // first 2 bytes are status_code(big endian) 412 | }; 413 | 414 | template> 416 | class WSClient : public ConnectionType 417 | { 418 | public: 419 | using Connection = ConnectionType; 420 | // using Connection = WSConnection; 421 | 422 | const char* getLastError() { return this->conn.getLastError(); } 423 | 424 | // timeout: connect timeout in milliseconds, 0 means no limit 425 | // if failed, call getLastError() for the reason 426 | bool wsConnect(uint64_t timeout, const char* server_ip, uint16_t server_port, const char* request_uri, 427 | const char* host, const char* origin = nullptr, const char* protocol = nullptr, 428 | const char* extensions = nullptr, char* resp_protocol = nullptr, uint32_t resp_protocol_size = 0, 429 | char* resp_extensions = nullptr, uint32_t resp_extensions_size = 0) { 430 | uint64_t now = getns(); 431 | uint64_t expire = timeout > 0 ? now + timeout * 1000000 : std::numeric_limits::max(); 432 | if (!this->conn.connect(server_ip, server_port)) return false; 433 | if (getns() > expire) { 434 | this->conn.close("timeout"); 435 | return false; 436 | } 437 | this->init(expire); 438 | char req[2048]; 439 | uint32_t req_len = 440 | snprintf(req, sizeof(req), 441 | "GET %s HTTP/1.1\r\nHost: %s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: " 442 | "dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n", 443 | request_uri, host); 444 | if (origin) req_len += snprintf(req + req_len, sizeof(req) - req_len, "Origin: %s\r\n", origin); 445 | if (protocol) req_len += snprintf(req + req_len, sizeof(req) - req_len, "Sec-WebSocket-Protocol: %s\r\n", protocol); 446 | if (extensions) 447 | req_len += snprintf(req + req_len, sizeof(req) - req_len, "Sec-WebSocket-Extensions: %s\r\n", extensions); 448 | req_len += snprintf(req + req_len, sizeof(req) - req_len, "\r\n"); 449 | if (req_len >= sizeof(req) - 1) { 450 | this->conn.close("request msg too long"); 451 | return false; 452 | } 453 | this->conn.write((uint8_t*)req, req_len); 454 | while (!this->open && this->isConnected()) { 455 | this->conn.read([&](const char* data, uint32_t size) -> uint32_t { 456 | const char* data_end = data + size; 457 | bool status_code_checked = false, upgrade_checked = false, connection_checked = false, accept_checked = false; 458 | while (true) { 459 | const char* ln = (char*)memchr(data, '\n', data_end - data); 460 | if (!ln) return size; 461 | if (*--ln != '\r') break; 462 | if (!status_code_checked) { // first line 463 | if (memcmp(data, "HTTP/", 5)) break; 464 | const char* status_code = (char*)memchr(data, ' ', ln - data); 465 | if (!status_code) break; 466 | while (*status_code == ' ') status_code++; 467 | if (memcmp(status_code, "101 ", 4)) break; 468 | status_code_checked = true; 469 | } 470 | else { 471 | const char* val_end = ln; 472 | while (val_end[-1] == ' ') val_end--; 473 | if (val_end == data) { // end of headers 474 | if (!upgrade_checked || !connection_checked || !accept_checked) break; 475 | this->open = true; 476 | return data_end - ln - 2; 477 | } 478 | const char* colon = (char*)memchr(data, ':', ln - data); 479 | if (!colon) break; 480 | const char* val = colon + 1; 481 | while (*val == ' ') val++; 482 | uint32_t key_len = colon - data; 483 | uint32_t val_len = val_end - val; 484 | if (key_len == 7 && !memcmp(data, "Upgrade", 7)) { 485 | if (memcmp(val, "websocket", 9)) break; 486 | upgrade_checked = true; 487 | } 488 | else if (key_len == 10 && !memcmp(data, "Connection", 10)) { 489 | if (!memcmp(val, "Upgrade", 7)) connection_checked = true; 490 | } 491 | else if (key_len == 20 && !memcmp(data, "Sec-WebSocket-Accept", 20)) { 492 | if (val_len != 28 || memcmp(val, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", 28)) break; 493 | accept_checked = true; 494 | } 495 | else if (key_len == 22 && !memcmp(data, "Sec-WebSocket-Protocol", 22) && resp_protocol_size > 0) { 496 | uint32_t cp_len = std::min(resp_protocol_size - 1, val_len); 497 | memcpy(resp_protocol, val, cp_len); 498 | resp_protocol[cp_len] = 0; 499 | } 500 | else if (key_len == 24 && !memcmp(data, "Sec-WebSocket-Extensions", 24) && resp_extensions_size > 0) { 501 | uint32_t cp_len = std::min(resp_extensions_size - 1, val_len); 502 | memcpy(resp_extensions, val, cp_len); 503 | resp_extensions[cp_len] = 0; 504 | } 505 | } 506 | data = ln + 2; // skip \r\n 507 | } 508 | this->conn.close("request failed"); 509 | return size; 510 | }); 511 | if (getns() > expire) this->conn.close("timeout"); 512 | } 513 | return this->isConnected(); 514 | } 515 | 516 | void poll(EventHandler* handler) { 517 | this->conn.read([&](const char* data, uint32_t size) { return this->handleWSMsg(handler, (uint8_t*)data, size); }); 518 | if (!this->isConnected()) this->handleWSClose(handler); 519 | } 520 | }; 521 | 522 | template 524 | class WSServer 525 | { 526 | public: 527 | using TcpServer = SocketTcpServer; 528 | using Connection = WSConnection; 529 | 530 | WSServer() { 531 | for (int i = 0; i < MaxConns; i++) { 532 | conns_[i] = conns_data_ + i; 533 | } 534 | } 535 | 536 | const char* getLastError() { return server_.getLastError(); } 537 | 538 | // newconn_timeout: new tcp connection max inactive time in milliseconds, 0 means no limit 539 | // openconn_timeout: open ws connection max inactive time in milliseconds, 0 means no limit 540 | // if failed, call getLastError() for the reason 541 | bool init(const char* server_ip, uint16_t server_port, uint64_t newconn_timeout = 0, uint64_t openconn_timeout = 0) { 542 | newconn_timeout_ = newconn_timeout * 1000000; 543 | openconn_timeout_ = openconn_timeout * 1000000; 544 | return server_.init("", server_ip, server_port); 545 | } 546 | 547 | void poll(EventHandler* handler) { 548 | uint64_t now = getns(); 549 | uint64_t new_expire = newconn_timeout_ ? now + newconn_timeout_ : std::numeric_limits::max(); 550 | uint64_t open_expire = openconn_timeout_ ? now + openconn_timeout_ : std::numeric_limits::max(); 551 | if (conns_cnt_ < MaxConns) { 552 | Connection& new_conn = *conns_[conns_cnt_]; 553 | if (server_.accept2(new_conn.conn)) { 554 | new_conn.init(new_expire); 555 | conns_cnt_++; 556 | } 557 | } 558 | for (int i = 0; i < conns_cnt_;) { 559 | Connection& conn = *conns_[i]; 560 | conn.conn.read([&](const char* data, uint32_t size) { 561 | uint32_t remaining = 562 | conn.open ? conn.handleWSMsg(handler, (uint8_t*)data, size) : handleHttpRequest(handler, conn, data, size); 563 | if (remaining < size) conn.expire_time = conn.open ? open_expire : new_expire; 564 | return remaining; 565 | }); 566 | if (now > conn.expire_time) conn.conn.close("timeout"); 567 | if (conn.isConnected()) 568 | i++; 569 | else { 570 | if (conn.open) conn.handleWSClose(handler); 571 | std::swap(conns_[i], conns_[--conns_cnt_]); 572 | } 573 | } 574 | } 575 | 576 | private: 577 | static uint32_t rol(uint32_t value, uint32_t bits) { return (value << bits) | (value >> (32 - bits)); } 578 | // Be cautious that *in* will be modified and up to 64 bytes will be appended, so make sure in buffer is long enough 579 | static uint32_t sha1base64(uint8_t* in, uint64_t in_len, char* out) { 580 | uint32_t h0[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; 581 | uint64_t total_len = in_len; 582 | in[total_len++] = 0x80; 583 | int padding_size = (64 - (total_len + 8) % 64) % 64; 584 | while (padding_size--) in[total_len++] = 0; 585 | for (uint64_t i = 0; i < total_len; i += 4) { 586 | uint32_t& w = *(uint32_t*)(in + i); 587 | w = be32toh(w); 588 | } 589 | *(uint32_t*)(in + total_len) = (uint32_t)(in_len >> 29); 590 | *(uint32_t*)(in + total_len + 4) = (uint32_t)(in_len << 3); 591 | for (uint8_t* in_end = in + total_len + 8; in < in_end; in += 64) { 592 | uint32_t* w = (uint32_t*)in; 593 | uint32_t h[5]; 594 | memcpy(h, h0, sizeof(h)); 595 | for (uint32_t i = 0, j = 0; i < 80; i++, j += 4) { 596 | uint32_t &a = h[j % 5], &b = h[(j + 1) % 5], &c = h[(j + 2) % 5], &d = h[(j + 3) % 5], &e = h[(j + 4) % 5]; 597 | if (i >= 16) w[i & 15] = rol(w[(i + 13) & 15] ^ w[(i + 8) & 15] ^ w[(i + 2) & 15] ^ w[i & 15], 1); 598 | if (i < 40) { 599 | if (i < 20) 600 | e += ((b & (c ^ d)) ^ d) + 0x5A827999; 601 | else 602 | e += (b ^ c ^ d) + 0x6ED9EBA1; 603 | } 604 | else { 605 | if (i < 60) 606 | e += (((b | c) & d) | (b & c)) + 0x8F1BBCDC; 607 | else 608 | e += (b ^ c ^ d) + 0xCA62C1D6; 609 | } 610 | e += w[i & 15] + rol(a, 5); 611 | b = rol(b, 30); 612 | } 613 | for (int i = 0; i < 5; i++) h0[i] += h[i]; 614 | } 615 | const char* base64tb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 616 | uint32_t triples[7] = {h0[0] >> 8, 617 | (h0[0] << 16) | (h0[1] >> 16), 618 | (h0[1] << 8) | (h0[2] >> 24), 619 | h0[2], 620 | h0[3] >> 8, 621 | (h0[3] << 16) | (h0[4] >> 16), 622 | h0[4] << 8}; 623 | for (uint32_t i = 0; i < 7; i++) { 624 | out[i * 4] = base64tb[(triples[i] >> 18) & 63]; 625 | out[i * 4 + 1] = base64tb[(triples[i] >> 12) & 63]; 626 | out[i * 4 + 2] = base64tb[(triples[i] >> 6) & 63]; 627 | out[i * 4 + 3] = base64tb[triples[i] & 63]; 628 | } 629 | out[27] = '='; 630 | return 28; 631 | } 632 | 633 | uint32_t handleHttpRequest(EventHandler* handler, Connection& conn, const char* data, uint32_t size) { 634 | const char* data_end = data + size; 635 | const int ValueBufSize = 128; 636 | char request_uri[1024] = {0}; 637 | char host[ValueBufSize] = {0}; 638 | char origin[ValueBufSize] = {0}; 639 | char wskey[ValueBufSize] = {0}; 640 | char wsprotocol[ValueBufSize] = {0}; 641 | char wsextensions[ValueBufSize] = {0}; 642 | bool upgrade_checked = false, connection_checked = false, wsversion_checked = false; 643 | while (true) { 644 | const char* ln = (char*)memchr(data, '\n', data_end - data); 645 | if (!ln) return size; 646 | if (*--ln != '\r') break; 647 | if (request_uri[0] == 0) { // first line 648 | if (memcmp(data, "GET ", 4)) break; 649 | data += 4; 650 | while (*data == ' ') data++; 651 | const char* uri_end = (char*)memchr(data, ' ', ln - data); 652 | uint32_t uri_len = uri_end - data; 653 | if (!uri_end || uri_len >= sizeof(request_uri)) break; 654 | memcpy(request_uri, data, uri_len); 655 | request_uri[uri_len] = 0; 656 | } 657 | else { 658 | const char* val_end = ln; 659 | while (val_end[-1] == ' ') val_end--; 660 | if (val_end == data) { // end of headers 661 | if (!host[0] || !wskey[0] || !upgrade_checked || !connection_checked || !wsversion_checked) break; 662 | char resp_wsprotocol[ValueBufSize] = {0}; 663 | char resp_wsextensions[ValueBufSize] = {0}; 664 | char resp[1024]; 665 | uint32_t resp_len = 0; 666 | bool accept = handler->onWSConnect( 667 | conn, request_uri, host, origin[0] ? origin : nullptr, wsprotocol[0] ? wsprotocol : nullptr, 668 | wsextensions[0] ? wsextensions : nullptr, resp_wsprotocol, ValueBufSize, resp_wsextensions, ValueBufSize); 669 | if (accept) { 670 | conn.open = true; 671 | memcpy(wskey + 24, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36); 672 | char accept_str[32]; 673 | accept_str[sha1base64((uint8_t*)wskey, 24 + 36, accept_str)] = 0; 674 | resp_len = sprintf(resp, 675 | "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: " 676 | "Upgrade\r\nSec-WebSocket-Accept: %s\r\n", 677 | accept_str); 678 | } 679 | else { 680 | resp_len = sprintf(resp, "HTTP/1.1 403 Forbidden\r\nSec-WebSocket-Version: 13\r\n"); 681 | } 682 | if (resp_wsprotocol[0]) 683 | resp_len += sprintf(resp + resp_len, "Sec-WebSocket-Protocol: %s\r\n", resp_wsprotocol); 684 | if (resp_wsextensions[0]) 685 | resp_len += sprintf(resp + resp_len, "Sec-WebSocket-Extensions: %s\r\n", resp_wsextensions); 686 | resp_len += sprintf(resp + resp_len, "\r\n"); 687 | conn.conn.write((uint8_t*)resp, resp_len); 688 | return data_end - ln - 2; 689 | } 690 | const char* colon = (char*)memchr(data, ':', ln - data); 691 | if (!colon) break; 692 | const char* val = colon + 1; 693 | while (*val == ' ') val++; 694 | uint32_t key_len = colon - data; 695 | uint32_t val_len = val_end - val; 696 | if (val_len < ValueBufSize) { 697 | if (key_len == 4 && !memcmp(data, "Host", 4)) { 698 | memcpy(host, val, val_len); 699 | host[val_len] = 0; 700 | } 701 | else if (key_len == 6 && !memcmp(data, "Origin", 6)) { 702 | memcpy(origin, val, val_len); 703 | origin[val_len] = 0; 704 | } 705 | else if (key_len == 7 && !memcmp(data, "Upgrade", 7)) { 706 | if (memcmp(val, "websocket", 9)) break; 707 | upgrade_checked = true; 708 | } 709 | else if (key_len == 10 && !memcmp(data, "Connection", 10)) { 710 | if (!memcmp(val, "Upgrade", 7)) connection_checked = true; 711 | } 712 | else if (key_len == 17 && !memcmp(data, "Sec-WebSocket-Key", 17)) { 713 | if (val_len != 24) break; 714 | memcpy(wskey, val, val_len); 715 | } 716 | else if (key_len == 21 && !memcmp(data, "Sec-WebSocket-Version", 21)) { 717 | if (val_len != 2 || memcmp(val, "13", 2)) break; 718 | wsversion_checked = true; 719 | } 720 | else if (key_len == 22 && !memcmp(data, "Sec-WebSocket-Protocol", 22)) { 721 | memcpy(wsprotocol, val, val_len); 722 | wsprotocol[val_len] = 0; 723 | } 724 | else if (key_len == 24 && !memcmp(data, "Sec-WebSocket-Extensions", 24)) { 725 | memcpy(wsextensions, val, val_len); 726 | wsextensions[val_len] = 0; 727 | } 728 | } 729 | } 730 | data = ln + 2; // skip \r\n 731 | } 732 | const char* resp400 = "HTTP/1.1 400 Bad Request\r\nSec-WebSocket-Version: 13\r\n\r\n"; 733 | conn.conn.write((uint8_t*)resp400, strlen(resp400)); 734 | conn.conn.close("bad request"); 735 | return size; 736 | } 737 | 738 | private: 739 | uint64_t newconn_timeout_; 740 | uint64_t openconn_timeout_; 741 | TcpServer server_; 742 | 743 | uint32_t conns_cnt_ = 0; 744 | Connection* conns_[MaxConns]; 745 | Connection conns_data_[MaxConns]; 746 | }; 747 | 748 | } // namespace websocket 749 | --------------------------------------------------------------------------------