This commit is contained in:
2025-02-22 20:22:58 -06:00
parent ec4312c189
commit cebe1bb05c
21 changed files with 402 additions and 18 deletions

View File

@ -0,0 +1,13 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
client.c
clientremote.c
)
# Subdirs

71
backup/client/client.c Normal file
View File

@ -0,0 +1,71 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "client.h"
#include "assert/assert.h"
void clientInit(
client_t *client,
const clientinit_t init
) {
assertNotNull(client, "Client cannot be NULL.");
memset(client, 0, sizeof(client_t));
client->type = init.type;
client->state = CLIENT_STATE_CONNECTING;
// Perform connection
switch(init.type) {
case CLIENT_TYPE_REMOTE:
clientRemoteInit(client, init.remote);
break;
default:
assertUnreachable("Invalid client type.");
}
// Begin exchanging.
// clientSend(client, (packet_t){
// .type = PACKET_TYPE_HELLO
// });
}
void clientSend(
client_t *client,
const packet_t packet
) {
assertNotNull(client, "Client cannot be NULL.");
assertTrue(
client->state == CLIENT_STATE_CONNECTED,
"Client must be connected to send packets."
);
switch(client->type) {
case CLIENT_TYPE_REMOTE:
clientRemoteSend(client, packet);
break;
default:
assertUnreachable("Invalid client type.");
}
}
void clientDispose(client_t *client) {
assertNotNull(client, "Client cannot be NULL.");
client->state = CLIENT_STATE_DISCONNECTING;
switch(client->type) {
case CLIENT_TYPE_REMOTE:
clientRemoteDispose(client);
break;
default:
assertUnreachable("Invalid client type.");
}
client->state = CLIENT_STATE_DISCONNECTED;
}

51
backup/client/client.h Normal file
View File

@ -0,0 +1,51 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "clientremote.h"
typedef enum {
CLIENT_TYPE_SINGLE_PLAYER,
CLIENT_TYPE_LOCAL,
CLIENT_TYPE_REMOTE
} clienttype_t;
typedef enum {
CLIENT_STATE_DISCONNECTED,
CLIENT_STATE_DISCONNECTING,
CLIENT_STATE_CONNECTING,
CLIENT_STATE_CONNECTED
} clientstate_t;
typedef struct _client_t {
clienttype_t type;
clientstate_t state;
union {
clientremote_t remote;
};
} client_t;
typedef struct {
clienttype_t type;
union {
clientremoteinit_t remote;
};
} clientinit_t;
void clientInit(
client_t *client,
const clientinit_t init
);
void clientSend(
client_t *client,
const packet_t packet
);
void clientDispose(client_t *client);

View File

@ -0,0 +1,61 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "client.h"
#include "assert/assert.h"
#include "console/console.h"
#include <fcntl.h>
#include <unistd.h>
void* clientRemoteThreadFunc(void* arg) {
client_t *client = (client_t*)arg;
assertNotNull(client, "Client is NULL");
client->state = CLIENT_STATE_CONNECTED;
// Set socket to non-blocking
int flags = fcntl(client->remote.clientSockDesc, F_GETFL, 0);
fcntl(client->remote.clientSockDesc, F_SETFL, flags | O_NONBLOCK);
char buffer[1024];
while (client->state == CLIENT_STATE_CONNECTED) {
ssize_t bytesRead = read(client->remote.clientSockDesc, buffer, sizeof(buffer));
if (bytesRead > 0) {
// Process the received data
printf("Received data: %.*s\n", (int)bytesRead, buffer);
} else if (bytesRead == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
// An error occurred
perror("Read error");
break;
}
// Sleep for a short duration to prevent busy-waiting
usleep(10000); // 10ms
}
return NULL;
}
void clientRemoteInit(
client_t *client,
const clientremoteinit_t init
) {
client->remote.clientSockDesc = init.clientSockDesc;
consolePrint("Accepted a new connection.");
pthread_create(&client->remote.thread, NULL, clientRemoteThreadFunc, client);
}
void clientRemoteSend(client_t *client, const packet_t packet) {
printf("send pack\n");
}
void clientRemoteDispose(client_t *client) {
pthread_join(client->remote.thread, NULL);
close(client->remote.clientSockDesc);
consolePrint("Client disconnected.");
}

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#include "net/packet.h"
typedef struct _client_t client_t;
typedef struct {
int32_t clientSockDesc;
} clientremoteinit_t;
typedef struct {
int32_t clientSockDesc;
pthread_t thread;
} clientremote_t;
void clientRemoteInit(client_t *client, const clientremoteinit_t init);
void clientRemoteSend(client_t *client, const packet_t packet);
void * clientRemoteThread(void *arg);
void clientRemoteDispose(client_t *client);