Lines Matching defs:handle

27 #include "handle-inl.h"
48 static int uv__tcp_nodelay(uv_tcp_t* handle, SOCKET socket, int enable) {
60 static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsigned int delay) {
82 uv_tcp_t* handle,
90 if (handle->socket != INVALID_SOCKET)
109 handle->flags |= UV_HANDLE_EMULATE_IOCP;
121 if (!(handle->flags & UV_HANDLE_EMULATE_IOCP) && !non_ifs_lsp) {
126 handle->flags |= UV_HANDLE_SYNC_BYPASS_IOCP;
129 if (handle->flags & UV_HANDLE_TCP_NODELAY) {
130 err = uv__tcp_nodelay(handle, socket, 1);
136 if (handle->flags & UV_HANDLE_TCP_KEEPALIVE) {
137 err = uv__tcp_keepalive(handle, socket, 1, 60);
142 handle->socket = socket;
145 handle->flags |= UV_HANDLE_IPV6;
147 assert(!(handle->flags & UV_HANDLE_IPV6));
154 int uv_tcp_init_ex(uv_loop_t* loop, uv_tcp_t* handle, unsigned int flags) {
165 uv__stream_init(loop, (uv_stream_t*) handle, UV_TCP);
166 handle->tcp.serv.accept_reqs = NULL;
167 handle->tcp.serv.pending_accepts = NULL;
168 handle->socket = INVALID_SOCKET;
169 handle->reqs_pending = 0;
170 handle->tcp.serv.func_acceptex = NULL;
171 handle->tcp.conn.func_connectex = NULL;
172 handle->tcp.serv.processed_accepts = 0;
173 handle->delayed_error = 0;
175 /* If anything fails beyond this point we need to remove the handle from
176 * the handle queue, since it was added by uv__handle_init in uv__stream_init.
186 QUEUE_REMOVE(&handle->handle_queue);
190 err = uv__tcp_set_socket(handle->loop, handle, sock, domain, 0);
193 QUEUE_REMOVE(&handle->handle_queue);
203 int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* handle) {
204 return uv_tcp_init_ex(loop, handle, AF_UNSPEC);
236 void uv__tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {
240 assert(handle->flags & UV_HANDLE_CLOSING);
241 assert(handle->reqs_pending == 0);
242 assert(!(handle->flags & UV_HANDLE_CLOSED));
243 assert(handle->socket == INVALID_SOCKET);
245 if (!(handle->flags & UV_HANDLE_CONNECTION) && handle->tcp.serv.accept_reqs) {
246 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
248 req = &handle->tcp.serv.accept_reqs[i];
260 uv__free(handle->tcp.serv.accept_reqs);
261 handle->tcp.serv.accept_reqs = NULL;
264 if (handle->flags & UV_HANDLE_CONNECTION &&
265 handle->flags & UV_HANDLE_EMULATE_IOCP) {
266 if (handle->read_req.wait_handle != INVALID_HANDLE_VALUE) {
267 UnregisterWait(handle->read_req.wait_handle);
268 handle->read_req.wait_handle = INVALID_HANDLE_VALUE;
270 if (handle->read_req.event_handle != NULL) {
271 CloseHandle(handle->read_req.event_handle);
272 handle->read_req.event_handle = NULL;
276 uv__handle_close(handle);
292 static int uv__tcp_try_bind(uv_tcp_t* handle,
299 if (handle->socket == INVALID_SOCKET) {
311 err = uv__tcp_set_socket(handle->loop, handle, sock, addr->sa_family, 0);
324 /* TODO: how to handle errors? This may fail if there is no ipv4 stack
327 setsockopt(handle->socket,
335 r = bind(handle->socket, addr, addrlen);
341 handle->delayed_error = err;
347 handle->flags |= UV_HANDLE_BOUND;
355 uv_tcp_t* handle;
359 handle = (uv_tcp_t*)req->data;
360 assert(handle != NULL);
363 if (!PostQueuedCompletionStatus(handle->loop->iocp,
374 uv_tcp_t* handle;
378 handle = (uv_tcp_t*)req->handle;
379 assert(handle != NULL);
382 if (!PostQueuedCompletionStatus(handle->loop->iocp,
391 static void uv__tcp_queue_accept(uv_tcp_t* handle, uv_tcp_accept_t* req) {
392 uv_loop_t* loop = handle->loop;
398 assert(handle->flags & UV_HANDLE_LISTENING);
402 if (handle->flags & UV_HANDLE_IPV6) {
413 handle->reqs_pending++;
421 handle->reqs_pending++;
428 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
433 success = handle->tcp.serv.func_acceptex(handle->socket,
445 handle->reqs_pending++;
450 handle->reqs_pending++;
451 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
463 handle->reqs_pending++;
466 /* Destroy the event handle */
467 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
475 static void uv__tcp_queue_read(uv_loop_t* loop, uv_tcp_t* handle) {
481 assert(handle->flags & UV_HANDLE_READING);
482 assert(!(handle->flags & UV_HANDLE_READ_PENDING));
484 req = &handle->read_req;
492 handle->flags &= ~UV_HANDLE_ZERO_READ;
493 handle->tcp.conn.read_buffer = uv_buf_init(NULL, 0);
494 handle->alloc_cb((uv_handle_t*) handle, 65536, &handle->tcp.conn.read_buffer);
495 if (handle->tcp.conn.read_buffer.base == NULL ||
496 handle->tcp.conn.read_buffer.len == 0) {
497 handle->read_cb((uv_stream_t*) handle, UV_ENOBUFS, &handle->tcp.conn.read_buffer);
500 assert(handle->tcp.conn.read_buffer.base != NULL);
501 buf = handle->tcp.conn.read_buffer;
503 handle->flags |= UV_HANDLE_ZERO_READ;
510 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
516 result = WSARecv(handle->socket,
524 handle->flags |= UV_HANDLE_READ_PENDING;
525 handle->reqs_pending++;
533 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
549 int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb) {
553 if (handle->flags & UV_HANDLE_SHUTTING)
556 if (0 != setsockopt(handle->socket, SOL_SOCKET, SO_LINGER, (const char*)&l, sizeof(l)))
559 uv_close((uv_handle_t*) handle, close_cb);
564 int uv__tcp_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb) {
571 if (handle->flags & UV_HANDLE_LISTENING) {
572 handle->stream.serv.connection_cb = cb;
575 if (handle->flags & UV_HANDLE_READING) {
579 if (handle->delayed_error) {
580 return handle->delayed_error;
583 if (!(handle->flags & UV_HANDLE_BOUND)) {
584 err = uv__tcp_try_bind(handle,
590 if (handle->delayed_error)
591 return handle->delayed_error;
594 if (!handle->tcp.serv.func_acceptex) {
595 if (!uv__get_acceptex_function(handle->socket, &handle->tcp.serv.func_acceptex)) {
601 if (!(handle->flags & UV_HANDLE_SHARED_TCP_SOCKET) &&
602 listen(handle->socket, backlog) == SOCKET_ERROR) {
606 handle->flags |= UV_HANDLE_LISTENING;
607 handle->stream.serv.connection_cb = cb;
608 INCREASE_ACTIVE_COUNT(loop, handle);
610 simultaneous_accepts = handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT ? 1
613 if (handle->tcp.serv.accept_reqs == NULL) {
614 handle->tcp.serv.accept_reqs =
616 if (!handle->tcp.serv.accept_reqs) {
621 req = &handle->tcp.serv.accept_reqs[i];
624 req->data = handle;
627 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
636 uv__tcp_queue_accept(handle, req);
643 req = &handle->tcp.serv.accept_reqs[i];
646 req->data = handle;
725 int uv__tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb,
727 uv_loop_t* loop = handle->loop;
729 handle->flags |= UV_HANDLE_READING;
730 handle->read_cb = read_cb;
731 handle->alloc_cb = alloc_cb;
732 INCREASE_ACTIVE_COUNT(loop, handle);
736 if (!(handle->flags & UV_HANDLE_READ_PENDING)) {
737 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
738 handle->read_req.event_handle == NULL) {
739 handle->read_req.event_handle = CreateEvent(NULL, 0, 0, NULL);
740 if (handle->read_req.event_handle == NULL) {
744 uv__tcp_queue_read(loop, handle);
786 uv_tcp_t* handle,
790 uv_loop_t* loop = handle->loop;
802 if (handle->delayed_error != 0)
805 if (!(handle->flags & UV_HANDLE_BOUND)) {
813 err = uv__tcp_try_bind(handle, bind_addr, addrlen, 0);
816 if (handle->delayed_error != 0)
820 if (!handle->tcp.conn.func_connectex) {
821 if (!uv__get_connectex_function(handle->socket, &handle->tcp.conn.func_connectex)) {
834 WSAIoctl(handle->socket,
848 req->handle = (uv_stream_t*) handle;
852 if (handle->delayed_error != 0) {
854 handle->reqs_pending++;
855 REGISTER_HANDLE_REQ(loop, handle, req);
860 success = handle->tcp.conn.func_connectex(handle->socket,
870 handle->reqs_pending++;
871 REGISTER_HANDLE_REQ(loop, handle, req);
875 handle->reqs_pending++;
876 REGISTER_HANDLE_REQ(loop, handle, req);
885 int uv_tcp_getsockname(const uv_tcp_t* handle,
889 return uv__getsockpeername((const uv_handle_t*) handle,
893 handle->delayed_error);
897 int uv_tcp_getpeername(const uv_tcp_t* handle,
901 return uv__getsockpeername((const uv_handle_t*) handle,
905 handle->delayed_error);
911 uv_tcp_t* handle,
919 req->handle = (uv_stream_t*) handle;
924 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
933 result = WSASend(handle->socket,
944 handle->reqs_pending++;
945 handle->stream.conn.write_reqs_pending++;
946 REGISTER_HANDLE_REQ(loop, handle, req);
951 handle->reqs_pending++;
952 handle->stream.conn.write_reqs_pending++;
953 REGISTER_HANDLE_REQ(loop, handle, req);
954 handle->write_queue_size += req->u.io.queued_bytes;
955 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
965 handle->reqs_pending++;
966 handle->stream.conn.write_reqs_pending++;
967 REGISTER_HANDLE_REQ(loop, handle, req);
976 int uv__tcp_try_write(uv_tcp_t* handle,
982 if (handle->stream.conn.write_reqs_pending > 0)
985 result = WSASend(handle->socket,
1000 void uv__process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle,
1006 assert(handle->type == UV_TCP);
1008 handle->flags &= ~UV_HANDLE_READ_PENDING;
1012 if ((handle->flags & UV_HANDLE_READING) ||
1013 !(handle->flags & UV_HANDLE_ZERO_READ)) {
1014 handle->flags &= ~UV_HANDLE_READING;
1015 DECREASE_ACTIVE_COUNT(loop, handle);
1016 buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
1017 uv_buf_init(NULL, 0) : handle->tcp.conn.read_buffer;
1026 handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
1028 handle->read_cb((uv_stream_t*)handle,
1033 if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
1037 handle->read_cb((uv_stream_t*)handle,
1039 &handle->tcp.conn.read_buffer);
1041 if (req->u.io.overlapped.InternalHigh < handle->tcp.conn.read_buffer.len) {
1046 if (handle->flags & UV_HANDLE_READING) {
1047 handle->flags &= ~UV_HANDLE_READING;
1048 DECREASE_ACTIVE_COUNT(loop, handle);
1053 handle->read_cb((uv_stream_t*)handle, UV_EOF, &handle->tcp.conn.read_buffer);
1060 while ((handle->flags & UV_HANDLE_READING) && (count-- > 0)) {
1062 handle->alloc_cb((uv_handle_t*) handle, 65536, &buf);
1064 handle->read_cb((uv_stream_t*) handle, UV_ENOBUFS, &buf);
1070 if (WSARecv(handle->socket,
1079 handle->read_cb((uv_stream_t*)handle, bytes, &buf);
1086 handle->flags &= ~UV_HANDLE_READING;
1087 DECREASE_ACTIVE_COUNT(loop, handle);
1089 handle->read_cb((uv_stream_t*)handle, UV_EOF, &buf);
1096 handle->read_cb((uv_stream_t*)handle, 0, &buf);
1099 handle->flags &= ~UV_HANDLE_READING;
1100 DECREASE_ACTIVE_COUNT(loop, handle);
1107 handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
1109 handle->read_cb((uv_stream_t*)handle,
1119 if ((handle->flags & UV_HANDLE_READING) &&
1120 !(handle->flags & UV_HANDLE_READ_PENDING)) {
1121 uv__tcp_queue_read(loop, handle);
1125 DECREASE_PENDING_REQ_COUNT(handle);
1129 void uv__process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle,
1133 assert(handle->type == UV_TCP);
1135 assert(handle->write_queue_size >= req->u.io.queued_bytes);
1136 handle->write_queue_size -= req->u.io.queued_bytes;
1138 UNREGISTER_HANDLE_REQ(loop, handle, req);
1140 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
1160 handle->stream.conn.write_reqs_pending--;
1161 if (handle->stream.conn.write_reqs_pending == 0) {
1162 if (handle->flags & UV_HANDLE_CLOSING) {
1163 closesocket(handle->socket);
1164 handle->socket = INVALID_SOCKET;
1166 if (handle->flags & UV_HANDLE_SHUTTING)
1168 handle,
1169 handle->stream.conn.shutdown_req);
1172 DECREASE_PENDING_REQ_COUNT(handle);
1176 void uv__process_tcp_accept_req(uv_loop_t* loop, uv_tcp_t* handle,
1181 assert(handle->type == UV_TCP);
1183 /* If handle->accepted_socket is not a valid socket, then uv_queue_accept
1187 if (handle->flags & UV_HANDLE_LISTENING) {
1188 handle->flags &= ~UV_HANDLE_LISTENING;
1189 DECREASE_ACTIVE_COUNT(loop, handle);
1190 if (handle->stream.serv.connection_cb) {
1192 handle->stream.serv.connection_cb((uv_stream_t*)handle,
1200 (char*)&handle->socket,
1201 sizeof(handle->socket)) == 0) {
1202 req->next_pending = handle->tcp.serv.pending_accepts;
1203 handle->tcp.serv.pending_accepts = req;
1206 if (handle->stream.serv.connection_cb) {
1207 handle->stream.serv.connection_cb((uv_stream_t*)handle, 0);
1215 if (handle->flags & UV_HANDLE_LISTENING) {
1216 uv__tcp_queue_accept(handle, req);
1220 DECREASE_PENDING_REQ_COUNT(handle);
1224 void uv__process_tcp_connect_req(uv_loop_t* loop, uv_tcp_t* handle,
1228 assert(handle->type == UV_TCP);
1230 UNREGISTER_HANDLE_REQ(loop, handle, req);
1233 if (handle->delayed_error) {
1238 err = handle->delayed_error;
1239 handle->delayed_error = 0;
1241 if (handle->flags & UV_HANDLE_CLOSING) {
1244 } else if (setsockopt(handle->socket,
1249 uv__connection_init((uv_stream_t*)handle);
1250 handle->flags |= UV_HANDLE_READABLE | UV_HANDLE_WRITABLE;
1260 DECREASE_PENDING_REQ_COUNT(handle);
1264 int uv__tcp_xfer_export(uv_tcp_t* handle,
1268 if (handle->flags & UV_HANDLE_CONNECTION) {
1276 if (!(handle->flags & UV_HANDLE_LISTENING)) {
1277 if (!(handle->flags & UV_HANDLE_BOUND)) {
1280 if (handle->delayed_error == 0 &&
1281 listen(handle->socket, SOMAXCONN) == SOCKET_ERROR) {
1282 handle->delayed_error = WSAGetLastError();
1287 if (WSADuplicateSocketW(handle->socket, target_pid, &xfer_info->socket_info))
1289 xfer_info->delayed_error = handle->delayed_error;
1291 /* Mark the local copy of the handle as 'shared' so we behave in a way that's
1293 handle->flags |= UV_HANDLE_SHARED_TCP_SOCKET;
1339 int uv_tcp_nodelay(uv_tcp_t* handle, int enable) {
1342 if (handle->socket != INVALID_SOCKET) {
1343 err = uv__tcp_nodelay(handle, handle->socket, enable);
1349 handle->flags |= UV_HANDLE_TCP_NODELAY;
1351 handle->flags &= ~UV_HANDLE_TCP_NODELAY;
1358 int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
1361 if (handle->socket != INVALID_SOCKET) {
1362 err = uv__tcp_keepalive(handle, handle->socket, enable, delay);
1368 handle->flags |= UV_HANDLE_TCP_KEEPALIVE;
1370 handle->flags &= ~UV_HANDLE_TCP_KEEPALIVE;
1373 /* TODO: Store delay if handle->socket isn't created yet. */
1379 int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable) {
1380 if (handle->flags & UV_HANDLE_CONNECTION) {
1385 if ((enable && !(handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT)) ||
1386 (!enable && handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT)) {
1396 if (handle->flags & UV_HANDLE_TCP_ACCEPT_STATE_CHANGING) {
1400 handle->flags |= UV_HANDLE_TCP_SINGLE_ACCEPT;
1403 if (handle->flags & UV_HANDLE_LISTENING) {
1404 handle->flags |= UV_HANDLE_TCP_ACCEPT_STATE_CHANGING;
1434 /* If there are non-ifs LSPs then try to obtain a base handle for the socket.
1509 int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) {
1526 err = uv__tcp_set_socket(handle->loop,
1527 handle,
1537 if (!uv_tcp_getsockname(handle, (struct sockaddr*) &saddr, &saddr_len)) {
1539 handle->flags |= UV_HANDLE_BOUND;
1541 if (!uv_tcp_getpeername(handle, (struct sockaddr*) &saddr, &saddr_len)) {
1543 uv__connection_init((uv_stream_t*) handle);
1544 handle->flags |= UV_HANDLE_READABLE | UV_HANDLE_WRITABLE;
1555 int uv__tcp_bind(uv_tcp_t* handle,
1561 err = uv__tcp_try_bind(handle, addr, addrlen, flags);
1573 uv_tcp_t* handle,
1579 err = uv__tcp_try_connect(req, handle, addr, addrlen, cb);