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) \
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); \
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
# define OPTIONAL_LEDC_SUPPORT

View File

@ -30,16 +30,20 @@ sockaddr httpd-port sockaddr client variable client-len
: handleClient
clientfd close-file drop
-1 to clientfd
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 clientfd read-file throw to chunk-filled
-1
;
: server ( port -- )
httpd-port ->port! ." Listening on port " httpd-port ->port@ . cr
AF_INET SOCK_STREAM 0 socket to sockfd
( 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 max-connections listen throw
;

View File

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

View File

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

View File

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

View File

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