-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathaccept_server.h
70 lines (49 loc) · 1.88 KB
/
accept_server.h
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
// Copyright 2023, Roman Gershman. All rights reserved.
// See LICENSE for licensing terms.
//
#pragma once
#include <functional>
#include <memory>
#include <vector>
#include "util/connection.h"
#include "util/fibers/synchronization.h"
namespace util {
class ListenerInterface;
class ProactorPool;
class AcceptServer {
AcceptServer(const AcceptServer&) = delete;
void operator=(const AcceptServer&) = delete;
public:
explicit AcceptServer(ProactorPool* pool, bool break_on_int = true);
~AcceptServer();
void Run();
// If wait is false - does not wait for the server to stop.
// Then you need to run Wait() to wait for proper shutdown.
void Stop(bool wait = false);
void Wait();
// Returns the port number to which the listener was bound.
// Check-fails in case of an error.
uint16_t AddListener(uint16_t port, ListenerInterface* listener);
// Advanced version that allows to specify bind address.
// bind_addr can be null, in that case the behavior is to bind on all interfaces.
// Does not check-fail - it's responsibility of the caller to check the error code.
std::error_code AddListener(const char* bind_addr, uint16_t port, ListenerInterface* listener);
// Adds a listener on unix domain sockets.
std::error_code AddUDSListener(const char* path, mode_t permissions, ListenerInterface* listener);
void TriggerOnBreakSignal(std::function<void()> f) {
on_break_hook_ = std::move(f);
}
void set_back_log(uint16_t backlog) {
backlog_ = backlog;
}
private:
void BreakListeners();
ProactorPool* pool_;
// Called if a termination signal has been caught (SIGTERM/SIGINT).
std::function<void()> on_break_hook_;
std::vector<std::unique_ptr<ListenerInterface>> list_interface_;
fb2::BlockingCounter ref_bc_; // to synchronize listener threads during the shutdown.
bool was_run_ = false;
uint16_t backlog_ = 128;
};
} // namespace util