Skip to content
File

Blob: src/workerd/api/hibernation-event-params.h

cpp63 lines
1// Copyright (c) 2017-2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5#pragma once
6 
7#include <kj/common.h>
8#include <kj/exception.h>
9#include <kj/one-of.h>
10#include <kj/string.h>
11 
12#include <cstdint>
13 
14namespace workerd::api {
15// Event types and their corresponding parameters.
16struct HibernatableSocketParams {
17 struct Text {
18 kj::String message;
19 };
20 
21 struct Data {
22 kj::Array<kj::byte> message;
23 };
24 
25 struct Close {
26 uint16_t code;
27 kj::String reason;
28 bool wasClean;
29 };
30 
31 struct Error {
32 kj::Exception error;
33 };
34 
35 kj::OneOf<Text, Data, Close, Error> eventType;
36 kj::String websocketId;
37 kj::Maybe<uint32_t> eventTimeoutMs;
38 
39 explicit HibernatableSocketParams(kj::String message, kj::String id)
40 : eventType(Text{kj::mv(message)}),
41 websocketId(kj::mv(id)) {}
42 explicit HibernatableSocketParams(kj::Array<kj::byte> message, kj::String id)
43 : eventType(Data{kj::mv(message)}),
44 websocketId(kj::mv(id)) {}
45 explicit HibernatableSocketParams(uint16_t code, kj::String reason, bool wasClean, kj::String id)
46 : eventType(Close{code, kj::mv(reason), wasClean}),
47 websocketId(kj::mv(id)) {}
48 explicit HibernatableSocketParams(kj::Exception e, kj::String id)
49 : eventType(Error{kj::mv(e)}),
50 websocketId(kj::mv(id)) {}
51 
52 HibernatableSocketParams(HibernatableSocketParams&& other) = default;
53 
54 bool isCloseEvent() {
55 return eventType.is<Close>();
56 }
57 
58 void setTimeout(kj::Maybe<uint32_t> timeoutMs) {
59 eventTimeoutMs = kj::mv(timeoutMs);
60 }
61};
62}; // namespace workerd::api