use phosg namespace

This commit is contained in:
Martin Michelsen
2024-07-28 11:54:41 -07:00
parent 5523388ad4
commit 1bfbf09891
116 changed files with 2969 additions and 3088 deletions
+51 -53
View File
@@ -19,13 +19,13 @@ ReplaySession::Event::Event(Type type, uint64_t client_id, size_t line_num)
string ReplaySession::Event::str() const {
string ret;
if (this->type == Type::CONNECT) {
ret = string_printf("Event[%" PRIu64 ", CONNECT", this->client_id);
ret = phosg::string_printf("Event[%" PRIu64 ", CONNECT", this->client_id);
} else if (this->type == Type::DISCONNECT) {
ret = string_printf("Event[%" PRIu64 ", DISCONNECT", this->client_id);
ret = phosg::string_printf("Event[%" PRIu64 ", DISCONNECT", this->client_id);
} else if (this->type == Type::SEND) {
ret = string_printf("Event[%" PRIu64 ", SEND %04zX", this->client_id, this->data.size());
ret = phosg::string_printf("Event[%" PRIu64 ", SEND %04zX", this->client_id, this->data.size());
} else if (this->type == Type::RECEIVE) {
ret = string_printf("Event[%" PRIu64 ", RECEIVE %04zX", this->client_id, this->data.size());
ret = phosg::string_printf("Event[%" PRIu64 ", RECEIVE %04zX", this->client_id, this->data.size());
}
if (this->allow_size_disparity) {
ret += ", size disparity allowed";
@@ -33,7 +33,7 @@ string ReplaySession::Event::str() const {
if (this->complete) {
ret += ", done";
}
ret += string_printf(", ev-line %zu]", this->line_num);
ret += phosg::string_printf(", ev-line %zu]", this->line_num);
return ret;
}
@@ -48,11 +48,10 @@ ReplaySession::Client::Client(
&ReplaySession::dispatch_on_command_received,
&ReplaySession::dispatch_on_error,
session,
string_printf("R-%" PRIX64, this->id)) {}
phosg::string_printf("R-%" PRIX64, this->id)) {}
string ReplaySession::Client::str() const {
return string_printf("Client[%" PRIu64 ", T-%hu, %s]",
this->id, this->port, name_for_enum(this->version));
return phosg::string_printf("Client[%" PRIu64 ", T-%hu, %s]", this->id, this->port, phosg::name_for_enum(this->version));
}
shared_ptr<ReplaySession::Event> ReplaySession::create_event(
@@ -88,14 +87,14 @@ void ReplaySession::check_for_password(shared_ptr<const Event> ev) const {
auto check_pw = [&](const string& pw) {
if (this->require_basic_credentials && !string_is_basic(pw)) {
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(string_printf("(ev-line %zu) sent password is incorrect", ev->line_num));
phosg::print_data(stderr, ev->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(phosg::string_printf("(ev-line %zu) sent password is incorrect", ev->line_num));
}
};
auto check_ak = [&](const string& ak) {
if (this->require_basic_credentials && !ak.empty() && !string_is_basic(ak)) {
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(string_printf("(ev-line %zu) sent access key is incorrect", ev->line_num));
phosg::print_data(stderr, ev->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(phosg::string_printf("(ev-line %zu) sent access key is incorrect", ev->line_num));
}
};
auto check_either = [&](const string& s) {
@@ -472,8 +471,8 @@ ReplaySession::ReplaySession(
size_t num_events = 0;
while (!feof(input_log)) {
line_num++;
string line = fgets(input_log);
if (ends_with(line, "\n")) {
string line = phosg::fgets(input_log);
if (phosg::ends_with(line, "\n")) {
line.resize(line.size() - 1);
}
if (line.empty()) {
@@ -481,12 +480,11 @@ ReplaySession::ReplaySession(
}
if (parsing_command.get()) {
string expected_start = string_printf("%04zX |", parsing_command->data.size());
if (starts_with(line, expected_start)) {
string expected_start = phosg::string_printf("%04zX |", parsing_command->data.size());
if (phosg::starts_with(line, expected_start)) {
// Parse out the hex part of the hex/ASCII dump
string mask_bytes;
string data_bytes = parse_data_string(
line.substr(expected_start.size(), 16 * 3 + 1), &mask_bytes);
string data_bytes = phosg::parse_data_string(line.substr(expected_start.size(), 16 * 3 + 1), &mask_bytes);
parsing_command->data += data_bytes;
parsing_command->mask += mask_bytes;
continue;
@@ -500,30 +498,30 @@ ReplaySession::ReplaySession(
}
}
if (starts_with(line, "I ")) {
if (phosg::starts_with(line, "I ")) {
// I <pid/ts> - [Server] Client connected: C-%X on fd %d via %d (T-%hu-%s-%s-%s)
// I <pid/ts> - [Server] Client connected: C-%X on virtual connection %p via T-%hu-VI
size_t offset = line.find(" - [Server] Client connected: C-");
if (offset != string::npos) {
auto tokens = split(line, ' ');
auto tokens = phosg::split(line, ' ');
if (tokens.size() != 15) {
throw runtime_error(string_printf("(ev-line %zu) client connection message has incorrect token count", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) client connection message has incorrect token count", line_num));
}
if (!starts_with(tokens[8], "C-")) {
throw runtime_error(string_printf("(ev-line %zu) client connection message missing client ID token", line_num));
if (!phosg::starts_with(tokens[8], "C-")) {
throw runtime_error(phosg::string_printf("(ev-line %zu) client connection message missing client ID token", line_num));
}
auto listen_tokens = split(tokens[14], '-');
auto listen_tokens = phosg::split(tokens[14], '-');
if (listen_tokens.size() < 4) {
throw runtime_error(string_printf("(ev-line %zu) client connection message listening socket token format is incorrect", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) client connection message listening socket token format is incorrect", line_num));
}
auto c = make_shared<Client>(
this,
stoull(tokens[8].substr(2), nullptr, 16),
stoul(listen_tokens[1], nullptr, 10),
enum_for_name<Version>(listen_tokens[2].c_str()));
phosg::enum_for_name<Version>(listen_tokens[2].c_str()));
if (!this->clients.emplace(c->id, c).second) {
throw runtime_error(string_printf("(ev-line %zu) duplicate client ID in input log", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) duplicate client ID in input log", line_num));
}
this->create_event(Event::Type::CONNECT, c, line_num);
num_events++;
@@ -533,23 +531,23 @@ ReplaySession::ReplaySession(
// I <pid/ts> - [Server] Disconnecting C-%X on fd %d
offset = line.find(" - [Server] Client disconnected: C-");
if (offset != string::npos) {
auto tokens = split(line, ' ');
auto tokens = phosg::split(line, ' ');
if (tokens.size() < 9) {
throw runtime_error(string_printf("(ev-line %zu) client disconnection message has incorrect token count", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) client disconnection message has incorrect token count", line_num));
}
if (!starts_with(tokens[8], "C-")) {
throw runtime_error(string_printf("(ev-line %zu) client disconnection message missing client ID token", line_num));
if (!phosg::starts_with(tokens[8], "C-")) {
throw runtime_error(phosg::string_printf("(ev-line %zu) client disconnection message missing client ID token", line_num));
}
uint64_t client_id = stoul(tokens[8].substr(2), nullptr, 16);
try {
auto& c = this->clients.at(client_id);
if (c->disconnect_event.get()) {
throw runtime_error(string_printf("(ev-line %zu) client has multiple disconnect events", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) client has multiple disconnect events", line_num));
}
c->disconnect_event = this->create_event(Event::Type::DISCONNECT, c, line_num);
num_events++;
} catch (const out_of_range&) {
throw runtime_error(string_printf("(ev-line %zu) unknown disconnecting client ID in input log", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) unknown disconnecting client ID in input log", line_num));
}
continue;
}
@@ -561,9 +559,9 @@ ReplaySession::ReplaySession(
offset = line.find(" - [Commands] Received from C-");
}
if (offset != string::npos) {
auto tokens = split(line, ' ');
auto tokens = phosg::split(line, ' ');
if (tokens.size() < 10) {
throw runtime_error(string_printf("(ev-line %zu) command header line too short", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) command header line too short", line_num));
}
bool from_client = (tokens[6] == "Received");
uint64_t client_id = stoull(tokens[8].substr(2), nullptr, 16);
@@ -574,7 +572,7 @@ ReplaySession::ReplaySession(
line_num);
num_events++;
} catch (const out_of_range&) {
throw runtime_error(string_printf("(ev-line %zu) input log contains command for missing client", line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) input log contains command for missing client", line_num));
}
continue;
}
@@ -605,7 +603,7 @@ void ReplaySession::update_timeout_event() {
event_new(this->base.get(), -1, EV_TIMEOUT, this->dispatch_on_timeout, this),
event_free);
}
struct timeval tv = usecs_to_timeval(3000000);
struct timeval tv = phosg::usecs_to_timeval(3000000);
event_add(this->timeout_ev.get(), &tv);
}
@@ -624,7 +622,7 @@ void ReplaySession::execute_pending_events() {
switch (this->first_event->type) {
case Event::Type::CONNECT: {
if (c->channel.connected()) {
throw runtime_error(string_printf("(ev-line %zu) connect event on already-connected client", this->first_event->line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) connect event on already-connected client", this->first_event->line_num));
}
struct bufferevent* bevs[2];
@@ -638,17 +636,17 @@ void ReplaySession::execute_pending_events() {
port_config = this->state->number_to_port_config.at(c->port);
} catch (const out_of_range&) {
bufferevent_free(bevs[1]);
throw runtime_error(string_printf("(ev-line %zu) client connected to port missing from configuration", this->first_event->line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) client connected to port missing from configuration", this->first_event->line_num));
}
if (port_config->behavior == ServerBehavior::PROXY_SERVER) {
// TODO: We should support this at some point in the future
throw runtime_error(string_printf("(ev-line %zu) client connected to proxy server", this->first_event->line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) client connected to proxy server", this->first_event->line_num));
} else if (this->state->game_server.get()) {
this->state->game_server->connect_virtual_client(bevs[1], 0, 0x20202020,
1025, c->port, port_config->version, port_config->behavior);
} else {
throw runtime_error(string_printf("(ev-line %zu) no server available for connection", this->first_event->line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) no server available for connection", this->first_event->line_num));
bufferevent_free(bevs[1]);
}
break;
@@ -659,7 +657,7 @@ void ReplaySession::execute_pending_events() {
break;
case Event::Type::SEND:
if (!c->channel.connected()) {
throw runtime_error(string_printf("(ev-line %zu) send event attempted on unconnected client", this->first_event->line_num));
throw runtime_error(phosg::string_printf("(ev-line %zu) send event attempted on unconnected client", this->first_event->line_num));
}
c->channel.send(this->first_event->data);
this->commands_sent++;
@@ -706,7 +704,7 @@ void ReplaySession::dispatch_on_error(Channel& ch, short events) {
void ReplaySession::on_command_received(
shared_ptr<Client> c, uint16_t command, uint32_t flag, string& data) {
// TODO: Use the iovec form of print_data here instead of
// TODO: Use the iovec form of phosg::print_data here instead of
// prepend_command_header (which copies the string)
string full_command = prepend_command_header(
c->version, c->channel.crypt_in.get(), command, flag, data);
@@ -714,25 +712,25 @@ void ReplaySession::on_command_received(
this->bytes_received += full_command.size();
if (c->receive_events.empty()) {
print_data(stderr, full_command, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stderr, full_command, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
throw runtime_error("received unexpected command for client");
}
auto& ev = c->receive_events.front();
if ((full_command.size() != ev->data.size()) && !ev->allow_size_disparity) {
replay_log.error("Expected command:");
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stderr, ev->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
replay_log.error("Received command:");
print_data(stderr, full_command, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(string_printf("(ev-line %zu) received command sizes do not match", ev->line_num));
phosg::print_data(stderr, full_command, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(phosg::string_printf("(ev-line %zu) received command sizes do not match", ev->line_num));
}
for (size_t x = 0; x < min<size_t>(full_command.size(), ev->data.size()); x++) {
if ((full_command[x] & ev->mask[x]) != (ev->data[x] & ev->mask[x])) {
replay_log.error("Expected command:");
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stderr, ev->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
replay_log.error("Received command:");
print_data(stderr, full_command, 0, ev->data.data(), PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(string_printf("(ev-line %zu) received command data does not match expected data", ev->line_num));
phosg::print_data(stderr, full_command, 0, ev->data.data(), phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
throw runtime_error(phosg::string_printf("(ev-line %zu) received command data does not match expected data", ev->line_num));
}
}
@@ -792,15 +790,15 @@ void ReplaySession::on_command_received(
void ReplaySession::on_error(shared_ptr<Client> c, short events) {
if (events & BEV_EVENT_ERROR) {
throw runtime_error(string_printf("C-%" PRIX64 " caused stream error", c->id));
throw runtime_error(phosg::string_printf("C-%" PRIX64 " caused stream error", c->id));
}
if (events & BEV_EVENT_EOF) {
if (!c->disconnect_event.get()) {
throw runtime_error(string_printf(
throw runtime_error(phosg::string_printf(
"C-%" PRIX64 " disconnected, but has no disconnect event", c->id));
}
if (!c->receive_events.empty()) {
throw runtime_error(string_printf(
throw runtime_error(phosg::string_printf(
"C-%" PRIX64 " disconnected, but has pending receive events", c->id));
}
c->disconnect_event->complete = true;