diff options
-rw-r--r-- | src/ChangeLog | 15 | ||||
-rw-r--r-- | src/include/sockio.h | 8 | ||||
-rw-r--r-- | src/interp/sys-os.boot | 18 | ||||
-rw-r--r-- | src/lib/sockio-c.c | 100 |
4 files changed, 141 insertions, 0 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index 04241cfd..483b732d 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,20 @@ 2008-08-28 Gabriel Dos Reis <gdr@cs.tamu.edu> + * interp/sys-os.boot (readFromFileHandle): New. + (writeToFileHandle): Likewise. + (closeFileHandle): Likewise. + (openLocalClientStreamSocket): Likewise + (oa_filedesc_read): Likewise. + (oa_filedesc_write): Likewise. + (oa_filedesc_close): Likewise. + * include/sockio.h (oa_open_local_client_stream_socket): Declare. + * lib/sockio-c.c (oa_open_local_client_stream_socket): Define. + (oa_filedesc_read): Likewise. + (oa_filedesc_write): Likewise. + (oa_filedesc_close): Likewise. + +2008-08-28 Gabriel Dos Reis <gdr@cs.tamu.edu> + * Makefile.pamphlet: Tidy. 2008-08-24 Gabriel Dos Reis <gdr@cs.tamu.edu> diff --git a/src/include/sockio.h b/src/include/sockio.h index 8efc2347..50fe3287 100644 --- a/src/include/sockio.h +++ b/src/include/sockio.h @@ -44,6 +44,7 @@ # include <sys/types.h> # include <sys/socket.h> # include <netinet/in.h> +# include <sys/un.h> #endif #include "openaxiom-c-macros.h" @@ -79,6 +80,13 @@ typedef struct openaxiom_sio { +OPENAXIOM_EXPORT int oa_open_local_client_stream_socket(const char*); +OPENAXIOM_EXPORT int oa_open_local_server_stream_socket(const char*); + +OPENAXIOM_EXPORT int oa_filedesc_write(int, const openaxiom_byte*, int); +OPENAXIOM_EXPORT int oa_filedesc_read(int, openaxiom_byte*, int); +OPENAXIOM_EXPORT int oa_filedesc_close(int); + OPENAXIOM_EXPORT int sread(openaxiom_sio*, openaxiom_byte*, int, const char*); OPENAXIOM_EXPORT int swrite(openaxiom_sio*, const openaxiom_byte*, int, const char*); diff --git a/src/interp/sys-os.boot b/src/interp/sys-os.boot index ab729a5c..85e9523d 100644 --- a/src/interp/sys-os.boot +++ b/src/interp/sys-os.boot @@ -61,6 +61,24 @@ import renameFile for import mkdir for oa__mkdir: string -> int -- 0: sucess, -1: failure. +import readFromFileHandle for + oa__filedesc__read: (int,buffer,int) -> int -- -1: failure; otherwise + -- actual read bytes count + +import writeToFileHandle for + oa__filedesc__write: (int,buffer,int) -> int -- -1: failure; otherwise + -- actual written bytes count + +import closeFileHandle for + oa__filedesc__close: int -> int -- -1: failure; otherwise 0. + +--% Local IPC socket support + +import openLocalClientStreamSocket for + oa__open__local__client__stream__socket: string -> int -- -1: failure + +--% OpenAxiom subsystem socket support + import getEnv for oa__getenv: string -> string diff --git a/src/lib/sockio-c.c b/src/lib/sockio-c.c index 492210f7..09f0b638 100644 --- a/src/lib/sockio-c.c +++ b/src/lib/sockio-c.c @@ -204,6 +204,106 @@ openaxiom_read(openaxiom_sio* s, openaxiom_byte* buf, size_t n) return recv(s->socket, buf, n, 0); } +/* Local IPC Socket: + On POSIX systems, this is just the Local IPC Socket. + On Windows, we Windows Named Pipes. + + Please, remember that Windows Named Pipes are closer to + Unix Domain Sockets than they are to Unix Named Pipes. They + ae full duplex communication links, supporting regular + file I/O operations. */ + +OPENAXIOM_EXPORT int +oa_open_local_client_stream_socket(const char* path) +{ +#ifdef __WIN32__ +# define NAMED_PIPE_PREFIX "\\\\.\\pipe" + char* pipename; + HANDLE handle; + + pipename = (char *) malloc(sizeof NAMED_PIPE_PREFIX + strlen(path)); + strcpy(pipename, NAMED_PIPE_PREFIX); + strcat(pipename, path); + + /* Try to open only an existing named pipe. */ + while (1) { + handle = CreateFile(/* lpFileName */ pipename, + /* dwDesiredAccess */ GENERIC_READ | GENERIC_WRITE, + /* dwSharedMode */ 0, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_EXISTING, + /* dwFlagsAttributes */ 0, + /* hTemplateFile */ NULL); + + if (handle != INVALID_HANDLE_VALUE) + return handle; + + if (GetLastError() != ERROR_PIPE_BUSY + || !WaitNamedPipe(pipename, NMPWAIT_WAIT_FOREVER)) + return -1; + } +# undef NAMED_PIPE_PREFIX +#else + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + struct sockaddr_un addr; + if (sock < 0) + return -1; + + memset(&addr, 0, sizeof addr); + addr.sun_family = AF_LOCAL; + strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); + if (connect(sock, (struct sockaddr*)&addr, sizeof addr) < 0) { + close(sock); + return -1; + } + return sock; +#endif +} + +OPENAXIOM_EXPORT int +oa_filedesc_read(int desc, openaxiom_byte* buf, int size) +{ +#ifdef __WIN32__ + DWORD count = -1; + if (ReadFile(/* hFile */ desc, + /* lpBuffer */ buf, + /* nNumberOfBytesToRead */ size, + /* lpNumberOfBytesRead */ &count, + /* lpOverlapped */ NULL)) + return count; + return -1; +#else + return read(desc, buf, size); +#endif +} + +OPENAXIOM_EXPORT int +oa_filedesc_write(int desc, const openaxiom_byte* buf, int size) +{ +#ifdef __WIN32__ + DWORD count = -1; + if (WriteFile(/* hFile */ desc, + /* lpBuffer */ buf, + /* nNumberOfBytesToWrite */ size, + /* lpNumberOfBytesWritten */ &count, + /* lpOverlapped */ NULL)) + return count; + return -1; +#else + return write(desc, buf, size); +#endif +} + +OPENAXIOM_EXPORT int +oa_filedesc_close(int desc) +{ +#ifdef __WIN32__ + return CloseHandle(desc) ? 0 : -1; +#else + return close(desc); +#endif +} + /* Return 1 is the last call was cancelled. */ |