Implement various WIN32 fixes

This commit is contained in:
Lion Kortlepel 2020-11-03 12:50:05 +01:00
parent f144d451c7
commit c5c21c43ad
5 changed files with 48 additions and 28 deletions

View File

@ -44,23 +44,15 @@ inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const ch
[[maybe_unused]] const char* condition_string, [[maybe_unused]] bool result) { [[maybe_unused]] const char* condition_string, [[maybe_unused]] bool result) {
if (!result) { if (!result) {
#if DEBUG #if DEBUG
std::stringstream ss; std::cout << std::flush << "(debug build) TID "
ss << std::this_thread::get_id(); << std::this_thread::get_id() << ": ASSERTION FAILED: at "
fprintf(stdout, << file << ":" << line << " \n\t-> in "
"(debug build) TID %s: %sASSERTION FAILED%s at %s%s:%u%s in \n\t-> in %s%s%s, Line %u: \n\t\t-> " << function << ", Line " << line << ": \n\t\t-> "
"Failed Condition: %s%s%s\n", << "Failed Condition: " << condition_string << std::endl;
ss.str().c_str(), ANSI_RED_BOLD, ANSI_RESET, ANSI_UNDERLINE, file, line, ANSI_RESET, std::cout << "... terminating ..." << std::endl;
ANSI_BOLD, function, ANSI_RESET, line, ANSI_RED, condition_string,
ANSI_RESET);
fprintf(stdout, "%s... terminating with SIGABRT ...%s\n", ANSI_BOLD, ANSI_RESET);
abort(); abort();
#else #else
char buf[2048]; std::cout << "Assertion '" << condition_string << "' failed, ignoring in release build" << std::endl;
sprintf(buf,
"%s=> ASSERTION `%s` FAILED IN RELEASE BUILD%s%s -> IGNORING FAILED ASSERTION "
"& HOPING IT WON'T CRASH%s",
ANSI_RED_BOLD, condition_string, ANSI_RESET, ANSI_RED, ANSI_RESET);
error(buf);
#endif #endif
} }
} }

View File

@ -17,8 +17,14 @@ struct Hold{
bool Done = false; bool Done = false;
}; };
bool Send(SOCKET TCPSock,std::string Data){ bool Send(SOCKET TCPSock,std::string Data){
ssize_t BytesSent; #ifdef WIN32
BytesSent = send(TCPSock, Data.c_str(), size_t(Data.size()), 0); int BytesSent;
int len = static_cast<int>(Data.size());
#else
int64_t BytesSent;
size_t len = Data.size();
#endif // WIN32
BytesSent = send(TCPSock, Data.c_str(), len, 0);
Data.clear(); Data.clear();
if (BytesSent <= 0)return false; if (BytesSent <= 0)return false;
return true; return true;
@ -27,7 +33,7 @@ std::string Rcv(SOCKET TCPSock){
char buf[6768]; char buf[6768];
size_t len = 6768; size_t len = 6768;
ZeroMemory(buf, len); ZeroMemory(buf, len);
ssize_t BytesRcv = recv(TCPSock, buf, len,0); int64_t BytesRcv = recv(TCPSock, buf, len,0);
if (BytesRcv <= 0)return ""; if (BytesRcv <= 0)return "";
return std::string(buf); return std::string(buf);
} }

View File

@ -17,7 +17,14 @@ void STCPSend(Client* c, std::string Data) {
Assert(c); Assert(c);
if (c == nullptr) if (c == nullptr)
return; return;
ssize_t BytesSent = send(c->GetTCPSock(), Data.c_str(), size_t(Data.size()), 0); #ifdef WIN32
int BytesSent;
int len = static_cast<int>(Data.size());
#else
int64_t BytesSent;
size_t len = Data.size();
#endif // WIN32
BytesSent = send(c->GetTCPSock(), Data.c_str(), len, 0);
Data.clear(); Data.clear();
if (BytesSent == 0) { if (BytesSent == 0) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
@ -42,19 +49,19 @@ void SendFile(Client* c, const std::string& Name) {
size_t Size = size_t(fileSize); size_t Size = size_t(fileSize);
size_t Sent = 0; size_t Sent = 0;
size_t Diff; size_t Diff;
ssize_t Split = 64000; int64_t Split = 64000;
while (c->GetStatus() > -1 && Sent < Size) { while (c->GetStatus() > -1 && Sent < Size) {
Diff = Size - Sent; Diff = Size - Sent;
if (Diff > size_t(Split)) { if (Diff > size_t(Split)) {
std::string Data(size_t(Split), 0); std::string Data(size_t(Split), 0);
f.seekg(ssize_t(Sent), std::ios_base::beg); f.seekg(int64_t(Sent), std::ios_base::beg);
f.read(&Data[0], Split); f.read(&Data[0], Split);
STCPSend(c, Data); STCPSend(c, Data);
Sent += size_t(Split); Sent += size_t(Split);
} else { } else {
std::string Data(Diff, 0); std::string Data(Diff, 0);
f.seekg(ssize_t(Sent), std::ios_base::beg); f.seekg(int64_t(Sent), std::ios_base::beg);
f.read(&Data[0], ssize_t(Diff)); f.read(&Data[0], int64_t(Diff));
STCPSend(c, Data); STCPSend(c, Data);
Sent += Diff; Sent += Diff;
} }
@ -93,7 +100,7 @@ bool STCPRecv(Client* c) {
char buf[200]; char buf[200];
size_t len = 200; size_t len = 200;
ZeroMemory(buf, len); ZeroMemory(buf, len);
ssize_t BytesRcv = recv(c->GetTCPSock(), buf, len, 0); int64_t BytesRcv = recv(c->GetTCPSock(), buf, len, 0);
if (BytesRcv == 0) { if (BytesRcv == 0) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
c->SetStatus(-1); c->SetStatus(-1);

View File

@ -11,7 +11,14 @@ void TCPSend(Client*c,const std::string&Data){
Assert(c); Assert(c);
if(c == nullptr)return; if(c == nullptr)return;
std::string Send = "\n" + Data.substr(0,Data.find(char(0))) + "\n"; std::string Send = "\n" + Data.substr(0,Data.find(char(0))) + "\n";
ssize_t Sent = send(c->GetTCPSock(), Send.c_str(), size_t(Send.size()), 0); #ifdef WIN32
int Sent;
int len = static_cast<int>(Send.size());
#else
int64_t Sent;
size_t len = Send.size();
#endif // WIN32
Sent = send(c->GetTCPSock(), Send.c_str(), len, 0);
if (Sent == 0){ if (Sent == 0){
if(c->GetStatus() > -1)c->SetStatus(-1); if(c->GetStatus() > -1)c->SetStatus(-1);
}else if (Sent < 0) { }else if (Sent < 0) {
@ -37,7 +44,7 @@ void TCPRcv(Client*c){
char buf[4096]; char buf[4096];
size_t len = 4096; size_t len = 4096;
ZeroMemory(buf, len); ZeroMemory(buf, len);
ssize_t BytesRcv = recv(c->GetTCPSock(), buf, len,0); int64_t BytesRcv = recv(c->GetTCPSock(), buf, len,0);
if (BytesRcv == 0){ if (BytesRcv == 0){
debug(Sec("(TCP) Connection closing...")); debug(Sec("(TCP) Connection closing..."));
if(c->GetStatus() > -1)c->SetStatus(-1); if(c->GetStatus() > -1)c->SetStatus(-1);

View File

@ -42,7 +42,15 @@ void UDPSend(Client* c, std::string Data) {
std::string CMP(Comp(Data)); std::string CMP(Comp(Data));
Data = "ABG:" + CMP; Data = "ABG:" + CMP;
} }
ssize_t sendOk = sendto(UDPSock, Data.c_str(), Data.size(), 0, (sockaddr*)&Addr, AddrSize); #ifdef WIN32
int sendOk;
int len = static_cast<int>(Data.size());
#else
int64_t sendOk;
size_t len = Data.size();
#endif // WIN32
sendOk = sendto(UDPSock, Data.c_str(), len, 0, (sockaddr*)&Addr, AddrSize);
#ifdef WIN32 #ifdef WIN32
if (sendOk != 0) { if (sendOk != 0) {
debug(Sec("(UDP) Send Failed Code : ") + std::to_string(WSAGetLastError())); debug(Sec("(UDP) Send Failed Code : ") + std::to_string(WSAGetLastError()));
@ -166,7 +174,7 @@ std::string UDPRcvFromClient(sockaddr_in& client) {
size_t clientLength = sizeof(client); size_t clientLength = sizeof(client);
ZeroMemory(&client, clientLength); ZeroMemory(&client, clientLength);
std::string Ret(10240, 0); std::string Ret(10240, 0);
ssize_t Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&client, (socklen_t*)&clientLength); int64_t Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&client, (socklen_t*)&clientLength);
if (Rcv == -1) { if (Rcv == -1) {
#ifdef WIN32 #ifdef WIN32
error(Sec("(UDP) Error receiving from Client! Code : ") + std::to_string(WSAGetLastError())); error(Sec("(UDP) Error receiving from Client! Code : ") + std::to_string(WSAGetLastError()));