feat(network): add network socket connection
This commit is contained in:
parent
7e4dc00d71
commit
e1dc82e70f
6
app.c
6
app.c
@ -2,8 +2,6 @@
|
|||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "gui.h"
|
|
||||||
#include "network.h"
|
|
||||||
|
|
||||||
void App_Init(App_t *app) {
|
void App_Init(App_t *app) {
|
||||||
app->isRunning = false;
|
app->isRunning = false;
|
||||||
@ -34,7 +32,7 @@ void App_Init(App_t *app) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Network_Init()) {
|
if (!Network_Init(&app->network)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +47,7 @@ void App_Run(App_t *app) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void App_Shutdown(const App_t* app) {
|
void App_Shutdown(const App_t* app) {
|
||||||
Network_Shutdown();
|
Network_Shutdown(&app->network);
|
||||||
Objects_Destroy(&app->graphics);
|
Objects_Destroy(&app->graphics);
|
||||||
Map_Destroy(&app->map);
|
Map_Destroy(&app->map);
|
||||||
Bitmap_Destroy(&app->bitmap);
|
Bitmap_Destroy(&app->bitmap);
|
||||||
|
|||||||
2
app.h
2
app.h
@ -9,6 +9,7 @@
|
|||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
#include "network.h"
|
||||||
|
|
||||||
typedef struct App {
|
typedef struct App {
|
||||||
bool isRunning;
|
bool isRunning;
|
||||||
@ -18,6 +19,7 @@ typedef struct App {
|
|||||||
Bitmap_t bitmap;
|
Bitmap_t bitmap;
|
||||||
Objects_t graphics;
|
Objects_t graphics;
|
||||||
Gui_t gui;
|
Gui_t gui;
|
||||||
|
Network_t network;
|
||||||
} App_t;
|
} App_t;
|
||||||
|
|
||||||
void App_Init(App_t* app);
|
void App_Init(App_t* app);
|
||||||
|
|||||||
68
network.c
68
network.c
@ -7,15 +7,79 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3_net/SDL_net.h>
|
#include <SDL3_net/SDL_net.h>
|
||||||
|
|
||||||
bool Network_Init() {
|
#define NET_TIMEOUT 30000
|
||||||
|
|
||||||
|
typedef enum ConnectionResult {
|
||||||
|
CONN_SUCCESS = 0,
|
||||||
|
CONN_ERR_BAD_HOSTNAME = 1,
|
||||||
|
CONN_ERR_NO_SERVER = 2
|
||||||
|
} CONNECTION_RESULT;
|
||||||
|
|
||||||
|
void Gui_UpdateStatusBar(const Gui_t* gui, const char *message);
|
||||||
|
|
||||||
|
bool Network_Init(Network_t *network) {
|
||||||
if (!NET_Init()) {
|
if (!NET_Init()) {
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Failed to initialize SDL_net.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Failed to initialize SDL_net.", NULL);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
network->socket = NULL;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network_Shutdown() {
|
void Network_Shutdown(const Network_t *network) {
|
||||||
|
NET_DestroyStreamSocket(network->socket);
|
||||||
NET_Quit();
|
NET_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CONNECTION_RESULT ConnectToServer(Network_t *network, const ConfigParams_t *configParams, const Gui_t *gui) {
|
||||||
|
Gui_UpdateStatusBar(gui, "Searching for hostname...");
|
||||||
|
|
||||||
|
NET_Address *netAddress = NET_ResolveHostname(configParams->serverAddress);
|
||||||
|
const NET_Status resolveHostnameStatus = NET_WaitUntilResolved(netAddress, NET_TIMEOUT);
|
||||||
|
if (resolveHostnameStatus != NET_SUCCESS) {
|
||||||
|
NET_UnrefAddress(netAddress);
|
||||||
|
return CONN_ERR_BAD_HOSTNAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
Gui_UpdateStatusBar(gui, "Trying to connect...");
|
||||||
|
|
||||||
|
network->socket = NET_CreateClient(netAddress, configParams->serverPort);
|
||||||
|
const NET_Status createClientStatus = NET_WaitUntilConnected(network->socket, NET_TIMEOUT);
|
||||||
|
if (createClientStatus != NET_SUCCESS) {
|
||||||
|
NET_DestroyStreamSocket(network->socket);
|
||||||
|
network->socket = NULL;
|
||||||
|
|
||||||
|
NET_UnrefAddress(netAddress);
|
||||||
|
return CONN_ERR_NO_SERVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: is this okay?
|
||||||
|
NET_UnrefAddress(netAddress);
|
||||||
|
|
||||||
|
return CONN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Network_ConnectAndReportStatus(Network_t *network, const ConfigParams_t *configParams, const Gui_t *gui) {
|
||||||
|
if (network->socket != NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CONNECTION_RESULT result = ConnectToServer(network, configParams, gui);
|
||||||
|
const char *message = NULL;
|
||||||
|
|
||||||
|
switch (result) {
|
||||||
|
case CONN_SUCCESS:
|
||||||
|
message = "Connection established.";
|
||||||
|
break;
|
||||||
|
case CONN_ERR_BAD_HOSTNAME:
|
||||||
|
message = "Error: IP address or host not found.";
|
||||||
|
break;
|
||||||
|
case CONN_ERR_NO_SERVER:
|
||||||
|
message = "Error: No server running on host.";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Gui_UpdateStatusBar(gui, message);
|
||||||
|
}
|
||||||
|
|||||||
14
network.h
14
network.h
@ -6,8 +6,18 @@
|
|||||||
#define TIBIA_NETWORK_H
|
#define TIBIA_NETWORK_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <SDL3_net/SDL_net.h>
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
bool Network_Init();
|
typedef struct Gui Gui_t;
|
||||||
void Network_Shutdown();
|
|
||||||
|
typedef struct Network {
|
||||||
|
NET_StreamSocket* socket;
|
||||||
|
} Network_t;
|
||||||
|
|
||||||
|
bool Network_Init(Network_t* network);
|
||||||
|
void Network_Shutdown(const Network_t* network);
|
||||||
|
|
||||||
|
void Network_ConnectAndReportStatus(Network_t* network, const ConfigParams_t* configParams, const Gui_t* gui);
|
||||||
|
|
||||||
#endif //TIBIA_NETWORK_H
|
#endif //TIBIA_NETWORK_H
|
||||||
Loading…
x
Reference in New Issue
Block a user