forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.cpp
More file actions
80 lines (71 loc) · 2.24 KB
/
websocket.cpp
File metadata and controls
80 lines (71 loc) · 2.24 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
#include "httpserver/websocket.hpp"
#include <string>
#include <vector>
#include <microhttpd.h>
#include <microhttpd_ws.h>
using namespace httpserver;
void websocket::send(const std::string& message) {
/* a chat message or command is pending */
char* frame_data = NULL;
size_t frame_len = 0;
int er = MHD_websocket_encode_text (ws,
message.data(),
message.size(),
MHD_WEBSOCKET_FRAGMENTATION_NONE,
&frame_data,
&frame_len,
NULL);
/* send the data via the TCP/IP socket */
if (MHD_WEBSOCKET_STATUS_OK == er)
{
send_raw(frame_data,
frame_len);
}
MHD_websocket_free (ws,
frame_data);
}
void websocket::send_raw(const char* buf, size_t len) {
ssize_t ret;
size_t off;
for (off = 0; off < len; off += ret)
{
ret = ::send(fd,
&buf[off],
(int) (len - off),
0);
if (0 > ret)
{
if (EAGAIN == errno)
{
ret = 0;
continue;
}
break;
}
if (0 == ret)
break;
}
}
void websocket::insert_into_receive_queue(const std::string& message) {
std::unique_lock<std::mutex> lck(receive_mutex_);
received_messages_.push_front(message);
}
std::string websocket::receive() {
std::unique_lock<std::mutex> lck(receive_mutex_);
while (received_messages_.empty()) receive_cv_.wait(lck);
std::string result = std::move(received_messages_.back());
received_messages_.pop_back();
return result;
}
bool websocket::receive(std::string& message, uint64_t timeout_milliseconds) {
std::unique_lock<std::mutex> lck(receive_mutex_);
if (!receive_cv_.wait_for(lck, std::chrono::milliseconds{timeout_milliseconds}, [this](){return !received_messages_.empty();})) {
return false;
}
message = std::move(received_messages_.back());
received_messages_.pop_back();
return true;
}
bool websocket::disconnect() const {
return disconnect_;
}