|
|
@@ -1,14 +1,14 @@ |
|
|
|
#include "IZ_net.h" |
|
|
|
#include "IZ_net_client.h" |
|
|
|
|
|
|
|
bool IZ_NetIsValidPacketIntervalMs(long packet_interval_ms) { |
|
|
|
bool IZ_NetClientIsValidPacketIntervalMs(long packet_interval_ms) { |
|
|
|
return (100 <= packet_interval_ms && packet_interval_ms <= 500); |
|
|
|
} |
|
|
|
|
|
|
|
bool IZ_NetIsValidMaxReconnectRetries(long max_reconnect_retries) { |
|
|
|
bool IZ_NetClientIsValidMaxReconnectRetries(long max_reconnect_retries) { |
|
|
|
return (0 <= max_reconnect_retries && max_reconnect_retries <= 8); |
|
|
|
} |
|
|
|
|
|
|
|
bool IZ_NetIsValidReconnectIntervalSeconds(long reconnect_interval_secs) { |
|
|
|
bool IZ_NetClientIsValidReconnectIntervalSeconds(long reconnect_interval_secs) { |
|
|
|
return (3 <= reconnect_interval_secs && reconnect_interval_secs <= 10); |
|
|
|
} |
|
|
|
|
|
|
@@ -19,7 +19,7 @@ static IZ_ConfigItem net_config_items[] = { |
|
|
|
"Network", |
|
|
|
"Username", |
|
|
|
NULL, |
|
|
|
&IZ_NET_DEFAULT_STATE.config.username, |
|
|
|
&IZ_NET_CLIENT_DEFAULT_STATE.config.username, |
|
|
|
NULL |
|
|
|
}, |
|
|
|
{ |
|
|
@@ -28,8 +28,8 @@ static IZ_ConfigItem net_config_items[] = { |
|
|
|
"Network", |
|
|
|
"PacketIntervalMs", |
|
|
|
"-i", |
|
|
|
&IZ_NET_DEFAULT_STATE.config.packet_interval_ms, |
|
|
|
IZ_NetIsValidPacketIntervalMs, |
|
|
|
&IZ_NET_CLIENT_DEFAULT_STATE.config.packet_interval_ms, |
|
|
|
IZ_NetClientIsValidPacketIntervalMs, |
|
|
|
}, |
|
|
|
{ |
|
|
|
IZ_CONFIG_TYPE_U8, |
|
|
@@ -37,8 +37,8 @@ static IZ_ConfigItem net_config_items[] = { |
|
|
|
"Network", |
|
|
|
"MaxReconnectRetries", |
|
|
|
NULL, |
|
|
|
&IZ_NET_DEFAULT_STATE.config.max_reconnect_retries, |
|
|
|
IZ_NetIsValidMaxReconnectRetries, |
|
|
|
&IZ_NET_CLIENT_DEFAULT_STATE.config.max_reconnect_retries, |
|
|
|
IZ_NetClientIsValidMaxReconnectRetries, |
|
|
|
}, |
|
|
|
{ |
|
|
|
IZ_CONFIG_TYPE_U8, |
|
|
@@ -46,33 +46,33 @@ static IZ_ConfigItem net_config_items[] = { |
|
|
|
"Network", |
|
|
|
"ReconnectIntervalSeconds", |
|
|
|
NULL, |
|
|
|
&IZ_NET_DEFAULT_STATE.config.reconnect_interval_secs, |
|
|
|
IZ_NetIsValidReconnectIntervalSeconds, |
|
|
|
&IZ_NET_CLIENT_DEFAULT_STATE.config.reconnect_interval_secs, |
|
|
|
IZ_NetClientIsValidReconnectIntervalSeconds, |
|
|
|
}, |
|
|
|
}; |
|
|
|
|
|
|
|
void IZ_NetBindStateToConfig(IZ_NetState* state, IZ_ConfigItem config_items[]) { |
|
|
|
void IZ_NetClientBindStateToConfig(IZ_NetClientState* state, IZ_ConfigItem config_items[]) { |
|
|
|
config_items[0].dest = &state->config.username; |
|
|
|
config_items[1].dest = &state->config.packet_interval_ms; |
|
|
|
config_items[2].dest = &state->config.max_reconnect_retries; |
|
|
|
config_items[3].dest = &state->config.reconnect_interval_secs; |
|
|
|
} |
|
|
|
|
|
|
|
IZ_ProcedureResult IZ_NetSaveConfig(IZ_NetState* state, const char* config_path) { |
|
|
|
IZ_NetBindStateToConfig(state, net_config_items); |
|
|
|
IZ_ProcedureResult IZ_NetClientSaveConfig(IZ_NetClientState* state, const char* config_path) { |
|
|
|
IZ_NetClientBindStateToConfig(state, net_config_items); |
|
|
|
return IZ_ConfigSave(net_config_items, config_path); |
|
|
|
} |
|
|
|
|
|
|
|
IZ_ProcedureResult IZ_NetInitialize( |
|
|
|
IZ_NetState* state, |
|
|
|
IZ_ProcedureResult IZ_NetClientInitialize( |
|
|
|
IZ_NetClientState* state, |
|
|
|
void* user_data, |
|
|
|
void* callback, |
|
|
|
const char* config_path, |
|
|
|
u8 argc, |
|
|
|
const char* argv[] |
|
|
|
) { |
|
|
|
memcpy_s(state, sizeof(IZ_NetState), &IZ_NET_DEFAULT_STATE, sizeof(IZ_NetState)); |
|
|
|
IZ_NetBindStateToConfig(state, net_config_items); |
|
|
|
memcpy_s(state, sizeof(IZ_NetClientState), &IZ_NET_CLIENT_DEFAULT_STATE, sizeof(IZ_NetClientState)); |
|
|
|
IZ_NetClientBindStateToConfig(state, net_config_items); |
|
|
|
IZ_ConfigInit(net_config_items, config_path, argc, argv); |
|
|
|
if (!user_data) { |
|
|
|
return -2; |
|
|
@@ -88,16 +88,16 @@ IZ_ProcedureResult IZ_NetInitialize( |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
void IZ_NetConnect(IZ_NetState* state, IZ_WSClientInitializeParams params) { |
|
|
|
void IZ_NetClientConnect(IZ_NetClientState* state, IZ_WSClientInitializeParams params) { |
|
|
|
if (!state->callback) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (state->status == IZ_NET_STATUS_CONNECTED) { |
|
|
|
if (state->status == IZ_NET_CLIENT_STATUS_CONNECTED) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (state->status == IZ_NET_STATUS_CONNECTING) { |
|
|
|
if (state->status == IZ_NET_CLIENT_STATUS_CONNECTING) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@@ -111,8 +111,8 @@ void IZ_NetConnect(IZ_NetState* state, IZ_WSClientInitializeParams params) { |
|
|
|
SDL_DetachThread(state->client_thread); |
|
|
|
} |
|
|
|
|
|
|
|
void IZ_NetDisconnect(IZ_NetState* state) { |
|
|
|
if (state->status == IZ_NET_STATUS_PRISTINE) { |
|
|
|
void IZ_NetClientDisconnect(IZ_NetClientState* state) { |
|
|
|
if (state->status == IZ_NET_CLIENT_STATUS_PRISTINE) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (state->binding.connection) { |
|
|
@@ -128,8 +128,8 @@ void IZ_NetDisconnect(IZ_NetState* state) { |
|
|
|
IZ_WSClientCancelService(&state->binding); |
|
|
|
} |
|
|
|
|
|
|
|
void IZ_NetSendBinaryMessage(IZ_NetState* state, void* in, size_t len) { |
|
|
|
if (state->status != IZ_NET_STATUS_CONNECTED) { |
|
|
|
void IZ_NetClientSendBinaryMessage(IZ_NetClientState* state, void* in, size_t len) { |
|
|
|
if (state->status != IZ_NET_CLIENT_STATUS_CONNECTED) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@@ -150,8 +150,8 @@ void IZ_NetSendBinaryMessage(IZ_NetState* state, void* in, size_t len) { |
|
|
|
lws_callback_on_writable(state->binding.connection); |
|
|
|
} |
|
|
|
|
|
|
|
void IZ_NetSendTextMessage(IZ_NetState* state, char* in, size_t len) { |
|
|
|
if (state->status != IZ_NET_STATUS_CONNECTED) { |
|
|
|
void IZ_NetClientSendTextMessage(IZ_NetClientState* state, char* in, size_t len) { |
|
|
|
if (state->status != IZ_NET_CLIENT_STATUS_CONNECTED) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|