Emacs Lisp network connections - what to do with a process object?

Suppose the TCP server is up and running on localhost: 8080, in another Lisp dialog that understands the lists. Now I open a network connection in Elisp

setq pserv (open-network-stream plisp "test1.l" "localhost" 8080) 

and successfully assign the open network process object to the pserv variable. But then, what's next, how can I use this process object to send requests to the server? I want to do this in order to send lists to another server (code as data) that were evaluated, and returned results.

In the above expression, "test1.l" is the Emacs buffer associated with the process, so the results should be printed in this buffer. What if I set zero and the process is not associated with any buffer - how can I access the server results (possibly in the form of a list too) from Elisp or from a process object?

The Elisp manual seems to take this for granted, but I lost it a bit. All hints would be appreciated.

+6
source share
2 answers

Communication between the client and server is processed through asynchronous processes. This means that, unfortunately, you do not have the "send" function, that you transfer your data to be sent to the server and returns a server response. The reason for this is that network communication can be slow and block all other operations in Emacs, which is a single-threaded program.

To send data to your server, use (process-send-string process string) . The first parameter is your network connection, which Emacs treats as asynchronous processes. Since you have this subprocess object already stored in the pserv variable, you can write:

 (process-send-string pserv "(my data (can be (in) (list) form))") 

to send a string to the server.

To read the server’s response, you use the process filter function , which are callbacks that are called with what the server sends back to you. Therefore, you first need to define such a callback function, say:

 (defun handle-server-reply (process content) "Gets invoked whenever the server sends data to the client." ...) 

This function takes two arguments, the network process and the content data that the server sends. There is one difficult point: the server response can be divided into sub-contents. That is, when the handle-server-reply call is called, the content argument can only contain parts of the server response. It can be called later with subsequent content. Therefore, make sure that you handle this correctly.

To declare your function as a callback, use:

 (set-process-filter pserv 'handle-server-reply) 

As always, character encoding can be lavash, so consider the following two functions and decide if you need them:

 (set-process-filter-multibyte pserv t) (set-process-coding-system pserv 'utf-8 'utf-8) 

Be sure to install them before assigning a process filter function.

You may also be interested in examining the status of your network connection, for example, to handle cases where the connection unexpectedly closes. To do this, you can use the so-called sentinel functions, another type of callback through which you receive information about changes in the state of the process:

 (set-process-sentinel pserv 'sentinel-function) (defun sentinel-function (process event) "Gets called when the status of the network connection changes." ...) 

The event parameter contains information on how the connection status has changed.

I think the Emacs documentation was mentioned earlier . It is definitely worth a read.

+10
source

In Emacs, a network connection is treated as an asynchronous subprocess. Thus, instructions for sending input or reading output from subprocesses also apply here.

To send something to the server, see "Entering Processes" and read the results, see Exiting Processes .

Information is transmitted over the network as a string, so you can use read or read-from-string after receiving data from the server. See Input Functions .

+4
source

Source: https://habr.com/ru/post/889784/


All Articles