Modify server and client to begin using ClientToServer messages.

This commit is contained in:
Barra Ó Catháin 2023-08-26 00:48:28 +01:00
parent 0104a11a7e
commit c043da64a2
5 changed files with 18 additions and 14 deletions

View File

@ -4,9 +4,11 @@ SilverMUDServer_CFLAGS = -I/usr/include/guile/3.0 -I/usr -lguile-3.0 -lgc -lpthr
SilverMUDClient_CFLAGS = -I/usr/include/guile/3.0 -I/usr -lguile-3.0 -lgc -lpthread -ldl -lgnutls -g
SilverMUDServer_SOURCES = \
source/messages.c \
source/server/connections.c \
source/server/scheme-integration.c \
source/server/main.c
SilverMUDClient_SOURCES = \
source/messages.c \
source/client/main.c

View File

@ -13,6 +13,8 @@
#include <sys/socket.h>
#include <gnutls/gnutls.h>
#include "../messages.h"
int main (int argc, char ** argv)
{
// Print a welcome message:
@ -64,11 +66,11 @@ int main (int argc, char ** argv)
while (returnValue < 0 && gnutls_error_is_fatal(returnValue) == 0);
char buffer[2048];
struct ClientToServerMessage message;
while (true)
{
fgets(buffer, 2048, stdin);
gnutls_record_send(tlsSession, &buffer, 2048);
fgets(message.content, 1024, stdin);
gnutls_record_send(tlsSession, &message, 1024);
}
// Return a successful status code to the operating system:

View File

@ -39,7 +39,7 @@ SCM scheme_createClientToServerMessage(SCM content)
free(contentString);
// Return the pointer as a Scheme object:
return scm_from_pointer(message);
return scm_from_pointer(message, NULL);
}
else
{
@ -87,7 +87,7 @@ SCM scheme_createServerToClientMessage(SCM type, SCM name, SCM content)
free(contentString);
// Return the pointer as a Scheme object:
return scm_from_pointer(message);
return scm_from_pointer(message, NULL);
}
else
{

View File

@ -6,9 +6,8 @@
#ifndef MESSAGES_H
#define MESSAGES_H
#include <libguile.h>
const size_t MESSAGE_NAME_LENGTH = 128;
const size_t MESSAGE_CONTENT_LENGTH = 1024;
#define MESSAGE_NAME_LENGTH 128
#define MESSAGE_CONTENT_LENGTH 1024
enum MessageTypes
{
@ -22,7 +21,7 @@ enum MessageTypes
struct ClientToServerMessage
{
char content[MESSAGE_CONTENT_LENGTH]
char content[MESSAGE_CONTENT_LENGTH];
};
struct ServerToClientMessage

View File

@ -20,6 +20,7 @@
#include <gnutls/gnutls.h>
#include "connections.h"
#include "../messages.h"
#include "scheme-integration.h"
static const int PORT = 5000;
@ -161,7 +162,7 @@ int main (int argc, char ** argv)
addNewConnection(&clientConnections, newSocket, tlsSession);
// Print a message:
printf("New connection established! %d clients, session ID %d.\n", clientConnections.clientCount, tlsSession);
printf("New connection established! %d clients, session ID %u.\n", clientConnections.clientCount, tlsSession);
}
else
{
@ -169,8 +170,8 @@ int main (int argc, char ** argv)
struct ClientConnection * connection = findConnectionByFileDescriptor(&clientConnections, events[index].data.fd);
if (connection != NULL)
{
char buffer[2048];
int returnValue = gnutls_record_recv(*connection->tlsSession, &buffer, 2048);
struct ClientToServerMessage message;
int returnValue = gnutls_record_recv(*connection->tlsSession, &message, sizeof(struct ClientToServerMessage));
if (returnValue == 0 || returnValue == -10)
{
printf("Closing session ID: %d\n", *connection->tlsSession);
@ -180,9 +181,9 @@ int main (int argc, char ** argv)
close(connection->fileDescriptor);
removeConnectionByFileDescriptor(&clientConnections, connection->fileDescriptor);
}
else if (returnValue == 2048)
else if (returnValue == sizeof(struct ClientToServerMessage))
{
printf("%s", buffer);
printf("%s", message.content);
fflush(stdout);
}
}