Partially fix httpd + telnetd w/ tasks when there's no connection.

This commit is contained in:
Brad Nelson
2022-01-03 22:51:22 -08:00
parent 5a05a4ff37
commit fc6700668f
7 changed files with 43 additions and 19 deletions

View File

@ -174,7 +174,9 @@
n0 = (cell_t) lseek(fd, tos, SEEK_SET); n0 = n0 < 0 ? errno : 0) \ n0 = (cell_t) lseek(fd, tos, SEEK_SET); n0 = n0 < 0 ? errno : 0) \
X("RESIZE-FILE", RESIZE_FILE, cell_t fd = n0; DROP; n0 = ResizeFile(fd, tos)) \ X("RESIZE-FILE", RESIZE_FILE, cell_t fd = n0; DROP; n0 = ResizeFile(fd, tos)) \
X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(n0, &st); \ X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(n0, &st); \
n0 = (cell_t) st.st_size; PUSH w < 0 ? errno : 0) n0 = (cell_t) st.st_size; PUSH w < 0 ? errno : 0) \
X("NON-BLOCK", NON_BLOCK, n0 = fcntl(n0, F_SETFL, O_NONBLOCK); \
n0 = n0 < 0 ? errno : 0)
#ifndef ENABLE_LEDC_SUPPORT #ifndef ENABLE_LEDC_SUPPORT
# define OPTIONAL_LEDC_SUPPORT # define OPTIONAL_LEDC_SUPPORT

View File

@ -30,16 +30,20 @@ sockaddr httpd-port sockaddr client variable client-len
: handleClient : handleClient
clientfd close-file drop clientfd close-file drop
-1 to clientfd
sockfd client client-len sockaccept sockfd client client-len sockaccept
dup 0< if drop exit then to clientfd dup 0< if drop 0 exit then
to clientfd
chunk chunk-size 0 fill chunk chunk-size 0 fill
chunk chunk-size clientfd read-file throw to chunk-filled chunk chunk-size clientfd read-file throw to chunk-filled
-1
; ;
: server ( port -- ) : server ( port -- )
httpd-port ->port! ." Listening on port " httpd-port ->port@ . cr httpd-port ->port! ." Listening on port " httpd-port ->port@ . cr
AF_INET SOCK_STREAM 0 socket to sockfd AF_INET SOCK_STREAM 0 socket to sockfd
( sockfd SOL_SOCKET SO_REUSEADDR 1 >r rp@ 4 setsockopt rdrop throw ) ( sockfd SOL_SOCKET SO_REUSEADDR 1 >r rp@ 4 setsockopt rdrop throw )
sockfd non-block throw
sockfd httpd-port sizeof(sockaddr_in) bind throw sockfd httpd-port sizeof(sockaddr_in) bind throw
sockfd max-connections listen throw sockfd max-connections listen throw
; ;

View File

@ -148,6 +148,11 @@ z" clock_gettime" 2 sysfunc clock_gettime
8 constant CLOCK_REALTIME_ALARM 8 constant CLOCK_REALTIME_ALARM
9 constant CLOCK_BOOTTIME_ALARM 9 constant CLOCK_BOOTTIME_ALARM
( File control )
z" fcntl" 3 sysfunc fcntl
4 constant F_SETFL
2048 constant FNDELAY ( 04000 )
forth definitions posix forth definitions posix
( Generic Files ) ( Generic Files )
@ -172,6 +177,9 @@ O_RDWR constant r/w
dup 0 SEEK_END lseek r> swap >r dup 0 SEEK_END lseek r> swap >r
SEEK_SET lseek drop r> d0<ior ; SEEK_SET lseek drop r> d0<ior ;
( Non-standard )
: non-block ( fh -- ior ) F_SETFL FNDELAY fcntl ;
( Other Utils ) ( Other Utils )
: ms ( n -- ) 1000 * usleep drop ; : ms ( n -- ) 1000 * usleep drop ;
: ms-ticks ( -- n ) : ms-ticks ( -- n )

View File

@ -36,4 +36,7 @@ z" setsockopt" 5 sysfunc setsockopt
: ->port@ ( a -- n ) 2 + >r r@ c@ 256 * r> 1+ c@ + ; : ->port@ ( a -- n ) 2 + >r r@ c@ 256 * r> 1+ c@ + ;
: ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ; : ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ;
( Fixup return )
: sockaccept sockaccept sign-extend ;
forth definitions forth definitions

View File

@ -32,13 +32,20 @@ defer broker
['] telnet-key is key ['] telnet-key is key
['] telnet-type is type quit ; ['] telnet-type is type quit ;
: wait-for-connection
begin
sockfd client client-len sockaccept
dup 0 >= if exit else drop then
again
;
: broker-connection : broker-connection
rp0 rp! sp0 sp! rp0 rp! sp0 sp!
begin begin
['] default-key is key ['] default-type is type ['] default-key is key ['] default-type is type
-1 echo ! -1 echo !
." Listening on port " telnet-port ->port@ . cr ." Listening on port " telnet-port ->port@ . cr
sockfd client client-len sockaccept wait-for-connection
." Connected: " dup . cr connection ." Connected: " dup . cr connection
again ; again ;
' broker-connection is broker ' broker-connection is broker
@ -46,6 +53,7 @@ defer broker
: server ( port -- ) : server ( port -- )
telnet-port ->port! telnet-port ->port!
AF_INET SOCK_STREAM 0 socket to sockfd AF_INET SOCK_STREAM 0 socket to sockfd
sockfd non-block throw
sockfd telnet-port sizeof(sockaddr_in) bind throw sockfd telnet-port sizeof(sockaddr_in) bind throw
sockfd 1 listen throw broker ; sockfd 1 listen throw broker ;

View File

@ -18,12 +18,9 @@ vocabulary termios termios definitions also posix
z" tcgetattr" 2 sysfunc tcgetattr z" tcgetattr" 2 sysfunc tcgetattr
z" tcsetattr" 3 sysfunc tcsetattr z" tcsetattr" 3 sysfunc tcsetattr
z" fcntl" 3 sysfunc fcntl
z" ioctl" 3 sysfunc ioctl z" ioctl" 3 sysfunc ioctl
( Blocking ) ( Blocking )
4 constant F_SETFL
2048 constant FNDELAY
: nodelay-mode stdin F_SETFL FNDELAY fcntl throw ; : nodelay-mode stdin F_SETFL FNDELAY fcntl throw ;
: delay-mode stdin F_SETFL 0 fcntl throw ; : delay-mode stdin F_SETFL 0 fcntl throw ;

View File

@ -47,11 +47,11 @@ body {
<body> <body>
<h2>ESP32forth v7</h2> <h2>ESP32forth v7</h2>
Upload File: <input id="filepick" type="file" name="files[]"></input><br/> Upload File: <input id="filepick" type="file" name="files[]"></input><br/>
<button onclick="ask('hex')">hex</button> <button onclick="ask('hex\n')">hex</button>
<button onclick="ask('decimal')">decimal</button> <button onclick="ask('decimal\n')">decimal</button>
<button onclick="ask('words')">words</button> <button onclick="ask('words\n')">words</button>
<button onclick="ask('low led pin')">LED OFF</button> <button onclick="ask('low led pin\n')">LED OFF</button>
<button onclick="ask('high led pin')">LED ON</button> <button onclick="ask('high led pin\n')">LED ON</button>
<br/> <br/>
<textarea id="output" readonly></textarea> <textarea id="output" readonly></textarea>
<input id="prompt" type="prompt"></input><br/> <input id="prompt" type="prompt"></input><br/>
@ -73,8 +73,9 @@ function httpPost(url, data, callback) {
r.open('POST', url); r.open('POST', url);
r.send(data); r.send(data);
} }
setInterval(function() { ask(''); }, 300);
function ask(cmd, callback) { function ask(cmd, callback) {
httpPost('/input', cmd + '\n', function(data) { httpPost('/input', cmd, function(data) {
if (data !== null) { output.value += data; } if (data !== null) { output.value += data; }
output.scrollTop = output.scrollHeight; // Scroll to the bottom output.scrollTop = output.scrollHeight; // Scroll to the bottom
if (callback !== undefined) { callback(); } if (callback !== undefined) { callback(); }
@ -83,7 +84,7 @@ function ask(cmd, callback) {
prompt.onkeyup = function(event) { prompt.onkeyup = function(event) {
if (event.keyCode === 13) { if (event.keyCode === 13) {
event.preventDefault(); event.preventDefault();
ask(prompt.value); ask(prompt.value + '\n');
prompt.value = ''; prompt.value = '';
} }
}; };
@ -102,7 +103,7 @@ filepick.onchange = function(event) {
} }
}; };
window.onload = function() { window.onload = function() {
ask(''); ask('\n');
prompt.focus(); prompt.focus();
}; };
</script> </script>
@ -130,10 +131,11 @@ create out-string out-size 1+ allot align
: serve-key ( -- n ) input-stream stream>ch ; : serve-key ( -- n ) input-stream stream>ch ;
: handle1 : handle1
handleClient handleClient if
s" /" path str= if handle-index exit then s" /" path str= if handle-index exit then
s" /input" path str= if handle-input exit then s" /input" path str= if handle-input exit then
notfound-response notfound-response
then
; ;
: do-serve begin handle1 pause again ; : do-serve begin handle1 pause again ;
@ -141,8 +143,8 @@ create out-string out-size 1+ allot align
: server ( port -- ) : server ( port -- )
server server
['] serve-type is type
['] serve-key is key ['] serve-key is key
['] serve-type is type
webserver-task start-task webserver-task start-task
; ;