moontalk/server/eventloop-server-experiment/sendbuffer.4th
Emil Williams 7721b77c93
update
2024-02-03 17:37:01 +00:00

29 lines
974 B
Forth

variable sendbuffer-len 0 sendbuffer-len !
2048 constant SENDBUFFER_SIZE
create sendbuffer SENDBUFFER_SIZE allot
\ Calling C here is just optimization.
c-library sanitizelib
\c void csanitize(char *buffer, int buffersize) {
\c int lastIsNewline = buffer[buffersize-1] == '\n' ? 1 : 0;
\c for(int i = 0; i<buffersize; i++) {
\c if(buffer[i]<32 || buffer[i]>127) { buffer[i] = '?'; }
\c }
\c if(lastIsNewline) { buffer[buffersize-1] = '\n'; }
\c return;
\c }
c-function csanitize csanitize a n -- void
end-c-library
: sendbuffer-reset ( -- ) 0 sendbuffer-len ! ;
: (overflow?) ( n -- flag )
sendbuffer-len @ + SENDBUFFER_SIZE u> ;
: (append) ( str -- )
dup -rot sendbuffer sendbuffer-len @ + swap move
sendbuffer-len +! ;
: sendbuffer-append ( str -- )
dup (overflow?) abort" sendbuffer overflow" (append) ;
: sendbuffer-sanitize ( -- )
sendbuffer sendbuffer-len @ csanitize ;
: sendbuffer@ ( -- str ) sendbuffer sendbuffer-len @ ;