From 6e1d4cc163dc37f0ea5cdae8b3e2b9e4b0a73254 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Thu, 4 Aug 2022 11:13:39 +1000 Subject: socket: add support for -async Very similar to Tcl except that read/write can't be done until writable indicates the socket is connected. Signed-off-by: Steve Bennett Documentation fixes - Co-authored-by: Adrian Ho --- jim_tcl.txt | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'jim_tcl.txt') diff --git a/jim_tcl.txt b/jim_tcl.txt index f1ab2cd..2738d0b 100644 --- a/jim_tcl.txt +++ b/jim_tcl.txt @@ -5135,18 +5135,18 @@ Various socket types may be created. +*socket unix.dgram.server* 'path'+:: A unix domain socket datagram server server listening on 'path' -+*socket ?-ipv6? stream* 'addr:port'+:: ++*socket ?-async? ?-ipv6? stream* 'addr:port'+:: A TCP socket client. (See the forms for +'addr'+ below) -+*socket ?-ipv6? stream.server* '?addr:?port'+:: ++*socket ?-async? ?-ipv6? stream.server* '?addr:?port'+:: A TCP socket server (+'addr'+ defaults to +0.0.0.0+ for IPv4 or +[::]+ for IPv6). -+*socket ?-ipv6? dgram* ?'addr:port'?+:: ++*socket ?-async? ?-ipv6? dgram* ?'addr:port'?+:: A UDP socket client. If the address is not specified, the client socket will be unbound and 'sendto' must be used to indicated the destination. -+*socket ?-ipv6? dgram.server* 'addr:port'+:: ++*socket ?-async? ?-ipv6? dgram.server* 'addr:port'+:: A UDP socket server. +*socket pipe*+:: @@ -5209,6 +5209,38 @@ The path for Unix domain sockets is automatically removed when the socket is closed. Use `close -nodelete` in the rare case where this behaviour should be avoided (e.g. after `os.fork`). +If '+-async+' is specified, the socket is opened in non-blocking (ndelay) mode +and 'connect' is non-blocking (applies to 'stream'). In this case, a 'writable' handler +should be used that will fire when the connect succeeds or fails, and calling 'peername' on the handle +will succeed if connected or fail if connect failed. Typical usage is as follows: + +---- + set s [socket -async stream host:port] + + $s writable { + # Remove writable either way + $s writable {} + try { + $s peername + } on error msg { + # Connect failed + incr done + return + } + + $s readable { + set buf [$s read] + if {[$s eof]} { + # Closed connection + incr done + } + ... + } + } + + vwait done +---- + syslog ~~~~~~ +*syslog* '?options? ?priority? message'+ -- cgit v1.1