Modules

Module index




Module: net-do.lsp

Author: Dmitry Chernyak
Version: 1.2

Simple TCP/IP server suite

Location: http://en.feautec.pp.ru/store/libs/net-do.lsp

- § -

net-do:close

syntax: (net-do:close conn)

Close and cleanout (net-do) connections. Use within handlers only.

parameter: conn - connection id, passed to a handler

- § -

net-do

syntax: (net-do port addr timeout accept-f receive-f clean-f cycle-f idle-f)

Multiconnection TCP/IP server, based on (net-select)

The function opens a listen port and starts to accept connections.
The list of accepted connections is maintained in lconn internal symbol (dynamically available).
The specific handlers will be executed on various events (see Handlers).
The core of the function is the (net-select) cycle, which is executed while there are new data or new incoming connections received.
When no more events occured, idle-function is called and timeout is waited before next cycle.
The function finishes after the state of exit-cond internal symbol (dynamically available) is set to true. After that all connections are closed with clean-f handler execution and listen socket is closed too.
If the handler wants to close a connection, (net-do:close conn) must be called instead of (net-close).

parameter: port - local port to listen on
parameter: addr - local address to listen on or nil
parameter: timeout - idle timeout to wait if no events occured, micro-seconds

Handlers which may be defined (or nil, if no handler): parameter: accept-f - (fn(conn)) - on new connection acception
parameter: receive-f - (fn(conn)) - on new data received
parameter: clean-f - (fn(conn)) - on broken or closed connection
parameter: cycle-f - (fn()) - after each (net-select) cycle
parameter: idle-f - (fn()) - on idle cycle, if no (net-select) events occured

example:
 ; use "nc host 8080" to connect, ? to request connections and x to shutdown server
 (define (net-do-test)
  (net-do 8080 nil 1000000
 	 ; accept
 	 (fn(conn)
 	   (println "accept: " conn)
 	   (net-send conn "hello!\n"))
 	 ; receive
 	 (fn(conn , (buf ""))
 	   (println "receive: "conn)
 	   (net-receive conn 'buf 1000)
 	   (if
 	     (starts-with buf "?")
 	     (net-send conn (string net-do:lconn))
 	     (starts-with buf "x")
 	     (set 'net-do:exit-cond true))
 	   (net-send conn "bye!\n")
 	   (net-do:close conn))
 	 ; cleanout 
 	 (fn(conn) (println "closed: " conn))
 	 ; cycle
 	 nil
 	 ; idle
 	 (fn()(println "idle: " net-do:lconn))))

- ∂ -

generated with newLISP  and newLISPdoc