about summary refs log tree commit diff stats
path: root/src/uv_link_t.c
blob: 78ada8294b614eed94eb08dfbeaccb152bcfb4ec (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdlib.h>
#include <string.h>

#include "src/common.h"

static const size_t kErrorPrefixShift = 16;
static const int kErrorValueMask = (1 << 16) - 1;
static const unsigned int kErrorPrefixMask = ~((1 << 16) - 1);

static void uv_link_maybe_close(uv_link_t* link);


static int uv_link_error(uv_link_t* link, int err) {
  if (link == NULL)
    return err;

  if (err >= UV_ERRNO_MAX)
    return err;

  if (((-err) & kErrorPrefixMask) != 0)
    return err;

  return -((-err) | link->err_prefix);
}


static void uv_link_def_alloc_cb(uv_link_t* link,
                                 size_t suggested_size,
                                 uv_buf_t* buf) {
  buf->base = malloc(suggested_size);
  buf->len = suggested_size;

  CHECK_NE(buf->base, NULL, "uv_link_t: alloc_cb failure");
}


static void uv_link_def_read_cb(uv_link_t* link,
                                ssize_t nread,
                                const uv_buf_t* buf) {
  if (buf != NULL)
    free(buf->base);

  /* Stop reading on error */
  if (nread < 0)
    uv_link_read_stop(link);
}


int uv_link_init(uv_link_t* link, uv_link_methods_t const* methods) {
  memset(link, 0, sizeof(*link));

  link->alloc_cb = uv_link_def_alloc_cb;
  link->read_cb = uv_link_def_read_cb;
  link->err_prefix = 1 << kErrorPrefixShift;

  link->methods = methods;

  return 0;
}


#define CLOSE_WRAP(RES)                                                       \
    do {                                                                      \
      int err;                                                                \
      link->close_depth++;                                                    \
      err = (RES);                                                            \
      if (--link->close_depth == 0)                                           \
        uv_link_maybe_close(link);                                            \
      return uv_link_error(link, err);                                        \
    } while (0)


int uv_link_propagate_write(uv_link_t* link, uv_link_t* source,
                            const uv_buf_t bufs[], unsigned int nbufs,
                            uv_stream_t* send_handle,
                            uv_link_write_cb cb, void* arg) {
  if (link == NULL)
    return uv_link_error(link, UV_EFAULT);
  CLOSE_WRAP(link->methods->write(link, source, bufs, nbufs, send_handle, cb,
                                  arg));
}


int uv_link_propagate_shutdown(uv_link_t* link,
                               uv_link_t* source,
                               uv_link_shutdown_cb cb,
                               void* arg) {
  if (link == NULL)
    return uv_link_error(link, UV_EFAULT);
  CLOSE_WRAP(link->methods->shutdown(link, source, cb, arg));
}


int uv_link_read_start(uv_link_t* link) {
  if (link == NULL)
    return uv_link_error(link, UV_EFAULT);
  CLOSE_WRAP(link->methods->read_start(link));
}


int uv_link_read_stop(uv_link_t* link) {
  if (link == NULL)
    return uv_link_error(link, UV_EFAULT);
  if (NULL != link->methods->read_stop)
    CLOSE_WRAP(link->methods->read_stop(link));
}


int uv_link_try_write(uv_link_t* link,
                      const uv_buf_t bufs[],
                      unsigned int nbufs) {
  if (link == NULL)
    return uv_link_error(link, UV_EFAULT);
  CLOSE_WRAP(link->methods->try_write(link, bufs, nbufs));
}


void uv_link_close(uv_link_t* link, uv_link_close_cb cb) {
  uv_link_propagate_close(link, link, cb);
}


static void uv_link_close_join(uv_link_t* link) {
  if (--link->close_waiting == 0)
    return link->saved_close_cb(link);
}


void uv_link_maybe_close(uv_link_t* link) {
  uv_link_t* source;

  if (link->pending_close_source == NULL)
    return;

  source = link->pending_close_source;

  link->pending_close_source = NULL;
  link->methods->close(link, source, uv_link_close_join);
}


void uv_link_propagate_close(uv_link_t* link, uv_link_t* source,
                             uv_link_close_cb cb) {
  uv_link_t* root;
  int count;

  CHECK_EQ(link->child, NULL, "uv_link_t: attempt to close chained link");

  /* Find root */
  count = 1;
  for (root = link; root->parent != NULL; root = root->parent)
    count++;

  /* NOTE: This is very important line. Only because we `+=` here the
   * recursive propagation is possible
   */
  source->close_waiting += count;

  source->saved_close_cb = cb;

  /* Go from the root to the leaf, disconnecting and closing everything */
  while (root != NULL) {
    uv_link_t* child;

    child = root->child;
    if (child != NULL)
      CHECK_EQ(uv_link_unchain(root, child), 0, "close unchain");

    if (root->close_depth == 0)
      root->methods->close(root, source, uv_link_close_join);
    else
      root->pending_close_source = source;
    root = child;
  }
}


static void uv_link_recalculate_prefixes(uv_link_t* link) {
  unsigned short prev;

  prev = link->parent->err_prefix >> kErrorPrefixShift;
  for (; link != NULL; link = link->child)
    link->err_prefix = (++prev) << kErrorPrefixShift;
}


int uv_link_chain(uv_link_t* from, uv_link_t* to) {
  if (from->child != NULL || to->parent != NULL)
    return UV_EINVAL;

  CHECK(to->methods->alloc_cb_override != NULL &&
            to->methods->read_cb_override != NULL,
        "Attempting to chain link without overriding callbacks");

  from->child = to;
  to->parent = from;

  from->saved_alloc_cb = from->alloc_cb;
  from->saved_read_cb = from->read_cb;
  from->alloc_cb = to->methods->alloc_cb_override;
  from->read_cb = to->methods->read_cb_override;

  uv_link_recalculate_prefixes(to);

  return 0;
}


int uv_link_unchain(uv_link_t* from, uv_link_t* to) {
  if (from->child != to || to->parent != from)
    return UV_EINVAL;

  from->child = NULL;
  to->parent = NULL;

  from->alloc_cb = from->saved_alloc_cb;
  from->read_cb = from->saved_read_cb;
  from->saved_alloc_cb = NULL;
  from->saved_read_cb = NULL;

  return 0;
}


int uv_link_errno(uv_link_t** link, int err) {
  unsigned int prefix;
  int local_err;
  uv_link_t* p;

  if (err >= UV_ERRNO_MAX) {
    *link = NULL;
    return err;
  }

  prefix = (-err) & kErrorPrefixMask;
  local_err = -((-err) & kErrorValueMask);

  for (p = *link; p != NULL; p = p->parent) {
    if (prefix == p->err_prefix) {
      *link = p;
      return local_err;
    }
  }

  *link = NULL;
  return err;
}


const char* uv_link_strerror(uv_link_t* link, int err) {
  int local_err;

  if (err >= UV_ERRNO_MAX)
    return uv_strerror(err);

  local_err = uv_link_errno(&link, err);
  if (link == NULL)
    return NULL;

  return link->methods->strerror(link, local_err);
}


void uv_link_propagate_alloc_cb(uv_link_t* link,
                                size_t suggested_size,
                                uv_buf_t* buf) {
  uv_link_t* target;

  target = link;
  if (link->child != NULL)
    target = link->child;

  target->close_depth++;
  link->alloc_cb(target, suggested_size, buf);
  if (--target->close_depth == 0)
    uv_link_maybe_close(target);
}


void uv_link_propagate_read_cb(uv_link_t* link,
                               ssize_t nread,
                               const uv_buf_t* buf) {
  uv_link_t* target;

  target = link;
  if (link->child != NULL)
    target = link->child;

  /* Prefix errors */
  if (nread < 0)
    nread = uv_link_error(link, nread);

  target->close_depth++;
  link->read_cb(target, nread, buf);
  if (--target->close_depth == 0)
    uv_link_maybe_close(target);
}