Action Engine
Loading...
Searching...
No Matches
server.h
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ACTIONENGINE_NET_WEBSOCKETS_SERVER_H_
16#define ACTIONENGINE_NET_WEBSOCKETS_SERVER_H_
17
18#include <cstdint>
19#include <memory>
20#include <optional>
21#include <string>
22#include <string_view>
23
24#define BOOST_ASIO_NO_DEPRECATED
25
26#include <absl/base/nullability.h>
27#include <absl/base/thread_annotations.h>
28#include <absl/log/check.h>
29#include <absl/status/status.h>
30#include <absl/status/statusor.h>
31#include <absl/strings/str_format.h>
32#include <boost/asio/ip/tcp.hpp>
33
36#include "actionengine/net/stream.h"
37#include "actionengine/net/websockets/fiber_aware_websocket_stream.h"
38#include "actionengine/service/service.h"
39
40namespace act::net {
41
42class WebsocketServer {
43 public:
44 explicit WebsocketServer(act::Service* absl_nonnull service,
45 std::string_view address = "0.0.0.0",
46 uint16_t port = 20000);
47
48 ~WebsocketServer();
49
50 void Run();
51
52 absl::Status Cancel();
53
54 absl::Status Join();
55
56 private:
57 absl::Status CancelInternal() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
58
59 absl::Status JoinInternal() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
60
61 act::Service* absl_nonnull const service_;
62 std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
63
64 mutable act::Mutex mu_;
65 std::unique_ptr<thread::Fiber> main_loop_;
66 bool cancelled_ ABSL_GUARDED_BY(mu_) = false;
67 act::CondVar join_cv_ ABSL_GUARDED_BY(mu_);
68 bool joining_ ABSL_GUARDED_BY(mu_) = false;
69 absl::Status status_;
70};
71
72} // namespace act::net
73
74#endif // ACTIONENGINE_NET_WEBSOCKETS_SERVER_H_
Concurrency utilities for ActionEngine.
ActionEngine data structures used to implement actions and nodes (data streams).