4#include <emscripten/websocket.h>
12#include "absl/memory/memory.h"
13#include "absl/strings/str_cat.h"
20constexpr int kOpenWait = 10000;
21constexpr int kAnswerWait = 10000;
24constexpr int kTick = 10;
29 const std::string& url) {
30 EmscriptenWebSocketCreateAttributes attributes;
31 emscripten_websocket_init_create_attributes(&attributes);
32 attributes.url = url.c_str();
33 attributes.createOnMainThread =
true;
34 const EMSCRIPTEN_WEBSOCKET_T handle = emscripten_websocket_new(&attributes);
36 return absl::UnavailableError(absl::StrCat(
"couldn't open ", url));
38 std::unique_ptr<WebSocket> socket = absl::WrapUnique(
new WebSocket(handle));
39 emscripten_websocket_set_onopen_callback(handle, socket.get(), OnOpen);
40 emscripten_websocket_set_onmessage_callback(handle, socket.get(), OnMessage);
41 emscripten_websocket_set_onerror_callback(handle, socket.get(), OnError);
42 emscripten_websocket_set_onclose_callback(handle, socket.get(), OnClose);
46 !socket->opened_ && !socket->closed_ && waited < kOpenWait;
48 emscripten_sleep(kTick);
50 if (!socket->open()) {
51 return absl::UnavailableError(
52 absl::StrCat(
"couldn't reach the gateway at ", url));
58 emscripten_websocket_close(socket_, 1000,
"");
59 emscripten_websocket_delete(socket_);
64 return absl::UnavailableError(
"the gateway closed the connection");
66 std::string owned(bytes);
67 if (emscripten_websocket_send_binary(socket_, owned.data(),
68 static_cast<uint32_t
>(owned.size())) !=
69 EMSCRIPTEN_RESULT_SUCCESS) {
70 return absl::UnavailableError(
"couldn't send to the gateway");
72 return absl::OkStatus();
78 for (
int waited = 0; received_.empty() &&
open() && waited < milliseconds;
80 emscripten_sleep(kTick);
84bool WebSocket::OnOpen(
int,
const EmscriptenWebSocketOpenEvent*,
void* self) {
85 static_cast<WebSocket*
>(self)->opened_ =
true;
89bool WebSocket::OnMessage(
int,
const EmscriptenWebSocketMessageEvent* event,
91 static_cast<WebSocket*
>(self)->received_.append(
92 reinterpret_cast<const char*
>(event->data), event->numBytes);
96bool WebSocket::OnError(
int,
const EmscriptenWebSocketErrorEvent*,
void* self) {
97 static_cast<WebSocket*
>(self)->closed_ =
true;
101bool WebSocket::OnClose(
int,
const EmscriptenWebSocketCloseEvent*,
void* self) {
102 static_cast<WebSocket*
>(self)->closed_ =
true;
107 const std::string& url,
const protocol::LoginRequest& request) {
108 absl::StatusOr<std::unique_ptr<WebSocket>> socket =
WebSocket::Open(url);
110 return socket.status();
112 if (
const absl::Status sent = (*socket)->Send(
Frame(request)); !sent.ok()) {
116 for (
int waited = 0; waited < kAnswerWait; waited += kTick) {
117 reader.
Append((*socket)->Take());
118 if (
const std::optional<std::string> message = reader.
Next()) {
119 protocol::LoginReply reply;
120 if (!reply.ParseFromString(*message)) {
121 return absl::DataLossError(
"couldn't read the login server's reply");
125 if (!reader.
status().ok()) {
128 if (!(*socket)->open()) {
129 return absl::UnavailableError(
"the gateway closed before the answer");
131 (*socket)->Wait(kTick);
133 return absl::DeadlineExceededError(
"the login server didn't answer");
137 const std::string& url,
const std::string& token) {
138 absl::StatusOr<std::unique_ptr<WebSocket>> socket =
WebSocket::Open(url);
140 return socket.status();
142 protocol::Hello hello;
143 hello.set_token(token);
144 if (
const absl::Status sent = (*socket)->Send(
Frame(hello)); !sent.ok()) {
147 return absl::WrapUnique(
new WebSocketLink(*std::move(socket)));
151 reader_.Append(socket_->Take());
152 while (
const std::optional<std::string> message = reader_.Next()) {
153 if (!inbox_.Take(*message).ok()) {
158 if (!reader_.status().ok() || !socket_->open()) {
164 if (connected_ && !socket_->Send(
Frame(input)).ok()) {
void Append(std::string_view bytes)
std::optional< std::string > Next()
const absl::Status & status() const
void Poll() override
Takes in what's arrived, without waiting.
void Send(const protocol::Input &input) override
static absl::StatusOr< std::unique_ptr< WebSocketLink > > Open(const std::string &url, const std::string &token)
Opens the world's url at the gateway, and says hello with the token.
WebSocket(const WebSocket &)=delete
std::string Take()
What's come since the last time.
absl::Status Send(std::string_view bytes)
void Wait(int milliseconds)
static absl::StatusOr< std::unique_ptr< WebSocket > > Open(const std::string &url)
std::string Frame(const google::protobuf::MessageLite &message)
The size goes in front, as four bytes in network byte order.
absl::StatusOr< protocol::LoginReply > AskOverWebSocket(const std::string &url, const protocol::LoginRequest &request)
The login server's answer to the request, through the gateway at url.