fix network (#22)

* fix network

* clean network

---------

Co-authored-by: MegaMech <MegaMech@users.noreply.github.com>
This commit is contained in:
coco875
2024-07-29 00:27:54 +02:00
committed by GitHub
parent 4e989b05cc
commit 3310098088
3 changed files with 12 additions and 20 deletions
+6 -17
View File
@@ -3,7 +3,6 @@
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#include <windows.h>
#include "networking.h"
#include "main.h"
@@ -28,8 +27,7 @@ Network gNetwork = {
// Global variables
HANDLE sNetworkThread;
DWORD threadID;
SDL_Thread *sNetworkThread;
bool threadStarted = false;
int isNetworkingThreadEnabled = true;
@@ -78,29 +76,20 @@ void networking_init(char* ip, uint16_t port) {
// Ensure no thread is already running
if (sNetworkThread != NULL) {
WaitForSingleObject(sNetworkThread, INFINITE);
CloseHandle(sNetworkThread);
SDL_WaitThread(sNetworkThread, NULL);
sNetworkThread = NULL;
}
sNetworkThread = CreateThread(
NULL, // default security attributes
0, // default stack size
networking_loop, // thread function
NULL, // argument to thread function
0, // default creation flags
&threadID // receive thread identifier
);
sNetworkThread = SDL_CreateThread(networking_loop, "NetworkingThread", NULL);
if (sNetworkThread == NULL) {
printf("CreateThread failed: %d\n", GetLastError());
printf("SDL_CreateThread failed: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
//sNetworkThread = std::thread(&GameInteractor::ReceiveFromServer, this);
}
DWORD WINAPI networking_loop(LPVOID arg) {
int networking_loop(void* data) {
while (isNetworkingThreadEnabled) {
while (!gNetwork.isConnected && isNetworkingThreadEnabled) { // && isRemoteInteractorEnabled) {
printf("[SpaghettiOnline] Attempting to make connection to server...\n");
@@ -207,7 +196,7 @@ void handleReceivedData(const char *buffer, size_t bufSize) {
handleMessagePacket(data);
break;
case PACKET_LOADED:
handle_start_game(data);
handle_start_game(); // handle_start_game(data);
break;
case PACKET_PLAYER:
replicate_player(data);
+4 -3
View File
@@ -4,7 +4,6 @@
#include <libultraship.h>
#include <common_structs.h>
#include <SDL2/SDL_net.h>
#include <windows.h>
#define NETWORK_MAX_PLAYERS 8
#define NETWORK_USERNAME_LENGTH 32
@@ -56,8 +55,8 @@ void ConnectToServer(char* ip, uint16_t port, char *username);
void networking_init(char* ip, uint16_t port);
void networking_update(void);
void networking_ready_up(bool);
void networking_cleanup(void);
DWORD WINAPI networking_loop(LPVOID);
void networking_cleanup(SDLNet_SocketSet);
int networking_loop(void*);
void handleReceivedData(const char *, size_t);
void set_username(const char *username);
@@ -77,5 +76,7 @@ void handleJoinPacket(const char *data);
void handleLeavePacket(const char *data);
void handleMessagePacket(const char *data);
void handle_start_game(void);
void send_str_packet(TCPsocket, uint8_t, const char *);
#endif // NETWORKING_H
+2
View File
@@ -5,6 +5,8 @@
#include "networking.h"
#include "code_800029B0.h"
#include "menus.h"
#include "audio/external.h"
#include "code_80091750.h"
// PLAYER_EXISTS | PLAYER_STAGING | PLAYER_START_SEQUENCE | PLAYER_HUMAN
// PLAYER_EXISTS | PLAYER_STAGING | PLAYER_START_SEQUENCE | PLAYER_KART_AI