summaryrefslogtreecommitdiff
path: root/deps/uv/src/unix/tcp.c
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2012-07-20 10:01:46 -0700
committerisaacs <i@izs.me>2012-07-20 11:08:36 -0700
commite16d506a58d46619c05b08f3f8ef4d5491b18b69 (patch)
tree82d73799208905b6d6d290704987dafde583c603 /deps/uv/src/unix/tcp.c
parent85185bbbaa9e99bd77232f529687e9fa5d26af78 (diff)
downloadandroid-node-v8-e16d506a58d46619c05b08f3f8ef4d5491b18b69.tar.gz
android-node-v8-e16d506a58d46619c05b08f3f8ef4d5491b18b69.tar.bz2
android-node-v8-e16d506a58d46619c05b08f3f8ef4d5491b18b69.zip
uv: Upgrade to ad382bca
Diffstat (limited to 'deps/uv/src/unix/tcp.c')
-rw-r--r--deps/uv/src/unix/tcp.c154
1 files changed, 61 insertions, 93 deletions
diff --git a/deps/uv/src/unix/tcp.c b/deps/uv/src/unix/tcp.c
index 233be82514..07ad2d9eca 100644
--- a/deps/uv/src/unix/tcp.c
+++ b/deps/uv/src/unix/tcp.c
@@ -34,26 +34,6 @@ int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* tcp) {
}
-static int maybe_new_socket(uv_tcp_t* handle, int domain, int flags) {
- int sockfd;
-
- if (handle->fd != -1)
- return 0;
-
- sockfd = uv__socket(domain, SOCK_STREAM, 0);
-
- if (sockfd == -1)
- return uv__set_sys_error(handle->loop, errno);
-
- if (uv__stream_open((uv_stream_t*)handle, sockfd, flags)) {
- close(sockfd);
- return -1;
- }
-
- return 0;
-}
-
-
static int uv__bind(uv_tcp_t* tcp,
int domain,
struct sockaddr* addr,
@@ -64,8 +44,23 @@ static int uv__bind(uv_tcp_t* tcp,
saved_errno = errno;
status = -1;
- if (maybe_new_socket(tcp, domain, UV_STREAM_READABLE|UV_STREAM_WRITABLE))
- return -1;
+ if (tcp->fd < 0) {
+ if ((tcp->fd = uv__socket(domain, SOCK_STREAM, 0)) == -1) {
+ uv__set_sys_error(tcp->loop, errno);
+ goto out;
+ }
+
+ if (uv__stream_open((uv_stream_t*)tcp,
+ tcp->fd,
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
+ close(tcp->fd);
+ tcp->fd = -1;
+ status = -2;
+ goto out;
+ }
+ }
+
+ assert(tcp->fd >= 0);
tcp->delayed_error = 0;
if (bind(tcp->fd, addr, addrsize) == -1) {
@@ -84,58 +79,6 @@ out:
}
-static int uv__connect(uv_connect_t* req,
- uv_tcp_t* handle,
- struct sockaddr* addr,
- socklen_t addrlen,
- uv_connect_cb cb) {
- int r;
-
- assert(handle->type == UV_TCP);
-
- if (handle->connect_req)
- return uv__set_sys_error(handle->loop, EALREADY);
-
- if (maybe_new_socket(handle,
- addr->sa_family,
- UV_STREAM_READABLE|UV_STREAM_WRITABLE)) {
- return -1;
- }
-
- handle->delayed_error = 0;
-
- do
- r = connect(handle->fd, addr, addrlen);
- while (r == -1 && errno == EINTR);
-
- if (r == -1) {
- if (errno == EINPROGRESS)
- ; /* not an error */
- else if (errno == ECONNREFUSED)
- /* If we get a ECONNREFUSED wait until the next tick to report the
- * error. Solaris wants to report immediately--other unixes want to
- * wait.
- */
- handle->delayed_error = errno;
- else
- return uv__set_sys_error(handle->loop, errno);
- }
-
- uv__req_init(handle->loop, req, UV_CONNECT);
- req->cb = cb;
- req->handle = (uv_stream_t*) handle;
- ngx_queue_init(&req->queue);
- handle->connect_req = req;
-
- uv__io_start(handle->loop, &handle->write_watcher);
-
- if (handle->delayed_error)
- uv__io_feed(handle->loop, &handle->write_watcher, UV__IO_WRITE);
-
- return 0;
-}
-
-
int uv__tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
return uv__bind(handle,
AF_INET,
@@ -227,14 +170,33 @@ out:
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
- if (tcp->delayed_error)
- return uv__set_sys_error(tcp->loop, tcp->delayed_error);
+ int r;
- if (maybe_new_socket(tcp, AF_INET, UV_STREAM_READABLE))
+ if (tcp->delayed_error) {
+ uv__set_sys_error(tcp->loop, tcp->delayed_error);
return -1;
+ }
- if (listen(tcp->fd, backlog))
- return uv__set_sys_error(tcp->loop, errno);
+ if (tcp->fd < 0) {
+ if ((tcp->fd = uv__socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+ uv__set_sys_error(tcp->loop, errno);
+ return -1;
+ }
+
+ if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_STREAM_READABLE)) {
+ close(tcp->fd);
+ tcp->fd = -1;
+ return -1;
+ }
+ }
+
+ assert(tcp->fd >= 0);
+
+ r = listen(tcp->fd, backlog);
+ if (r < 0) {
+ uv__set_sys_error(tcp->loop, errno);
+ return -1;
+ }
tcp->connection_cb = cb;
@@ -247,31 +209,37 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
int uv__tcp_connect(uv_connect_t* req,
- uv_tcp_t* handle,
- struct sockaddr_in addr,
- uv_connect_cb cb) {
- int saved_errno;
+ uv_tcp_t* handle,
+ struct sockaddr_in address,
+ uv_connect_cb cb) {
+ int saved_errno = errno;
int status;
- saved_errno = errno;
- status = uv__connect(req, handle, (struct sockaddr*)&addr, sizeof addr, cb);
- errno = saved_errno;
+ status = uv__connect(req,
+ (uv_stream_t*)handle,
+ (struct sockaddr*)&address,
+ sizeof address,
+ cb);
+ errno = saved_errno;
return status;
}
int uv__tcp_connect6(uv_connect_t* req,
- uv_tcp_t* handle,
- struct sockaddr_in6 addr,
- uv_connect_cb cb) {
- int saved_errno;
+ uv_tcp_t* handle,
+ struct sockaddr_in6 address,
+ uv_connect_cb cb) {
+ int saved_errno = errno;
int status;
- saved_errno = errno;
- status = uv__connect(req, handle, (struct sockaddr*)&addr, sizeof addr, cb);
- errno = saved_errno;
+ status = uv__connect(req,
+ (uv_stream_t*)handle,
+ (struct sockaddr*)&address,
+ sizeof address,
+ cb);
+ errno = saved_errno;
return status;
}