Lines Matching defs:handle
27 #include "handle-inl.h"
40 static int uv__tcp_nodelay(uv_tcp_t* handle, SOCKET socket, int enable) {
52 static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsigned int delay) {
74 uv_tcp_t* handle,
82 if (handle->socket != INVALID_SOCKET)
101 handle->flags |= UV_HANDLE_EMULATE_IOCP;
113 if (!(handle->flags & UV_HANDLE_EMULATE_IOCP) && !non_ifs_lsp) {
118 handle->flags |= UV_HANDLE_SYNC_BYPASS_IOCP;
121 if (handle->flags & UV_HANDLE_TCP_NODELAY) {
122 err = uv__tcp_nodelay(handle, socket, 1);
128 if (handle->flags & UV_HANDLE_TCP_KEEPALIVE) {
129 err = uv__tcp_keepalive(handle, socket, 1, 60);
134 handle->socket = socket;
137 handle->flags |= UV_HANDLE_IPV6;
139 assert(!(handle->flags & UV_HANDLE_IPV6));
146 int uv_tcp_init_ex(uv_loop_t* loop, uv_tcp_t* handle, unsigned int flags) {
157 uv__stream_init(loop, (uv_stream_t*) handle, UV_TCP);
158 handle->tcp.serv.accept_reqs = NULL;
159 handle->tcp.serv.pending_accepts = NULL;
160 handle->socket = INVALID_SOCKET;
161 handle->reqs_pending = 0;
162 handle->tcp.serv.func_acceptex = NULL;
163 handle->tcp.conn.func_connectex = NULL;
164 handle->tcp.serv.processed_accepts = 0;
165 handle->delayed_error = 0;
167 /* If anything fails beyond this point we need to remove the handle from
168 * the handle queue, since it was added by uv__handle_init in uv__stream_init.
178 uv__queue_remove(&handle->handle_queue);
182 err = uv__tcp_set_socket(handle->loop, handle, sock, domain, 0);
185 uv__queue_remove(&handle->handle_queue);
195 int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* handle) {
196 return uv_tcp_init_ex(loop, handle, AF_UNSPEC);
227 void uv__tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {
231 assert(handle->flags & UV_HANDLE_CLOSING);
232 assert(handle->reqs_pending == 0);
233 assert(!(handle->flags & UV_HANDLE_CLOSED));
234 assert(handle->socket == INVALID_SOCKET);
236 if (!(handle->flags & UV_HANDLE_CONNECTION) && handle->tcp.serv.accept_reqs) {
237 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
239 req = &handle->tcp.serv.accept_reqs[i];
251 uv__free(handle->tcp.serv.accept_reqs);
252 handle->tcp.serv.accept_reqs = NULL;
255 if (handle->flags & UV_HANDLE_CONNECTION &&
256 handle->flags & UV_HANDLE_EMULATE_IOCP) {
257 if (handle->read_req.wait_handle != INVALID_HANDLE_VALUE) {
258 UnregisterWait(handle->read_req.wait_handle);
259 handle->read_req.wait_handle = INVALID_HANDLE_VALUE;
261 if (handle->read_req.event_handle != NULL) {
262 CloseHandle(handle->read_req.event_handle);
263 handle->read_req.event_handle = NULL;
267 uv__handle_close(handle);
282 static int uv__tcp_try_bind(uv_tcp_t* handle,
289 if (handle->socket == INVALID_SOCKET) {
301 err = uv__tcp_set_socket(handle->loop, handle, sock, addr->sa_family, 0);
314 /* TODO: how to handle errors? This may fail if there is no ipv4 stack
317 setsockopt(handle->socket,
325 r = bind(handle->socket, addr, addrlen);
331 handle->delayed_error = err;
337 handle->flags |= UV_HANDLE_BOUND;
345 uv_tcp_t* handle;
349 handle = (uv_tcp_t*)req->data;
350 assert(handle != NULL);
353 if (!PostQueuedCompletionStatus(handle->loop->iocp,
364 uv_tcp_t* handle;
368 handle = (uv_tcp_t*)req->handle;
369 assert(handle != NULL);
372 if (!PostQueuedCompletionStatus(handle->loop->iocp,
381 static void uv__tcp_queue_accept(uv_tcp_t* handle, uv_tcp_accept_t* req) {
382 uv_loop_t* loop = handle->loop;
388 assert(handle->flags & UV_HANDLE_LISTENING);
392 if (handle->flags & UV_HANDLE_IPV6) {
403 handle->reqs_pending++;
411 handle->reqs_pending++;
418 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
423 success = handle->tcp.serv.func_acceptex(handle->socket,
435 handle->reqs_pending++;
440 handle->reqs_pending++;
441 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
453 handle->reqs_pending++;
456 /* Destroy the event handle */
457 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
465 static void uv__tcp_queue_read(uv_loop_t* loop, uv_tcp_t* handle) {
471 assert(handle->flags & UV_HANDLE_READING);
472 assert(!(handle->flags & UV_HANDLE_READ_PENDING));
474 req = &handle->read_req;
477 handle->flags |= UV_HANDLE_ZERO_READ;
483 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
489 result = WSARecv(handle->socket,
497 handle->flags |= UV_HANDLE_READ_PENDING;
498 handle->reqs_pending++;
506 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
522 int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb) {
526 if (uv__is_stream_shutting(handle))
529 if (0 != setsockopt(handle->socket, SOL_SOCKET, SO_LINGER, (const char*)&l, sizeof(l)))
532 uv_close((uv_handle_t*) handle, close_cb);
537 int uv__tcp_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb) {
544 if (handle->flags & UV_HANDLE_LISTENING) {
545 handle->stream.serv.connection_cb = cb;
548 if (handle->flags & UV_HANDLE_READING) {
552 if (handle->delayed_error) {
553 return handle->delayed_error;
556 if (!(handle->flags & UV_HANDLE_BOUND)) {
557 err = uv__tcp_try_bind(handle,
563 if (handle->delayed_error)
564 return handle->delayed_error;
567 if (!handle->tcp.serv.func_acceptex) {
568 if (!uv__get_acceptex_function(handle->socket, &handle->tcp.serv.func_acceptex)) {
574 if (!(handle->flags & UV_HANDLE_SHARED_TCP_SOCKET) &&
575 listen(handle->socket, backlog) == SOCKET_ERROR) {
579 handle->flags |= UV_HANDLE_LISTENING;
580 handle->stream.serv.connection_cb = cb;
581 INCREASE_ACTIVE_COUNT(loop, handle);
583 simultaneous_accepts = handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT ? 1
586 if (handle->tcp.serv.accept_reqs == NULL) {
587 handle->tcp.serv.accept_reqs =
589 if (!handle->tcp.serv.accept_reqs) {
594 req = &handle->tcp.serv.accept_reqs[i];
597 req->data = handle;
600 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
609 uv__tcp_queue_accept(handle, req);
616 req = &handle->tcp.serv.accept_reqs[i];
619 req->data = handle;
695 int uv__tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb,
697 uv_loop_t* loop = handle->loop;
699 handle->flags |= UV_HANDLE_READING;
700 handle->read_cb = read_cb;
701 handle->alloc_cb = alloc_cb;
702 INCREASE_ACTIVE_COUNT(loop, handle);
706 if (!(handle->flags & UV_HANDLE_READ_PENDING)) {
707 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
708 handle->read_req.event_handle == NULL) {
709 handle->read_req.event_handle = CreateEvent(NULL, 0, 0, NULL);
710 if (handle->read_req.event_handle == NULL) {
714 uv__tcp_queue_read(loop, handle);
756 uv_tcp_t* handle,
760 uv_loop_t* loop = handle->loop;
772 if (handle->delayed_error != 0)
775 if (!(handle->flags & UV_HANDLE_BOUND)) {
783 err = uv__tcp_try_bind(handle, bind_addr, addrlen, 0);
786 if (handle->delayed_error != 0)
790 if (!handle->tcp.conn.func_connectex) {
791 if (!uv__get_connectex_function(handle->socket, &handle->tcp.conn.func_connectex)) {
804 WSAIoctl(handle->socket,
818 req->handle = (uv_stream_t*) handle;
822 if (handle->delayed_error != 0) {
824 handle->reqs_pending++;
825 REGISTER_HANDLE_REQ(loop, handle, req);
830 success = handle->tcp.conn.func_connectex(handle->socket,
840 handle->reqs_pending++;
841 REGISTER_HANDLE_REQ(loop, handle, req);
845 handle->reqs_pending++;
846 REGISTER_HANDLE_REQ(loop, handle, req);
855 int uv_tcp_getsockname(const uv_tcp_t* handle,
859 return uv__getsockpeername((const uv_handle_t*) handle,
863 handle->delayed_error);
867 int uv_tcp_getpeername(const uv_tcp_t* handle,
871 return uv__getsockpeername((const uv_handle_t*) handle,
875 handle->delayed_error);
881 uv_tcp_t* handle,
889 req->handle = (uv_stream_t*) handle;
894 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
903 result = WSASend(handle->socket,
914 handle->reqs_pending++;
915 handle->stream.conn.write_reqs_pending++;
916 REGISTER_HANDLE_REQ(loop, handle, req);
921 handle->reqs_pending++;
922 handle->stream.conn.write_reqs_pending++;
923 REGISTER_HANDLE_REQ(loop, handle, req);
924 handle->write_queue_size += req->u.io.queued_bytes;
925 if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
935 handle->reqs_pending++;
936 handle->stream.conn.write_reqs_pending++;
937 REGISTER_HANDLE_REQ(loop, handle, req);
946 int uv__tcp_try_write(uv_tcp_t* handle,
952 if (handle->stream.conn.write_reqs_pending > 0)
955 result = WSASend(handle->socket,
970 void uv__process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle,
976 assert(handle->type == UV_TCP);
978 handle->flags &= ~UV_HANDLE_READ_PENDING;
982 if ((handle->flags & UV_HANDLE_READING) ||
983 !(handle->flags & UV_HANDLE_ZERO_READ)) {
984 handle->flags &= ~UV_HANDLE_READING;
985 DECREASE_ACTIVE_COUNT(loop, handle);
986 buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
987 uv_buf_init(NULL, 0) : handle->tcp.conn.read_buffer;
996 handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
998 handle->read_cb((uv_stream_t*)handle,
1003 if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
1007 handle->read_cb((uv_stream_t*)handle,
1009 &handle->tcp.conn.read_buffer);
1011 if (req->u.io.overlapped.InternalHigh < handle->tcp.conn.read_buffer.len) {
1016 if (handle->flags & UV_HANDLE_READING) {
1017 handle->flags &= ~UV_HANDLE_READING;
1018 DECREASE_ACTIVE_COUNT(loop, handle);
1023 handle->read_cb((uv_stream_t*)handle, UV_EOF, &handle->tcp.conn.read_buffer);
1030 while ((handle->flags & UV_HANDLE_READING) && (count-- > 0)) {
1032 handle->alloc_cb((uv_handle_t*) handle, 65536, &buf);
1034 handle->read_cb((uv_stream_t*) handle, UV_ENOBUFS, &buf);
1040 if (WSARecv(handle->socket,
1049 handle->read_cb((uv_stream_t*)handle, bytes, &buf);
1056 handle->flags &= ~UV_HANDLE_READING;
1057 DECREASE_ACTIVE_COUNT(loop, handle);
1059 handle->read_cb((uv_stream_t*)handle, UV_EOF, &buf);
1066 handle->read_cb((uv_stream_t*)handle, 0, &buf);
1069 handle->flags &= ~UV_HANDLE_READING;
1070 DECREASE_ACTIVE_COUNT(loop, handle);
1077 handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
1079 handle->read_cb((uv_stream_t*)handle,
1089 if ((handle->flags & UV_HANDLE_READING) &&
1090 !(handle->flags & UV_HANDLE_READ_PENDING)) {
1091 uv__tcp_queue_read(loop, handle);
1095 DECREASE_PENDING_REQ_COUNT(handle);
1099 void uv__process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle,
1103 assert(handle->type == UV_TCP);
1105 assert(handle->write_queue_size >= req->u.io.queued_bytes);
1106 handle->write_queue_size -= req->u.io.queued_bytes;
1108 UNREGISTER_HANDLE_REQ(loop, handle, req);
1110 if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
1130 handle->stream.conn.write_reqs_pending--;
1131 if (handle->stream.conn.write_reqs_pending == 0) {
1132 if (handle->flags & UV_HANDLE_CLOSING) {
1133 closesocket(handle->socket);
1134 handle->socket = INVALID_SOCKET;
1136 if (uv__is_stream_shutting(handle))
1138 handle,
1139 handle->stream.conn.shutdown_req);
1142 DECREASE_PENDING_REQ_COUNT(handle);
1146 void uv__process_tcp_accept_req(uv_loop_t* loop, uv_tcp_t* handle,
1151 assert(handle->type == UV_TCP);
1153 /* If handle->accepted_socket is not a valid socket, then uv_queue_accept
1157 if (handle->flags & UV_HANDLE_LISTENING) {
1158 handle->flags &= ~UV_HANDLE_LISTENING;
1159 DECREASE_ACTIVE_COUNT(loop, handle);
1160 if (handle->stream.serv.connection_cb) {
1162 handle->stream.serv.connection_cb((uv_stream_t*)handle,
1170 (char*)&handle->socket,
1171 sizeof(handle->socket)) == 0) {
1172 req->next_pending = handle->tcp.serv.pending_accepts;
1173 handle->tcp.serv.pending_accepts = req;
1176 if (handle->stream.serv.connection_cb) {
1177 handle->stream.serv.connection_cb((uv_stream_t*)handle, 0);
1185 if (handle->flags & UV_HANDLE_LISTENING) {
1186 uv__tcp_queue_accept(handle, req);
1190 DECREASE_PENDING_REQ_COUNT(handle);
1194 void uv__process_tcp_connect_req(uv_loop_t* loop, uv_tcp_t* handle,
1198 assert(handle->type == UV_TCP);
1200 UNREGISTER_HANDLE_REQ(loop, handle, req);
1203 if (handle->delayed_error) {
1208 err = handle->delayed_error;
1209 handle->delayed_error = 0;
1211 if (handle->flags & UV_HANDLE_CLOSING) {
1214 } else if (setsockopt(handle->socket,
1219 uv__connection_init((uv_stream_t*)handle);
1220 handle->flags |= UV_HANDLE_READABLE | UV_HANDLE_WRITABLE;
1229 DECREASE_PENDING_REQ_COUNT(handle);
1233 int uv__tcp_xfer_export(uv_tcp_t* handle,
1237 if (handle->flags & UV_HANDLE_CONNECTION) {
1245 if (!(handle->flags & UV_HANDLE_LISTENING)) {
1246 if (!(handle->flags & UV_HANDLE_BOUND)) {
1249 if (handle->delayed_error == 0 &&
1250 listen(handle->socket, SOMAXCONN) == SOCKET_ERROR) {
1251 handle->delayed_error = WSAGetLastError();
1256 if (WSADuplicateSocketW(handle->socket, target_pid, &xfer_info->socket_info))
1258 xfer_info->delayed_error = handle->delayed_error;
1260 /* Mark the local copy of the handle as 'shared' so we behave in a way that's
1262 handle->flags |= UV_HANDLE_SHARED_TCP_SOCKET;
1307 int uv_tcp_nodelay(uv_tcp_t* handle, int enable) {
1310 if (handle->socket != INVALID_SOCKET) {
1311 err = uv__tcp_nodelay(handle, handle->socket, enable);
1317 handle->flags |= UV_HANDLE_TCP_NODELAY;
1319 handle->flags &= ~UV_HANDLE_TCP_NODELAY;
1326 int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
1329 if (handle->socket != INVALID_SOCKET) {
1330 err = uv__tcp_keepalive(handle, handle->socket, enable, delay);
1336 handle->flags |= UV_HANDLE_TCP_KEEPALIVE;
1338 handle->flags &= ~UV_HANDLE_TCP_KEEPALIVE;
1341 /* TODO: Store delay if handle->socket isn't created yet. */
1347 int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable) {
1348 if (handle->flags & UV_HANDLE_CONNECTION) {
1353 if ((enable && !(handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT)) ||
1354 (!enable && handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT)) {
1364 if (handle->flags & UV_HANDLE_TCP_ACCEPT_STATE_CHANGING) {
1368 handle->flags |= UV_HANDLE_TCP_SINGLE_ACCEPT;
1371 if (handle->flags & UV_HANDLE_LISTENING) {
1372 handle->flags |= UV_HANDLE_TCP_ACCEPT_STATE_CHANGING;
1402 /* If there are non-ifs LSPs then try to obtain a base handle for the socket.
1477 int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) {
1494 err = uv__tcp_set_socket(handle->loop,
1495 handle,
1505 if (!uv_tcp_getsockname(handle, (struct sockaddr*) &saddr, &saddr_len)) {
1507 handle->flags |= UV_HANDLE_BOUND;
1509 if (!uv_tcp_getpeername(handle, (struct sockaddr*) &saddr, &saddr_len)) {
1511 uv__connection_init((uv_stream_t*) handle);
1512 handle->flags |= UV_HANDLE_READABLE | UV_HANDLE_WRITABLE;
1523 int uv__tcp_bind(uv_tcp_t* handle,
1529 err = uv__tcp_try_bind(handle, addr, addrlen, flags);
1541 uv_tcp_t* handle,
1547 err = uv__tcp_try_connect(req, handle, addr, addrlen, cb);