From e11a7b3a76d305c2bbbb1b103c299f46ac24f10b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barra=20=C3=93=20Cath=C3=A1in?= Date: Tue, 7 Nov 2023 22:46:15 +0000 Subject: [PATCH] Basic global messaging functions for Scheme --- source/server/main.c | 9 +++++++-- source/server/output-queue.h | 6 +++--- source/server/scheme-integration.c | 30 +++++++++++++++++++++++++++++- source/server/scheme-integration.h | 2 ++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/source/server/main.c b/source/server/main.c index 23fac61..fdc4475 100644 --- a/source/server/main.c +++ b/source/server/main.c @@ -106,6 +106,11 @@ int main (int argc, char ** argv) // Create some structures needed to store global state: struct PlayerList * globalPlayerList = createPlayerList(); struct OutputQueue * globalOutputQueue = createOutputQueue(); + + scm_c_define_gsubr("push-output-message", 6, 0, 0, &push_output_message); + scm_c_define("*globalPlayerList*", scm_from_pointer(globalPlayerList, NULL)); + scm_c_define("*globalOutputQueue*", scm_from_pointer(globalOutputQueue, NULL)); + // Start a REPL thread: //pthread_t schemeREPLThread; //pthread_create(&schemeREPLThread, NULL, schemeREPLHandler, NULL); @@ -214,10 +219,10 @@ int main (int argc, char ** argv) continue; } // ONLY FOR DEMO -] + pushOutputMessage(globalOutputQueue, false, globalPlayerList, LOCAL_CHAT, connection->player->name, message.content, - MESSAGE_NAME_LENGTH, MESSAGE_CONTENT_LENGTH); + MESSAGE_NAME_LENGTH, MESSAGE_CONTENT_LENGTH); } } else diff --git a/source/server/output-queue.h b/source/server/output-queue.h index d2b1f9a..b988ba8 100644 --- a/source/server/output-queue.h +++ b/source/server/output-queue.h @@ -14,8 +14,8 @@ struct OutputMessage { // Allows for easy reuse of existing player lists, such as the global list // or an area's playerlist: - bool deallocatePlayerList; - + bool deallocatePlayerList; + struct OutputMessage * next; struct PlayerList * recepients; struct ServerToClientMessage * message; @@ -37,7 +37,7 @@ size_t pushOutputMessage(struct OutputQueue * const queue, const char const * name, const char const * content, const size_t nameLength, const size_t contentLength); -struct OutputMessage * popOutputMessage(); +struct OutputMessage * popOutputMessage(struct OutputQueue * queue); void deallocateOutputMessage(struct OutputMessage ** message); diff --git a/source/server/scheme-integration.c b/source/server/scheme-integration.c index 56bd86d..ce61391 100644 --- a/source/server/scheme-integration.c +++ b/source/server/scheme-integration.c @@ -3,9 +3,37 @@ // | Copyright (C) 2023, Barra Ó Catháin | // | See end of file for copyright notice. | // =========================================== +#include #include -SCM scheme_get_player_by_name(SCM name); +#include "../messages.h" +#include "output-queue.h" + +SCM scheme_get_player_by_name(SCM name, SCM queue); + +SCM push_output_message(SCM queue, SCM deallocate_list, SCM recepients, SCM type, SCM name, SCM content) +{ + // Convert our scheme values into appropriate data types: + struct OutputQueue * queue_c = scm_to_pointer(queue); + bool deallocate_list_c = scm_to_bool(deallocate_list); + struct PlayerList * recepients_c = scm_to_pointer(recepients); + enum MessageTypes type_c = scm_to_int(type); + + // Turn the Scheme strings into C strings: + size_t nameLength, contentLength; + char * name_c = scm_to_locale_stringn(name, &nameLength); + char * content_c = scm_to_locale_stringn(content, &contentLength); + + // Call the C function: + pushOutputMessage(queue_c, deallocate_list_c, recepients_c, type_c, name_c, content_c, + nameLength, contentLength); + + // Free the created C strings: + free(name_c); + free(content_c); + + return SCM_BOOL_T; +} // ========================================================== // | End of scheme-integration.c, copyright notice follows. | diff --git a/source/server/scheme-integration.h b/source/server/scheme-integration.h index 9992bfb..41accfc 100644 --- a/source/server/scheme-integration.h +++ b/source/server/scheme-integration.h @@ -8,6 +8,8 @@ SCM scheme_get_player_by_name(SCM name); +SCM push_output_message(SCM queue, SCM deallocate_list, SCM recepients, SCM type, SCM name, SCM content); + #endif // ========================================================== // | End of scheme-integration.h, copyright notice follows. |