logging and unix socket fixes

cvs
Slava Pestov 2005-04-25 03:02:19 +00:00
parent dfd3901a39
commit 6e253bb8bb
11 changed files with 54 additions and 92 deletions

View File

@ -21,8 +21,6 @@
+ ffi: + ffi:
- char* struct members - char* struct members
- if a boxer triggers GC, already-pushed addresses might become
invalid!
- box/unbox_signed/unsigned_8 - box/unbox_signed/unsigned_8
- ffi unicode strings: null char security hole - ffi unicode strings: null char security hole
- utf16 string boxing - utf16 string boxing
@ -88,6 +86,7 @@
- generational gc - generational gc
- doc comments of generics - doc comments of generics
- proper ordering for classes - proper ordering for classes
- M: object should not inhibit delegation
+ i/o: + i/o:
@ -97,14 +96,12 @@
GENERIC: set-style ( style stream -- ) GENERIC: set-style ( style stream -- )
GENERIC: stream-write GENERIC: stream-write
GENERIC: stream-write-char GENERIC: stream-write-char
- linux/ppc and mac os x ffi - mac os x ffi
- stream server can hang because of exception handler limitations - stream server can hang because of exception handler limitations
- better i/o scheduler - better i/o scheduler
- add a socket timeout - add a socket timeout
- get sockets working
- renumber types appopriately - renumber types appopriately
- linux? freebsd? words, linux i/o stuff - linux? freebsd? words, linux i/o stuff
- clean up errors
- implement fcopy - implement fcopy
- unify unparse and prettyprint - unify unparse and prettyprint

View File

@ -79,3 +79,10 @@ math namespaces parser strings words ;
: END-UNION ( max -- ) : END-UNION ( max -- )
define-struct-type ; parsing define-struct-type ; parsing
BEGIN-STRUCT: int-box
FIELD: int i
END-STRUCT
: box-int ( n -- box )
<int-box> [ set-int-box-i ] keep ;

View File

@ -47,8 +47,8 @@ stdio streams strings threads url-encoding ;
: httpd-client ( socket -- ) : httpd-client ( socket -- )
[ [
[ dup log-client [
stdio get log-client read-line [ parse-request ] when* read-line [ parse-request ] when*
] with-stream ] with-stream
] try ; ] try ;
@ -58,11 +58,13 @@ stdio streams strings threads url-encoding ;
: httpd-loop ( -- ) httpd-connection httpd-loop ; : httpd-loop ( -- ) httpd-connection httpd-loop ;
: httpd ( port -- ) : httpd ( port -- )
[
<server> "http-server" set [ <server> "http-server" set [
httpd-loop httpd-loop
] [ ] [
"http-server" get stream-close rethrow "http-server" get stream-close rethrow
] catch ; ] catch
] with-logging ;
: stop-httpd ( -- ) : stop-httpd ( -- )
#! Stop the server. #! Stop the server.

View File

@ -8,24 +8,26 @@ SYMBOL: log-stream
: log ( msg -- ) : log ( msg -- )
#! Log a message to the log stream, either stdio or a file. #! Log a message to the log stream, either stdio or a file.
log-stream get dup [ log-stream get [
tuck stream-print stream-flush [ stream-print ] keep stream-flush
] [ ] [
2drop print flush
] ifte ; ] ifte* ;
: with-logging ( file quot -- )
#! Calls to log inside quot will output to a file.
[ swap <file-writer> log-stream set call ] with-scope ;
! Helpful words.
: log-error ( error -- ) "Error: " swap cat2 log ; : log-error ( error -- ) "Error: " swap cat2 log ;
: log-client ( client-stream -- ) : log-client ( client-stream -- )
[ [
"Accepted connection from " % "Accepted connection from " %
( dup ) client-stream-host % dup client-stream-host %
! CHAR: : , CHAR: : ,
! client-stream-port unparse % client-stream-port unparse %
] make-string log ; ] make-string log ;
: with-log-file ( file quot -- )
#! Calls to log inside quot will output to a file.
[ swap <file-writer> log-stream set call ] with-scope ;
: with-logging ( quot -- )
#! Calls to log inside quot will output to stdio.
[ stdio get log-stream set call ] with-scope ;

View File

@ -53,10 +53,6 @@ USE: alien
: TTF_FontFaceStyleName ( font -- n ) : TTF_FontFaceStyleName ( font -- n )
"char*" "sdl-ttf" "TTF_FontFaceStyleName" [ "void*" ] alien-invoke ; "char*" "sdl-ttf" "TTF_FontFaceStyleName" [ "void*" ] alien-invoke ;
BEGIN-STRUCT: int-box
FIELD: int i
END-STRUCT
: TTF_SizeUNICODE ( font text w h -- ? ) : TTF_SizeUNICODE ( font text w h -- ? )
"bool" "sdl-ttf" "TTF_SizeUNICODE" [ "void*" "ushort*" "int-box*" "int-box*" ] alien-invoke ; "bool" "sdl-ttf" "TTF_SizeUNICODE" [ "void*" "ushort*" "int-box*" "int-box*" ] alien-invoke ;

View File

@ -7,13 +7,6 @@ parser prettyprint stdio streams strings unparser vectors words ;
: expired-error. ( obj -- ) : expired-error. ( obj -- )
"Object did not survive image save/load: " write . ; "Object did not survive image save/load: " write . ;
: io-task-twice-error. ( obj -- )
"Attempting to perform two simultaneous I/O operations on "
write . ;
: no-io-tasks-error. ( obj -- )
"No I/O tasks" print ;
: undefined-word-error. ( obj -- ) : undefined-word-error. ( obj -- )
"Undefined word: " write . ; "Undefined word: " write . ;
@ -45,15 +38,9 @@ parser prettyprint stdio streams strings unparser vectors words ;
: c-string-error. ( obj -- ) : c-string-error. ( obj -- )
"Cannot convert to C string: " write . ; "Cannot convert to C string: " write . ;
: ffi-disabled-error. ( obj -- )
drop "Recompile Factor with #define FFI." print ;
: ffi-error. ( obj -- ) : ffi-error. ( obj -- )
"FFI: " write print ; "FFI: " write print ;
: port-closed-error. ( obj -- )
"Port closed: " write . ;
: heap-scan-error. ( obj -- ) : heap-scan-error. ( obj -- )
"Cannot do next-object outside begin/end-scan" write drop ; "Cannot do next-object outside begin/end-scan" write drop ;
@ -64,9 +51,6 @@ M: kernel-error error. ( error -- )
#! Kernel errors are indexed by integers. #! Kernel errors are indexed by integers.
cdr uncons car swap { cdr uncons car swap {
expired-error. expired-error.
io-task-twice-error.
no-io-tasks-error.
f
io-error. io-error.
undefined-word-error. undefined-word-error.
type-check-error. type-check-error.
@ -75,9 +59,7 @@ M: kernel-error error. ( error -- )
signal-error. signal-error.
negative-array-size-error. negative-array-size-error.
c-string-error. c-string-error.
ffi-disabled-error.
ffi-error. ffi-error.
port-closed-error.
heap-scan-error. heap-scan-error.
} vector-nth execute ; } vector-nth execute ;

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: io-internals IN: io-internals
USING: errors generic hashtables kernel lists math USING: errors generic hashtables kernel lists math
sequences stdio streams strings threads unix-internals vectors ; sequences streams strings threads unix-internals vectors ;
! We want namespaces::bind to shadow the bind system call from ! We want namespaces::bind to shadow the bind system call from
! unix-internals ! unix-internals
@ -20,9 +20,14 @@ USING: namespaces ;
TUPLE: port handle buffer error ; TUPLE: port handle buffer error ;
C: port ( handle buffer -- port ) C: port ( handle buffer -- port )
[ >r <buffer> r> set-delegate ] keep [
>r dup 0 > [ <buffer> ] [ drop f ] ifte r> set-delegate
] keep
[ >r dup init-handle r> set-port-handle ] keep ; [ >r dup init-handle r> set-port-handle ] keep ;
M: port stream-close ( port -- )
dup port-handle close buffer-free ;
: buffered-port 8192 <port> ; : buffered-port 8192 <port> ;
: >port< dup port-handle swap delegate ; : >port< dup port-handle swap delegate ;
@ -118,8 +123,6 @@ C: reader ( handle -- reader )
"reader not ready" throw "reader not ready" throw
] ifte ; ] ifte ;
M: reader stream-close ( stream -- ) port-handle close ;
! Reading lines ! Reading lines
: read-line-loop ( line buffer -- ? ) : read-line-loop ( line buffer -- ? )
dup buffer-length 0 = [ dup buffer-length 0 = [
@ -311,7 +314,7 @@ M: writer stream-write-attr ( string style writer -- )
nip >r dup string? [ ch>string ] unless r> blocking-write ; nip >r dup string? [ ch>string ] unless r> blocking-write ;
M: writer stream-close ( stream -- ) M: writer stream-close ( stream -- )
dup stream-flush dup port-handle close buffer-free ; dup stream-flush delegate stream-close ;
! Make a duplex stream for reading/writing a pair of fds ! Make a duplex stream for reading/writing a pair of fds
: <fd-stream> ( infd outfd flush? -- stream ) : <fd-stream> ( infd outfd flush? -- stream )
@ -337,6 +340,8 @@ M: writer stream-close ( stream -- )
2drop f 2drop f
] ifte ; ] ifte ;
USE: stdio
: init-io ( -- ) : init-io ( -- )
#! Should only be called on startup. Calling this at any #! Should only be called on startup. Calling this at any
#! other time can have unintended consequences. #! other time can have unintended consequences.

View File

@ -176,7 +176,7 @@ END-STRUCT
"int" "libc" "listen" [ "int" "int" ] alien-invoke ; "int" "libc" "listen" [ "int" "int" ] alien-invoke ;
: accept ( s sockaddr socklen -- n ) : accept ( s sockaddr socklen -- n )
"int" "libc" "accept" [ "int" "sockaddr-in*" "socklen_t" ] alien-invoke ; "int" "libc" "accept" [ "int" "sockaddr-in*" "int-box*" ] alien-invoke ;
: inet-ntoa ( sockaddr -- string ) : inet-ntoa ( sockaddr -- string )
"char*" "libc" "inet_ntoa" [ "in_addr_t" ] alien-invoke ; "char*" "libc" "inet_ntoa" [ "in_addr_t" ] alien-invoke ;

View File

@ -1,19 +1,14 @@
#define ERROR_EXPIRED (0<<3) #define ERROR_EXPIRED (0<<3)
#define ERROR_IO_TASK_TWICE (1<<3) #define ERROR_IO (1<<3)
#define ERROR_IO_TASK_NONE (2<<3) #define ERROR_UNDEFINED_WORD (2<<3)
#define ERROR_INCOMPATIBLE_PORT (3<<3) #define ERROR_TYPE (3<<3)
#define ERROR_IO (4<<3) #define ERROR_RANGE (4<<3)
#define ERROR_UNDEFINED_WORD (5<<3) #define ERROR_FLOAT_FORMAT (5<<3)
#define ERROR_TYPE (6<<3) #define ERROR_SIGNAL (6<<3)
#define ERROR_RANGE (7<<3) #define ERROR_NEGATIVE_ARRAY_SIZE (7<<3)
#define ERROR_FLOAT_FORMAT (8<<3) #define ERROR_C_STRING (8<<3)
#define ERROR_SIGNAL (9<<3) #define ERROR_FFI (9<<3)
#define ERROR_NEGATIVE_ARRAY_SIZE (10<<3) #define ERROR_HEAP_SCAN (10<<3)
#define ERROR_C_STRING (11<<3)
#define ERROR_FFI_DISABLED (12<<3)
#define ERROR_FFI (13<<3)
#define ERROR_CLOSED (14<<3)
#define ERROR_HEAP_SCAN (15<<3)
/* When throw_error throws an error, it sets this global and /* When throw_error throws an error, it sets this global and
longjmps back to the top-level. */ longjmps back to the top-level. */

View File

@ -2,7 +2,6 @@
void ffi_dlopen(DLL* dll) void ffi_dlopen(DLL* dll)
{ {
#ifdef FFI
void* dllptr; void* dllptr;
dllptr = dlopen(to_c_string(untag_string(dll->path)), RTLD_LAZY); dllptr = dlopen(to_c_string(untag_string(dll->path)), RTLD_LAZY);
@ -14,14 +13,10 @@ void ffi_dlopen(DLL* dll)
} }
dll->dll = dllptr; dll->dll = dllptr;
#else
general_error(ERROR_FFI_DISABLED,F);
#endif
} }
void *ffi_dlsym(DLL *dll, F_STRING *symbol) void *ffi_dlsym(DLL *dll, F_STRING *symbol)
{ {
#ifdef FFI
void* sym = dlsym(dll ? dll->dll : NULL, to_c_string(symbol)); void* sym = dlsym(dll ? dll->dll : NULL, to_c_string(symbol));
if(sym == NULL) if(sym == NULL)
{ {
@ -29,22 +24,15 @@ void *ffi_dlsym(DLL *dll, F_STRING *symbol)
from_c_string(dlerror()))); from_c_string(dlerror())));
} }
return sym; return sym;
#else
general_error(ERROR_FFI_DISABLED,F);
#endif
} }
void ffi_dlclose(DLL *dll) void ffi_dlclose(DLL *dll)
{ {
#ifdef FFI
if(dlclose(dll->dll) == -1) if(dlclose(dll->dll) == -1)
{ {
general_error(ERROR_FFI,tag_object( general_error(ERROR_FFI,tag_object(
from_c_string(dlerror()))); from_c_string(dlerror())));
} }
dll->dll = NULL; dll->dll = NULL;
#else
general_error(ERROR_FFI_DISABLED,F);
#endif
} }

View File

@ -2,7 +2,6 @@
void ffi_dlopen (DLL *dll) void ffi_dlopen (DLL *dll)
{ {
#ifdef FFI
HMODULE module; HMODULE module;
char *path = to_c_string(untag_string(dll->path)); char *path = to_c_string(untag_string(dll->path));
@ -12,14 +11,10 @@ void ffi_dlopen (DLL *dll)
general_error(ERROR_FFI, tag_object(last_error())); general_error(ERROR_FFI, tag_object(last_error()));
dll->dll = module; dll->dll = module;
#else
general_error(ERROR_FFI_DISABLED, F);
#endif
} }
void *ffi_dlsym (DLL *dll, F_STRING *symbol) void *ffi_dlsym (DLL *dll, F_STRING *symbol)
{ {
#ifdef FFI
void *sym = GetProcAddress(dll ? (HMODULE)dll->dll : GetModuleHandle(NULL), void *sym = GetProcAddress(dll ? (HMODULE)dll->dll : GetModuleHandle(NULL),
to_c_string(symbol)); to_c_string(symbol));
@ -27,17 +22,10 @@ void *ffi_dlsym (DLL *dll, F_STRING *symbol)
general_error(ERROR_FFI, tag_object(last_error())); general_error(ERROR_FFI, tag_object(last_error()));
return sym; return sym;
#else
general_error(ERROR_FFI_DISABLED, F);
#endif
} }
void ffi_dlclose (DLL *dll) void ffi_dlclose (DLL *dll)
{ {
#ifdef FFI
FreeLibrary((HMODULE)dll->dll); FreeLibrary((HMODULE)dll->dll);
dll->dll = NULL; dll->dll = NULL;
#else
general_error(ERROR_FFI_DISABLED, F);
#endif
} }