Example #1
0
int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
  socklen_t yes;

  assert(fd >= 0);
  stream->fd = fd;

  stream->flags |= flags;

  if (stream->type == UV_TCP) {
    /* Reuse the port address if applicable. */
    yes = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
      uv__set_sys_error(stream->loop, errno);
      return -1;
    }

    if ((stream->flags & UV_TCP_NODELAY) &&
        uv__tcp_nodelay((uv_tcp_t*)stream, 1)) {
      return -1;
    }

    /* TODO Use delay the user passed in. */
    if ((stream->flags & UV_TCP_KEEPALIVE) &&
        uv__tcp_keepalive((uv_tcp_t*)stream, 1, 60)) {
      return -1;
    }
  }

  /* Associate the fd with each watcher. */
  uv__io_set(&stream->read_watcher, uv__stream_io, fd, UV__IO_READ);
  uv__io_set(&stream->write_watcher, uv__stream_io, fd, UV__IO_WRITE);

  return 0;
}
Example #2
0
File: poll.c Project: 4rejin/node
int uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
  int events;

  assert((pevents & ~(UV_READABLE | UV_WRITABLE)) == 0);
  assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));

  if (pevents == 0) {
    uv__poll_stop(handle);
    return 0;
  }

  events = 0;
  if (pevents & UV_READABLE)
    events |= UV__IO_READ;
  if (pevents & UV_WRITABLE)
    events |= UV__IO_WRITE;

  uv__io_stop(handle->loop, &handle->io_watcher);
  uv__io_set(&handle->io_watcher, uv__poll_io, handle->fd, events);
  uv__io_start(handle->loop, &handle->io_watcher);

  handle->poll_cb = poll_cb;
  uv__handle_start(handle);

  return 0;
}
Example #3
0
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);

  if (maybe_new_socket(tcp, AF_INET, UV_STREAM_READABLE))
    return -1;

  if (listen(tcp->fd, backlog))
    return uv__set_sys_error(tcp->loop, errno);

  tcp->connection_cb = cb;

  /* Start listening for connections. */
  uv__io_set(&tcp->read_watcher, uv__server_io, tcp->fd, UV__IO_READ);
  uv__io_start(tcp->loop, &tcp->read_watcher);

  return 0;
}
Example #4
0
File: tcp.c Project: kazupon/libuv
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
  static int single_accept = -1;

  if (tcp->delayed_error)
    return uv__set_sys_error(tcp->loop, tcp->delayed_error);

  if (single_accept == -1) {
    const char* val = getenv("UV_TCP_SINGLE_ACCEPT");
    single_accept = (val == NULL) || (atoi(val) != 0); /* on by default */
  }

  if (!single_accept)
    goto no_single_accept;

  tcp->idle_handle = malloc(sizeof(*tcp->idle_handle));
  if (tcp->idle_handle == NULL)
    return uv__set_sys_error(tcp->loop, ENOMEM);

  if (uv_idle_init(tcp->loop, tcp->idle_handle))
    abort();

  tcp->flags |= UV_TCP_SINGLE_ACCEPT;

no_single_accept:
  if (maybe_new_socket(tcp, AF_INET, UV_STREAM_READABLE))
    return -1;

  if (listen(tcp->fd, backlog))
    return uv__set_sys_error(tcp->loop, errno);

  tcp->connection_cb = cb;

  /* Start listening for connections. */
  uv__io_set(&tcp->read_watcher, uv__server_io, tcp->fd, UV__IO_READ);
  uv__io_start(tcp->loop, &tcp->read_watcher);

  return 0;
}