This commit is contained in:
Anonymous275
2020-06-17 23:21:08 +03:00
parent 1d88fb755c
commit 3997eb089b
+27 -28
View File
@@ -57,9 +57,10 @@ std::string Parse(const std::string& Data){
bool once = false; bool once = false;
[[noreturn]] void MemoryInit(); [[noreturn]] void MemoryInit();
[[noreturn]] void CoreNetworkThread(){ [[noreturn]] void CoreNetworkThread(){
try {
std::cout << "Ready!" << std::endl; std::cout << "Ready!" << std::endl;
do{ do {
if(MPDEV)std::cout << "Core Network on start!" << std::endl; if (MPDEV)std::cout << "Core Network on start!" << std::endl;
WSADATA wsaData; WSADATA wsaData;
int iResult; int iResult;
auto ListenSocket = INVALID_SOCKET; auto ListenSocket = INVALID_SOCKET;
@@ -73,9 +74,9 @@ bool once = false;
int recvbuflen = DEFAULT_BUFLEN; int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock // Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData); iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) { if (iResult != 0) {
if(MPDEV)std::cout <<"WSAStartup failed with error: " << iResult << std::endl; if (MPDEV)std::cout << "WSAStartup failed with error: " << iResult << std::endl;
} }
ZeroMemory(&hints, sizeof(hints)); ZeroMemory(&hints, sizeof(hints));
@@ -86,30 +87,23 @@ bool once = false;
// Resolve the server address and port // Resolve the server address and port
iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result); iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result);
if ( iResult != 0 ) { if (iResult != 0) {
if(MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl; if (MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl;
WSACleanup(); WSACleanup();
} }
// Create a socket for connecting to server // Create a socket for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) { if (ListenSocket == INVALID_SOCKET) {
if(MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl; if (MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl;
freeaddrinfo(result); freeaddrinfo(result);
WSACleanup(); WSACleanup();
} }
// Setup the TCP listening socket // Setup the TCP listening socket
try{ iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
} catch (std::exception&e) {
std::cout << "Exception! : " << e.what() << std::endl;
system("pause");
exit(1);
}
if (iResult == SOCKET_ERROR) { if (iResult == SOCKET_ERROR) {
if(MPDEV)Exit("(Core) bind failed with error: " + std::to_string(WSAGetLastError())); if (MPDEV)Exit("(Core) bind failed with error: " + std::to_string(WSAGetLastError()));
freeaddrinfo(result); freeaddrinfo(result);
closesocket(ListenSocket); closesocket(ListenSocket);
WSACleanup(); WSACleanup();
@@ -119,18 +113,18 @@ bool once = false;
iResult = listen(ListenSocket, SOMAXCONN); iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) { if (iResult == SOCKET_ERROR) {
if(MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl; if (MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket); closesocket(ListenSocket);
WSACleanup(); WSACleanup();
} }
ClientSocket = accept(ListenSocket, nullptr, nullptr); ClientSocket = accept(ListenSocket, nullptr, nullptr);
if (ClientSocket == INVALID_SOCKET) { if (ClientSocket == INVALID_SOCKET) {
if(MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl; if (MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket); closesocket(ListenSocket);
WSACleanup(); WSACleanup();
} }
closesocket(ListenSocket); closesocket(ListenSocket);
if(!once){ if (!once) {
std::thread Memory(MemoryInit); std::thread Memory(MemoryInit);
Memory.detach(); Memory.detach();
once = true; once = true;
@@ -142,20 +136,20 @@ bool once = false;
std::string data = recvbuf; std::string data = recvbuf;
data.resize(iResult); data.resize(iResult);
Response = Parse(data) + "\n"; Response = Parse(data) + "\n";
} else if (iResult == 0){ } else if (iResult == 0) {
if (MPDEV)std::cout << "(Core) Connection closing...\n"; if (MPDEV)std::cout << "(Core) Connection closing...\n";
}else{ } else {
if(MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl; if (MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket); closesocket(ClientSocket);
WSACleanup(); WSACleanup();
} }
if(!Response.empty()){ if (!Response.empty()) {
iSendResult = send( ClientSocket, Response.c_str(), Response.length(), 0); iSendResult = send(ClientSocket, Response.c_str(), Response.length(), 0);
if (iSendResult == SOCKET_ERROR) { if (iSendResult == SOCKET_ERROR) {
if(MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl; if (MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket); closesocket(ClientSocket);
WSACleanup(); WSACleanup();
}else{ } else {
///std::cout << "Bytes sent: " << iSendResult << std::endl; ///std::cout << "Bytes sent: " << iSendResult << std::endl;
} }
} }
@@ -163,12 +157,17 @@ bool once = false;
iResult = shutdown(ClientSocket, SD_SEND); iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) { if (iResult == SOCKET_ERROR) {
if(MPDEV)std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl; if (MPDEV)std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket); closesocket(ClientSocket);
WSACleanup(); WSACleanup();
Sleep(500); Sleep(500);
} }
closesocket(ClientSocket); closesocket(ClientSocket);
WSACleanup(); WSACleanup();
}while (true); } while (true);
} catch (std::exception&e) {
std::cout << "Exception! : " << e.what() << std::endl;
system("pause");
exit(1);
}
} }