Compare commits
1 Commits
playertest
...
socket
| Author | SHA1 | Date | |
|---|---|---|---|
| 643a8077dd |
@@ -77,4 +77,4 @@ add_subdirectory(time)
|
|||||||
add_subdirectory(ui)
|
add_subdirectory(ui)
|
||||||
add_subdirectory(network)
|
add_subdirectory(network)
|
||||||
add_subdirectory(util)
|
add_subdirectory(util)
|
||||||
# add_subdirectory(thread)
|
add_subdirectory(thread)
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
#include "physics/physicsmanager.h"
|
#include "physics/physicsmanager.h"
|
||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
#include "network/networkinfo.h"
|
#include "network/networkinfo.h"
|
||||||
|
#include "network/networksocketclient.h"
|
||||||
#include "system/system.h"
|
#include "system/system.h"
|
||||||
|
|
||||||
#include "display/mesh/cube.h"
|
#include "display/mesh/cube.h"
|
||||||
@@ -30,14 +31,28 @@
|
|||||||
engine_t ENGINE;
|
engine_t ENGINE;
|
||||||
entityid_t phBoxEnt;
|
entityid_t phBoxEnt;
|
||||||
componentid_t phBoxPhys;
|
componentid_t phBoxPhys;
|
||||||
|
networksocketclient_t sockClient;
|
||||||
|
|
||||||
float_t onlineSwapTime = FLT_MAX;
|
float_t onlineSwapTime = FLT_MAX;
|
||||||
|
|
||||||
void goOnline();
|
void goOnline();
|
||||||
void goOffline();
|
void goOffline();
|
||||||
|
|
||||||
|
void onSocketConnected(void *user) {
|
||||||
|
sceneLog("Socket connected.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void onSocketError(errorret_t error, void *user) {
|
||||||
|
sceneLog("Socket error: %s\n", error.state->message);
|
||||||
|
errorCatch(errorPrint(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
void onSocketDisconnected(void *user) {
|
||||||
|
sceneLog("Socket disconnected.\n");
|
||||||
|
}
|
||||||
|
|
||||||
void onNetworkConnected(void *user) {
|
void onNetworkConnected(void *user) {
|
||||||
onlineSwapTime = TIME.time + 3.0f;
|
onlineSwapTime = TIME.time + 1.5f;
|
||||||
|
|
||||||
networkinfo_t info = networkGetInfo();
|
networkinfo_t info = networkGetInfo();
|
||||||
if(info.type == NETWORK_TYPE_IPV4) {
|
if(info.type == NETWORK_TYPE_IPV4) {
|
||||||
@@ -57,7 +72,19 @@ void onNetworkConnected(void *user) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
sceneLog("Network connected, I will disconnect at: %.2f1.\n", onlineSwapTime);
|
sceneLog("Network connected, opening socket: %.2f1.\n", onlineSwapTime);
|
||||||
|
networkSocketClientInit(
|
||||||
|
&sockClient,
|
||||||
|
"google.com",
|
||||||
|
443,
|
||||||
|
NULL,
|
||||||
|
onSocketConnected,
|
||||||
|
onSocketError,
|
||||||
|
onSocketDisconnected
|
||||||
|
);
|
||||||
|
onlineSwapTime = FLT_MAX;
|
||||||
|
|
||||||
|
// sceneLog("Network connected, I will disconnect at: %.2f1.\n", onlineSwapTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onNetworkFailed(errorret_t error, void *user) {
|
void onNetworkFailed(errorret_t error, void *user) {
|
||||||
@@ -113,8 +140,8 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
// errorChain(networkInit());
|
// errorChain(networkInit());
|
||||||
errorChain(gameInit());
|
errorChain(gameInit());
|
||||||
|
|
||||||
sceneLog("Init done, going to queue online in 3 seconds...\n");
|
onlineSwapTime = TIME.time + 1.0f;
|
||||||
onlineSwapTime = TIME.time + 3.0f;
|
sceneLog("Init done, going to queue online at %.2f1.\n", onlineSwapTime);
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
entityid_t cam = entityManagerAdd();
|
entityid_t cam = entityManagerAdd();
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
PUBLIC
|
PUBLIC
|
||||||
network.c
|
network.c
|
||||||
networkinfo.c
|
networkinfo.c
|
||||||
|
networksocketclient.c
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char_t *key;
|
||||||
|
const char_t *value;
|
||||||
|
} networkhttpheader_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
|
||||||
|
} networkhttprequest_t;
|
||||||
|
|
||||||
|
void networkHTTP(
|
||||||
|
const char_t *url,
|
||||||
|
const char_t *method,
|
||||||
|
void *user,
|
||||||
|
void (*onComplete)(void *user),
|
||||||
|
void (*onError)(errorret_t error, void *user)
|
||||||
|
);
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "networksocketclient.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
|
void networkSocketClientInit(
|
||||||
|
networksocketclient_t *client,
|
||||||
|
const char_t *host,
|
||||||
|
uint16_t port,
|
||||||
|
void *user,
|
||||||
|
void (*onConnect)(void *user),
|
||||||
|
void (*onError)(errorret_t error, void *user),
|
||||||
|
void (*onDisconnect)(void *user)
|
||||||
|
) {
|
||||||
|
assertNotNull(client, "Client cannot be NULL");
|
||||||
|
assertStrLenMin(host, 1, "Host cannot be empty");
|
||||||
|
assertNotNull(onConnect, "onConnect callback cannot be NULL");
|
||||||
|
assertNotNull(onError, "onError callback cannot be NULL");
|
||||||
|
assertNotNull(onDisconnect, "onDisconnect callback cannot be NULL");
|
||||||
|
|
||||||
|
memoryZero(client, sizeof(networksocketclient_t));
|
||||||
|
client->user = user;
|
||||||
|
client->onConnect = onConnect;
|
||||||
|
client->onError = onError;
|
||||||
|
client->onDisconnect = onDisconnect;
|
||||||
|
client->state = NETWORK_SOCKET_CLIENT_STATE_CONNECTING;
|
||||||
|
|
||||||
|
// Pass to platform for implementation.
|
||||||
|
networkSocketClientPlatformInit(
|
||||||
|
client,
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
user,
|
||||||
|
onConnect,
|
||||||
|
onError,
|
||||||
|
onDisconnect
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "error/error.h"
|
||||||
|
#include "network/networksocketclientplatform.h"
|
||||||
|
|
||||||
|
#ifndef networkSocketClientPlatformInit
|
||||||
|
#error "Define networkSocketClientPlatformInit"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
NETWORK_SOCKET_CLIENT_STATE_DISCONNECTED,
|
||||||
|
NETWORK_SOCKET_CLIENT_STATE_CONNECTING,
|
||||||
|
} networksocketclientstate_t;
|
||||||
|
|
||||||
|
typedef struct networksocketclient_s {
|
||||||
|
void *user;
|
||||||
|
void (*onConnect)(void *user);
|
||||||
|
void (*onError)(errorret_t error, void *user);
|
||||||
|
void (*onDisconnect)(void *user);
|
||||||
|
networksocketclientstate_t state;
|
||||||
|
networksocketclientplatform_t platform;
|
||||||
|
errorstate_t errorState;
|
||||||
|
} networksocketclient_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a network socket client connection.
|
||||||
|
*
|
||||||
|
* @param client The client struct to initialize.
|
||||||
|
* @param host The hostname or IP address to connect to.
|
||||||
|
* @param port The port number to connect to.
|
||||||
|
* @param user User data to pass to callbacks.
|
||||||
|
* @param onConnect Callback for when the connection is established.
|
||||||
|
* @param onError Callback for when an error occurs.
|
||||||
|
* @param onDisconnect Callback for when the connection is disconnected.
|
||||||
|
*/
|
||||||
|
void networkSocketClientInit(
|
||||||
|
networksocketclient_t *client,
|
||||||
|
const char_t *host,
|
||||||
|
uint16_t port,
|
||||||
|
void *user,
|
||||||
|
void (*onConnect)(void *user),
|
||||||
|
void (*onError)(errorret_t error, void *user),
|
||||||
|
void (*onDisconnect)(void *user)
|
||||||
|
);
|
||||||
@@ -6,4 +6,5 @@
|
|||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
networklinux.c
|
networklinux.c
|
||||||
|
networksocketclientlinux.c
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "network/networksocketclient.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
void networkSocketClientLinuxInit(
|
||||||
|
networksocketclient_t *client,
|
||||||
|
const char_t *host,
|
||||||
|
uint16_t port,
|
||||||
|
void *user,
|
||||||
|
void (*onConnect)(void *user),
|
||||||
|
void (*onError)(errorret_t error, void *user),
|
||||||
|
void (*onDisconnect)(void *user)
|
||||||
|
) {
|
||||||
|
assertTrue(client->user == user, "Net client init mismatch?");
|
||||||
|
assertTrue(client->onConnect == onConnect, "Net client init mismatch?");
|
||||||
|
assertTrue(client->onError == onError, "Net client init mismatch?");
|
||||||
|
assertTrue(client->onDisconnect == onDisconnect, "Net client init mismatch?");
|
||||||
|
|
||||||
|
// Create the thread.
|
||||||
|
threadInit(&client->platform.thread, networkSocketClientLinuxThread);
|
||||||
|
client->platform.thread.data = client;
|
||||||
|
|
||||||
|
threadStart(&client->platform.thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
void networkSocketClientLinuxThread(thread_t *thread) {
|
||||||
|
assertNotNull(thread, "Thread cannot be NULL.");
|
||||||
|
assertNotNull(thread->data, "Thread data cannot be NULL.");
|
||||||
|
|
||||||
|
networksocketclient_t *client = (networksocketclient_t *)thread->data;
|
||||||
|
|
||||||
|
// Setup socket FD
|
||||||
|
client->platform.sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if(client->platform.sockfd < 0) {
|
||||||
|
errorret_t ret = errorThrowImpl(
|
||||||
|
&client->errorState, ERROR_NOT_OK,
|
||||||
|
__FILE__, __func__, __LINE__,
|
||||||
|
"Failed to create socket."
|
||||||
|
);
|
||||||
|
client->onError(ret, client->user);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure server address.
|
||||||
|
char_t *host = "google.com";
|
||||||
|
int port = 443;
|
||||||
|
|
||||||
|
struct sockaddr_in serverAddr;
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
serverAddr.sin_port = htons(port);
|
||||||
|
struct hostent *server = gethostbyname(host);
|
||||||
|
if(server == NULL) {
|
||||||
|
errorret_t ret = errorThrowImpl(
|
||||||
|
&client->errorState, ERROR_NOT_OK,
|
||||||
|
__FILE__, __func__, __LINE__,
|
||||||
|
"Failed to resolve host."
|
||||||
|
);
|
||||||
|
client->onError(ret, client->user);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryCopy(&serverAddr.sin_addr.s_addr, server->h_addr, server->h_length);
|
||||||
|
|
||||||
|
// Connect to server.
|
||||||
|
if(connect(
|
||||||
|
client->platform.sockfd,
|
||||||
|
(struct sockaddr *)&serverAddr,
|
||||||
|
sizeof(serverAddr)
|
||||||
|
) < 0) {
|
||||||
|
errorret_t ret = errorThrowImpl(
|
||||||
|
&client->errorState, ERROR_NOT_OK,
|
||||||
|
__FILE__, __func__, __LINE__,
|
||||||
|
"Failed to connect to server."
|
||||||
|
);
|
||||||
|
client->onError(ret, client->user);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// At this point we are connected.
|
||||||
|
client->onConnect(client->user);
|
||||||
|
|
||||||
|
// Hangup for now.
|
||||||
|
close(client->platform.sockfd);
|
||||||
|
client->onDisconnect(client->user);
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "error/error.h"
|
||||||
|
#include "thread/thread.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int sockfd;
|
||||||
|
thread_t thread;
|
||||||
|
} networksocketclientlinux_t;
|
||||||
|
|
||||||
|
typedef struct networksocketclient_s networksocketclient_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refer to networkSocketClientInit for documentation.
|
||||||
|
*/
|
||||||
|
void networkSocketClientLinuxInit(
|
||||||
|
networksocketclient_t *client,
|
||||||
|
const char_t *host,
|
||||||
|
uint16_t port,
|
||||||
|
void *user,
|
||||||
|
void (*onConnect)(void *user),
|
||||||
|
void (*onError)(errorret_t error, void *user),
|
||||||
|
void (*onDisconnect)(void *user)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async thread for handling the socket connection.
|
||||||
|
*
|
||||||
|
* @param thread Thread structure passed.
|
||||||
|
*/
|
||||||
|
void networkSocketClientLinuxThread(thread_t *thread);
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "networksocketclientlinux.h"
|
||||||
|
|
||||||
|
#define networkSocketClientPlatformInit networkSocketClientLinuxInit
|
||||||
|
|
||||||
|
typedef networksocketclientlinux_t networksocketclientplatform_t;
|
||||||
Reference in New Issue
Block a user