summaryrefslogtreecommitdiff
path: root/modules/websocket/lws_peer.cpp
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2018-09-23 21:14:20 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2018-09-24 01:50:37 +0200
commit5d91e87c64f757fb1c20f43a0a30db3c73073625 (patch)
tree29000bb4eabc80bb8f208d71bafd6291d9df9868 /modules/websocket/lws_peer.cpp
parent399910ddd82e84f078ebefb3ed2c51d2ef989235 (diff)
Implement WebSocket close notify.
Diffstat (limited to 'modules/websocket/lws_peer.cpp')
-rw-r--r--modules/websocket/lws_peer.cpp41
1 files changed, 39 insertions, 2 deletions
diff --git a/modules/websocket/lws_peer.cpp b/modules/websocket/lws_peer.cpp
index 0989357258..43ffcfdea1 100644
--- a/modules/websocket/lws_peer.cpp
+++ b/modules/websocket/lws_peer.cpp
@@ -178,11 +178,48 @@ bool LWSPeer::is_connected_to_host() const {
return wsi != NULL;
};
-void LWSPeer::close() {
+String LWSPeer::get_close_reason(void *in, size_t len, int &r_code) {
+ String s;
+ r_code = 0;
+ if (len < 2) // From docs this should not happen
+ return s;
+
+ const uint8_t *b = (const uint8_t *)in;
+ r_code = b[0] << 8 | b[1];
+
+ if (len > 2) {
+ const char *utf8 = (const char *)&b[2];
+ s.parse_utf8(utf8, len - 2);
+ }
+ return s;
+}
+
+void LWSPeer::send_close_status(struct lws *p_wsi) {
+ if (close_code == -1)
+ return;
+
+ int len = close_reason.size();
+ ERR_FAIL_COND(len > 123); // Maximum allowed reason size in bytes
+
+ lws_close_status code = (lws_close_status)close_code;
+ unsigned char *reason = len > 0 ? (unsigned char *)close_reason.utf8().ptrw() : NULL;
+
+ lws_close_reason(p_wsi, code, reason, len);
+
+ close_code = -1;
+ close_reason = "";
+}
+
+void LWSPeer::close(int p_code, String p_reason) {
if (wsi != NULL) {
+ close_code = p_code;
+ close_reason = p_reason;
PeerData *data = ((PeerData *)lws_wsi_user(wsi));
data->force_close = true;
- lws_callback_on_writable(wsi); // notify that we want to disconnect
+ lws_callback_on_writable(wsi); // Notify that we want to disconnect
+ } else {
+ close_code = -1;
+ close_reason = "";
}
wsi = NULL;
rbw.resize(0);