Correctly format WinSock errors with a message

This has lead to many(!) issues in the past, and makes
life harder for everyone doing support. This will display the
error code, together with an error message windows provides.
This commit is contained in:
Lion Kortlepel
2022-05-05 00:53:42 +02:00
parent d50980b10f
commit be219d289e
2 changed files with 30 additions and 5 deletions

View File

@@ -38,6 +38,7 @@ private:
void UpdateUl(bool D, const std::string& msg);
std::unique_ptr<sockaddr_in> UDPSockAddress;
void ServerParser(const std::string& Data);
static std::string GetSocketApiError();
void TCPSend(const std::string& Data);
void UDPParser(std::string Packet);
void SendLarge(std::string Data);

View File

@@ -30,7 +30,7 @@ void Server::TCPClientMain() {
SOCKADDR_IN ServerAddr;
TCPSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(TCPSocket == -1) {
LOG(ERROR) << "Socket failed! Error code: " << WSAGetLastError();
LOG(ERROR) << "Socket failed! Error code: " << GetSocketApiError();
return;
}
const char optval = 0;
@@ -41,10 +41,10 @@ void Server::TCPClientMain() {
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);
int status = connect(TCPSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
status = connect(TCPSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
if(status != 0){
UStatus = "Connection Failed!";
LOG(ERROR) << "Connect failed! Error code: " << WSAGetLastError();
LOG(ERROR) << "Connect failed! Error code: " << GetSocketApiError();
Close();
return;
}
@@ -74,7 +74,7 @@ void Server::UDPSend(std::string Data) {
}
std::string Packet = char(ClientID+1) + std::string(":") + Data;
int sendOk = sendto(UDPSocket, Packet.c_str(), int(Packet.size()), 0, (sockaddr*)UDPSockAddress.get(), sizeof(sockaddr_in));
if (sendOk == SOCKET_ERROR)LOG(ERROR) << "UDP Socket Error Code : " << WSAGetLastError();
if (sendOk == SOCKET_ERROR)LOG(ERROR) << "UDP Socket Error Code : " << GetSocketApiError();
}
void Server::UDPParser(std::string Packet) {
@@ -141,6 +141,30 @@ void Server::SendLarge(std::string Data) {
TCPSend(Data);
}
static std::string Server::GetSocketApiError() {
// This will provide us with the error code and an error message, all in one.
// The resulting format is "<CODE> - <MESSAGE>"
int err;
char msgbuf[256];
msgbuf[0] = '\0';
err = WSAGetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf,
sizeof(msgbuf),
nullptr);
if (*msgbuf) {
return std::to_string(WSAGetLastError()) + " - " + std::string(msgbuf);
} else {
return std::to_string(WSAGetLastError());
}
}
void Server::ServerSend(std::string Data, bool Rel) {
if(Terminate || Data.empty())return;
char C = 0;
@@ -230,7 +254,7 @@ bool Server::CheckBytes(int32_t Bytes) {
Terminate = true;
return false;
}else if (Bytes < 0) {
//debug("(TCP CB) recv failed with error: " + std::to_string(WSAGetLastError()));
//debug("(TCP CB) recv failed with error: " + GetSocketApiError();
KillSocket(TCPSocket);
Terminate = true;
return false;