aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/channel.c
blob: 4981a268b2ff646c4281ac7dba595be9462fbe65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <string.h>

#include <uv.h>
#include <msgpack.h>

#include "nvim/api/private/helpers.h"
#include "nvim/os/channel.h"
#include "nvim/os/channel_defs.h"
#include "nvim/os/rstream.h"
#include "nvim/os/rstream_defs.h"
#include "nvim/os/wstream.h"
#include "nvim/os/wstream_defs.h"
#include "nvim/os/job.h"
#include "nvim/os/job_defs.h"
#include "nvim/os/msgpack_rpc.h"
#include "nvim/vim.h"
#include "nvim/memory.h"
#include "nvim/map.h"

typedef struct {
  uint64_t id;
  ChannelProtocol protocol;
  bool is_job;
  union {
    struct {
      msgpack_unpacker *unpacker;
      msgpack_sbuffer *sbuffer;
    } msgpack;
  } proto;
  union {
    int job_id;
    struct {
      RStream *read;
      WStream *write;
      uv_stream_t *uv;
    } streams;
  } data;
} Channel;

static uint64_t next_id = 1;
static Map(uint64_t) *channels = NULL;
static msgpack_sbuffer msgpack_event_buffer;

static void on_job_stdout(RStream *rstream, void *data, bool eof);
static void on_job_stderr(RStream *rstream, void *data, bool eof);
static void parse_msgpack(RStream *rstream, void *data, bool eof);
static void send_msgpack(Channel *channel, String type, Object data);
static void close_channel(Channel *channel);
static void close_cb(uv_handle_t *handle);

void channel_init()
{
  channels = map_new(uint64_t)();
  msgpack_sbuffer_init(&msgpack_event_buffer);
}

void channel_teardown()
{
  if (!channels) {
    return;
  }

  Channel *channel;

  map_foreach_value(channels, channel, {
    close_channel(channel);
  });
}

void channel_from_job(char **argv, ChannelProtocol prot)
{
  Channel *channel = xmalloc(sizeof(Channel));
  rstream_cb rcb = NULL;

  switch (prot) {
    case kChannelProtocolMsgpack:
      rcb = on_job_stdout;
      channel->proto.msgpack.unpacker =
        msgpack_unpacker_new(MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
      channel->proto.msgpack.sbuffer = msgpack_sbuffer_new();
      break;
    default:
      abort();
  }

  channel->id = next_id++;
  channel->protocol = prot;
  channel->is_job = true;
  channel->data.job_id = job_start(argv, channel, rcb, on_job_stderr, NULL);
  map_put(uint64_t)(channels, channel->id, channel);
}

void channel_from_stream(uv_stream_t *stream, ChannelProtocol prot)
{
  Channel *channel = xmalloc(sizeof(Channel));
  rstream_cb rcb = NULL;

  switch (prot) {
    case kChannelProtocolMsgpack:
      rcb = parse_msgpack;
      channel->proto.msgpack.unpacker =
        msgpack_unpacker_new(MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
      channel->proto.msgpack.sbuffer = msgpack_sbuffer_new();
      break;
    default:
      abort();
  }

  stream->data = NULL;
  channel->id = next_id++;
  channel->protocol = prot;
  channel->is_job = false;
  // read stream
  channel->data.streams.read = rstream_new(rcb, 1024, channel, true);
  rstream_set_stream(channel->data.streams.read, stream);
  rstream_start(channel->data.streams.read);
  // write stream
  channel->data.streams.write = wstream_new(1024 * 1024);
  wstream_set_stream(channel->data.streams.write, stream);
  channel->data.streams.uv = stream;
  map_put(uint64_t)(channels, channel->id, channel);
}

bool channel_send_event(uint64_t id, char *type, typval_T *data)
{
  Channel *channel = map_get(uint64_t)(channels, id);

  if (!channel) {
    return false;
  }

  String event_type = {.size = strnlen(type, 1024), .data = type};
  Object event_data = vim_to_object(data);

  switch (channel->protocol) {
    case kChannelProtocolMsgpack:
      send_msgpack(channel, event_type, event_data);
      break;
    default:
      abort();
  }

  msgpack_rpc_free_object(event_data);

  return true;
}

static void on_job_stdout(RStream *rstream, void *data, bool eof)
{
  Job *job = data;
  parse_msgpack(rstream, job_data(job), eof);
}

static void on_job_stderr(RStream *rstream, void *data, bool eof)
{
  // TODO(tarruda): plugin error messages should be sent to the error buffer
}

static void parse_msgpack(RStream *rstream, void *data, bool eof)
{
  Channel *channel = data;

  if (eof) {
    close_channel(channel);
    return;
  }

  uint32_t count = rstream_available(rstream);

  // Feed the unpacker with data
  msgpack_unpacker_reserve_buffer(channel->proto.msgpack.unpacker, count);
  rstream_read(rstream,
               msgpack_unpacker_buffer(channel->proto.msgpack.unpacker),
               count);
  msgpack_unpacker_buffer_consumed(channel->proto.msgpack.unpacker, count);

  msgpack_unpacked unpacked;
  msgpack_unpacked_init(&unpacked);

  // Deserialize everything we can. 
  while (msgpack_unpacker_next(channel->proto.msgpack.unpacker, &unpacked)) {
    // Each object is a new msgpack-rpc request and requires an empty response 
    msgpack_packer response;
    msgpack_packer_init(&response,
                        channel->proto.msgpack.sbuffer,
                        msgpack_sbuffer_write);
    // Perform the call
    msgpack_rpc_call(channel->id, &unpacked.data, &response);
    wstream_write(channel->data.streams.write,
                  xmemdup(channel->proto.msgpack.sbuffer->data,
                          channel->proto.msgpack.sbuffer->size),
                  channel->proto.msgpack.sbuffer->size,
                  true);

    // Clear the buffer for future calls
    msgpack_sbuffer_clear(channel->proto.msgpack.sbuffer);
  }
}

static void send_msgpack(Channel *channel, String type, Object data)
{
  msgpack_packer packer;
  msgpack_packer_init(&packer, &msgpack_event_buffer, msgpack_sbuffer_write);
  msgpack_rpc_notification(type, data, &packer);
  char *bytes = xmemdup(msgpack_event_buffer.data, msgpack_event_buffer.size);

  wstream_write(channel->data.streams.write,
      bytes,
      msgpack_event_buffer.size,
      true);

  msgpack_sbuffer_clear(&msgpack_event_buffer);
}

static void close_channel(Channel *channel)
{
  map_del(uint64_t)(channels, channel->id);

  switch (channel->protocol) {
    case kChannelProtocolMsgpack:
      msgpack_sbuffer_free(channel->proto.msgpack.sbuffer);
      msgpack_unpacker_free(channel->proto.msgpack.unpacker);
      break;
    default:
      abort();
  }

  if (channel->is_job) {
    job_stop(channel->data.job_id);
  } else {
    rstream_free(channel->data.streams.read);
    wstream_free(channel->data.streams.write);
    uv_close((uv_handle_t *)channel->data.streams.uv, close_cb);
  }

  free(channel);
}

static void close_cb(uv_handle_t *handle)
{
  free(handle->data);
  free(handle);
}