From 85368e8d45dbbf8165ad64d5001e5359ba21c05b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 28 Sep 2011 08:22:00 -0700 Subject: [PATCH] unix,win: Start unifying shared tcp connect code. --- src/unix/tcp.c | 26 ++++---------------------- src/uv-common.c | 24 ++++++++++++++++++++++++ src/uv-common.h | 11 +++++++++++ src/win/tcp.c | 22 ++++++++-------------- 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/unix/tcp.c b/src/unix/tcp.c index 162dcce0..5ff39699 100644 --- a/src/unix/tcp.c +++ b/src/unix/tcp.c @@ -216,55 +216,37 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) { } -int uv_tcp_connect(uv_connect_t* req, +int uv__tcp_connect(uv_connect_t* req, uv_tcp_t* handle, struct sockaddr_in address, uv_connect_cb cb) { - int saved_errno; + int saved_errno = errno; int status; - saved_errno = errno; - status = -1; - - if (handle->type != UV_TCP || address.sin_family != AF_INET) { - uv__set_sys_error(handle->loop, EINVAL); - goto out; - } - status = uv__connect(req, (uv_stream_t*)handle, (struct sockaddr*)&address, sizeof address, cb); -out: errno = saved_errno; return status; } -int uv_tcp_connect6(uv_connect_t* req, +int uv__tcp_connect6(uv_connect_t* req, uv_tcp_t* handle, struct sockaddr_in6 address, uv_connect_cb cb) { - int saved_errno; + int saved_errno = errno; int status; - saved_errno = errno; - status = -1; - - if (handle->type != UV_TCP || address.sin6_family != AF_INET6) { - uv__set_sys_error(handle->loop, EINVAL); - goto out; - } - status = uv__connect(req, (uv_stream_t*)handle, (struct sockaddr*)&address, sizeof address, cb); -out: errno = saved_errno; return status; } diff --git a/src/uv-common.c b/src/uv-common.c index dddcd8b3..d5e2a546 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -206,3 +206,27 @@ void uv_remove_ares_handle(uv_ares_task_t* handle) { int uv_ares_handles_empty(uv_loop_t* loop) { return loop->uv_ares_handles_ ? 0 : 1; } + +int uv_tcp_connect(uv_connect_t* req, + uv_tcp_t* handle, + struct sockaddr_in address, + uv_connect_cb cb) { + if (handle->type != UV_TCP || address.sin_family != AF_INET) { + uv__set_artificial_error(handle->loop, UV_EINVAL); + return -1; + } + + return uv__tcp_connect(req, handle, address, cb); +} + +int uv_tcp_connect6(uv_connect_t* req, + uv_tcp_t* handle, + struct sockaddr_in6 address, + uv_connect_cb cb) { + if (handle->type != UV_TCP || address.sin6_family != AF_INET6) { + uv__set_artificial_error(handle->loop, UV_EINVAL); + return -1; + } + + return uv__tcp_connect6(req, handle, address, cb); +} diff --git a/src/uv-common.h b/src/uv-common.h index f534d0e1..e803812f 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -53,4 +53,15 @@ void uv__set_error(uv_loop_t* loop, uv_err_code code, int sys_error); void uv__set_sys_error(uv_loop_t* loop, int sys_error); void uv__set_artificial_error(uv_loop_t* loop, uv_err_code code); +int uv__tcp_connect(uv_connect_t* req, + uv_tcp_t* handle, + struct sockaddr_in address, + uv_connect_cb cb); + +int uv__tcp_connect6(uv_connect_t* req, + uv_tcp_t* handle, + struct sockaddr_in6 address, + uv_connect_cb cb); + + #endif /* UV_COMMON_H_ */ diff --git a/src/win/tcp.c b/src/win/tcp.c index 29bf981d..0fa93020 100644 --- a/src/win/tcp.c +++ b/src/win/tcp.c @@ -472,8 +472,10 @@ int uv_tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb, } -int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, - struct sockaddr_in address, uv_connect_cb cb) { +int uv__tcp_connect(uv_connect_t* req, + uv_tcp_t* handle, + struct sockaddr_in address, + uv_connect_cb cb) { uv_loop_t* loop = handle->loop; int addrsize = sizeof(struct sockaddr_in); BOOL success; @@ -484,11 +486,6 @@ int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, return -1; } - if (handle->type != UV_TCP || address.sin_family != AF_INET) { - uv__set_sys_error(loop, WSAEINVAL); - return -1; - } - if (!(handle->flags & UV_HANDLE_BOUND) && uv_tcp_bind(handle, uv_addr_ip4_any_) < 0) return -1; @@ -523,8 +520,10 @@ int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, } -int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle, - struct sockaddr_in6 address, uv_connect_cb cb) { +int uv__tcp_connect6(uv_connect_t* req, + uv_tcp_t* handle, + struct sockaddr_in6 address, + uv_connect_cb cb) { uv_loop_t* loop = handle->loop; int addrsize = sizeof(struct sockaddr_in6); BOOL success; @@ -540,11 +539,6 @@ int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle, return -1; } - if (handle->type != UV_TCP || address.sin6_family != AF_INET6) { - uv__set_sys_error(loop, WSAEINVAL); - return -1; - } - if (!(handle->flags & UV_HANDLE_BOUND) && uv_tcp_bind6(handle, uv_addr_ip6_any_) < 0) return -1;