mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-02 07:56:26 +00:00
tweaks
This commit is contained in:
parent
1d88fb755c
commit
3997eb089b
@ -57,118 +57,117 @@ 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(){
|
||||||
std::cout << "Ready!" << std::endl;
|
try {
|
||||||
do{
|
std::cout << "Ready!" << std::endl;
|
||||||
if(MPDEV)std::cout << "Core Network on start!" << std::endl;
|
|
||||||
WSADATA wsaData;
|
|
||||||
int iResult;
|
|
||||||
auto ListenSocket = INVALID_SOCKET;
|
|
||||||
auto ClientSocket = INVALID_SOCKET;
|
|
||||||
|
|
||||||
struct addrinfo *result = nullptr;
|
|
||||||
struct addrinfo hints{};
|
|
||||||
|
|
||||||
int iSendResult;
|
|
||||||
char recvbuf[DEFAULT_BUFLEN];
|
|
||||||
int recvbuflen = DEFAULT_BUFLEN;
|
|
||||||
|
|
||||||
// Initialize Winsock
|
|
||||||
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
||||||
if (iResult != 0) {
|
|
||||||
if(MPDEV)std::cout <<"WSAStartup failed with error: " << iResult << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ZeroMemory(&hints, sizeof(hints));
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
|
||||||
hints.ai_flags = AI_PASSIVE;
|
|
||||||
|
|
||||||
// Resolve the server address and port
|
|
||||||
iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result);
|
|
||||||
if ( iResult != 0 ) {
|
|
||||||
if(MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl;
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a socket for connecting to server
|
|
||||||
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
|
||||||
if (ListenSocket == INVALID_SOCKET) {
|
|
||||||
if(MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl;
|
|
||||||
freeaddrinfo(result);
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup the TCP listening socket
|
|
||||||
try{
|
|
||||||
|
|
||||||
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(MPDEV)Exit("(Core) bind failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
freeaddrinfo(result);
|
|
||||||
closesocket(ListenSocket);
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(result);
|
|
||||||
|
|
||||||
iResult = listen(ListenSocket, SOMAXCONN);
|
|
||||||
if (iResult == SOCKET_ERROR) {
|
|
||||||
if(MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl;
|
|
||||||
closesocket(ListenSocket);
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
ClientSocket = accept(ListenSocket, nullptr, nullptr);
|
|
||||||
if (ClientSocket == INVALID_SOCKET) {
|
|
||||||
if(MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl;
|
|
||||||
closesocket(ListenSocket);
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
closesocket(ListenSocket);
|
|
||||||
if(!once){
|
|
||||||
std::thread Memory(MemoryInit);
|
|
||||||
Memory.detach();
|
|
||||||
once = true;
|
|
||||||
}
|
|
||||||
do {
|
do {
|
||||||
std::string Response;
|
if (MPDEV)std::cout << "Core Network on start!" << std::endl;
|
||||||
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
|
WSADATA wsaData;
|
||||||
if (iResult > 0) {
|
int iResult;
|
||||||
std::string data = recvbuf;
|
auto ListenSocket = INVALID_SOCKET;
|
||||||
data.resize(iResult);
|
auto ClientSocket = INVALID_SOCKET;
|
||||||
Response = Parse(data) + "\n";
|
|
||||||
} else if (iResult == 0){
|
struct addrinfo *result = nullptr;
|
||||||
if (MPDEV)std::cout << "(Core) Connection closing...\n";
|
struct addrinfo hints{};
|
||||||
}else{
|
|
||||||
if(MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl;
|
int iSendResult;
|
||||||
closesocket(ClientSocket);
|
char recvbuf[DEFAULT_BUFLEN];
|
||||||
|
int recvbuflen = DEFAULT_BUFLEN;
|
||||||
|
|
||||||
|
// Initialize Winsock
|
||||||
|
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
|
if (iResult != 0) {
|
||||||
|
if (MPDEV)std::cout << "WSAStartup failed with error: " << iResult << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
ZeroMemory(&hints, sizeof(hints));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
|
// Resolve the server address and port
|
||||||
|
iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result);
|
||||||
|
if (iResult != 0) {
|
||||||
|
if (MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl;
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
if(!Response.empty()){
|
|
||||||
iSendResult = send( ClientSocket, Response.c_str(), Response.length(), 0);
|
// Create a socket for connecting to server
|
||||||
if (iSendResult == SOCKET_ERROR) {
|
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||||
if(MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
|
if (ListenSocket == INVALID_SOCKET) {
|
||||||
|
if (MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl;
|
||||||
|
freeaddrinfo(result);
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup the TCP listening socket
|
||||||
|
iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
|
||||||
|
if (iResult == SOCKET_ERROR) {
|
||||||
|
if (MPDEV)Exit("(Core) bind failed with error: " + std::to_string(WSAGetLastError()));
|
||||||
|
freeaddrinfo(result);
|
||||||
|
closesocket(ListenSocket);
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(result);
|
||||||
|
|
||||||
|
iResult = listen(ListenSocket, SOMAXCONN);
|
||||||
|
if (iResult == SOCKET_ERROR) {
|
||||||
|
if (MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl;
|
||||||
|
closesocket(ListenSocket);
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
ClientSocket = accept(ListenSocket, nullptr, nullptr);
|
||||||
|
if (ClientSocket == INVALID_SOCKET) {
|
||||||
|
if (MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl;
|
||||||
|
closesocket(ListenSocket);
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
closesocket(ListenSocket);
|
||||||
|
if (!once) {
|
||||||
|
std::thread Memory(MemoryInit);
|
||||||
|
Memory.detach();
|
||||||
|
once = true;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
std::string Response;
|
||||||
|
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
|
||||||
|
if (iResult > 0) {
|
||||||
|
std::string data = recvbuf;
|
||||||
|
data.resize(iResult);
|
||||||
|
Response = Parse(data) + "\n";
|
||||||
|
} else if (iResult == 0) {
|
||||||
|
if (MPDEV)std::cout << "(Core) Connection closing...\n";
|
||||||
|
} else {
|
||||||
|
if (MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}else{
|
|
||||||
///std::cout << "Bytes sent: " << iSendResult << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
if (!Response.empty()) {
|
||||||
} while (iResult > 0);
|
iSendResult = send(ClientSocket, Response.c_str(), Response.length(), 0);
|
||||||
|
if (iSendResult == SOCKET_ERROR) {
|
||||||
|
if (MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
|
||||||
|
closesocket(ClientSocket);
|
||||||
|
WSACleanup();
|
||||||
|
} else {
|
||||||
|
///std::cout << "Bytes sent: " << iSendResult << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (iResult > 0);
|
||||||
|
|
||||||
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);
|
||||||
|
WSACleanup();
|
||||||
|
Sleep(500);
|
||||||
|
}
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
Sleep(500);
|
} while (true);
|
||||||
}
|
} catch (std::exception&e) {
|
||||||
closesocket(ClientSocket);
|
std::cout << "Exception! : " << e.what() << std::endl;
|
||||||
WSACleanup();
|
system("pause");
|
||||||
}while (true);
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user