add session replay functions

This commit is contained in:
Martin Michelsen
2022-07-01 00:26:48 -07:00
parent 38b0539124
commit a7e3d4853a
25 changed files with 686 additions and 125 deletions
+34 -29
View File
@@ -31,11 +31,11 @@ using namespace std::placeholders;
void Server::disconnect_client(shared_ptr<Client> c) {
if (c->channel.is_virtual_connection) {
server_log.info("Disconnecting client on virtual connection %p",
c->channel.bev.get());
server_log.info("Client disconnected: C-%" PRIX64 " on virtual connection %p",
c->id, c->channel.bev.get());
} else {
server_log.info("Disconnecting client on fd %d",
bufferevent_getfd(c->channel.bev.get()));
server_log.info("Client disconnected: C-%" PRIX64 " on fd %d",
c->id, bufferevent_getfd(c->channel.bev.get()));
}
this->channel_to_client.erase(&c->channel);
@@ -76,9 +76,6 @@ void Server::on_listen_accept(struct evconnlistener* listener,
return;
}
server_log.info("Client fd %d connected via fd %d (%s)",
fd, listen_fd, listening_socket->name.c_str());
struct bufferevent *bev = bufferevent_socket_new(this->base.get(), fd,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
shared_ptr<Client> c(new Client(
@@ -88,6 +85,9 @@ void Server::on_listen_accept(struct evconnlistener* listener,
c->channel.context_obj = this;
this->channel_to_client.emplace(&c->channel, c);
server_log.info("Client connected: C-%" PRIX64 " on fd %d via %d (%s)",
c->id, fd, listen_fd, listening_socket->addr_str.c_str());
try {
process_connect(this->state, c);
} catch (const exception& e) {
@@ -97,23 +97,28 @@ void Server::on_listen_accept(struct evconnlistener* listener,
}
void Server::connect_client(
struct bufferevent* bev, uint32_t address, uint16_t port,
GameVersion version, ServerBehavior initial_state) {
server_log.info("Client connected on virtual connection %p", bev);
struct bufferevent* bev, uint32_t address, uint16_t client_port,
uint16_t server_port, GameVersion version, ServerBehavior initial_state) {
shared_ptr<Client> c(new Client(bev, version, initial_state));
c->channel.on_command_received = Server::on_client_input;
c->channel.on_error = Server::on_client_error;
c->channel.context_obj = this;
server_log.info("Client connected: C-%" PRIX64 " on virtual connection %p via T-%hu-%s-%s-VI",
c->id,
bev,
server_port,
name_for_version(version),
name_for_server_behavior(initial_state));
this->channel_to_client.emplace(&c->channel, c);
// Manually set the remote address, since the bufferevent has no fd and the
// Channel constructor can't figure out the virtual remote address
auto* sin = reinterpret_cast<sockaddr_in*>(&c->channel.remote_addr);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = htonl(address);
sin->sin_port = htons(port);
auto* remote_sin = reinterpret_cast<sockaddr_in*>(&c->channel.remote_addr);
remote_sin->sin_family = AF_INET;
remote_sin->sin_addr.s_addr = htonl(address);
remote_sin->sin_port = htons(client_port);
try {
process_connect(this->state, c);
@@ -169,36 +174,36 @@ Server::Server(
: base(base), state(state) { }
void Server::listen(
const std::string& name,
const std::string& addr_str,
const string& socket_path,
GameVersion version,
ServerBehavior behavior) {
int fd = ::listen(socket_path, 0, SOMAXCONN);
server_log.info("Listening on Unix socket %s (%s) on fd %d (name: %s)",
socket_path.c_str(), name_for_version(version), fd, name.c_str());
this->add_socket(name, fd, version, behavior);
server_log.info("Listening on Unix socket %s on fd %d as %s",
socket_path.c_str(), fd, addr_str.c_str());
this->add_socket(addr_str, fd, version, behavior);
}
void Server::listen(
const std::string& name,
const std::string& addr_str,
const string& addr,
int port,
GameVersion version,
ServerBehavior behavior) {
int fd = ::listen(addr, port, SOMAXCONN);
string netloc_str = render_netloc(addr, port);
server_log.info("Listening on TCP interface %s (%s) on fd %d (name: %s)",
netloc_str.c_str(), name_for_version(version), fd, name.c_str());
this->add_socket(name, fd, version, behavior);
server_log.info("Listening on TCP interface %s on fd %d as %s",
netloc_str.c_str(), fd, addr_str.c_str());
this->add_socket(addr_str, fd, version, behavior);
}
void Server::listen(const std::string& name, int port, GameVersion version, ServerBehavior behavior) {
this->listen(name, "", port, version, behavior);
void Server::listen(const std::string& addr_str, int port, GameVersion version, ServerBehavior behavior) {
this->listen(addr_str, "", port, version, behavior);
}
Server::ListeningSocket::ListeningSocket(Server* s, const std::string& name,
Server::ListeningSocket::ListeningSocket(Server* s, const std::string& addr_str,
int fd, GameVersion version, ServerBehavior behavior) :
name(name), fd(fd), version(version), behavior(behavior), listener(
addr_str(addr_str), fd(fd), version(version), behavior(behavior), listener(
evconnlistener_new(s->base.get(), Server::dispatch_on_listen_accept, s,
LEV_OPT_REUSEABLE, 0, this->fd), evconnlistener_free) {
evconnlistener_set_error_cb(this->listener.get(),
@@ -206,12 +211,12 @@ Server::ListeningSocket::ListeningSocket(Server* s, const std::string& name,
}
void Server::add_socket(
const std::string& name,
const std::string& addr_str,
int fd,
GameVersion version,
ServerBehavior behavior) {
this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd),
forward_as_tuple(this, name, fd, version, behavior));
forward_as_tuple(this, addr_str, fd, version, behavior));
}
shared_ptr<Client> Server::get_client() const {