This repository has been archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 653
/
uv.h
2430 lines (2131 loc) · 83.1 KB
/
uv.h
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/* See https://github.com/joyent/libuv#documentation for documentation. */
#ifndef UV_H
#define UV_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
/* Windows - set up dll import/export decorators. */
# if defined(BUILDING_UV_SHARED)
/* Building shared library. */
# define UV_EXTERN __declspec(dllexport)
# elif defined(USING_UV_SHARED)
/* Using shared library. */
# define UV_EXTERN __declspec(dllimport)
# else
/* Building static library. */
# define UV_EXTERN /* nothing */
# endif
#elif __GNUC__ >= 4
# define UV_EXTERN __attribute__((visibility("default")))
#else
# define UV_EXTERN /* nothing */
#endif
#include "uv-errno.h"
#include "uv-version.h"
#include <stddef.h>
#if defined(_MSC_VER) && _MSC_VER < 1600
# include "stdint-msvc2008.h"
#else
# include <stdint.h>
#endif
#if defined(_WIN32)
# include "uv-win.h"
#else
# include "uv-unix.h"
#endif
/* Expand this list if necessary. */
#define UV_ERRNO_MAP(XX) \
XX(E2BIG, "argument list too long") \
XX(EACCES, "permission denied") \
XX(EADDRINUSE, "address already in use") \
XX(EADDRNOTAVAIL, "address not available") \
XX(EAFNOSUPPORT, "address family not supported") \
XX(EAGAIN, "resource temporarily unavailable") \
XX(EAI_ADDRFAMILY, "address family not supported") \
XX(EAI_AGAIN, "temporary failure") \
XX(EAI_BADFLAGS, "bad ai_flags value") \
XX(EAI_BADHINTS, "invalid value for hints") \
XX(EAI_CANCELED, "request canceled") \
XX(EAI_FAIL, "permanent failure") \
XX(EAI_FAMILY, "ai_family not supported") \
XX(EAI_MEMORY, "out of memory") \
XX(EAI_NODATA, "no address") \
XX(EAI_NONAME, "unknown node or service") \
XX(EAI_OVERFLOW, "argument buffer overflow") \
XX(EAI_PROTOCOL, "resolved protocol is unknown") \
XX(EAI_SERVICE, "service not available for socket type") \
XX(EAI_SOCKTYPE, "socket type not supported") \
XX(EALREADY, "connection already in progress") \
XX(EBADF, "bad file descriptor") \
XX(EBUSY, "resource busy or locked") \
XX(ECANCELED, "operation canceled") \
XX(ECHARSET, "invalid Unicode character") \
XX(ECONNABORTED, "software caused connection abort") \
XX(ECONNREFUSED, "connection refused") \
XX(ECONNRESET, "connection reset by peer") \
XX(EDESTADDRREQ, "destination address required") \
XX(EEXIST, "file already exists") \
XX(EFAULT, "bad address in system call argument") \
XX(EFBIG, "file too large") \
XX(EHOSTUNREACH, "host is unreachable") \
XX(EINTR, "interrupted system call") \
XX(EINVAL, "invalid argument") \
XX(EIO, "i/o error") \
XX(EISCONN, "socket is already connected") \
XX(EISDIR, "illegal operation on a directory") \
XX(ELOOP, "too many symbolic links encountered") \
XX(EMFILE, "too many open files") \
XX(EMSGSIZE, "message too long") \
XX(ENAMETOOLONG, "name too long") \
XX(ENETDOWN, "network is down") \
XX(ENETUNREACH, "network is unreachable") \
XX(ENFILE, "file table overflow") \
XX(ENOBUFS, "no buffer space available") \
XX(ENODEV, "no such device") \
XX(ENOENT, "no such file or directory") \
XX(ENOMEM, "not enough memory") \
XX(ENONET, "machine is not on the network") \
XX(ENOPROTOOPT, "protocol not available") \
XX(ENOSPC, "no space left on device") \
XX(ENOSYS, "function not implemented") \
XX(ENOTCONN, "socket is not connected") \
XX(ENOTDIR, "not a directory") \
XX(ENOTEMPTY, "directory not empty") \
XX(ENOTSOCK, "socket operation on non-socket") \
XX(ENOTSUP, "operation not supported on socket") \
XX(EPERM, "operation not permitted") \
XX(EPIPE, "broken pipe") \
XX(EPROTO, "protocol error") \
XX(EPROTONOSUPPORT, "protocol not supported") \
XX(EPROTOTYPE, "protocol wrong type for socket") \
XX(ERANGE, "result too large") \
XX(EROFS, "read-only file system") \
XX(ESHUTDOWN, "cannot send after transport endpoint shutdown") \
XX(ESPIPE, "invalid seek") \
XX(ESRCH, "no such process") \
XX(ETIMEDOUT, "connection timed out") \
XX(ETXTBSY, "text file is busy") \
XX(EXDEV, "cross-device link not permitted") \
XX(UNKNOWN, "unknown error") \
XX(EOF, "end of file") \
XX(ENXIO, "no such device or address") \
XX(EMLINK, "too many links") \
#define UV_HANDLE_TYPE_MAP(XX) \
XX(ASYNC, async) \
XX(CHECK, check) \
XX(FS_EVENT, fs_event) \
XX(FS_POLL, fs_poll) \
XX(HANDLE, handle) \
XX(IDLE, idle) \
XX(NAMED_PIPE, pipe) \
XX(POLL, poll) \
XX(PREPARE, prepare) \
XX(PROCESS, process) \
XX(STREAM, stream) \
XX(TCP, tcp) \
XX(TIMER, timer) \
XX(TTY, tty) \
XX(UDP, udp) \
XX(SIGNAL, signal) \
#define UV_REQ_TYPE_MAP(XX) \
XX(REQ, req) \
XX(CONNECT, connect) \
XX(WRITE, write) \
XX(SHUTDOWN, shutdown) \
XX(UDP_SEND, udp_send) \
XX(FS, fs) \
XX(WORK, work) \
XX(GETADDRINFO, getaddrinfo) \
XX(GETNAMEINFO, getnameinfo) \
typedef enum {
#define XX(code, _) UV_ ## code = UV__ ## code,
UV_ERRNO_MAP(XX)
#undef XX
UV_ERRNO_MAX = UV__EOF - 1
} uv_errno_t;
typedef enum {
UV_UNKNOWN_HANDLE = 0,
#define XX(uc, lc) UV_##uc,
UV_HANDLE_TYPE_MAP(XX)
#undef XX
UV_FILE,
UV_HANDLE_TYPE_MAX
} uv_handle_type;
typedef enum {
UV_UNKNOWN_REQ = 0,
#define XX(uc, lc) UV_##uc,
UV_REQ_TYPE_MAP(XX)
#undef XX
UV_REQ_TYPE_PRIVATE
UV_REQ_TYPE_MAX
} uv_req_type;
/* Handle types. */
typedef struct uv_loop_s uv_loop_t;
typedef struct uv_handle_s uv_handle_t;
typedef struct uv_stream_s uv_stream_t;
typedef struct uv_tcp_s uv_tcp_t;
typedef struct uv_udp_s uv_udp_t;
typedef struct uv_pipe_s uv_pipe_t;
typedef struct uv_tty_s uv_tty_t;
typedef struct uv_poll_s uv_poll_t;
typedef struct uv_timer_s uv_timer_t;
typedef struct uv_prepare_s uv_prepare_t;
typedef struct uv_check_s uv_check_t;
typedef struct uv_idle_s uv_idle_t;
typedef struct uv_async_s uv_async_t;
typedef struct uv_process_s uv_process_t;
typedef struct uv_fs_event_s uv_fs_event_t;
typedef struct uv_fs_poll_s uv_fs_poll_t;
typedef struct uv_signal_s uv_signal_t;
/* Request types. */
typedef struct uv_req_s uv_req_t;
typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
typedef struct uv_getnameinfo_s uv_getnameinfo_t;
typedef struct uv_shutdown_s uv_shutdown_t;
typedef struct uv_write_s uv_write_t;
typedef struct uv_connect_s uv_connect_t;
typedef struct uv_udp_send_s uv_udp_send_t;
typedef struct uv_fs_s uv_fs_t;
typedef struct uv_work_s uv_work_t;
/* None of the above. */
typedef struct uv_cpu_info_s uv_cpu_info_t;
typedef struct uv_interface_address_s uv_interface_address_t;
typedef struct uv_dirent_s uv_dirent_t;
typedef enum {
UV_RUN_DEFAULT = 0,
UV_RUN_ONCE,
UV_RUN_NOWAIT
} uv_run_mode;
/*
* Returns the libuv version packed into a single integer. 8 bits are used for
* each component, with the patch number stored in the 8 least significant
* bits. E.g. for libuv 1.2.3 this would return 0x010203.
*/
UV_EXTERN unsigned int uv_version(void);
/*
* Returns the libuv version number as a string. For non-release versions
* "-pre" is appended, so the version number could be "1.2.3-pre".
*/
UV_EXTERN const char* uv_version_string(void);
/*
* All functions besides uv_run() are non-blocking.
*
* All callbacks in libuv are made asynchronously. That is they are never
* made by the function that takes them as a parameter.
*/
/*
* Returns the initialized default loop. It may return NULL in case of
* allocation failture.
*/
UV_EXTERN uv_loop_t* uv_default_loop(void);
/*
* Initializes a uv_loop_t structure.
*/
UV_EXTERN int uv_loop_init(uv_loop_t* loop);
/*
* Closes all internal loop resources. This function must only be called once
* the loop has finished it's execution or it will return UV_EBUSY. After this
* function returns the user shall free the memory allocated for the loop.
*/
UV_EXTERN int uv_loop_close(uv_loop_t* loop);
/*
* Allocates and initializes a new loop.
*
* NOTE:
* This function is DEPRECATED (to be removed after 0.12), users should
* allocate the loop manually and use uv_loop_init instead.
*/
UV_EXTERN uv_loop_t* uv_loop_new(void);
/*
* Cleans up a loop once it has finished executio and frees its memory.
*
* NOTE:
* This function is DEPRECATED (to be removed after 0.12). Users should use
* uv_loop_close and free the memory manually instead.
*/
UV_EXTERN void uv_loop_delete(uv_loop_t*);
/*
* Returns size of the loop struct, useful for dynamic lookup with FFI.
*/
UV_EXTERN size_t uv_loop_size(void);
/*
* This function runs the event loop. It will act differently depending on the
* specified mode:
* - UV_RUN_DEFAULT: Runs the event loop until the reference count drops to
* zero. Always returns zero.
* - UV_RUN_ONCE: Poll for new events once. Note that this function blocks if
* there are no pending events. Returns zero when done (no active handles
* or requests left), or non-zero if more events are expected (meaning you
* should run the event loop again sometime in the future).
* - UV_RUN_NOWAIT: Poll for new events once but don't block if there are no
* pending events. Returns zero when done (no active handles
* or requests left), or non-zero if more events are expected (meaning you
* should run the event loop again sometime in the future).
*/
UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
/*
* This function checks whether the reference count, the number of active
* handles or requests left in the event loop, is non-zero.
*/
UV_EXTERN int uv_loop_alive(const uv_loop_t* loop);
/*
* This function will stop the event loop by forcing uv_run to end as soon as
* possible, but not sooner than the next loop iteration.
* If this function was called before blocking for i/o, the loop won't block
* for i/o on this iteration.
*/
UV_EXTERN void uv_stop(uv_loop_t*);
/*
* Manually modify the event loop's reference count. Useful if the user wants
* to have a handle or timeout that doesn't keep the loop alive.
*/
UV_EXTERN void uv_ref(uv_handle_t*);
UV_EXTERN void uv_unref(uv_handle_t*);
UV_EXTERN int uv_has_ref(const uv_handle_t*);
/*
* Update the event loop's concept of "now". Libuv caches the current time
* at the start of the event loop tick in order to reduce the number of
* time-related system calls.
*
* You won't normally need to call this function unless you have callbacks
* that block the event loop for longer periods of time, where "longer" is
* somewhat subjective but probably on the order of a millisecond or more.
*/
UV_EXTERN void uv_update_time(uv_loop_t*);
/*
* Return the current timestamp in milliseconds. The timestamp is cached at
* the start of the event loop tick, see |uv_update_time()| for details and
* rationale.
*
* The timestamp increases monotonically from some arbitrary point in time.
* Don't make assumptions about the starting point, you will only get
* disappointed.
*
* Use uv_hrtime() if you need sub-millisecond granularity.
*/
UV_EXTERN uint64_t uv_now(const uv_loop_t*);
/*
* Get backend file descriptor. Only kqueue, epoll and event ports are
* supported.
*
* This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
* poll in one thread and run the event loop's event callbacks in another.
*
* Useful for embedding libuv's event loop in another event loop.
* See test/test-embed.c for an example.
*
* Note that embedding a kqueue fd in another kqueue pollset doesn't work on
* all platforms. It's not an error to add the fd but it never generates
* events.
*/
UV_EXTERN int uv_backend_fd(const uv_loop_t*);
/*
* Get the poll timeout. The return value is in milliseconds, or -1 for no
* timeout.
*/
UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
/*
* Should prepare a buffer that libuv can use to read data into.
*
* `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
* okay as long as `buf.len > 0`.
*
* If you return a buffer with `buf.len == 0`, libuv skips the read and calls
* your read or recv callback with nread=UV_ENOBUFS.
*
* Note that returning a zero-length buffer does not stop the handle, call
* uv_read_stop() or uv_udp_recv_stop() for that.
*/
typedef void (*uv_alloc_cb)(uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf);
/*
* `nread` is > 0 if there is data available, 0 if libuv is done reading for
* now, or < 0 on error.
*
* The callee is responsible for closing the stream when an error happens
* by calling uv_close(). Trying to read from the stream again is undefined.
*
* The callee is responsible for freeing the buffer, libuv does not reuse it.
* The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
* error.
*/
typedef void (*uv_read_cb)(uv_stream_t* stream,
ssize_t nread,
const uv_buf_t* buf);
typedef void (*uv_write_cb)(uv_write_t* req, int status);
typedef void (*uv_connect_cb)(uv_connect_t* req, int status);
typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status);
typedef void (*uv_connection_cb)(uv_stream_t* server, int status);
typedef void (*uv_close_cb)(uv_handle_t* handle);
typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events);
typedef void (*uv_timer_cb)(uv_timer_t* handle);
typedef void (*uv_async_cb)(uv_async_t* handle);
typedef void (*uv_prepare_cb)(uv_prepare_t* handle);
typedef void (*uv_check_cb)(uv_check_t* handle);
typedef void (*uv_idle_cb)(uv_idle_t* handle);
typedef void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal);
typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
typedef void (*uv_fs_cb)(uv_fs_t* req);
typedef void (*uv_work_cb)(uv_work_t* req);
typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req,
int status,
struct addrinfo* res);
typedef void (*uv_getnameinfo_cb)(uv_getnameinfo_t* req,
int status,
const char* hostname,
const char* service);
typedef struct {
long tv_sec;
long tv_nsec;
} uv_timespec_t;
typedef struct {
uint64_t st_dev;
uint64_t st_mode;
uint64_t st_nlink;
uint64_t st_uid;
uint64_t st_gid;
uint64_t st_rdev;
uint64_t st_ino;
uint64_t st_size;
uint64_t st_blksize;
uint64_t st_blocks;
uint64_t st_flags;
uint64_t st_gen;
uv_timespec_t st_atim;
uv_timespec_t st_mtim;
uv_timespec_t st_ctim;
uv_timespec_t st_birthtim;
} uv_stat_t;
/*
* This will be called repeatedly after the uv_fs_event_t is initialized.
* If uv_fs_event_t was initialized with a directory the filename parameter
* will be a relative path to a file contained in the directory.
* The events parameter is an ORed mask of enum uv_fs_event elements.
*/
typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle,
const char* filename,
int events,
int status);
typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
int status,
const uv_stat_t* prev,
const uv_stat_t* curr);
typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
typedef enum {
UV_LEAVE_GROUP = 0,
UV_JOIN_GROUP
} uv_membership;
/*
* Most functions return 0 on success or an error code < 0 on failure.
*/
UV_EXTERN const char* uv_strerror(int err);
UV_EXTERN const char* uv_err_name(int err);
#define UV_REQ_FIELDS \
/* public */ \
void* data; \
/* read-only */ \
uv_req_type type; \
/* private */ \
void* active_queue[2]; \
void* reserved[4]; \
UV_REQ_PRIVATE_FIELDS \
/* Abstract base class of all requests. */
struct uv_req_s {
UV_REQ_FIELDS
};
/* Platform-specific request types. */
UV_PRIVATE_REQ_TYPES
/*
* uv_shutdown_t is a subclass of uv_req_t.
*
* Shutdown the outgoing (write) side of a duplex stream. It waits for pending
* write requests to complete. The handle should refer to a initialized stream.
* req should be an uninitialized shutdown request struct. The cb is called
* after shutdown is complete.
*/
UV_EXTERN int uv_shutdown(uv_shutdown_t* req,
uv_stream_t* handle,
uv_shutdown_cb cb);
struct uv_shutdown_s {
UV_REQ_FIELDS
uv_stream_t* handle;
uv_shutdown_cb cb;
UV_SHUTDOWN_PRIVATE_FIELDS
};
#define UV_HANDLE_FIELDS \
/* public */ \
void* data; \
/* read-only */ \
uv_loop_t* loop; \
uv_handle_type type; \
/* private */ \
uv_close_cb close_cb; \
void* handle_queue[2]; \
void* reserved[4]; \
UV_HANDLE_PRIVATE_FIELDS \
/* The abstract base class of all handles. */
struct uv_handle_s {
UV_HANDLE_FIELDS
};
/*
* Returns size of various handle types, useful for FFI bindings to allocate
* correct memory without copying struct definitions.
*/
UV_EXTERN size_t uv_handle_size(uv_handle_type type);
/*
* Returns size of request types, useful for dynamic lookup with FFI.
*/
UV_EXTERN size_t uv_req_size(uv_req_type type);
/*
* Returns non-zero if the handle is active, zero if it's inactive.
*
* What "active" means depends on the type of handle:
*
* - A uv_async_t handle is always active and cannot be deactivated, except
* by closing it with uv_close().
*
* - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that
* deals with i/o - is active when it is doing something that involves i/o,
* like reading, writing, connecting, accepting new connections, etc.
*
* - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has
* been started with a call to uv_check_start(), uv_idle_start(), etc.
*
* Rule of thumb: if a handle of type uv_foo_t has a uv_foo_start()
* function, then it's active from the moment that function is called.
* Likewise, uv_foo_stop() deactivates the handle again.
*
*/
UV_EXTERN int uv_is_active(const uv_handle_t* handle);
/*
* Walk the list of open handles.
*/
UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
/*
* Request handle to be closed. close_cb will be called asynchronously after
* this call. This MUST be called on each handle before memory is released.
*
* Note that handles that wrap file descriptors are closed immediately but
* close_cb will still be deferred to the next iteration of the event loop.
* It gives you a chance to free up any resources associated with the handle.
*
* In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
* have their callbacks called asynchronously with status=UV_ECANCELED.
*/
UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
/*
* Returns or sets the size of the receive buffer that the operating
* system uses for the socket.
*
* If *value == 0, it will return the current receive buffer size,
* otherwise it will use *value to set the new receive buffer size.
*
* NOTE: linux will set double the size and return double the size
* of the original set value.
*/
UV_EXTERN int uv_recv_buffer_size(uv_handle_t* handle, int* value);
UV_EXTERN int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd);
/*
* Returns or sets the size of the send buffer that the operating
* system uses for the socket.
*
* If *value == 0, it will return the current send buffer size,
* otherwise it will use *value to set the new send buffer size.
*
* NOTE: linux will set double the size and return double the size
* of the original set value.
*/
UV_EXTERN int uv_send_buffer_size(uv_handle_t* handle, int* value);
/*
* Constructor for uv_buf_t.
*
* Due to platform differences the user cannot rely on the ordering of the
* base and len members of the uv_buf_t struct. The user is responsible for
* freeing base after the uv_buf_t is done. Return struct passed by value.
*/
UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
#define UV_STREAM_FIELDS \
/* number of bytes queued for writing */ \
size_t write_queue_size; \
uv_alloc_cb alloc_cb; \
uv_read_cb read_cb; \
/* private */ \
UV_STREAM_PRIVATE_FIELDS
/*
* uv_stream_t is a subclass of uv_handle_t.
*
* uv_stream is an abstract class.
*
* uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t and uv_tty_t.
*/
struct uv_stream_s {
UV_HANDLE_FIELDS
UV_STREAM_FIELDS
};
UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
/*
* This call is used in conjunction with uv_listen() to accept incoming
* connections. Call uv_accept after receiving a uv_connection_cb to accept
* the connection. Before calling uv_accept use uv_*_init() must be
* called on the client. Non-zero return value indicates an error.
*
* When the uv_connection_cb is called it is guaranteed that uv_accept() will
* complete successfully the first time. If you attempt to use it more than
* once, it may fail. It is suggested to only call uv_accept() once per
* uv_connection_cb call.
*/
UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
/*
* Read data from an incoming stream. The callback will be made several
* times until there is no more data to read or uv_read_stop() is called.
* When we've reached EOF nread will be set to UV_EOF.
*
* When nread < 0, the buf parameter might not point to a valid buffer;
* in that case buf.len and buf.base are both set to 0.
*
* Note that nread might also be 0, which does *not* indicate an error or
* eof; it happens when libuv requested a buffer through the alloc callback
* but then decided that it didn't need that buffer.
*/
UV_EXTERN int uv_read_start(uv_stream_t*,
uv_alloc_cb alloc_cb,
uv_read_cb read_cb);
UV_EXTERN int uv_read_stop(uv_stream_t*);
/*
* Write data to stream. Buffers are written in order. Example:
*
* uv_buf_t a[] = {
* { .base = "1", .len = 1 },
* { .base = "2", .len = 1 }
* };
*
* uv_buf_t b[] = {
* { .base = "3", .len = 1 },
* { .base = "4", .len = 1 }
* };
*
* uv_write_t req1;
* uv_write_t req2;
*
* // writes "1234"
* uv_write(&req1, stream, a, 2);
* uv_write(&req2, stream, b, 2);
*
*/
UV_EXTERN int uv_write(uv_write_t* req,
uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs,
uv_write_cb cb);
/*
* Extended write function for sending handles over a pipe. The pipe must be
* initialized with ipc == 1.
* send_handle must be a TCP socket or pipe, which is a server or a connection
* (listening or connected state). Bound sockets or pipes will be assumed to
* be servers.
*/
UV_EXTERN int uv_write2(uv_write_t* req,
uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs,
uv_stream_t* send_handle,
uv_write_cb cb);
/*
* Same as uv_write(), but won't queue write request if it can't be completed
* immediately.
*
* Will return either:
* - > 0: number of bytes written (can be less than the supplied buffer size).
* - < 0: negative error code (UV_EAGAIN is returned if no data can be sent
* immediately).
*/
UV_EXTERN int uv_try_write(uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs);
/* uv_write_t is a subclass of uv_req_t. */
struct uv_write_s {
UV_REQ_FIELDS
uv_write_cb cb;
uv_stream_t* send_handle;
uv_stream_t* handle;
UV_WRITE_PRIVATE_FIELDS
};
/*
* Used to determine whether a stream is readable or writable.
*/
UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
/*
* Enable or disable blocking mode for a stream.
*
* When blocking mode is enabled all writes complete synchronously. The
* interface remains unchanged otherwise, e.g. completion or failure of the
* operation will still be reported through a callback which is made
* asychronously.
*
* Relying too much on this API is not recommended. It is likely to change
* significantly in the future.
*
* Currently this only works on Windows and only for uv_pipe_t handles.
*
* Also libuv currently makes no ordering guarantee when the blocking mode
* is changed after write requests have already been submitted. Therefore it is
* recommended to set the blocking mode immediately after opening or creating
* the stream.
*/
UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking);
/*
* Used to determine whether a stream is closing or closed.
*
* N.B. is only valid between the initialization of the handle and the arrival
* of the close callback, and cannot be used to validate the handle.
*/
UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
/*
* uv_tcp_t is a subclass of uv_stream_t.
*
* Represents a TCP stream or TCP server.
*/
struct uv_tcp_s {
UV_HANDLE_FIELDS
UV_STREAM_FIELDS
UV_TCP_PRIVATE_FIELDS
};
UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
/*
* Opens an existing file descriptor or SOCKET as a tcp handle.
*/
UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
/* Enable/disable Nagle's algorithm. */
UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
/*
* Enable/disable TCP keep-alive.
*
* `delay` is the initial delay in seconds, ignored when `enable` is zero.
*/
UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
int enable,
unsigned int delay);
/*
* Enable/disable simultaneous asynchronous accept requests that are
* queued by the operating system when listening for new tcp connections.
*
* This setting is used to tune a tcp server for the desired performance.
* Having simultaneous accepts can significantly improve the rate of accepting
* connections (which is why it is enabled by default) but may lead to uneven
* load distribution in multi-process setups.
*/
UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
enum uv_tcp_flags {
/* Used with uv_tcp_bind, when an IPv6 address is used. */
UV_TCP_IPV6ONLY = 1
};
/*
* Bind the handle to an address and port. `addr` should point to an
* initialized struct sockaddr_in or struct sockaddr_in6.
*
* When the port is already taken, you can expect to see an UV_EADDRINUSE error
* from either uv_tcp_bind(), uv_listen() or uv_tcp_connect().
*
* That is, a successful call to uv_tcp_bind() does not guarantee that the call
* to uv_listen() or uv_tcp_connect() will succeed as well.
*/
UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle,
const struct sockaddr* addr,
unsigned int flags);
UV_EXTERN int uv_tcp_getsockname(const uv_tcp_t* handle,
struct sockaddr* name,
int* namelen);
UV_EXTERN int uv_tcp_getpeername(const uv_tcp_t* handle,
struct sockaddr* name,
int* namelen);
/*
* Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle
* and an uninitialized uv_connect_t*. `addr` should point to an initialized
* struct sockaddr_in or struct sockaddr_in6.
*
* The callback is made when the connection has been established or when a
* connection error happened.
*/
UV_EXTERN int uv_tcp_connect(uv_connect_t* req,
uv_tcp_t* handle,
const struct sockaddr* addr,
uv_connect_cb cb);
/* uv_connect_t is a subclass of uv_req_t. */
struct uv_connect_s {
UV_REQ_FIELDS
uv_connect_cb cb;
uv_stream_t* handle;
UV_CONNECT_PRIVATE_FIELDS
};
/*
* UDP support.
*/
enum uv_udp_flags {
/* Disables dual stack mode. */
UV_UDP_IPV6ONLY = 1,
/*
* Indicates message was truncated because read buffer was too small. The
* remainder was discarded by the OS. Used in uv_udp_recv_cb.
*/
UV_UDP_PARTIAL = 2,
/*
* Indicates if SO_REUSEADDR will be set when binding the handle.
* This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
* Unix platforms, it sets the SO_REUSEADDR flag. What that means is that
* multiple threads or processes can bind to the same address without error
* (provided they all set the flag) but only the last one to bind will receive
* any traffic, in effect "stealing" the port from the previous listener.
*/
UV_UDP_REUSEADDR = 4
};
/*
* Called after uv_udp_send(). status 0 indicates success otherwise error.
*/
typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
/*
* Callback that is invoked when a new UDP datagram is received.
*
* handle UDP handle.
* nread Number of bytes that have been received.
* - 0 if there is no more data to read. You may discard or repurpose
* the read buffer. Note that 0 may also mean that an empty datagram
* was received (in this case `addr` is not NULL).
* - < 0 if a transmission error was detected.
* buf uv_buf_t with the received data.
* addr struct sockaddr* containing the address of the sender. Can be NULL.
* Valid for the duration of the callback only.
* flags One or more OR'ed UV_UDP_* constants. Right now only UV_UDP_PARTIAL
* is used.
*
* NOTE:
* The receive callback will be called with nread == 0 and addr == NULL when
* there is nothing to read, and with nread == 0 and addr != NULL when an empty
* UDP packet is received.
*/
typedef void (*uv_udp_recv_cb)(uv_udp_t* handle,
ssize_t nread,
const uv_buf_t* buf,
const struct sockaddr* addr,
unsigned flags);
/* uv_udp_t is a subclass of uv_handle_t. */
struct uv_udp_s {
UV_HANDLE_FIELDS
/* read-only */
/*
* Number of bytes queued for sending. This field strictly shows how much
* information is currently queued.
*/
size_t send_queue_size;
/*
* Number of send requests currently in the queue awaiting to be processed.
*/
size_t send_queue_count;
UV_UDP_PRIVATE_FIELDS
};
/* uv_udp_send_t is a subclass of uv_req_t. */
struct uv_udp_send_s {
UV_REQ_FIELDS
uv_udp_t* handle;
uv_udp_send_cb cb;
UV_UDP_SEND_PRIVATE_FIELDS
};
/*
* Initialize a new UDP handle. The actual socket is created lazily.
* Returns 0 on success.
*/
UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
/*
* Opens an existing file descriptor or SOCKET as a udp handle.
*
* Unix only:
* The only requirement of the sock argument is that it follows the datagram
* contract (works in unconnected mode, supports sendmsg()/recvmsg(), etc).
* In other words, other datagram-type sockets like raw sockets or netlink
* sockets can also be passed to this function.
*
* This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other Unix
* platforms, it sets the SO_REUSEADDR flag. What that means is that multiple
* threads or processes can bind to the same address without error (provided
* they all set the flag) but only the last one to bind will receive any
* traffic, in effect "stealing" the port from the previous listener.
* This behavior is something of an anomaly and may be replaced by an explicit
* opt-in mechanism in future versions of libuv.
*/
UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
/*
* Bind to an IP address and port.
*
* Arguments:
* handle UDP handle. Should have been initialized with uv_udp_init().
* addr struct sockaddr_in or struct sockaddr_in6 with the address and
* port to bind to.
* flags Indicate how the socket will be bound, UV_UDP_IPV6ONLY and
* UV_UDP_REUSEADDR are supported.
*