mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-26 06:50:06 +00:00
Print informative error messages for socket operations
Windows implementation pending.
This commit is contained in:
parent
86e0b1bc13
commit
ada8af0b82
@ -78,7 +78,7 @@ bool CNSocket::sendData(uint8_t* data, int size) {
|
|||||||
maxTries--;
|
maxTries--;
|
||||||
continue; // try again
|
continue; // try again
|
||||||
}
|
}
|
||||||
std::cout << "[FATAL] SOCKET ERROR: " << OF_ERRNO << std::endl;
|
printSocketError("send");
|
||||||
return false; // error occured while sending bytes
|
return false; // error occured while sending bytes
|
||||||
}
|
}
|
||||||
sentBytes += sent;
|
sentBytes += sent;
|
||||||
@ -180,6 +180,7 @@ void CNSocket::step() {
|
|||||||
activelyReading = true;
|
activelyReading = true;
|
||||||
} else if (OF_ERRNO != OF_EWOULD) {
|
} else if (OF_ERRNO != OF_EWOULD) {
|
||||||
// serious socket issue, disconnect connection
|
// serious socket issue, disconnect connection
|
||||||
|
printSocketError("recv");
|
||||||
kill();
|
kill();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -192,6 +193,7 @@ void CNSocket::step() {
|
|||||||
readBufferIndex += recved;
|
readBufferIndex += recved;
|
||||||
else if (OF_ERRNO != OF_EWOULD) {
|
else if (OF_ERRNO != OF_EWOULD) {
|
||||||
// serious socket issue, disconnect connection
|
// serious socket issue, disconnect connection
|
||||||
|
printSocketError("recv");
|
||||||
kill();
|
kill();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -214,6 +216,14 @@ void CNSocket::step() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printSocketError(const char *call) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: ycc plz implement
|
||||||
|
#else
|
||||||
|
perror(call);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
bool setSockNonblocking(SOCKET listener, SOCKET newSock) {
|
bool setSockNonblocking(SOCKET listener, SOCKET newSock) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
unsigned long mode = 1;
|
unsigned long mode = 1;
|
||||||
@ -221,6 +231,7 @@ bool setSockNonblocking(SOCKET listener, SOCKET newSock) {
|
|||||||
#else
|
#else
|
||||||
if (fcntl(newSock, F_SETFL, (fcntl(newSock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
if (fcntl(newSock, F_SETFL, (fcntl(newSock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
||||||
#endif
|
#endif
|
||||||
|
printSocketError("fcntl");
|
||||||
std::cerr << "[WARN] OpenFusion: fcntl failed on new connection" << std::endl;
|
std::cerr << "[WARN] OpenFusion: fcntl failed on new connection" << std::endl;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
shutdown(newSock, SD_BOTH);
|
shutdown(newSock, SD_BOTH);
|
||||||
@ -241,6 +252,7 @@ void CNServer::init() {
|
|||||||
// create socket file descriptor
|
// create socket file descriptor
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (SOCKETINVALID(sock)) {
|
if (SOCKETINVALID(sock)) {
|
||||||
|
printSocketError("socket");
|
||||||
std::cerr << "[FATAL] OpenFusion: socket failed" << std::endl;
|
std::cerr << "[FATAL] OpenFusion: socket failed" << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -253,6 +265,7 @@ void CNServer::init() {
|
|||||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != 0) {
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != 0) {
|
||||||
#endif
|
#endif
|
||||||
std::cerr << "[FATAL] OpenFusion: setsockopt failed" << std::endl;
|
std::cerr << "[FATAL] OpenFusion: setsockopt failed" << std::endl;
|
||||||
|
printSocketError("setsockopt");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
address.sin_family = AF_INET;
|
address.sin_family = AF_INET;
|
||||||
@ -264,11 +277,13 @@ void CNServer::init() {
|
|||||||
// Bind to the port
|
// Bind to the port
|
||||||
if (SOCKETERROR(bind(sock, (struct sockaddr *)&address, addressSize))) {
|
if (SOCKETERROR(bind(sock, (struct sockaddr *)&address, addressSize))) {
|
||||||
std::cerr << "[FATAL] OpenFusion: bind failed" << std::endl;
|
std::cerr << "[FATAL] OpenFusion: bind failed" << std::endl;
|
||||||
|
printSocketError("bind");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SOCKETERROR(listen(sock, SOMAXCONN))) {
|
if (SOCKETERROR(listen(sock, SOMAXCONN))) {
|
||||||
std::cerr << "[FATAL] OpenFusion: listen failed" << std::endl;
|
std::cerr << "[FATAL] OpenFusion: listen failed" << std::endl;
|
||||||
|
printSocketError("listen");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,6 +294,7 @@ void CNServer::init() {
|
|||||||
#else
|
#else
|
||||||
if (fcntl(sock, F_SETFL, (fcntl(sock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
if (fcntl(sock, F_SETFL, (fcntl(sock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
||||||
#endif
|
#endif
|
||||||
|
printSocketError("fcntl");
|
||||||
std::cerr << "[FATAL] OpenFusion: fcntl failed" << std::endl;
|
std::cerr << "[FATAL] OpenFusion: fcntl failed" << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -317,6 +333,7 @@ void CNServer::start() {
|
|||||||
continue;
|
continue;
|
||||||
#endif
|
#endif
|
||||||
std::cout << "[FATAL] poll() returned error" << std::endl;
|
std::cout << "[FATAL] poll() returned error" << std::endl;
|
||||||
|
printSocketError("poll");
|
||||||
terminate(0);
|
terminate(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,8 +354,10 @@ void CNServer::start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SOCKET newConnectionSocket = accept(sock, (struct sockaddr *)&address, (socklen_t*)&addressSize);
|
SOCKET newConnectionSocket = accept(sock, (struct sockaddr *)&address, (socklen_t*)&addressSize);
|
||||||
if (SOCKETINVALID(newConnectionSocket))
|
if (SOCKETINVALID(newConnectionSocket)) {
|
||||||
|
printSocketError("accept");
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!setSockNonblocking(sock, newConnectionSocket))
|
if (!setSockNonblocking(sock, newConnectionSocket))
|
||||||
continue;
|
continue;
|
||||||
|
@ -118,6 +118,7 @@ inline bool validInVarPacket(size_t base, int32_t npayloads, size_t plsize, size
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printSocketError(const char *func);
|
||||||
bool setSockNonblocking(SOCKET listener, SOCKET newSock);
|
bool setSockNonblocking(SOCKET listener, SOCKET newSock);
|
||||||
|
|
||||||
namespace CNSocketEncryption {
|
namespace CNSocketEncryption {
|
||||||
|
@ -15,6 +15,7 @@ SOCKET Monitor::init() {
|
|||||||
listener = socket(AF_INET, SOCK_STREAM, 0);
|
listener = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (SOCKETERROR(listener)) {
|
if (SOCKETERROR(listener)) {
|
||||||
std::cout << "Failed to create monitor socket" << std::endl;
|
std::cout << "Failed to create monitor socket" << std::endl;
|
||||||
|
printSocketError("socket");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +26,7 @@ SOCKET Monitor::init() {
|
|||||||
#endif
|
#endif
|
||||||
if (SOCKETERROR(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))) {
|
if (SOCKETERROR(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))) {
|
||||||
std::cout << "Failed to set SO_REUSEADDR on monitor socket" << std::endl;
|
std::cout << "Failed to set SO_REUSEADDR on monitor socket" << std::endl;
|
||||||
|
printSocketError("setsockopt");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,11 +36,13 @@ SOCKET Monitor::init() {
|
|||||||
|
|
||||||
if (SOCKETERROR(bind(listener, (struct sockaddr*)&address, sizeof(address)))) {
|
if (SOCKETERROR(bind(listener, (struct sockaddr*)&address, sizeof(address)))) {
|
||||||
std::cout << "Failed to bind to monitor port" << std::endl;
|
std::cout << "Failed to bind to monitor port" << std::endl;
|
||||||
|
printSocketError("bind");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SOCKETERROR(listen(listener, SOMAXCONN))) {
|
if (SOCKETERROR(listen(listener, SOMAXCONN))) {
|
||||||
std::cout << "Failed to listen on monitor port" << std::endl;
|
std::cout << "Failed to listen on monitor port" << std::endl;
|
||||||
|
printSocketError("listen");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +53,7 @@ SOCKET Monitor::init() {
|
|||||||
if (fcntl(listener, F_SETFL, (fcntl(listener, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
if (fcntl(listener, F_SETFL, (fcntl(listener, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
||||||
#endif
|
#endif
|
||||||
std::cerr << "[FATAL] OpenFusion: fcntl failed" << std::endl;
|
std::cerr << "[FATAL] OpenFusion: fcntl failed" << std::endl;
|
||||||
|
printSocketError("fcntl");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +71,8 @@ static bool transmit(std::list<SOCKET>::iterator& it, char *buff, int len) {
|
|||||||
while (n < len) {
|
while (n < len) {
|
||||||
n += send(sock, buff+n, len-n, 0);
|
n += send(sock, buff+n, len-n, 0);
|
||||||
if (SOCKETERROR(n)) {
|
if (SOCKETERROR(n)) {
|
||||||
|
printSocketError("send");
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
shutdown(sock, SD_BOTH);
|
shutdown(sock, SD_BOTH);
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
@ -90,7 +97,7 @@ void Monitor::tick(CNServer *serv, time_t delta) {
|
|||||||
int n;
|
int n;
|
||||||
|
|
||||||
auto it = sockets.begin();
|
auto it = sockets.begin();
|
||||||
outer:
|
outer:
|
||||||
while (it != sockets.end()) {
|
while (it != sockets.end()) {
|
||||||
if (!transmit(it, (char*)"begin\n", 6))
|
if (!transmit(it, (char*)"begin\n", 6))
|
||||||
continue;
|
continue;
|
||||||
@ -129,8 +136,10 @@ bool Monitor::acceptConnection(SOCKET fd, uint16_t revents) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int sock = accept(listener, (struct sockaddr*)&address, &len);
|
int sock = accept(listener, (struct sockaddr*)&address, &len);
|
||||||
if (SOCKETERROR(sock))
|
if (SOCKETERROR(sock)) {
|
||||||
|
printSocketError("accept");
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
setSockNonblocking(listener, sock);
|
setSockNonblocking(listener, sock);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user