-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathwebsocket_handler.cpp
More file actions
135 lines (109 loc) · 4.25 KB
/
websocket_handler.cpp
File metadata and controls
135 lines (109 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#ifdef HAVE_WEBSOCKET
#include "httpserver/websocket_handler.hpp"
#include <microhttpd.h>
#include <microhttpd_ws.h>
#if !defined(__MINGW32__)
#include <sys/socket.h>
#endif
#include <cstring>
#include <string>
namespace httpserver {
// websocket_session implementation
websocket_session::websocket_session(MHD_socket sock, struct MHD_UpgradeResponseHandle* urh,
struct MHD_WebSocketStream* ws_stream):
sock(sock), urh(urh), ws_stream(ws_stream), valid(true) {
}
websocket_session::~websocket_session() {
if (ws_stream != nullptr) {
MHD_websocket_stream_free(ws_stream);
}
if (urh != nullptr) {
MHD_upgrade_action(urh, MHD_UPGRADE_ACTION_CLOSE);
}
}
static bool send_all(MHD_socket sock, const char* data, size_t len) {
size_t sent = 0;
while (sent < len) {
ssize_t ret = send(sock, data + sent, len - sent, 0);
if (ret <= 0) return false;
sent += static_cast<size_t>(ret);
}
return true;
}
void websocket_session::send_text(const std::string& msg) {
if (!valid) return;
char* frame = nullptr;
size_t frame_len = 0;
if (MHD_websocket_encode_text(ws_stream, msg.c_str(), msg.size(), 0, &frame, &frame_len, nullptr) == MHD_WEBSOCKET_STATUS_OK) {
if (!send_all(sock, frame, frame_len)) valid = false;
MHD_websocket_free(ws_stream, frame);
}
}
void websocket_session::send_binary(const void* data, size_t len) {
if (!valid) return;
char* frame = nullptr;
size_t frame_len = 0;
if (MHD_websocket_encode_binary(ws_stream, static_cast<const char*>(data), len, 0, &frame, &frame_len) == MHD_WEBSOCKET_STATUS_OK) {
if (!send_all(sock, frame, frame_len)) valid = false;
MHD_websocket_free(ws_stream, frame);
}
}
void websocket_session::send_ping(const std::string& payload) {
if (!valid) return;
char* frame = nullptr;
size_t frame_len = 0;
if (MHD_websocket_encode_ping(ws_stream, payload.c_str(), payload.size(), &frame, &frame_len) == MHD_WEBSOCKET_STATUS_OK) {
if (!send_all(sock, frame, frame_len)) valid = false;
MHD_websocket_free(ws_stream, frame);
}
}
void websocket_session::send_pong(const std::string& payload) {
if (!valid) return;
char* frame = nullptr;
size_t frame_len = 0;
if (MHD_websocket_encode_pong(ws_stream, payload.c_str(), payload.size(), &frame, &frame_len) == MHD_WEBSOCKET_STATUS_OK) {
if (!send_all(sock, frame, frame_len)) valid = false;
MHD_websocket_free(ws_stream, frame);
}
}
void websocket_session::close(uint16_t code, const std::string& reason) {
if (!valid) return;
valid = false;
char* frame = nullptr;
size_t frame_len = 0;
if (MHD_websocket_encode_close(ws_stream, code, reason.c_str(), reason.size(), &frame, &frame_len) == MHD_WEBSOCKET_STATUS_OK) {
send_all(sock, frame, frame_len);
MHD_websocket_free(ws_stream, frame);
}
}
bool websocket_session::is_valid() const {
return valid;
}
// websocket_handler default implementations
void websocket_handler::on_open(websocket_session&) {
}
void websocket_handler::on_binary(websocket_session&, const void*, size_t) {
}
void websocket_handler::on_ping(websocket_session& session, std::string_view payload) {
session.send_pong(std::string(payload));
}
void websocket_handler::on_close(websocket_session&, uint16_t, const std::string&) {
}
} // namespace httpserver
#endif // HAVE_WEBSOCKET