Navigation Menu

Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: deduplicate stream init logic
Browse files Browse the repository at this point in the history
Move shared init logic into uv__stream_init().
  • Loading branch information
bnoordhuis committed Sep 9, 2011
1 parent 52eca75 commit eb987bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/unix/internal.h
Expand Up @@ -80,6 +80,8 @@ uv_err_t uv_err_new_artificial(uv_loop_t* loop, int code);
void uv_fatal_error(const int errorno, const char* syscall);

/* stream */
void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
uv_handle_type type);
int uv__stream_open(uv_stream_t*, int fd, int flags);
void uv__stream_io(EV_P_ ev_io* watcher, int revents);
void uv__server_io(EV_P_ ev_io* watcher, int revents);
Expand Down
19 changes: 2 additions & 17 deletions src/unix/pipe.c
Expand Up @@ -30,24 +30,9 @@
#include <stdlib.h>

int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle) {
memset(handle, 0, sizeof *handle);

uv__handle_init(loop, (uv_handle_t*)handle, UV_NAMED_PIPE);
uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
loop->counters.pipe_init++;

handle->type = UV_NAMED_PIPE;
handle->pipe_fname = NULL; /* Only set by listener. */

ev_init(&handle->write_watcher, uv__stream_io);
ev_init(&handle->read_watcher, uv__stream_io);
handle->write_watcher.data = handle;
handle->read_watcher.data = handle;
handle->accepted_fd = -1;
handle->fd = -1;

ngx_queue_init(&handle->write_completed_queue);
ngx_queue_init(&handle->write_queue);

handle->pipe_fname = NULL;
return 0;
}

Expand Down
28 changes: 28 additions & 0 deletions src/unix/stream.c
Expand Up @@ -47,6 +47,34 @@ static size_t uv__buf_count(uv_buf_t bufs[], int bufcnt) {
}


void uv__stream_init(uv_loop_t* loop,
uv_stream_t* stream,
uv_handle_type type) {
uv__handle_init(loop, (uv_handle_t*)stream, type);

stream->alloc_cb = NULL;
stream->close_cb = NULL;
stream->connection_cb = NULL;
stream->connect_req = NULL;
stream->accepted_fd = -1;
stream->fd = -1;
stream->delayed_error = 0;
ngx_queue_init(&stream->write_queue);
ngx_queue_init(&stream->write_completed_queue);
stream->write_queue_size = 0;

ev_init(&stream->read_watcher, uv__stream_io);
stream->read_watcher.data = stream;

ev_init(&stream->write_watcher, uv__stream_io);
stream->write_watcher.data = stream;

assert(ngx_queue_empty(&stream->write_queue));
assert(ngx_queue_empty(&stream->write_completed_queue));
assert(stream->write_queue_size == 0);
}


int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
socklen_t yes;

Expand Down
22 changes: 1 addition & 21 deletions src/unix/tcp.c
Expand Up @@ -27,28 +27,8 @@


int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* tcp) {
uv__handle_init(loop, (uv_handle_t*)tcp, UV_TCP);
uv__stream_init(loop, (uv_stream_t*)tcp, UV_TCP);
loop->counters.tcp_init++;

tcp->alloc_cb = NULL;
tcp->connect_req = NULL;
tcp->accepted_fd = -1;
tcp->fd = -1;
tcp->delayed_error = 0;
ngx_queue_init(&tcp->write_queue);
ngx_queue_init(&tcp->write_completed_queue);
tcp->write_queue_size = 0;

ev_init(&tcp->read_watcher, uv__stream_io);
tcp->read_watcher.data = tcp;

ev_init(&tcp->write_watcher, uv__stream_io);
tcp->write_watcher.data = tcp;

assert(ngx_queue_empty(&tcp->write_queue));
assert(ngx_queue_empty(&tcp->write_completed_queue));
assert(tcp->write_queue_size == 0);

return 0;
}

Expand Down

0 comments on commit eb987bc

Please sign in to comment.