Ejemplo n.º 1
0
static void
tcp_bind(uv_loop_t *loop, struct server_context *server) {
    int rc;

    uv_tcp_init(loop, &server->tcp);

    rc = uv_tcp_open(&server->tcp, server->tcp_fd);
    if (rc) {
        logger_stderr("tcp open error: %s", uv_strerror(rc));
    }

    uv_async_init(loop, &server->async_handle, consumer_close);

    rc = uv_tcp_bind(&server->tcp, server->local_addr, 0);
    if (rc || errno) {
        logger_stderr("bind error: %s", rc ? uv_strerror(rc) : strerror(errno));
        exit(1);
    }

    rc = uv_listen((uv_stream_t*)&server->tcp, SOMAXCONN, server->accept_cb);
    if (rc) {
        logger_stderr("listen error: %s", rc ? uv_strerror(rc) : strerror(errno));
        exit(1);
    }
}
Ejemplo n.º 2
0
int
tcp_server_start(struct tundev_context *ctx, uv_loop_t *loop) {
    int rc;

    uv_tcp_init(loop, &ctx->inet_tcp.tcp);

    ctx->inet_tcp_fd = create_socket(SOCK_STREAM, 1);
    if ((rc = uv_tcp_open(&ctx->inet_tcp.tcp, ctx->inet_tcp_fd))) {
        logger_stderr("tcp open error: %s", uv_strerror(rc));
        exit(1);
    }

    uv_tcp_bind(&ctx->inet_tcp.tcp, &ctx->tun->addr, 0);
    if (rc) {
        logger_stderr("tcp bind error: %s", uv_strerror(rc));
        exit(1);
    }

    ctx->inet_tcp.tcp.data = ctx;
    rc = uv_listen(&ctx->inet_tcp.stream, 128, accept_cb);
    if (rc) {
        logger_stderr("tcp listen error: %s", uv_strerror(rc));
        exit(1);
    }
    return rc;
}
Ejemplo n.º 3
0
void test1()
{
	Sleep(5000);

	struct sockaddr_in addr;
	uv_tcp_t client;
	uv_os_sock_t sock;
	int r;

	//int nRet = uv_ip4_addr("127.0.0.1", 1234, &addr);
	TEST_ASSERT(0 == uv_ip4_addr("127.0.0.1", 1234, &addr));

	startup();
	sock = create_tcp_socket();

	r = uv_tcp_init(uv_default_loop(), &client);
	TEST_ASSERT(r == 0);

	r = uv_tcp_open(&client, sock);
	TEST_ASSERT(r == 0);

	r = uv_tcp_connect(&connect_req,
		&client,
		(const struct sockaddr*) &addr,
		connect_cb);
	TEST_ASSERT(r == 0);

	uv_run(uv_default_loop(), UV_RUN_DEFAULT);

// 	ASSERT(shutdown_cb_called == 1);
// 	ASSERT(connect_cb_called == 1);
// 	ASSERT(write_cb_called == 1);
// 	ASSERT(close_cb_called == 1);

}
Ejemplo n.º 4
0
Archivo: tcp.c Proyecto: senlinms/luv
static int luv_tcp_open(lua_State* L) {
  uv_tcp_t* handle = luv_check_tcp(L, 1);
  uv_os_sock_t sock = luaL_checkinteger(L, 2);
  int ret = uv_tcp_open(handle, sock);
  if (ret < 0) return luv_error(L, ret);
  lua_pushinteger(L, ret);
  return 1;
}
Ejemplo n.º 5
0
static int lluv_tcp_open(lua_State *L){
  lluv_handle_t  *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  uv_os_sock_t sock = lluv_check_os_sock(L, 2);
  int err = uv_tcp_open(LLUV_H(handle, uv_tcp_t), sock);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Ejemplo n.º 6
0
static void
as_uv_connect(as_event_command* cmd)
{
	int fd = as_event_create_socket(cmd);
	
	if (fd < 0) {
		return;
	}
	
	as_event_connection* conn = cmd->conn;
	uv_tcp_t* socket = &conn->socket;
	int status = uv_tcp_init(cmd->event_loop->loop, socket);
	
	if (status) {
		as_error err;
		as_error_update(&err, AEROSPIKE_ERR_ASYNC_CONNECTION, "uv_tcp_init failed: %s", uv_strerror(status));
		// Call standard event connection error handler because as_uv_connect_error() requires that
		// uv_tcp_init() has already succeeded.
		as_event_connect_error(cmd, &err, fd);
		return;
	}
	
	// Define externally created fd to uv_tcp_t.
	status = uv_tcp_open(socket, fd);
	
	if (status) {
		as_error err;
		as_error_update(&err, AEROSPIKE_ERR_ASYNC_CONNECTION, "uv_tcp_open failed: %s", uv_strerror(status));
		// Close fd directly because we created it outside of libuv and uv_tcp_t does not know about it here.
		close(fd);
		as_uv_connect_error(cmd, &err);
		return;
	}
	
	socket->data = conn;
	conn->req.connect.data = cmd;
	
	as_node* node = cmd->node;
	as_address* primary = as_vector_get(&node->addresses, node->address_index);
	
	status = uv_tcp_connect(&conn->req.connect, socket, (struct sockaddr*)&primary->addr, as_uv_connected);
	
	if (status) {
		as_error err;
		as_error_update(&err, AEROSPIKE_ERR_ASYNC_CONNECTION, "uv_tcp_connect failed: %s", uv_strerror(status));
		as_uv_connect_error(cmd, &err);
	}
}
Ejemplo n.º 7
0
int uv_bind(uv_handle_t* handle, struct sockaddr* addr) {
    int addrsize;
    int domain;
    int r;

    if (handle->fd <= 0) {
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0) {
            uv_err_new(handle, errno);
            return -1;
        }

        if (uv_tcp_open(handle, fd)) {
            close(fd);
            return -2;
        }
    }

    assert(handle->fd >= 0);

    if (addr->sa_family == AF_INET) {
        addrsize = sizeof(struct sockaddr_in);
        domain = AF_INET;
    } else if (addr->sa_family == AF_INET6) {
        addrsize = sizeof(struct sockaddr_in6);
        domain = AF_INET6;
    } else {
        uv_err_new(handle, EFAULT);
        return -1;
    }

    r = bind(handle->fd, addr, addrsize);
    handle->delayed_error = 0;

    if (r) {
        switch (errno) {
        case EADDRINUSE:
            handle->delayed_error = errno;
            return 0;

        default:
            uv_err_new(handle, errno);
            return -1;
        }
    }

    return 0;
}
Ejemplo n.º 8
0
int uv_accept(uv_tcp_t* server, uv_tcp_t* client) {
  if (server->accepted_fd < 0) {
    uv_err_new((uv_handle_t*) server, EAGAIN);
    return -1;
  }

  if (uv_tcp_open(client, server->accepted_fd)) {
    /* Ignore error for now */
    server->accepted_fd = -1;
    close(server->accepted_fd);
    return -1;
  } else {
    server->accepted_fd = -1;
    ev_io_start(EV_DEFAULT_ &server->read_watcher);
    return 0;
  }
}
Ejemplo n.º 9
0
static int _tcp_bindfd(uv_tcp_t *handle, int fd, uv_connection_cb connection)
{
	if (!handle) {
		return kr_error(EINVAL);
	}

	int ret = uv_tcp_open(handle, (uv_os_sock_t) fd);
	if (ret != 0) {
		return ret;
	}

	ret = uv_listen((uv_stream_t *)handle, 16, connection);
	if (ret != 0) {
		return ret;
	}
	return tcp_bind_finalize((uv_handle_t *)handle);
}
Ejemplo n.º 10
0
int uv_bind(uv_tcp_t* tcp, struct sockaddr_in addr) {
  int addrsize = sizeof(struct sockaddr_in);
  int domain = AF_INET;
  int r;

  if (tcp->fd <= 0) {
    int fd = socket(AF_INET, SOCK_STREAM, 0);

    if (fd < 0) {
      uv_err_new((uv_handle_t*)tcp, errno);
      return -1;
    }

    if (uv_tcp_open(tcp, fd)) {
      close(fd);
      return -2;
    }
  }

  assert(tcp->fd >= 0);

  if (addr.sin_family != AF_INET) {
    uv_err_new((uv_handle_t*)tcp, EFAULT);
    return -1;
  }

  r = bind(tcp->fd, (struct sockaddr*) &addr, addrsize);
  tcp->delayed_error = 0;

  if (r) {
    switch (errno) {
      case EADDRINUSE:
        tcp->delayed_error = errno;
        return 0;

      default:
        uv_err_new((uv_handle_t*)tcp, errno);
        return -1;
    }
  }

  return 0;
}
Ejemplo n.º 11
0
int uv_accept(uv_handle_t* server, uv_stream_t* client) {
  uv_tcp_t* tcpServer = (uv_tcp_t*)server;
  uv_tcp_t* tcpClient = (uv_tcp_t*)client;

  if (tcpServer->accepted_fd < 0) {
    uv_err_new(server, EAGAIN);
    return -1;
  }

  if (uv_tcp_open(tcpClient, tcpServer->accepted_fd)) {
    /* Ignore error for now */
    tcpServer->accepted_fd = -1;
    close(tcpServer->accepted_fd);
    return -1;
  } else {
    tcpServer->accepted_fd = -1;
    ev_io_start(EV_DEFAULT_ &tcpServer->read_watcher);
    return 0;
  }
}
Ejemplo n.º 12
0
int uv_accept(uv_handle_t* server, uv_handle_t* client,
              uv_close_cb close_cb, void* data) {
    if (server->accepted_fd < 0) {
        return -1;
    }

    if (uv_tcp_init(client, close_cb, data)) {
        return -1;
    }

    if (uv_tcp_open(client, server->accepted_fd)) {
        /* Ignore error for now */
        server->accepted_fd = -1;
        close(server->accepted_fd);
        return -1;
    } else {
        server->accepted_fd = -1;
        ev_io_start(EV_DEFAULT_ &server->read_watcher);
        return 0;
    }
}
Ejemplo n.º 13
0
Archivo: tcp.c Proyecto: eagles125/pyuv
static PyObject *
TCP_func_open(TCP *self, PyObject *args)
{
    int r;
    long fd;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "l:open", &fd)) {
        return NULL;
    }

    r = uv_tcp_open(&self->tcp_h, (uv_os_sock_t)fd);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TCPError);
        return NULL;
    }

    Py_RETURN_NONE;
}
Ejemplo n.º 14
0
int spawn_tcp_server_helper(void) {
  uv_tcp_t tcp;
  uv_os_sock_t handle;
  int r;

  r = uv_tcp_init(uv_default_loop(), &tcp);
  ASSERT(r == 0);

#ifdef _WIN32
  handle = _get_osfhandle(3);
#else
  handle = 3;
#endif
  r = uv_tcp_open(&tcp, handle);
  ASSERT(r == 0);

  /* Make sure that we can listen on a socket that was
   * passed down from the parent process
   */
  r = uv_listen((uv_stream_t*)&tcp, SOMAXCONN, NULL);
  ASSERT(r == 0);

  return 1;
}
Ejemplo n.º 15
0
int uv__bind(uv_tcp_t* tcp, int domain, struct sockaddr* addr, int addrsize) {
  int r;

  if (tcp->fd <= 0) {
    int fd = socket(domain, SOCK_STREAM, 0);

    if (fd < 0) {
      uv_err_new((uv_handle_t*)tcp, errno);
      return -1;
    }

    if (uv_tcp_open(tcp, fd)) {
      close(fd);
      return -2;
    }
  }

  assert(tcp->fd >= 0);

  r = bind(tcp->fd, addr, addrsize);
  tcp->delayed_error = 0;

  if (r) {
    switch (errno) {
      case EADDRINUSE:
        tcp->delayed_error = errno;
        return 0;

      default:
        uv_err_new((uv_handle_t*)tcp, errno);
        return -1;
    }
  }

  return 0;
}
Ejemplo n.º 16
0
int uv_connect(uv_req_t* req, struct sockaddr* addr) {
    uv_handle_t* handle = req->handle;

    if (handle->fd <= 0) {
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0) {
            uv_err_new(handle, errno);
            return -1;
        }

        if (uv_tcp_open(handle, fd)) {
            close(fd);
            return -2;
        }
    }

    req->type = UV_CONNECT;
    ngx_queue_init(&req->queue);

    if (handle->connect_req) {
        uv_err_new(handle, EALREADY);
        return -1;
    }

    if (handle->type != UV_TCP) {
        uv_err_new(handle, ENOTSOCK);
        return -1;
    }

    handle->connect_req = req;

    int addrsize = sizeof(struct sockaddr_in);

    int r = connect(handle->fd, addr, addrsize);
    handle->delayed_error = 0;

    if (r != 0 && errno != EINPROGRESS) {
        switch (errno) {
        /* If we get a ECONNREFUSED wait until the next tick to report the
         * error. Solaris wants to report immediately--other unixes want to
         * wait.
         */
        case ECONNREFUSED:
            handle->delayed_error = errno;
            break;

        default:
            uv_err_new(handle, errno);
            return -1;
        }
    }

    assert(handle->write_watcher.data == handle);
    ev_io_start(EV_DEFAULT_ &handle->write_watcher);

    if (handle->delayed_error) {
        ev_feed_event(EV_DEFAULT_ &handle->write_watcher, EV_WRITE);
    }

    return 0;
}
Ejemplo n.º 17
0
/* Adds a client from an already open socket. The server's connection_cb
 * will be called on the client after it has been added.
 *
 * Parameters:
 * 		server: The UvTcpServer that the client should be added to.
 * 		sock: The socket of the client that will be added to the server.
 */
int UvTcpServer_add_client(UvTcpServer* server, int sock)
{
	uv_tcp_t* client_handle;
	uscb_package* package;
	int err;

	if(!server || sock < 0)return(ALIB_BAD_ARG);

	/* Allocate needed memory. */
	client_handle = malloc(sizeof(uv_tcp_t));
	if(!client_handle)return(ALIB_MEM_ERR);
	package = malloc(sizeof(uscb_package));
	if(!package)
	{
		err = ALIB_MEM_ERR;
		goto f_error;
	}

	/* Initialize the client handle. */
	if(uv_tcp_init(server->loop, client_handle) ||
			uv_tcp_open(client_handle, sock))
	{
		err = ALIB_FD_ERR;
		goto f_error;
	}

	/* Initialize the package. */
	package->server = server;
	package->data = server;
	package->free_data_proc = NULL;
	package->mem_block = NULL;

	/* Call the client's connection callback. */
	if(server->client_con_cb)
	{
		/* Call the client connection callback. */
		uscb_rval r_code = server->client_con_cb(server, client_handle, &package->data,
				&package->free_data_proc);

		/* Check to see if we need to close anything down. */
		if((r_code & USCB_CLIENT_CLOSE) || (r_code & USCB_SERVER_SHUTDOWN))
		{
			close_client((uv_handle_t*)client_handle);

			err = ALIB_OK;
			if(r_code & USCB_SERVER_SHUTDOWN)
				UvTcpServer_stop(server);

			goto f_error;
		}
	}

	/* Add the package */
	client_handle->data = package;

	/* Add the client to the client list. */
	ArrayList_add_tsafe(server->client_list, client_handle);

	/* Start listening for data sent by the client. */
	if(uv_read_start((uv_stream_t*)client_handle, client_buff_init, client_data_in_cb))
		ArrayList_remove_tsafe(server->client_list, client_handle);
	uv_run(client_handle->loop, UV_RUN_NOWAIT);

	return(ALIB_OK);
f_error:
	if(client_handle)free(client_handle);
	if(package)free(package);

	return(err);
}
Ejemplo n.º 18
0
static int uv__connect(uv_connect_t* req, uv_tcp_t* tcp, struct sockaddr* addr,
    socklen_t addrlen, uv_connect_cb cb) {
  int r;

  if (tcp->fd <= 0) {
    int fd = socket(addr->sa_family, SOCK_STREAM, 0);

    if (fd < 0) {
      uv_err_new((uv_handle_t*)tcp, errno);
      return -1;
    }

    if (uv_tcp_open(tcp, fd)) {
      close(fd);
      return -2;
    }
  }

  uv__req_init((uv_req_t*)req);
  req->cb = cb;
  req->handle = (uv_stream_t*)tcp;
  req->type = UV_CONNECT;
  ngx_queue_init(&req->queue);

  if (tcp->connect_req) {
    uv_err_new((uv_handle_t*)tcp, EALREADY);
    return -1;
  }

  if (tcp->type != UV_TCP) {
    uv_err_new((uv_handle_t*)tcp, ENOTSOCK);
    return -1;
  }

  tcp->connect_req = req;

  r = connect(tcp->fd, addr, addrlen);

  tcp->delayed_error = 0;

  if (r != 0 && errno != EINPROGRESS) {
    switch (errno) {
      /* If we get a ECONNREFUSED wait until the next tick to report the
       * error. Solaris wants to report immediately--other unixes want to
       * wait.
       */
      case ECONNREFUSED:
        tcp->delayed_error = errno;
        break;

      default:
        uv_err_new((uv_handle_t*)tcp, errno);
        return -1;
    }
  }

  assert(tcp->write_watcher.data == tcp);
  ev_io_start(EV_DEFAULT_ &tcp->write_watcher);

  if (tcp->delayed_error) {
    ev_feed_event(EV_DEFAULT_ &tcp->write_watcher, EV_WRITE);
  }

  return 0;
}
Ejemplo n.º 19
0
Archivo: tcp.cpp Proyecto: Xsoda/uvplus
int uvplus_tcp::open(uv_os_sock_t sock) {
  auto tcp = (uv_tcp_t *)context_ptr();
  return uv_tcp_open(tcp, sock);
}
Ejemplo n.º 20
0
Archivo: init.c Proyecto: aviks/julia
void *init_stdio_handle(uv_file fd,int readable)
{
    void *handle;
    uv_handle_type type = uv_guess_handle(fd);
    jl_uv_file_t *file;
#ifndef _OS_WINDOWS_
    // Duplicate the file descritor so we can later dup it over if we want to redirect
    // STDIO without having to worry about closing the associated libuv object.
    // On windows however, libuv objects remember streams by their HANDLE, so this is
    // unnessecary.
    fd = dup(fd);
#endif
    //printf("%d: %d -- %d\n", fd, type);
    switch(type)
    {
    case UV_TTY:
        handle = malloc(sizeof(uv_tty_t));
        if (uv_tty_init(jl_io_loop,(uv_tty_t*)handle,fd,readable)) {
            jl_errorf("Error initializing stdio in uv_tty_init (%d, %d)\n", fd, type);
            abort();
        }
        ((uv_tty_t*)handle)->data=0;
        uv_tty_set_mode((void*)handle,0); //cooked stdio
        break;
    case UV_FILE:
        file = malloc(sizeof(jl_uv_file_t));
        file->loop = jl_io_loop;
        file->type = UV_FILE;
        file->file = fd;
        file->data = 0;
        handle = file;
        break;
    case UV_NAMED_PIPE:
        handle = malloc(sizeof(uv_pipe_t));
        if (uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle, (readable?UV_PIPE_READABLE:UV_PIPE_WRITABLE))) {
            jl_errorf("Error initializing stdio in uv_pipe_init (%d, %d)\n", fd, type);
            abort();
        }
        if (uv_pipe_open((uv_pipe_t*)handle,fd)) {
            jl_errorf("Error initializing stdio in uv_pipe_open (%d, %d)\n", fd, type);
            abort();
        }
        ((uv_pipe_t*)handle)->data=0;
        break;
    case UV_TCP:
        handle = malloc(sizeof(uv_tcp_t));
        if (uv_tcp_init(jl_io_loop, (uv_tcp_t*)handle)) {
            jl_errorf("Error initializing stdio in uv_tcp_init (%d, %d)\n", fd, type);
            abort();
        }
        if (uv_tcp_open((uv_tcp_t*)handle,fd)) {
            jl_errorf("Error initializing stdio in uv_tcp_open (%d, %d)\n", fd, type);
            abort();
        }
        ((uv_tcp_t*)handle)->data=0;
        break;
    case UV_UDP:
    default:
        jl_errorf("This type of handle for stdio is not yet supported (%d, %d)!\n", fd, type);
        handle = NULL;
        break;
    }
    return handle;
}