Pārlūkot izejas kodu

Refactor network initialization

Isolate network logic to each subsystem.
feature/data-structs
TheoryOfNekomata pirms 2 gadiem
vecāks
revīzija
97cfbab7ad
9 mainītis faili ar 83 papildinājumiem un 88 dzēšanām
  1. +4
    -2
      CMakeLists.txt
  2. +0
    -2
      src/packages/game/IZ_common.h
  3. +6
    -0
      src/packages/game/network/IZ_network.h
  4. +1
    -0
      src/packages/game/network/IZ_wsclient.h
  5. +3
    -72
      src/packages/server/IZ_app.c
  6. +0
    -4
      src/packages/server/IZ_app.h
  7. +1
    -0
      src/packages/server/network/IZ_network.h
  8. +61
    -1
      src/packages/server/network/IZ_wsserver.c
  9. +7
    -7
      src/packages/server/network/IZ_wsserver.h

+ 4
- 2
CMakeLists.txt Parādīt failu

@@ -70,7 +70,7 @@ add_executable(
src/packages/game/input/IZ_midi.h
src/packages/game/data/IZ_list.c
src/packages/game/data/IZ_list.h
src/packages/game/network/IZ_wsclient.c src/packages/game/network/IZ_wsclient.h src/packages/game/log/IZ_log.c src/packages/game/log/IZ_log.h)
src/packages/game/network/IZ_wsclient.c src/packages/game/network/IZ_wsclient.h src/packages/game/log/IZ_log.c src/packages/game/log/IZ_log.h src/packages/game/network/IZ_network.h)

target_link_libraries(
game
@@ -173,7 +173,9 @@ add_executable(
src/packages/server/log/IZ_log.h
src/packages/server/log/IZ_log.c
src/packages/server/main.c
src/packages/server/network/IZ_wsserver.c src/packages/server/IZ_app.c src/packages/server/IZ_app.h src/packages/server/network/IZ_wsserver.h)
src/packages/server/network/IZ_wsserver.c src/packages/server/IZ_app.c src/packages/server/IZ_app.h src/packages/server/network/IZ_wsserver.h
src/packages/server/network/IZ_network.h
)

target_link_libraries(
server


+ 0
- 2
src/packages/game/IZ_common.h Parādīt failu

@@ -7,8 +7,6 @@
#define PLAYERS (unsigned char) 1
#define APP_NAME "SDL2"

#define NETWORK_PROTOCOL "izanagi-networking"

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;


+ 6
- 0
src/packages/game/network/IZ_network.h Parādīt failu

@@ -0,0 +1,6 @@
#ifndef IZ_NETWORK_H
#define IZ_NETWORK_H

#define NETWORK_PROTOCOL "izanagi-networking"

#endif

+ 1
- 0
src/packages/game/network/IZ_wsclient.h Parādīt failu

@@ -3,6 +3,7 @@

#include <libwebsockets.h>
#include "../IZ_common.h"
#include "IZ_network.h"

typedef struct {
struct lws_context* context;


+ 3
- 72
src/packages/server/IZ_app.c Parādīt failu

@@ -1,71 +1,11 @@
#include "IZ_app.h"

static struct lws_protocols protocols[] = {
{
.name = NETWORK_PROTOCOL,
.callback = IZ_WSServerCallback,
.per_session_data_size = sizeof(IZ_WSServerSessionData),
.rx_buffer_size = 0,
.id = 0,
.user = NULL,
.tx_packet_size = 0,
},
{
.name = "http",
.callback = lws_callback_http_dummy,
.per_session_data_size = 0,
.rx_buffer_size = 0,
.id = 0,
.user = NULL,
.tx_packet_size = 0,
},
LWS_PROTOCOL_LIST_TERM,
};

static const struct lws_http_mount mount = {
.mount_next = NULL, /* linked-list "next" */
.mountpoint = "/", /* mountpoint URL */
.origin = "./mount-origin", /* serve from dir */
.def = "index.html", /* default filename */
.protocol = NULL,
.cgienv = NULL,
.extra_mimetypes = NULL,
.interpret = NULL,
.cgi_timeout = 0,
.cache_max_age = 0,
.auth_mask = 0,
.cache_reusable = 0,
.cache_revalidate = 0,
.cache_intermediaries = 0,
.origin_protocol = LWSMPRO_FILE, /* files in a dir */
.mountpoint_len = 1, /* char count */
.basic_auth_login_file = NULL,
};

void IZ_AppHandleSignal(i32 _signal) {
interrupted = true;
}

IZ_ProcedureResult IZ_AppCreateContext(IZ_App *app) {
struct lws_context_creation_info info;
memset(&info, 0, sizeof info);
info.port = app->config.port;
info.mounts = &mount;
info.protocols = protocols;
// TODO initialize protocols
info.options = (
LWS_SERVER_OPTION_VALIDATE_UTF8
| LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE
);

app->context = lws_create_context(&info);
if (!app->context) {
return 1;
}

return 0;
}

// TODO move to each subsystem
// TODO unify loading of config from cmdline and config file
void IZ_AppLoadConfig(IZ_App *app, u8 argc, const char **argv) {
memcpy_s(app, sizeof(IZ_App), &IZ_APP_DEFAULT_STATE, sizeof(IZ_App));

@@ -77,15 +17,6 @@ void IZ_AppLoadConfig(IZ_App *app, u8 argc, const char **argv) {
if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-p"))) {
app->config.port = atoi(cmdline_buffer);
}

if (lws_cmdline_option(argc, argv, "-o")) {
// connect once
app->config.vhost_options |= 1;
}

if (!lws_cmdline_option(argc, argv, "-n")) {
app->config.extensions_enabled = true;
}
}

IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) {
@@ -94,7 +25,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) {

IZ_AppLoadConfig(app, argc, argv);
IZ_LogInterceptWSMessages(app->config.log_level);
if (IZ_AppCreateContext(app)) {
if (IZ_WSServerCreateContext(&app->context, app->config.port)) {
return 1;
}



+ 0
- 4
src/packages/server/IZ_app.h Parādīt failu

@@ -12,8 +12,6 @@ static bool interrupted;
typedef struct {
i32 log_level;
u16 port;
i32 vhost_options;
bool extensions_enabled;
} IZ_AppConfig;

typedef struct {
@@ -25,8 +23,6 @@ static const IZ_App IZ_APP_DEFAULT_STATE = {
.config = {
.log_level = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE,
.port = 42069,
.vhost_options = 0,
.extensions_enabled = false,
},
.context = NULL,
};


+ 1
- 0
src/packages/server/network/IZ_network.h Parādīt failu

@@ -0,0 +1 @@
E:/Projects/Games/izanagi/src/packages/game/network/IZ_network.h

+ 61
- 1
src/packages/server/network/IZ_wsserver.c Parādīt failu

@@ -1,6 +1,46 @@
#include "IZ_wsserver.h"

/* one of these created for each message */
static struct lws_protocols protocols[] = {
{
.name = NETWORK_PROTOCOL,
.callback = IZ_WSServerCallback,
.per_session_data_size = sizeof(IZ_WSServerSessionData),
.rx_buffer_size = 0,
.id = 0,
.user = NULL,
.tx_packet_size = 0,
},
{
.name = "http",
.callback = lws_callback_http_dummy,
.per_session_data_size = 0,
.rx_buffer_size = 0,
.id = 0,
.user = NULL,
.tx_packet_size = 0,
},
LWS_PROTOCOL_LIST_TERM,
};

static const struct lws_http_mount mount = {
.mount_next = NULL, /* linked-list "next" */
.mountpoint = "/", /* mountpoint URL */
.origin = "./mount-origin", /* serve from dir */
.def = "index.html", /* default filename */
.protocol = NULL,
.cgienv = NULL,
.extra_mimetypes = NULL,
.interpret = NULL,
.cgi_timeout = 0,
.cache_max_age = 0,
.auth_mask = 0,
.cache_reusable = 0,
.cache_revalidate = 0,
.cache_intermediaries = 0,
.origin_protocol = LWSMPRO_FILE, /* files in a dir */
.mountpoint_len = 1, /* char count */
.basic_auth_login_file = NULL,
};

static void IZ_ProtocolCullLaggingClients(IZ_WSServerVHostData *vhd) {
u32 oldest_tail = lws_ring_get_oldest_tail(vhd->ring);
@@ -283,3 +323,23 @@ i32 IZ_WSServerCallback(

return 0;
}

IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context** context, u16 port) {
struct lws_context_creation_info info;
memset(&info, 0, sizeof info);
info.port = port;
info.mounts = &mount;
info.protocols = protocols;
// TODO initialize protocols
info.options = (
LWS_SERVER_OPTION_VALIDATE_UTF8
| LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE
);

*context = lws_create_context(&info);
if (!*context) {
return 1;
}

return 0;
}

+ 7
- 7
src/packages/server/network/IZ_wsserver.h Parādīt failu

@@ -4,33 +4,31 @@
#include "libwebsockets.h"
#include <string.h>
#include "../IZ_common.h"
#include "IZ_network.h"

#define RING_COUNT 32

/* one of these created for each message */
typedef struct {
void *payload; /* is malloc'd */
void* payload; /* is malloc'd */
size_t len;
u8 binary: 1;
} IZ_WSServerMessage;

/* one of these is created for each client connecting to us */

typedef struct IZ_WSServerSessionData {
struct IZ_WSServerSessionData *pss_list;
struct lws *wsi;
struct IZ_WSServerSessionData* pss_list;
struct lws* wsi;
u32 tail;
u8 culled: 1;
} IZ_WSServerSessionData;

/* one of these is created for each vhost our protocol is used with */

typedef struct {
struct lws_context *context;
struct lws_vhost *vhost;
const struct lws_protocols *protocol;

IZ_WSServerSessionData *pss_list; /* linked-list of live pss*/

struct lws_ring *ring; /* ringbuffer holding unsent messages */
} IZ_WSServerVHostData;

@@ -42,4 +40,6 @@ i32 IZ_WSServerCallback(
size_t
);

IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context**, u16);

#endif

Notiek ielāde…
Atcelt
Saglabāt