![]() |
The link to the servers | ![]() |
This page describes
lsp_link.cpp
: how the messages of a language server are framed over its pipe, and how they reach the Scheme programs. The link does not know the protocol itself: it only cuts the bytes into messages.
Each message of the protocol is preceded by a header: lines Name: value ended by CR LF, then an empty line. Only Content-Length, the length of the message in bytes, is used; the other fields (Content-Type) are ignored, and the name of the field is compared without case. A header whose lines end by LF alone (against the protocol) is read too, and a negative length is refused.
lsp_link_rep is a tm_link_rep (
tm_link.hpp
) built on another link, the transport, which carries the bytes: make_lsp_link (make_pipe_link (cmd)) for a program (
pipe_link.cpp
, whose writes do not block, see below); a socket or a Web Worker could be used the same way. Its fields:
The transport.
The bytes received which are not yet a whole message, from pos on. The part before pos is dropped once all of buf is used or once it reaches 64 KB, so that a large message which comes in many parts is not copied at each part.
The whole messages which have not been read yet, from first on. A read only moves first; the array is emptied once all its messages are read, so that the messages are not copied at each read.
What the server wrote on its standard error, and the errors of the framing.
Set when something has come (bytes, errors, the end of the server) since the last dispatch, see below.
write (s, LINK_IN) sends lsp_frame (s), that is Content-Length: n\r\n\r\n followed by s, to the transport, and then drains it: what the server has sent is taken at once (a listen of the pipe, as the one of each dispatch which writes what is left of the input, reads the output of the child into the buffers of the pipe, whose notifier will not tell it again, and it would otherwise wait for the next notification). drain reads what the transport has received on LINK_OUT and LINK_ERR, and parse moves the whole messages from buf to msgs. Some servers print lines on their standard output by mistake; parse therefore splits the header at each LF (not only at CR LF) and passes the lines which are not Name: value to errbuf (lsp: skipped '…'), and a block without Content-Length is skipped with an error (lsp: no Content-Length in '…') instead of blocking the link. The end of a header is the first CR LF CR LF, or an LF LF before it; once a CR LF CR LF has been found, the LF LF is only looked for in the header before it, so that the body of a long message which comes in many parts is not searched again at each part.
read (LINK_OUT) returns the next whole message (or the empty string), read (LINK_ERR) returns and empties errbuf. listen (msecs) waits until a whole message, an error or the end of the server has come, for at most msecs: the bytes of a message may come in several parts, so it listens to the transport again until the message is complete. When the transport ends, the link is no longer alive.
A server which does not read its input (it is busy, or it waits until it can write its own output, which nobody reads while the editor waits) would block a write to its pipe once the pipe is full, and thus freeze the editor. tm_link_rep::set_blocking_write (
tm_link.hpp
) therefore lets a link write without blocking; by default (true) it waits until all is written, as the plug-ins expect, and lsp_start sets it to false for the pipe of each server.
In
pipe_link.cpp
, the input of the child is then non-blocking (O_NONBLOCK), and a write appends to inbuf (from inpos on) what the child has not taken yet. flush_input (msecs) writes what the child takes, for at most msecs, and reads its output while it waits; write calls it with 0, that is it tries once without waiting, so that a child which reads no more costs no wait at each write. What is left is written by listen, whenever the pipe can take more: the virtual tm_link_rep::pending_write tells that some input is not written yet, and lsp_dispatch calls listen (0) on the transport of each client which has some, then drains it, so that the rest goes at the next turns of the event loop. Once 64 KB of inbuf have been written, the part which is written is cut off, so that a long queue of messages to a slow server does not keep growing. The blocking writes of the plug-ins also retry a partial or interrupted write instead of losing the rest. With Qt, the pipe is a QProcess (
QTMPipeLink.cpp
), which keeps the rest itself: its flag blocking only tells writeStdin whether to wait with waitForBytesWritten.
The second half of the file keeps a list lsp_clients of lsp_client records: an integer id, the link, a Scheme call back, and two flags, exited (the call back got its "exit") and removed (stopped by lsp_stop). The glue routines of
build-glue-lsp.scm
(compiled only in a build with the servers, USE_LSP) give them to Scheme:
Starts the program cmd on a pipe which does not block the writes, and returns the id of its client, or a string with the error if it cannot be started.
Sends the message msg (the JSON text, without header); #f if the client is not alive, also when its link died while writing.
Waits for at most msecs until the server has sent something, then dispatches what has come to the call backs. This is how a request is waited for synchronously (lsp-request-wait), and how the tests wait.
Whether the server runs.
Stops the server at once (the shutdown and exit of the protocol are sent before, in Scheme); its call back gets nothing more.
The text of the source document t (a tree), in UTF-8, for its server (lsp_document_text): its lines joined by LF, each converted from Cork apart, a line with markup as its verbatim text on one line. It is the text of lsp-lines->text of lsp-lines, without the Scheme lists of the lines, which fill the heap for a long file.
The call back is called with a kind and a string: "message" and the JSON text of one message, "stderr" and what the server wrote on its standard error (with the errors of the framing), or "exit" and "" once, when the server has ended and everything it sent has been given.
The command of the transport (lsp_link_callback, called by the pipe when data arrive) only drains: the bytes are framed and news is set, but no Scheme code runs there. The call backs are called by lsp_dispatch, from two places:
process_all_lsp, which the interpose handler of the server (tm_server_rep::interpose_handler in
tm_server.cpp
) calls at each turn of the event loop, with all the GUIs: it is one of the processors of the links (add_link_processor,
tm_link.hpp
), which lsp_start registers the first time, so that the server does not name the language servers;
lsp_listen, after waiting.
lsp_dispatch first writes the rest of the messages which the servers have not taken yet (the clients whose link has a pending_write, see above). It then goes over the clients which have news (or whose link has ended) and gives each one first its errors, then one message; it sets news again for a client which gave a message, and loops until no call back was called, or for about 100 ms at most: a server which sends without pause (progress, logs) does not keep the editor from its events, and the rest is dispatched at the next turns (the news of the clients stay). A client whose link has ended and whose errors are all given gets "exit" and is marked exited.
A call back may send, listen or stop in its turn. In particular a call back which waits for the answer of a request (lsp-request-wait, for instance a handler which asks the server something) calls lsp_listen, and thus dispatches within the dispatch. To keep this safe:
a counter lsp_dispatching holds the depth of the nested dispatches;
no client is deleted during a dispatch: lsp_stop only marks it removed, and process_all_lsp deletes the clients which are removed or exited, with their links, only when lsp_dispatching is 0 (it can be called from an event loop which runs inside a call back);
clients which are started during a dispatch are added at the end of lsp_clients, so that the loop over the clients stays valid;
the news flag is per link, so that a dispatch does not read the links which have nothing new.