emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature suggestion: server sockets


From: Kim F. Storm
Subject: Re: Feature suggestion: server sockets
Date: 18 Feb 2002 16:10:45 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2.50

In gnu.emacs.bug, address@hidden (Helmut Eller) wrote:

> Dear Emacs Developers,
> 
> Emacs has currently no support for server sockets.  Server sockets
> (sometimes called passive sockets) would be useful for packages like
> emacserver, gnuserv, or EmacsHttpd.  Such "server like" packages are
> currently somewhat hard to write, because they must use an external
> program that opens the server socket and forwards the input to Emacs.
> With the patch below, Emacs would be able to open server sockets
> itself.

This sounds like a useful addition to me.

I am currently working on a different aspect of network support
(optionally making open-network-stream non-blocking), and I have
modified open-network-stream to take two optional arguments:
        FILTER and SENTINEL
to set the process filter and sentinel as part of opening the
network stream (to avoid race conditions).  The SENTINEL is
called when the connect completes (either in success or failure).

I suspect that your proposal for open-server-socket could similarly
have the FILTER and SENTINEL functions as arguments.  Then I don't see
any need for your accept-connection function as SENTINEL could be
called when a new connection is accepted, and the FILTER could be used
as a normal filter (processing received data) for the connection.

If you don't want to accept more connections on the server-socket,
it should be closed in the sentinel.

> 
> I propose the following Lisp level interface to server sockets:
> 
> Server sockets, like network connections, are represented as
> processes.  The only purpose of a "server socket process" is to handle
> incoming connections.  Server socket processes work asynchronous: the
> process waits for an incoming connection and passes the connection to
> the filter function.  The new connection is treated afterwards exactly
> like a connection created with `open-network-stream'.  Two new
> primitives are needed for server sockets:
> 
>   - Function: open-server-socket NAME PORT -> PROCESS
> 
>     This function opens a TCP server socket on the given port and
>     returns a process object.  NAME is the name for the process.
> 
> Incoming connections are accepted one at a time with the second new
> primitive: accept-connection.  accept-connection allows the server
> socket process to accept the next connection.  accept-connection is
> non-blocking and returns nil.
> 
>   - Function: accept-connection PROCESS NAME -> nil 
> 
>     PROCESS is a server socket process.  NAME is the name for the
>     incoming connection that will be passed to the process filter.
> 
> Server sockets can be closed with delete-process.  The process-status
> of a server socket process is either `listen' or `closed'.  The
> process-buffer slot is unused for server sockets.  
> 
> I hope the following example helps to clarify a bit; it implements an
> echo server:
> 
>   (defun server-socket-filter (server-socket client-socket)
>     (set-process-filter client-socket #'send-string)
>     (accept-connection server-socket "client-socket")) 
>     
>   (defun start-echo-server ()
>     (let ((server-socket (open-server-socket "server-socket" 7)))
>       (set-process-filter server-socket #'server-socket-filter)
>       (accept-connection server-socket "client-socket")))
> 
> We open a server socket on port 7; install a filter function and allow
> the process to accept the next connection.  When the next request
> arrives, Emacs will create a network stream with name "client-socket"
> and pass it along with the server socket to server-socket-filter.  In
> server-socket-filter we set #'send-string as filter function (to
> perform the actual echoing) and accept the the next connection (to
> handle multiple connections in parallel).
> 
> Note that filter functions are called only when Emacs is idle or
> waiting for something.
> 
> 
> The patch affects the files process.c and process.h (a comment).  I
> have tested it only on my GNU/Linux box.
> 
> Let me know if you decide to integrate the patch in the standard
> Emacs.
> 

In that case, I guess papers are needed.

-- 
Kim F. Storm <address@hidden> http://www.cua.dk




reply via email to

[Prev in Thread] Current Thread [Next in Thread]