forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.cpp
73 lines (52 loc) · 2.15 KB
/
websocket.cpp
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
/**
* Copyright (c) 2020-2021 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#if RTC_ENABLE_WEBSOCKET
#include "websocket.hpp"
#include "common.hpp"
#include "impl/internals.hpp"
#include "impl/websocket.hpp"
namespace rtc {
WebSocket::WebSocket() : WebSocket(Configuration()) {}
WebSocket::WebSocket(Configuration config)
: CheshireCat<impl::WebSocket>(std::move(config)),
Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
WebSocket::WebSocket(impl_ptr<impl::WebSocket> impl)
: CheshireCat<impl::WebSocket>(std::move(impl)),
Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
WebSocket::~WebSocket() {
try {
impl()->remoteClose();
impl()->resetCallbacks(); // not done by impl::WebSocket
} catch (const std::exception &e) {
PLOG_ERROR << e.what();
}
}
WebSocket::State WebSocket::readyState() const { return impl()->state; }
bool WebSocket::isOpen() const { return impl()->state.load() == State::Open; }
bool WebSocket::isClosed() const { return impl()->state.load() == State::Closed; }
size_t WebSocket::maxMessageSize() const { return DEFAULT_MAX_MESSAGE_SIZE; }
void WebSocket::open(const string &url) { impl()->open(url); }
void WebSocket::close() { impl()->close(); }
void WebSocket::forceClose() { impl()->remoteClose(); }
bool WebSocket::send(message_variant data) {
return impl()->outgoing(make_message(std::move(data)));
}
bool WebSocket::send(const byte *data, size_t size) {
return impl()->outgoing(make_message(data, data + size, Message::Binary));
}
optional<string> WebSocket::remoteAddress() const {
auto tcpTransport = impl()->getTcpTransport();
return tcpTransport ? make_optional(tcpTransport->remoteAddress()) : nullopt;
}
optional<string> WebSocket::path() const {
auto state = impl()->state.load();
auto handshake = impl()->getWsHandshake();
return state != State::Connecting && handshake ? make_optional(handshake->path()) : nullopt;
}
} // namespace rtc
#endif