-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunix_socket_server.h
54 lines (40 loc) · 1.48 KB
/
unix_socket_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
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <uvw.hpp>
#include "unix_socket_client.h"
namespace stream_data_processor {
class UnixSocketServer {
public:
UnixSocketServer() = delete;
UnixSocketServer(std::shared_ptr<UnixSocketClientFactory> client_factory,
const std::string& socket_path, uvw::Loop* loop);
UnixSocketServer(const UnixSocketServer& other) = delete;
UnixSocketServer& operator=(const UnixSocketServer& other) = delete;
UnixSocketServer(UnixSocketServer&& other);
UnixSocketServer& operator=(UnixSocketServer&& other);
~UnixSocketServer() = default;
void start();
void stop();
private:
void handleNewConnection(const uvw::ListenEvent& event,
uvw::PipeHandle& socket_handle) {
spdlog::info("New socket connection!");
auto connection = socket_handle.loop().resource<uvw::PipeHandle>();
clients_.push_back(client_factory_->createClient(connection));
socket_handle_->accept(*connection);
clients_.back()->start();
}
private:
static const std::string SOCKET_LOCK_FILE_SUFFIX;
static constexpr mode_t LOCK_FILE_MODE = 0644;
private:
std::shared_ptr<UnixSocketClientFactory> client_factory_;
std::string socket_path_;
std::shared_ptr<uvw::PipeHandle> socket_handle_;
uvw::PipeHandle::Connection<uvw::ListenEvent> listen_event_listener_;
int socket_lock_fd_;
std::vector<std::shared_ptr<UnixSocketClient>> clients_;
};
} // namespace stream_data_processor