moontalk/server/eventloop-server-experiment/sendbuffer.4th

29 lines
974 B
Plaintext
Raw Normal View History

2024-02-03 12:37:01 -05:00
variable sendbuffer-len 0 sendbuffer-len !
2024-02-03 14:15:37 -05:00
4096 constant SENDBUFFER_SIZE
2024-02-03 12:37:01 -05:00
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++) {
2024-02-03 14:15:37 -05:00
\c if(buffer[i]<32 || buffer[i]>126) { buffer[i] = '?'; }
2024-02-03 12:37:01 -05:00
\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 @ ;