From 28ad1a0c2576904b2a625bf13a2728ad90ee77b5 Mon Sep 17 00:00:00 2001 From: CPunch Date: Mon, 24 Aug 2020 13:23:28 -0500 Subject: [PATCH] fix windows support for sockets --- src/CNProtocol.cpp | 16 +++++++--------- src/CNProtocol.hpp | 9 +++++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/CNProtocol.cpp b/src/CNProtocol.cpp index ac6b16a..d1e4a2a 100644 --- a/src/CNProtocol.cpp +++ b/src/CNProtocol.cpp @@ -1,10 +1,6 @@ #include "CNProtocol.hpp" #include "CNStructs.hpp" -#ifdef _MSC_VER - #define _WINSOCK_DEPRECATED_NO_WARNINGS -#endif - // ========================================================[[ CNSocketEncryption ]]======================================================== // literally C/P from the client and converted to C++ (does some byte swapping /shrug) @@ -78,11 +74,11 @@ bool CNSocket::sendData(uint8_t* data, int size) { while (sentBytes < size) { int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined if (SOCKETERROR(sent)) { - if (errno == EAGAIN && maxTries > 0) { + if (OF_ERRNO == OF_EWOULD && maxTries > 0) { maxTries--; continue; // try again } - std::cout << "[FATAL] SOCKET ERROR: " << errno << std::endl; + std::cout << "[FATAL] SOCKET ERROR: " << OF_ERRNO << std::endl; return false; // error occured while sending bytes } sentBytes += sent; @@ -122,7 +118,7 @@ void CNSocket::kill() { #endif } -// we don't own buf +// we don't own buf, TODO: queue packets up to send in step() void CNSocket::sendPacket(void* buf, uint32_t type, size_t size) { if (!alive) return; @@ -167,6 +163,8 @@ void CNSocket::setActiveKey(ACTIVEKEY key) { } void CNSocket::step() { + // read step + if (readSize <= 0) { // we aren't reading a packet yet, try to start looking for one int recved = recv(sock, (buffer_t*)readBuffer, sizeof(int32_t), 0); @@ -181,7 +179,7 @@ void CNSocket::step() { // we'll just leave bufferIndex at 0 since we already have the packet size, it's safe to overwrite those bytes activelyReading = true; - } else if (errno != EAGAIN) { + } else if (OF_ERRNO != OF_EWOULD) { // serious socket issue, disconnect connection kill(); return; @@ -193,7 +191,7 @@ void CNSocket::step() { int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0); if (!SOCKETERROR(recved)) readBufferIndex += recved; - else if (errno != EAGAIN) { + else if (OF_ERRNO != OF_EWOULD) { // serious socket issue, disconnect connection kill(); return; diff --git a/src/CNProtocol.hpp b/src/CNProtocol.hpp index 654bd81..a260901 100644 --- a/src/CNProtocol.hpp +++ b/src/CNProtocol.hpp @@ -7,14 +7,16 @@ #include #include #ifdef _WIN32 -// windows (UNTESTED) +// windows + #define _WINSOCK_DEPRECATED_NO_WARNINGS #include #include #include #pragma comment(lib, "Ws2_32.lib") typedef char buffer_t; - //#define errno WSAGetLastError() + #define OF_ERRNO WSAGetLastError() + #define OF_EWOULD WSAEWOULDBLOCK #define SOCKETINVALID(x) (x == INVALID_SOCKET) #define SOCKETERROR(x) (x == SOCKET_ERROR) #else @@ -27,6 +29,8 @@ typedef int SOCKET; typedef void buffer_t; + #define OF_ERRNO errno + #define OF_EWOULD EWOULDBLOCK #define SOCKETINVALID(x) (x < 0) #define SOCKETERROR(x) (x == -1) #endif @@ -108,6 +112,7 @@ private: ACTIVEKEY activeKey; bool sendData(uint8_t* data, int size); + int recvData(buffer_t* data, int size); public: SOCKET sock;