decode error code

This commit is contained in:
Erik Fröjdh
2026-06-23 19:14:52 +02:00
parent c99f28924f
commit d697edddf6
3 changed files with 83 additions and 1 deletions
+2
View File
@@ -88,6 +88,8 @@ class DataSocket {
private:
int sockfd_ = -1;
int fnum_{0};
std::string_view errno_name(int e);
};
}; // namespace sls
+46 -1
View File
@@ -73,7 +73,7 @@ int DataSocket::Receive(void *buffer, size_t size) {
if (this_read == 0)
ss << ": connection closed by peer (EOF)";
else if (this_read < 0)
ss << ": read error: " << std::strerror(err);
ss << ": read error: " << std::strerror(err) << " (" << errno_name(err) << ")";
ss << " after " << timer.elapsed_ms() << " ms";
throw SocketError(ss.str());
}
@@ -165,4 +165,49 @@ void DataSocket::shutDownSocket() {
void DataSocket::shutdown() { ::shutdown(sockfd_, SHUT_RDWR); }
std::string_view DataSocket::errno_name(int e) {
switch (e) {
#ifdef EACCES
case EACCES: return "EACCES";
#endif
#ifdef EAGAIN
case EAGAIN: return "EAGAIN";
#endif
#ifdef EBADF
case EBADF: return "EBADF";
#endif
#ifdef ECONNABORTED
case ECONNABORTED: return "ECONNABORTED";
#endif
#ifdef ECONNREFUSED
case ECONNREFUSED: return "ECONNREFUSED";
#endif
#ifdef ECONNRESET
case ECONNRESET: return "ECONNRESET";
#endif
#ifdef EINPROGRESS
case EINPROGRESS: return "EINPROGRESS";
#endif
#ifdef EINTR
case EINTR: return "EINTR";
#endif
#ifdef EINVAL
case EINVAL: return "EINVAL";
#endif
#ifdef EPIPE
case EPIPE: return "EPIPE";
#endif
#ifdef ETIMEDOUT
case ETIMEDOUT: return "ETIMEDOUT";
#endif
#ifdef EWOULDBLOCK
#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK: return "EWOULDBLOCK";
#endif
#endif
default:
return "UNKNOWN_ERRNO";
}
}
} // namespace sls
+35
View File
@@ -10,6 +10,7 @@
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>
namespace sls {
@@ -24,6 +25,18 @@ std::vector<char> echo_server(uint16_t port, size_t bytes_to_send,
std::vector<char> buffer(100, '\0');
s.Receive(buffer.data(), buffer.size());
if (port==1960){
struct linger ling = {
.l_onoff = 1,
.l_linger = 0
};
auto fd = s.getSocketId();
setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof ling);
close(fd);
return buffer;
}
if (bytes_to_send > 0) {
std::vector<char> to_send(bytes_to_send, '\0');
to_send[0] = 'O';
@@ -126,4 +139,26 @@ TEST_CASE("Receiving with a socket error throws and reports the error",
CHECK_THAT(error_message, Catch::Matchers::Contains("read error:"));
}
TEST_CASE("Socket crash?", "[support]") {
std::vector<char> received_message(100, '\0');
std::vector<char> sent_message(100, '\0');
const char m[]{"some message"};
std::copy(std::begin(m), std::end(m), sent_message.data());
auto s = std::async(std::launch::async, echo_server, 1960, 100,
std::chrono::milliseconds(0));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto client = DetectorSocket("localhost", 1960);
client.Send(sent_message.data(), sent_message.size());
REQUIRE_THROWS(client.Receive(received_message.data(), received_message.size()));
// client.close();
}
} // namespace sls