server: repurpose legacy v:servername

- On startup, v:servername is equivalent to $NVIM_LISTEN_ADDRESS
- v:servername may be considered the "default" server address
- v:servername does not change unless the associated server is stopped
  by serverstop()
This commit is contained in:
Justin M. Keyes
2015-05-09 03:35:05 -04:00
parent 7a7d082e12
commit 3e4534f40f

View File

@@ -9,6 +9,7 @@
#include "nvim/msgpack_rpc/server.h" #include "nvim/msgpack_rpc/server.h"
#include "nvim/os/os.h" #include "nvim/os/os.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/eval.h"
#include "nvim/garray.h" #include "nvim/garray.h"
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/memory.h" #include "nvim/memory.h"
@@ -18,7 +19,7 @@
#define MAX_CONNECTIONS 32 #define MAX_CONNECTIONS 32
#define ADDRESS_MAX_SIZE 256 #define ADDRESS_MAX_SIZE 256
#define NEOVIM_DEFAULT_TCP_PORT 7450 #define NVIM_DEFAULT_TCP_PORT 7450
#define LISTEN_ADDRESS_ENV_VAR "NVIM_LISTEN_ADDRESS" #define LISTEN_ADDRESS_ENV_VAR "NVIM_LISTEN_ADDRESS"
typedef enum { typedef enum {
@@ -84,23 +85,32 @@ static void server_close_cb(Server **server)
uv_close(server_handle(*server), free_server); uv_close(server_handle(*server), free_server);
} }
/// Set v:servername to the first server in the server list, or unset it if no
/// servers are known.
static void set_vservername(garray_T *srvs)
{
char *default_server = (srvs->ga_len > 0)
? ((Server **)srvs->ga_data)[0]->addr
: NULL;
set_vim_var_string(VV_SEND_SERVER, (char_u *)default_server, -1);
}
/// Teardown the server module /// Teardown the server module
void server_teardown(void) void server_teardown(void)
{ {
GA_DEEP_CLEAR(&servers, Server *, server_close_cb); GA_DEEP_CLEAR(&servers, Server *, server_close_cb);
} }
/// Starts listening on arbitrary tcp/unix addresses specified by /// Starts listening for API calls on the TCP address or pipe path `endpoint`.
/// `endpoint` for API calls. The type of socket used(tcp or unix/pipe) will /// The socket type is determined by parsing `endpoint`: If it's a valid IPv4
/// be determined by parsing `endpoint`: If it's a valid tcp address in the /// address in 'ip[:port]' format, then it will be TCP socket. The port is
/// 'ip[:port]' format, then it will be tcp socket. The port is optional /// optional and if omitted defaults to NVIM_DEFAULT_TCP_PORT. Otherwise it
/// and if omitted will default to NEOVIM_DEFAULT_TCP_PORT. Otherwise it will /// will be a unix socket or named pipe.
/// be a unix socket or named pipe.
/// ///
/// @param endpoint Address of the server. Either a 'ip[:port]' string or an /// @param endpoint Address of the server. Either a 'ip[:port]' string or an
/// arbitrary identifier(trimmed to 256 bytes) for the unix socket or /// arbitrary identifier (trimmed to 256 bytes) for the unix socket or
/// named pipe. /// named pipe.
/// @returns zero if successful, one on a regular error, and negative errno /// @returns 0 on success, 1 on a regular error, and negative errno
/// on failure to bind or connect. /// on failure to bind or connect.
int server_start(const char *endpoint) int server_start(const char *endpoint)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
@@ -141,7 +151,7 @@ int server_start(const char *endpoint)
// Extract the address part // Extract the address part
xstrlcpy(ip, addr, addr_len + 1); xstrlcpy(ip, addr, addr_len + 1);
int port = NEOVIM_DEFAULT_TCP_PORT; int port = NVIM_DEFAULT_TCP_PORT;
if (*ip_end == ':') { if (*ip_end == ':') {
// Extract the port // Extract the port
@@ -216,6 +226,11 @@ int server_start(const char *endpoint)
ga_grow(&servers, 1); ga_grow(&servers, 1);
((Server **)servers.ga_data)[servers.ga_len++] = server; ((Server **)servers.ga_data)[servers.ga_len++] = server;
// Update v:servername, if not set.
if (STRLEN(get_vim_var_str(VV_SEND_SERVER)) == 0) {
set_vservername(&servers);
}
return 0; return 0;
} }
@@ -243,9 +258,9 @@ void server_stop(char *endpoint)
return; return;
} }
// If we are invalidating the listen address, unset it. // Unset $NVIM_LISTEN_ADDRESS if it is the stopped address.
const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR); const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
if (listen_address && strcmp(addr, listen_address) == 0) { if (listen_address && STRCMP(addr, listen_address) == 0) {
os_unsetenv(LISTEN_ADDRESS_ENV_VAR); os_unsetenv(LISTEN_ADDRESS_ENV_VAR);
} }
@@ -257,6 +272,11 @@ void server_stop(char *endpoint)
((Server **)servers.ga_data)[servers.ga_len - 1]; ((Server **)servers.ga_data)[servers.ga_len - 1];
} }
servers.ga_len--; servers.ga_len--;
// If v:servername is the stopped address, re-initialize it.
if (STRCMP(addr, get_vim_var_str(VV_SEND_SERVER)) == 0) {
set_vservername(&servers);
}
} }
/// Returns an allocated array of server addresses. /// Returns an allocated array of server addresses.