Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 27, 2025
1 parent a624009 commit f2df702
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions cpp/s2p/s2p_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

#include "s2p_server.h"
#include <cassert>
#include <csignal>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <spdlog/spdlog.h>

using namespace spdlog;

string S2pServer::Init(int port)
{
Expand All @@ -23,12 +21,12 @@ string S2pServer::Init(int port)

server_socket = socket(PF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
return fmt::format("Can't create server socket: {}", strerror(errno));
return "Can't create server socket: " + string(strerror(errno));
}

if (const int enable = 1; setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) {
Stop();
return fmt::format("Can't reuse socket: {}", strerror(errno));
return "Can't reuse socket: " + string(strerror(errno));
}

sockaddr_in server = { };
Expand All @@ -38,7 +36,7 @@ string S2pServer::Init(int port)
if (bind(server_socket, reinterpret_cast<const sockaddr*>(&server), // NOSONAR bit_cast is not supported by the bullseye compiler
static_cast<socklen_t>(sizeof(sockaddr_in))) < 0) {
Stop();
return fmt::format("Port {} is in use, s2p may already be running", port);
return "Port " + to_string(port) + " is in use, s2p may already be running";
}

if (listen(server_socket, 2) == -1) {
Expand Down

0 comments on commit f2df702

Please sign in to comment.