forked from brainboxdotcc/DPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketengine.cpp
More file actions
153 lines (137 loc) · 3.89 KB
/
socketengine.cpp
File metadata and controls
153 lines (137 loc) · 3.89 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#include <dpp/socketengine.h>
#include <dpp/exception.h>
#include <csignal>
#include <memory>
#include <dpp/sslconnection.h>
#include <iostream>
#include <dpp/cache.h>
#include <dpp/cluster.h>
namespace dpp {
bool socket_engine_base::register_socket(const socket_events &e) {
std::unique_lock lock(fds_mutex);
auto i = fds.find(e.fd);
if (e.fd != INVALID_SOCKET && i == fds.end()) {
fds.emplace(e.fd, std::make_unique<socket_events>(e));
stats.active_fds++;
return true;
}
if (e.fd != INVALID_SOCKET && i != fds.end()) {
remove_socket(e.fd);
fds.erase(i);
fds.emplace(e.fd, std::make_unique<socket_events>(e));
stats.updates++;
return true;
}
return false;
}
bool socket_engine_base::update_socket(const socket_events &e) {
std::unique_lock lock(fds_mutex);
if (e.fd != INVALID_SOCKET && fds.find(e.fd) != fds.end()) {
auto iter = fds.find(e.fd);
*(iter->second) = e;
stats.updates++;
return true;
}
return false;
}
socket_engine_base::socket_engine_base(cluster* creator) : owner(creator) {
#ifndef _WIN32
set_signal_handler(SIGALRM);
set_signal_handler(SIGXFSZ);
set_signal_handler(SIGCHLD);
signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
#else
// Set up winsock.
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 2), &wsadata)) {
throw dpp::connection_exception(err_connect_failure, "WSAStartup failure");
}
#endif
}
socket_engine_base::~socket_engine_base() {
#ifdef _WIN32
WSACleanup();
#endif
}
time_t last_time = time(nullptr);
socket_events* socket_engine_base::get_fd(dpp::socket fd) {
std::unique_lock lock(fds_mutex);
auto iter = fds.find(fd);
if (iter == fds.end()) {
return nullptr;
}
return iter->second.get();
}
void socket_engine_base::inplace_modify_fd(dpp::socket fd, uint8_t extra_flags) {
bool should_modify;
socket_events s{};
{
std::lock_guard lk(fds_mutex);
auto i = fds.find(fd);
should_modify = i != fds.end() && (i->second->flags & extra_flags) != extra_flags;
if (should_modify) {
i->second->flags |= extra_flags;
s = *(i->second);
}
}
if (should_modify) {
update_socket(s);
}
}
void socket_engine_base::prune() {
if (time(nullptr) != last_time) {
try {
owner->tick_timers();
} catch (const std::exception& e) {
owner->log(dpp::ll_error, "Uncaught exception in tick_timers: " + std::string(e.what()));
}
if ((time(nullptr) % 60) == 0) {
/* Every minute, rehash all cache containers.
* We do this from the socket engine now, not from
* shard 0, so no need to run shards to have timers!
*/
dpp::garbage_collection();
}
last_time = time(nullptr);
}
stats.iterations++;
}
bool socket_engine_base::delete_socket(dpp::socket fd) {
std::unique_lock lock(fds_mutex);
auto iter = fds.find(fd);
if (iter == fds.end() || ((iter->second->flags & WANT_DELETION) != 0L)) {
return false;
}
iter->second->flags |= WANT_DELETION;
stats.deletions++;
stats.active_fds--;
return true;
}
bool socket_engine_base::remove_socket(dpp::socket fd) {
return true;
}
const socket_stats& socket_engine_base::get_stats() const {
return stats;
}
}