Skip to content

Commit

Permalink
Merge pull request #1909 from equalsraf/tb-os_serveraddress
Browse files Browse the repository at this point in the history
server_address()
  • Loading branch information
justinmk committed Oct 19, 2015
2 parents ea01c40 + 3e84a91 commit e38cbb9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -14572,7 +14572,7 @@ static void f_serverstart(typval_T *argvars, typval_T *rettv)
rettv->vval.v_string = vim_strsave(get_tv_string(argvars));
}
} else {
rettv->vval.v_string = vim_tempname();
rettv->vval.v_string = (char_u *)server_address_new();
}

int result = server_start((char *) rettv->vval.v_string);
Expand Down
30 changes: 28 additions & 2 deletions src/nvim/msgpack_rpc/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>

#include "nvim/msgpack_rpc/channel.h"
#include "nvim/msgpack_rpc/server.h"
Expand Down Expand Up @@ -35,7 +36,7 @@ bool server_init(void)
const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
if (listen_address == NULL) {
must_free = true;
listen_address = (char *)vim_tempname();
listen_address = server_address_new();
}

bool ok = (server_start(listen_address) == 0);
Expand Down Expand Up @@ -67,6 +68,27 @@ void server_teardown(void)
GA_DEEP_CLEAR(&watchers, SocketWatcher *, close_socket_watcher);
}

/// Generates unique address for local server.
///
/// In Windows this is a named pipe in the format
/// \\.\pipe\nvim-<PID>-<COUNTER>.
///
/// For other systems it is a path returned by vim_tempname().
///
/// This function is NOT thread safe
char *server_address_new(void)
{
#ifdef WIN32
static uint32_t count = 0;
char template[ADDRESS_MAX_SIZE];
snprintf(template, ADDRESS_MAX_SIZE,
"\\\\.\\pipe\\nvim-%" PRIu64 "-%" PRIu32, os_get_pid(), count++);
return xstrdup(template);
#else
return (char *)vim_tempname();
#endif
}

/// Starts listening for API calls on the TCP address or pipe path `endpoint`.
/// The socket type is determined by parsing `endpoint`: If it's a valid IPv4
/// address in 'ip[:port]' format, then it will be TCP socket. The port is
Expand All @@ -79,8 +101,12 @@ void server_teardown(void)
/// @returns 0 on success, 1 on a regular error, and negative errno
/// on failure to bind or connect.
int server_start(const char *endpoint)
FUNC_ATTR_NONNULL_ALL
{
if (endpoint == NULL) {
ELOG("Attempting to start server on NULL endpoint");
return 1;
}

SocketWatcher *watcher = xmalloc(sizeof(SocketWatcher));
socket_watcher_init(&loop, watcher, endpoint, NULL);

Expand Down

0 comments on commit e38cbb9

Please sign in to comment.