prog
This commit is contained in:
61
backup/client/clientremote.c
Normal file
61
backup/client/clientremote.c
Normal 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.");
|
||||
}
|
Reference in New Issue
Block a user