From 76faf8a71ca29aef0de066fb3a31823227b6d95d Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Fri, 5 Feb 2016 16:38:58 +1000 Subject: The next step in cleaning up the network Lua messages. --- src/LuaSL/LuaSL_main.c | 70 +++++++++------ src/extantz/extantz.c | 8 +- src/libraries/SledjHamr.c | 211 ++++++++++++++++++++++++++++------------------ src/libraries/SledjHamr.h | 8 +- src/love/love.c | 32 ++++--- 5 files changed, 195 insertions(+), 134 deletions(-) (limited to 'src') diff --git a/src/LuaSL/LuaSL_main.c b/src/LuaSL/LuaSL_main.c index d7223f9..cf3c946 100644 --- a/src/LuaSL/LuaSL_main.c +++ b/src/LuaSL/LuaSL_main.c @@ -71,19 +71,37 @@ static script *findThem(gameGlobals *ourGlobals, const char *base, const char *t static void send2server(script *me, const char *message) { - gameGlobals *ourGlobals = me->data; + takeScript(me); + // NOTE - This gets intercepted by send2parser() below, before possibly being sent away. + send2(me->client, me->SID, message); + releaseScript(me); +} + +static boolean send2parser(void *data, Connection *connection, char *SID, char *command, char *arguments) +{ + gameGlobals *ourGlobals = data; + char buf[PATH_MAX]; + script *me = getScript(SID); + + if (NULL == me) + { +// PE("GOT MESSAGE from UNKNOWN script %s - '%s' (%s", SID, command, arguments); + return FALSE; + } + + sprintf(buf, "%s(%s", command, arguments); -//PD("GOT MESSAGE from script %s - '%s'", me->name, message); +//PD("GOT MESSAGE from script %s - '%s'", me->name, buf); - if (0 == strncmp(message, "llSleep(", 8)) - ecore_timer_add(atof(&(message)[8]), _sleep_timer_cb, me); - else if (0 == strncmp(message, "llResetTime(", 12)) + if (0 == strncmp(buf, "llSleep(", 8)) + ecore_timer_add(atof(&(buf)[8]), _sleep_timer_cb, me); + else if (0 == strncmp(buf, "llResetTime(", 12)) { takeScript(me); gettimeofday(&me->startTime, NULL); releaseScript(me); } - else if (0 == strncmp(message, "llGetTime(", 10)) + else if (0 == strncmp(buf, "llGetTime(", 10)) { struct timeval now; float time = timeDiff(&now, &me->startTime); @@ -92,7 +110,7 @@ static void send2server(script *me, const char *message) snprintf(result, sizeof(result), "return %f", time); send2script(me->SID, result); } - else if (0 == strncmp(message, "llGetAndResetTime(", 18)) + else if (0 == strncmp(buf, "llGetAndResetTime(", 18)) { struct timeval now; float time = timeDiff(&now, &me->startTime); @@ -105,10 +123,10 @@ static void send2server(script *me, const char *message) snprintf(result, sizeof(result), "return %f", time); send2script(me->SID, result); } - else if (0 == strncmp(message, "llSetTimerEvent(", 16)) + else if (0 == strncmp(buf, "llSetTimerEvent(", 16)) { takeScript(me); - me->timerTime = atof(&(message)[16]); + me->timerTime = atof(&(buf)[16]); if (0.0 == me->timerTime) { if (me->timer) @@ -119,13 +137,13 @@ static void send2server(script *me, const char *message) me->timer = ecore_timer_add(me->timerTime, _timer_timer_cb, me); releaseScript(me); } - else if (0 == strncmp(message, "llSetScriptState(", 17)) + else if (0 == strncmp(buf, "llSetScriptState(", 17)) { script *them; - if ((them = findThem(ourGlobals, me->fileName, &(message[18])))) + if ((them = findThem(ourGlobals, me->fileName, &(buf[18])))) { - char *temp = rindex(&(message[18]), ','); + char *temp = rindex(&(buf[18]), ','); if (temp) { @@ -143,34 +161,32 @@ static void send2server(script *me, const char *message) } else { - char *temp = rindex(&(message[18]), '"'); + char *temp = rindex(&(buf[18]), '"'); if (temp) *temp = '\0'; - PE("Can't stop script, can't find %s", &(message[18])); + PE("Can't stop script, can't find %s", &(buf[18])); } } - else if (0 == strncmp(message, "llResetOtherScript(", 19)) + else if (0 == strncmp(buf, "llResetOtherScript(", 19)) { script *them; - if ((them = findThem(ourGlobals, me->fileName, &(message[20])))) + if ((them = findThem(ourGlobals, me->fileName, &(buf[20])))) { PD("RESETTING OTHER %s", them->name); resetScript(them); } } - else if (0 == strncmp(message, "llResetScript(", 14)) + else if (0 == strncmp(buf, "llResetScript(", 14)) { PD("RESETTING %s", me->name); resetScript(me); } else - { - takeScript(me); - send2(me->client, me->SID, message); - releaseScript(me); - } + return FALSE; + + return TRUE; } static void _compileCb(LuaCompiler *compiler) @@ -218,12 +234,12 @@ static void _compileCbSingle(LuaCompiler *compiler) printf("Compile of %s failed!\n", compiler->file); } -static Eina_Bool parser(void *data, Connection *connection, char *SID, char *command, char *arguments) +static boolean parser(void *data, Connection *connection, char *SID, char *command, char *arguments) { gameGlobals *ourGlobals = data; char buf[PATH_MAX]; -//PD("PARSE COMMAND - %s", command); +//PD("PARSE COMMAND %s - '%s' (%s", SID, command, arguments); if (0 == strcmp(command, "compile")) { char *temp; @@ -275,6 +291,8 @@ static Eina_Bool parser(void *data, Connection *connection, char *SID, char *com } else { + // TODO - Even after moving the above into the command hash, this bit might still remain, unless script.return() can make it go away. + // Though perhaps the "else" part stays? if (0 == strcmp("return", command)) snprintf(buf, sizeof(buf), "%s %s", command, arguments); else @@ -283,7 +301,7 @@ static Eina_Bool parser(void *data, Connection *connection, char *SID, char *com send2script(SID, buf); } - return ECORE_CALLBACK_RENEW; + return TRUE; } int main(int argc, char **argv) @@ -320,7 +338,7 @@ int main(int argc, char **argv) else if (ecore_con_init()) { // PD("LuaSL is about to try creating a LuaSL server."); - if (openArms("LuaSL", ourGlobals.address, ourGlobals.port, &ourGlobals, NULL, NULL, NULL, parser)) + if (openArms("LuaSL", ourGlobals.address, ourGlobals.port, &ourGlobals, NULL, NULL, NULL, parser, send2parser)) { Eina_Iterator *scripts; script *me; diff --git a/src/extantz/extantz.c b/src/extantz/extantz.c index 6dc7c3b..089cc41 100644 --- a/src/extantz/extantz.c +++ b/src/extantz/extantz.c @@ -33,7 +33,7 @@ static Eina_Bool _add(void *data, int type, Ecore_Con_Event_Server_Add *ev) return ECORE_CALLBACK_RENEW; } -static Eina_Bool clientParser(void *data, Connection *connection, char *SID, char *command, char *arguments) +static boolean clientParser(void *data, Connection *connection, char *SID, char *command, char *arguments) { globals *ourGlobals = data; char buf[PATH_MAX]; @@ -111,9 +111,9 @@ static Eina_Bool clientParser(void *data, Connection *connection, char *SID, cha #endif } else - PI("Some random command %s(%s", command, arguments); + return FALSE; - return ECORE_CALLBACK_RENEW; + return TRUE; } static Eina_Bool _del(void *data, int type, Ecore_Con_Event_Server_Del *ev) @@ -560,7 +560,7 @@ static Eina_Bool _makeSkang(void *data) #if USE_LOVE case 5 : // PD("About to try connecting to a love server."); - reachOut("love", "./love", "127.0.0.1", 8211 + 1, ourGlobals, (Ecore_Event_Handler_Cb) _add, /*(Ecore_Event_Handler_Cb) _data*/ NULL, (Ecore_Event_Handler_Cb) _del, clientParser); + reachOut("love", "./love", "127.0.0.1", 8211 + 1, ourGlobals, (Ecore_Event_Handler_Cb) _add, /*(Ecore_Event_Handler_Cb) _data*/ NULL, (Ecore_Event_Handler_Cb) _del, clientParser, NULL); break; #endif diff --git a/src/libraries/SledjHamr.c b/src/libraries/SledjHamr.c index b44c1cb..7092c10 100644 --- a/src/libraries/SledjHamr.c +++ b/src/libraries/SledjHamr.c @@ -88,78 +88,41 @@ static boolean checkConnection(Connection *conn, char *func, connType wanted, bo return result; } -void send2(Connection *conn, const char *SID, const char *message, ...) -{ - va_list args; - char buf[PATH_MAX * 2]; - int length = strlen(SID); - - strncpy(buf, SID, length); - buf[length++] = '.'; - va_start(args, message); - length += vsprintf(&buf[length], message, args); - va_end(args); - buf[length++] = '\n'; - buf[length] = '\0'; -// TODO - Should check if this is always gonna be local? Likely not. -// TODO - Figure out what the above line meant. Then ... -// Check a hashtable somewhere for a command extracted from the message. -// conn->commands[command](message ...) - if (checkConnection(conn, "send2", conn->type, FALSE)) - { - switch (conn->type) - { - case CT_CLIENT : -// PD("vvv send2(%*s", length, buf); - ecore_con_client_send(conn->conn.client.client, strndup(buf, length), length); - ecore_con_client_flush(conn->conn.client.client); - break; - - case CT_SERVER : -// PD("^^^ send2(%*s", length, buf); - ecore_con_server_send(conn->conn.server.server, strndup(buf, length), length); - ecore_con_server_flush(conn->conn.server.server); - break; - - default : - PE("send2() unable to send to partially bogus Connection object!"); - break; - } - } - else - PE("send2() unable to send to bogus Connection object!"); -} +typedef boolean (* notFoundCb)(Connection *conn, char *command, int len); -static Eina_Bool parseStream(void *data, int type, void *evData, int evSize, void *ev) +/* TODO - + It should loop through all lines, looking for the closing ) before dealing with it. + If we get to the end, and there's left over open ( or something, then return the length. + Bitch and move on if it's not well formed. +*/ +static int _checkForCommand(Connection *conn, char *commands, notFoundCb notFound, boolean in) { - Connection *conn = data; - char SID[PATH_MAX]; - const char *command; - char *ext; + int result = 0; + boolean handled = FALSE; + char *ext, *ext2; - if (NULL == conn->stream) - conn->stream = eina_strbuf_new(); + while ((ext = index(commands, '\n'))) + { + char SID[PATH_MAX * 3]; + int length = ext - commands; - eina_strbuf_append_length(conn->stream, evData, evSize); - command = eina_strbuf_string_get(conn->stream); - while ((ext = index(command, '\n'))) + strncpy(SID, commands, length + 1); + + SID[length] = '\0'; + + ext = index(SID, '.'); + if (ext) { - int length = ext - command; + char *command = ext + 1; - strncpy(SID, command, length + 1); - SID[length] = '\0'; - eina_strbuf_remove(conn->stream, 0, length + 1); - ext = index(SID, '.'); - if (ext) - { - ext[0] = '\0'; - command = ext + 1; - ext = index(command, '('); - if (NULL == ext) - ext = index(command, ' '); - if (ext) - { + ext[0] = '\0'; + ext2 = index(command, '('); + if (NULL == ext2) + ext2 = index(command, ' '); + if (ext2) + { + ext2[0] = '\0'; // TODO - Currently SID.command(arguments), should change that to nameSpace.command(arguments) // Not sure what to do with SID, but there maybe some common parameters we can shift around differently. // - First check if the connection has a hashtable of conn->commands. @@ -171,22 +134,101 @@ static Eina_Bool parseStream(void *data, int type, void *evData, int evSize, voi // Finally pass it to conn->unknownCommand() // * The Lua check can live in unknownCommand(). // else bitch. - streamParser func = eina_hash_find(conn->commands, command); - - ext[0] = '\0'; - // Need a callback if we can't find the command. - if (NULL == func) - func = conn->unknownCommand; - if (func) - func(conn->pointer, conn, SID, (char *) command, ext + 1); - else - PE("parseStream() No function found for command %s!", command); - } + + // Check if it's in the connections hash table. + streamParser func = eina_hash_find(conn->commands, command); + + if (NULL == func) + { + // Check if the connection has a function for handling it. + if (in) + func = conn->unknownInCommand; + else + func = conn->unknownOutCommand; + } + + // Try it out if we have a function. + if (func) + { + handled = func(conn->pointer, conn, SID, command, ext2 + 1); + if (handled) + notFound = NULL; } - // Get the next blob to check it. - command = eina_strbuf_string_get(conn->stream); + // Last resort, let the caller deal with it. + if (notFound) + handled = notFound(conn, commands, length + 1); + if (!handled) + PE("No function found for command %s(%s!", command, ext2 + 1); + + result += length; + } + } + + commands = &commands[length + 1]; + } + + return result; +} + +static boolean _actuallySendIt(Connection *conn, char *command, int len) +{ + if (checkConnection(conn, "send2", conn->type, FALSE)) + { + switch (conn->type) + { + case CT_CLIENT : +// PD("vvv send2(%*s", len, command); + ecore_con_client_send(conn->conn.client.client, strndup(command, len), len); + ecore_con_client_flush(conn->conn.client.client); + return TRUE; + + case CT_SERVER : +// PD("^^^ send2(%*s", len, command); + ecore_con_server_send(conn->conn.server.server, strndup(command, len), len); + ecore_con_server_flush(conn->conn.server.server); + return TRUE; + + default : + PE("send2() unable to send to partially bogus Connection object!"); + break; + } } + else + PE("send2() unable to send to bogus Connection object!"); + + return FALSE; +} + +void send2(Connection *conn, const char *SID, const char *message, ...) +{ + va_list args; + char buf[PATH_MAX * 3]; + int length = strlen(SID); + + strncpy(buf, SID, length); + buf[length++] = '.'; + va_start(args, message); + length += vsprintf(&buf[length], message, args); + va_end(args); + buf[length++] = '\n'; + buf[length] = '\0'; + +//PD("%s", (char *) buf); +// TODO - Do we care about the returned length? Either the caller sent us proper commands, or they didn't. + _checkForCommand(conn, buf, _actuallySendIt, FALSE); +} + +static Eina_Bool parseStream(void *data, int type, void *evData, int evSize, void *ev) +{ + Connection *conn = data; + + if (NULL == conn->stream) + conn->stream = eina_strbuf_new(); + + eina_strbuf_append_length(conn->stream, evData, evSize); +//PD("%s", (char *) eina_strbuf_string_get(conn->stream)); + eina_strbuf_remove(conn->stream, 0, _checkForCommand(conn, (char *) eina_strbuf_string_get(conn->stream), NULL, TRUE)); if (conn->_data) conn->_data(conn->pointer, type, ev); @@ -234,7 +276,8 @@ static Eina_Bool clientAdd(void *data, int type, Ecore_Con_Event_Client_Add *ev) conn->_add = connection->_add; conn->_data = connection->_data; conn->_del = connection->_del; - conn->unknownCommand = connection->unknownCommand; + conn->unknownInCommand = connection->unknownInCommand; + conn->unknownOutCommand = connection->unknownOutCommand; conn->commands = eina_hash_string_superfast_new(NULL); ecore_con_client_data_set(ev->client, conn); @@ -284,7 +327,7 @@ static Eina_Bool clientDel(void *data, int type, Ecore_Con_Event_Client_Del *ev) return ECORE_CALLBACK_RENEW; } -Connection *openArms(char *name, const char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _parser) +Connection *openArms(char *name, const char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _inParser, streamParser _outParser) { Connection *conn = calloc(1, sizeof(Connection)); Ecore_Con_Server *server; @@ -299,7 +342,8 @@ Connection *openArms(char *name, const char *address, int port, void *data, Ecor conn->_add = _add; conn->_data = _data; conn->_del = _del; - conn->unknownCommand = _parser; + conn->unknownInCommand = _inParser; + conn->unknownOutCommand = _outParser; conn->commands = eina_hash_string_superfast_new(NULL); conn->add = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb) clientAdd, conn); @@ -449,7 +493,7 @@ static Eina_Bool _reachOutTimer(void *data) return result; } -Connection *reachOut(char *name, char *command, char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _parser) +Connection *reachOut(char *name, char *command, char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _inParser, streamParser _outParser) { Connection *conn = calloc(1, sizeof(Connection)); @@ -464,7 +508,8 @@ Connection *reachOut(char *name, char *command, char *address, int port, void *d conn->_add = _add; conn->_data = _data; conn->_del = _del; - conn->unknownCommand = _parser; + conn->unknownInCommand = _inParser; + conn->unknownOutCommand = _outParser; conn->commands = eina_hash_string_superfast_new(NULL); conn->add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb) serverAdd, conn); diff --git a/src/libraries/SledjHamr.h b/src/libraries/SledjHamr.h index d5e0b1f..061f081 100644 --- a/src/libraries/SledjHamr.h +++ b/src/libraries/SledjHamr.h @@ -10,7 +10,7 @@ typedef struct _Connection Connection; -typedef Eina_Bool (* streamParser)(void *data, Connection *connection, char *SID, char *command, char *arguments); +typedef boolean (* streamParser)(void *data, Connection *connection, char *SID, char *command, char *arguments); typedef enum { @@ -59,7 +59,7 @@ struct _Connection void *pointer; Ecore_Event_Handler_Cb _add, _data, _del; Ecore_Event_Handler *add, *data, *del, *died; - streamParser unknownCommand; + streamParser unknownInCommand, unknownOutCommand; int stage; // Stage of creation for the Connection. }; @@ -67,7 +67,7 @@ struct _Connection void *addMessage(Eina_Clist *list, size_t size, const char *message, ...); void send2(Connection *conn, const char *SID, const char *message, ...); -Connection *openArms(char *name, const char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _parser); -Connection *reachOut(char *name, char *command, char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _parser); +Connection *openArms(char *name, const char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _inParser, streamParser _outParser); +Connection *reachOut(char *name, char *command, char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _inParser, streamParser _outParser); #endif diff --git a/src/love/love.c b/src/love/love.c index 9ad9842..8d1bcd4 100644 --- a/src/love/love.c +++ b/src/love/love.c @@ -191,7 +191,7 @@ char *get_rawline(int fd, long *plen, char end) } -static Eina_Bool LuaSLParser(void *data, Connection *conn, char *SID, char *command, char *arguments) +static boolean LuaSLParser(void *data, Connection *conn, char *SID, char *command, char *arguments) { gameGlobals *ourGlobals = data; char buf[PATH_MAX]; @@ -256,22 +256,20 @@ static Eina_Bool LuaSLParser(void *data, Connection *conn, char *SID, char *comm struct timeval now; compiledCount++; + if ('t' == arguments[0]) + send2(ourGlobals->serverLuaSL, SID, "run(%s)", me->fileName); + else + PE("The compile of %s.%s failed!", SID, me->fileName); + if (compiledCount == scriptCount) { float total = timeDiff(&now, &startTime); + PI("Compile speed scripts: %d time: %fs total: %f scripts per second", compiledCount, total, compiledCount / total); } } - - if ('t' == arguments[0]) - { -//PD("About to run %s", me->fileName); - send2(ourGlobals->serverLuaSL, SID, "run(%s)", me->fileName); - } else - { -// PE("The compile of %s failed!", SID); - } + PE("We got a compiled() on non existing script - %s?", SID); } else { @@ -453,10 +451,10 @@ static Eina_Bool LuaSLParser(void *data, Connection *conn, char *SID, char *comm } else - PI("Script %s sent command %s(%s", SID, command, arguments); + return FALSE; } - return ECORE_CALLBACK_RENEW; + return TRUE; } static Eina_Bool _delLuaSL(void *data, int type, Ecore_Con_Event_Server_Del *ev) @@ -489,7 +487,7 @@ static Eina_Bool _addClient(void *data, int type, Ecore_Con_Event_Client_Add *ev return ECORE_CALLBACK_RENEW; } -static Eina_Bool clientParser(void *data, Connection *conn, char *SID, char *command, char *arguments) +static boolean clientParser(void *data, Connection *conn, char *SID, char *command, char *arguments) { gameGlobals *ourGlobals = data; @@ -524,9 +522,9 @@ static Eina_Bool clientParser(void *data, Connection *conn, char *SID, char *com eina_iterator_free(scripts); } else - PW("Unknown command from client - %s(%s", command, arguments); + return FALSE; - return ECORE_CALLBACK_RENEW; + return TRUE; } static Eina_Bool _delClient(void *data, int type, Ecore_Con_Event_Client_Del *ev) @@ -670,10 +668,10 @@ int main(int argc, char **argv) // PD("About to try connecting to a LuaSL server."); // Try to connect to a local LuaSL server. - reachOut("LuaSL", "./LuaSL", "127.0.0.1", ourGlobals.port, &ourGlobals, (Ecore_Event_Handler_Cb) _addLuaSL, /*(Ecore_Event_Handler_Cb) _dataLuaSL*/ NULL, (Ecore_Event_Handler_Cb) _delLuaSL, LuaSLParser); + reachOut("LuaSL", "./LuaSL", "127.0.0.1", ourGlobals.port, &ourGlobals, (Ecore_Event_Handler_Cb) _addLuaSL, /*(Ecore_Event_Handler_Cb) _dataLuaSL*/ NULL, (Ecore_Event_Handler_Cb) _delLuaSL, LuaSLParser, NULL); // PD("Love is about to try creating a love server."); - if (openArms("love", ourGlobals.address, ourGlobals.port + 1, &ourGlobals, (Ecore_Event_Handler_Cb) _addClient, NULL, (Ecore_Event_Handler_Cb) _delClient, clientParser)) + if (openArms("love", ourGlobals.address, ourGlobals.port + 1, &ourGlobals, (Ecore_Event_Handler_Cb) _addClient, NULL, (Ecore_Event_Handler_Cb) _delClient, clientParser, NULL)) { ecore_main_loop_begin(); PD("Fell out of the main loop"); -- cgit v1.1