71 lines
1.5 KiB
C
71 lines
1.5 KiB
C
/**
|
|
* 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;
|
|
} |