From 8b0920c35dde3ad766bfc17528ce83e5891a4f1b Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Fri, 18 Aug 2023 00:45:24 +0100 Subject: [PATCH] Added basic implemantation of message structures. --- notes/SilverMUD-Implementation.org | 1 + src/messages.c | 113 +++++++++++++++++++++++++++++ src/messages.h | 59 +++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 src/messages.c create mode 100644 src/messages.h diff --git a/notes/SilverMUD-Implementation.org b/notes/SilverMUD-Implementation.org index 0c7e57b..88b1d7d 100644 --- a/notes/SilverMUD-Implementation.org +++ b/notes/SilverMUD-Implementation.org @@ -19,6 +19,7 @@ SilverMUD, as a scratchpad for decisions before implementation. Further values remain open for possible additional message types. *** Sender Name - 128 Character String. +This field may be repurposed for message types without a need for a name. *** Message Content - 1024 Character String. diff --git a/src/messages.c b/src/messages.c new file mode 100644 index 0000000..96e2d86 --- /dev/null +++ b/src/messages.c @@ -0,0 +1,113 @@ +// ========================================= +// | SilverMUD - messages.c | +// | Copyright (C) 2023, Barra Ó Catháin | +// | See end of file for copyright notice. | +// ========================================= +#include +#include +#include + +#include "messages.h" + +// Allocate and initialize a client to server message using the passed values: +struct ClientToServerMessage * createClientToServerMessage(char * content) +{ + // Allocate the needed memory for the message: + struct ClientToServerMessage * newMessage = calloc(1, sizeof(struct ClientToServerMessage)); + + // Copy the string and terminate it: + strncpy(newMessage->content, content, MESSAGE_CONTENT_LENGTH - 1); + newMessage->content[MESSAGE_CONTENT_LENGTH - 1] = '\0'; + + // Return the pointer: + return newMessage; +} + +// A Scheme wrapper for creating client to server messages: +SCM scheme_createClientToServerMessage(SCM content) +{ + // Check that we have been provided the right Scheme type: + if (scm_string_p(content)) + { + // Convert the Scheme string to a C string: + char * contentString = scm_to_locale_stringn(content, NULL); + + // Create the message: + struct ClientToServerMessage * message = createClientToServerMessage(contentString); + + // Free the converted string: + free(contentString); + + // Return the pointer as a Scheme object: + return scm_from_pointer(message); + } + else + { + return SCM_BOOL_F; + } +} + +// Allocate and initialize a server to client message using the passed values: +struct ServerToClientMessage * createServerToClientMessage(uint8_t type, char * name, char * content) +{ + // Allocate the needed memory for the message: + struct ServerToClientMessage * newMessage = calloc(1, sizeof(struct ServerToClientMessage)); + + // Copy the type: + newMessage->type = type; + + // Copy the strings and terminate them: + strncpy(newMessage->name, name, MESSAGE_NAME_LENGTH - 1); + newMessage->name[MESSAGE_NAME_LENGTH - 1] = '\0'; + strncpy(newMessage->content, content, MESSAGE_CONTENT_LENGTH - 1); + newMessage->content[MESSAGE_CONTENT_LENGTH - 1] = '\0'; + + return newMessage; +} + +// A Scheme wrapper for creating server to client messages: +SCM scheme_createServerToClientMessage(SCM type, SCM name, SCM content) +{ + // Check that we have been provided the right Scheme type: + if (scm_integer_p(type) && scm_string_p(name) && scm_string_p(content)) + { + // Convert the integer to a C integer: + uint8_t typeInteger = scm_to_uint8(type); + + // Convert the Scheme strings to C strings: + char * nameString = scm_to_locale_stringn(name, NULL); + char * contentString = scm_to_locale_stringn(content, NULL); + + // Create the message: + struct ServerToClientMessage * message = + createServerToClientMessage(typeInteger, nameString, contentString); + + // Free the converted string: + free(nameString); + free(contentString); + + // Return the pointer as a Scheme object: + return scm_from_pointer(message); + } + else + { + return SCM_BOOL_F; + } +} + +// ================================================ +// | End of messages.c, copyright notice follows. | +// ================================================ + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . diff --git a/src/messages.h b/src/messages.h new file mode 100644 index 0000000..e0814fd --- /dev/null +++ b/src/messages.h @@ -0,0 +1,59 @@ +// ========================================= +// | SilverMUD - messages.h | +// | Copyright (C) 2023, Barra Ó Catháin | +// | See end of file for copyright notice. | +// ========================================= +#ifndef MESSAGES_H +#define MESSAGES_H +#include + +const size_t MESSAGE_NAME_LENGTH = 128; +const size_t MESSAGE_CONTENT_LENGTH = 1024; + +enum MessageTypes +{ + SYSTEM, + CLIENT_SETTING, + COMMAND_OUTPUT, + LOCAL_CHAT, + PLAYER_CHAT, + PARTY_CHAT +}; + +struct ClientToServerMessage +{ + char content[MESSAGE_CONTENT_LENGTH] +}; + +struct ServerToClientMessage +{ + uint8_t type; + char name[MESSAGE_NAME_LENGTH]; + char content[MESSAGE_CONTENT_LENGTH]; +}; + +// Allocate and initialize a client to server message using the passed values: +struct ClientToServerMessage * createClientToServerMessage(char * content); +SCM scheme_createClientToServerMessage(SCM content); + + // Allocate and initialize a server to client message using the passed values: +struct ServerToClientMessage * createServerToClientMessage(uint8_t type, char * name, char * content); +SCM scheme_createServerToClientMessage(SCM type, SCM name, SCM content); + +#endif +// ================================================ +// | End of messages.h, copyright notice follows. | +// ================================================ + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see .