logger added
This commit is contained in:
+16
-9
@@ -14,6 +14,7 @@
|
||||
#include "net_util.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "utils.hpp"
|
||||
#include "logger.hpp"
|
||||
|
||||
|
||||
Server::Server(const Config& config)
|
||||
@@ -51,36 +52,39 @@ void Server::setup() {
|
||||
throw std::runtime_error("Error listening for connections");
|
||||
}
|
||||
|
||||
std::cout << "Server started on " << address << ":" << port << "\n";
|
||||
Logger::log_info("Server started on " + address + ":" + std::to_string(port));
|
||||
}
|
||||
|
||||
int32_t Server::handle_connection(int connectionfd) {
|
||||
std::cout << "Waiting for data from " << connectionfd << "\n";
|
||||
Logger::log_debug("Waiting for data from fd=" + std::to_string(connectionfd));
|
||||
|
||||
uint32_t len_net = 0;
|
||||
errno = 0;
|
||||
if (read_full(connectionfd, &len_net, sizeof(len_net)) != 0) {
|
||||
print_error(errno == 0 ? "EOF from client" : "Error reading length");
|
||||
if (errno == 0) {
|
||||
Logger::log_info("Client disconnected (EOF) from fd=" + std::to_string(connectionfd));
|
||||
} else {
|
||||
Logger::log_error("Error reading length from fd=" + std::to_string(connectionfd) + ": " + std::strerror(errno));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t len = ntohl(len_net);
|
||||
|
||||
if (len > k_max_message_size) {
|
||||
print_error("Message length is too long");
|
||||
std::cerr << len << " > " << k_max_message_size << "\n";
|
||||
Logger::log_error("Message length is too long: " + std::to_string(len) + " (max: " + std::to_string(k_max_message_size) + ")");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string message(len, '\0');
|
||||
if (len > 0) {
|
||||
if (read_full(connectionfd, message.data(), len) != 0) {
|
||||
print_error("Error reading payload");
|
||||
Logger::log_error("Error reading payload from fd=" + std::to_string(connectionfd));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Client says: " << message << "\n";
|
||||
Logger::log_info("Client (fd=" + std::to_string(connectionfd) + ") says: " + message);
|
||||
|
||||
const std::string reply = "world";
|
||||
uint32_t reply_len_net = htonl(static_cast<uint32_t>(reply.size()));
|
||||
@@ -99,16 +103,19 @@ void Server::run() {
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
int connectionfd = accept(sockfd, (struct sockaddr *)&client_addr, &addrlen);
|
||||
if (connectionfd < 0) {
|
||||
Logger::log_error("accept() error: " + std::string(std::strerror(errno)));
|
||||
continue;
|
||||
}
|
||||
std::cout << "Accepted connection from " << connectionfd << "\n";
|
||||
|
||||
Logger::log_info("Accepted connection from " + std::string(inet_ntoa(client_addr.sin_addr)) + ":" + std::to_string(ntohs(client_addr.sin_port)) + " (fd=" + std::to_string(connectionfd) + ")");
|
||||
|
||||
while (true) {
|
||||
int32_t err = handle_connection(connectionfd);
|
||||
if (err) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << "Closing connection from " << connectionfd << "\n";
|
||||
Logger::log_info("Closing connection from " + std::to_string(connectionfd));
|
||||
close(connectionfd);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user