From 24f3b3f3c6dddd3af70c63a66625872005241d23 Mon Sep 17 00:00:00 2001 From: Gavan Fantom Date: Wed, 19 Dec 2007 21:59:08 +0000 Subject: [PATCH] Implement cmdsocket_write to allow responses on the socket. --- src/lsi/abi.h | 1 + src/lsi/abispec | 1 + src/lsi/cmdsocket.c | 34 +++++++++++++++++++++++++++++++--- src/lsi/cmdsocket.h | 1 + src/lsi/vm.c | 18 ++++++++++++++++++ src/lsi/vm.h | 1 + 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/lsi/abi.h b/src/lsi/abi.h index 5b184f5..1c13a79 100644 --- a/src/lsi/abi.h +++ b/src/lsi/abi.h @@ -47,4 +47,5 @@ int vm_intfn_mouse_read(void); int vm_intfn_cmdsocket_listen(void); int vm_intfn_cmdsocket_accept(void); int vm_intfn_cmdsocket_read(void); +int vm_intfn_cmdsocket_write(void); int vm_intfn_cmdsocket_prefix(void); diff --git a/src/lsi/abispec b/src/lsi/abispec index b002fb4..634f603 100644 --- a/src/lsi/abispec +++ b/src/lsi/abispec @@ -36,6 +36,7 @@ function mouse_read function cmdsocket_listen function cmdsocket_accept function cmdsocket_read +function cmdsocket_write function cmdsocket_prefix /* diff --git a/src/lsi/cmdsocket.c b/src/lsi/cmdsocket.c index abaf283..ad85126 100644 --- a/src/lsi/cmdsocket.c +++ b/src/lsi/cmdsocket.c @@ -130,7 +130,7 @@ void cmd_parse(char *cmd) { char function[CMD_MAXSIZE + PREFIX_MAXSIZE]; *strchr(cmd, '\n') = '\0'; - printf("DEBUG: Received command: %s\n", cmd); +// printf("DEBUG: Received command: %s\n", cmd); fflush(stdout); sp = strtok(cmd, " \t\r"); @@ -143,7 +143,7 @@ void cmd_parse(char *cmd) { strcpy(function, cmd_prefix); strncat(function, cmd, CMD_MAXSIZE); - printf("DEBUG: function: %s, arg %d\n", function, arg); +// printf("DEBUG: function: %s, arg %d\n", function, arg); fflush(stdout); if (vm_spawn_args(function, 1, arg)) { /* Write an ack here, once a proper function exists */ @@ -154,7 +154,7 @@ void cmd_parse(char *cmd) { write(cmd_active, "ERROR\n", strlen("ERROR\n")); } - printf("Received command: %s\n", cmd); +// printf("Received command: %s\n", cmd); } int cmdsocket_read(void) @@ -202,3 +202,31 @@ int cmdsocket_read(void) } } } + +/* + * Returns offset to restart at. If off == len, then we have finished. + * If the return value is the same as off passed in, then the caller + * should sleep. + */ +int cmdsocket_write(char *buffer, int len, int off) +{ +// printf("cmdsocket_write called with %p, %d, %d\n", buffer, len, off); + if (!cmdsocket_initialised) + return 0; + if (!cmd_active) + return 0; + + while (off < len) { + int r; + r = write(cmd_active, buffer + off, len - off); + if (r == -1) { + if (errno == EAGAIN) + return off; + warn("error writing packet"); + } + if (r == 0) + return off; + off += r; + } + return off; +} diff --git a/src/lsi/cmdsocket.h b/src/lsi/cmdsocket.h index c061d8e..c40c502 100644 --- a/src/lsi/cmdsocket.h +++ b/src/lsi/cmdsocket.h @@ -5,4 +5,5 @@ void cmdsocket_close(void); int cmdsocket_listen(int port); int cmdsocket_accept(void); int cmdsocket_read(void); +int cmdsocket_write(char *buffer, int len, int off); int cmdsocket_prefix(char *); diff --git a/src/lsi/vm.c b/src/lsi/vm.c index b85a498..9f72b64 100644 --- a/src/lsi/vm.c +++ b/src/lsi/vm.c @@ -464,6 +464,24 @@ int vm_intfn_cmdsocket_read(void) return 1; } +int vm_intfn_cmdsocket_write(void) +{ + int off = stack_get(vm_current, 1); + int len = stack_get(vm_current, 2); + char *buffer = stack_getstr(vm_current, len, 2); + int newoff; + + newoff = cmdsocket_write(buffer, len, off); + stack_poke(vm_current, 1, newoff); + + if (newoff != len) { + vm_queue(vm_current, VM_CMDWRITEQ); + vm_current = NULL; + return 0; + } + return 1; +} + int vm_intfn_beatdetect_read(void) { if (!beatdetect_read()) { diff --git a/src/lsi/vm.h b/src/lsi/vm.h index 3494097..ba90d4e 100644 --- a/src/lsi/vm.h +++ b/src/lsi/vm.h @@ -18,6 +18,7 @@ void vm_wakeup(int queue); #define VM_MIDIQ 8 #define VM_CMDLISTENQ 9 #define VM_CMDREADQ 10 +#define VM_CMDWRITEQ 20 #define VM_USERQMIN 20 #define VM_NOQUEUE (-1)