fix windows support for sockets

This commit is contained in:
CPunch 2020-08-24 13:23:28 -05:00
parent ff5f3966e3
commit 28ad1a0c25
2 changed files with 14 additions and 11 deletions

View File

@ -1,10 +1,6 @@
#include "CNProtocol.hpp" #include "CNProtocol.hpp"
#include "CNStructs.hpp" #include "CNStructs.hpp"
#ifdef _MSC_VER
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
// ========================================================[[ CNSocketEncryption ]]======================================================== // ========================================================[[ CNSocketEncryption ]]========================================================
// literally C/P from the client and converted to C++ (does some byte swapping /shrug) // 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) { while (sentBytes < size) {
int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined
if (SOCKETERROR(sent)) { if (SOCKETERROR(sent)) {
if (errno == EAGAIN && maxTries > 0) { if (OF_ERRNO == OF_EWOULD && maxTries > 0) {
maxTries--; maxTries--;
continue; // try again 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 return false; // error occured while sending bytes
} }
sentBytes += sent; sentBytes += sent;
@ -122,7 +118,7 @@ void CNSocket::kill() {
#endif #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) { void CNSocket::sendPacket(void* buf, uint32_t type, size_t size) {
if (!alive) if (!alive)
return; return;
@ -167,6 +163,8 @@ void CNSocket::setActiveKey(ACTIVEKEY key) {
} }
void CNSocket::step() { void CNSocket::step() {
// read step
if (readSize <= 0) { if (readSize <= 0) {
// we aren't reading a packet yet, try to start looking for one // we aren't reading a packet yet, try to start looking for one
int recved = recv(sock, (buffer_t*)readBuffer, sizeof(int32_t), 0); 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 // we'll just leave bufferIndex at 0 since we already have the packet size, it's safe to overwrite those bytes
activelyReading = true; activelyReading = true;
} else if (errno != EAGAIN) { } else if (OF_ERRNO != OF_EWOULD) {
// serious socket issue, disconnect connection // serious socket issue, disconnect connection
kill(); kill();
return; return;
@ -193,7 +191,7 @@ void CNSocket::step() {
int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0); int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0);
if (!SOCKETERROR(recved)) if (!SOCKETERROR(recved))
readBufferIndex += recved; readBufferIndex += recved;
else if (errno != EAGAIN) { else if (OF_ERRNO != OF_EWOULD) {
// serious socket issue, disconnect connection // serious socket issue, disconnect connection
kill(); kill();
return; return;

View File

@ -7,14 +7,16 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#ifdef _WIN32 #ifdef _WIN32
// windows (UNTESTED) // windows
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib") #pragma comment(lib, "Ws2_32.lib")
typedef char buffer_t; typedef char buffer_t;
//#define errno WSAGetLastError() #define OF_ERRNO WSAGetLastError()
#define OF_EWOULD WSAEWOULDBLOCK
#define SOCKETINVALID(x) (x == INVALID_SOCKET) #define SOCKETINVALID(x) (x == INVALID_SOCKET)
#define SOCKETERROR(x) (x == SOCKET_ERROR) #define SOCKETERROR(x) (x == SOCKET_ERROR)
#else #else
@ -27,6 +29,8 @@
typedef int SOCKET; typedef int SOCKET;
typedef void buffer_t; typedef void buffer_t;
#define OF_ERRNO errno
#define OF_EWOULD EWOULDBLOCK
#define SOCKETINVALID(x) (x < 0) #define SOCKETINVALID(x) (x < 0)
#define SOCKETERROR(x) (x == -1) #define SOCKETERROR(x) (x == -1)
#endif #endif
@ -108,6 +112,7 @@ private:
ACTIVEKEY activeKey; ACTIVEKEY activeKey;
bool sendData(uint8_t* data, int size); bool sendData(uint8_t* data, int size);
int recvData(buffer_t* data, int size);
public: public:
SOCKET sock; SOCKET sock;