示例#1
0
文件: util.c 项目: tarruda/luv
static int new_timer(lua_State* L) {
  uv_timer_t* handle = luv_create_timer(L);
  if (uv_timer_init(uv_default_loop(), handle)) {
    uv_err_t err = uv_last_error(uv_default_loop());
    return luaL_error(L, "new_timer: %s", uv_strerror(err));
  }
  return 1;
}
示例#2
0
void connect_cb(uv_connect_t* req, int status) {
  int r;
  if (!status) {
    state_t* state = (state_t*) req->handle->data;
    state->is_connected = 1;
    r = uv_read_start(req->handle, alloc_cb, read_cb);
    if (r) {
      uv_err_t err = uv_last_error(req->handle->loop);
      printf("error occurred. %s\n", uv_strerror(err));
    }
  } else {
    uv_err_t err = uv_last_error(req->handle->loop);
    printf("error occurred. %s\n", uv_strerror(err));
  }

  free(req);
}
示例#3
0
void HttpRequest::handleRequest() {
  int r = uv_read_start(handle(), &on_alloc, &HttpRequest_on_request_read);
  if (r) {
    uv_err_t err = uv_last_error(_pLoop);
    fatal_error("read_start", uv_strerror(err));
    return;
  }
}
示例#4
0
static void read_cb(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  /* The server will not send anything, it should close gracefully. */

  if (buf.base) {
    free(buf.base);
  }

  if (nread != -1) {
    ASSERT(nread == 0);
    ASSERT(uv_last_error().code == UV_EAGAIN);
  } else {
    ASSERT(tcp != NULL);
    ASSERT(nread == -1);
    ASSERT(uv_last_error().code == UV_EOF);
    uv_close((uv_handle_t*)tcp, close_cb);
  }
}
示例#5
0
文件: main.c 项目: AndrewTsao/uvbook
void on_read(uv_stream_t *client, ssize_t nread, uv_buf_t buf) {
    if (nread == -1) {
        if (uv_last_error(loop).code != UV_EOF)
            fprintf(stderr, "Read error %s\n", uv_err_name(uv_last_error(loop)));
        uv_close((uv_handle_t*) client, NULL);
        free(client);
        return;
    }

    char *data = (char*) malloc(sizeof(char) * (nread+1));
    data[nread] = '\0';
    strncpy(data, buf.base, nread);

    fprintf(stderr, "%s", data);
    free(data);
    free(buf.base);
}
示例#6
0
void echo_write(uv_write_t *req, int status) {
    if (status == -1) {
        fprintf(stderr, "Write error %s\n", uv_err_name(uv_last_error(loop)));
    }
    char *base = (char*) req->data;
    free(base);
    free(req);
}
示例#7
0
static void after_write(uv_write_t* req, int status) {
  if (status != 0) {
    fprintf(stderr, "write error %s\n", uv_err_name(uv_last_error(loop)));
    uv_close((uv_handle_t*)req->handle, close_cb);
    conns_failed++;
    return;
  }
}
示例#8
0
static void create_dir(uv_loop_t* loop, const char* name) {
  int r;
  uv_fs_t req;
  r = uv_fs_rmdir(loop, &req, name, NULL);
  r = uv_fs_mkdir(loop, &req, name, 0755, NULL);
  ASSERT(r == 0 || uv_last_error(loop).code == UV_EEXIST);
  uv_fs_req_cleanup(&req);
}
示例#9
0
文件: forza.c 项目: revington/forza
void spawn() {
  int i;
  char** env = NULL;

  env = env_copy(environ, env);

  child = malloc(sizeof(uv_process_t));
  uv_stdio_container_t stdio[4];
  uv_process_options_t options;

  uv_pipe_init(loop, &child_stdout, 0);
  uv_pipe_init(loop, &child_stderr, 0);
  uv_pipe_init(loop, &child_ipc, 0);

  //
  // Setup child's stdio. stdout and stderr are pipes so that we can read
  // child process' output.
  // FD 3 is a pipe used for IPC.
  //
  options.stdio_count = 4;
  stdio[0].flags = UV_INHERIT_FD;
  stdio[0].data.fd = 0;
  stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  stdio[1].data.stream = (uv_stream_t*) &child_stdout;
  stdio[2].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  stdio[2].data.stream = (uv_stream_t*) &child_stderr;
  stdio[3].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  stdio[3].data.stream = (uv_stream_t*) &child_ipc;

  options.env = env;
  options.cwd = NULL;
  options.file = arguments[0];
  options.args = arguments;
  options.flags = 0;
  options.stdio = stdio;
  options.exit_cb = on_process_exit;

  for (i = 0; i < PLUGIN_COUNT; i++) {
    if (plugins[i].process_options_cb) {
      plugins[i].process_options_cb(&options);
    }
  }

  if (uv_spawn(loop, child, options)) {
    fprintf(stderr, "uv_spawn: %s\n", uv_err_name(uv_last_error(loop)));
    return;
  }

  for (i = 0; i < PLUGIN_COUNT; i++) {
    if (plugins[i].process_spawned_cb) {
      plugins[i].process_spawned_cb(child, &options);
    }
  }

  uv_read_start(options.stdio[1].data.stream, forza__on_alloc, forza__on_stdout_read);
  uv_read_start(options.stdio[2].data.stream, forza__on_alloc, forza__on_stderr_read);
  uv_read_start(options.stdio[3].data.stream, forza__on_alloc, forza__on_ipc_read);
}
示例#10
0
/*
 * Class:     com_iwebpp_libuvpp_handles_LoopHandle
 * Method:    _get_last_error
 * Signature: (J)Lcom/iwebpp/libuvpp/NativeException;
 */
extern "C" JNIEXPORT  jthrowable JNICALL Java_com_iwebpp_libuvpp_handles_LoopHandle__1get_1last_1error
  (JNIEnv *env, jobject that, jlong ptr) {

  assert(ptr);
  uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(ptr);
  int code = uv_last_error(loop).code;

  return NewException(env, code);
}
示例#11
0
文件: main.c 项目: 343829084/uvbook
void on_write(uv_fs_t *req) {
    uv_fs_req_cleanup(req);
    if (req->result < 0) {
        fprintf(stderr, "Write error: %s\n", uv_strerror(uv_last_error(uv_default_loop())));
    }
    else {
        uv_fs_read(uv_default_loop(), &read_req, open_req.result, buffer, sizeof(buffer), -1, on_read);
    }
}
static void on_connect_with_close(uv_connect_t *req, int status) {
  ASSERT((uv_stream_t*) &tcp == req->handle);
  ASSERT(status == -1);
  ASSERT(uv_last_error(uv_default_loop()).code == UV_ECONNREFUSED);
  connect_cb_calls++;

  ASSERT(close_cb_calls == 0);
  uv_close((uv_handle_t*)req->handle, on_close);
}
示例#13
0
int luv_udp_recv_stop(lua_State* L) {
  uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, 1, "udp");
  if (uv_udp_recv_stop(handle)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "udp_recv_stop: %s", uv_strerror(err));
  }
  luv_handle_unref(L, handle->data);
  return 0;
}
static void on_connect_without_close(uv_connect_t *req, int status) {
  ASSERT(status == -1);
  ASSERT(uv_last_error(uv_default_loop()).code == UV_ECONNREFUSED);
  connect_cb_calls++;

  uv_timer_start(&timer, timer_cb, 100, 0);

  ASSERT(close_cb_calls == 0);
}
示例#15
0
文件: test-fs.c 项目: amuxtux/libuv
static void file_readdir_cb(uv_fs_t* req) {
  ASSERT(req == &readdir_req);
  ASSERT(req->fs_type == UV_FS_READDIR);
  ASSERT(req->result == -1);
  ASSERT(req->ptr == NULL);
  ASSERT(uv_last_error(req->loop).code == UV_ENOTDIR);
  uv_fs_req_cleanup(req);
  readdir_cb_count++;
}
示例#16
0
文件: http.c 项目: gphummer/urbit
/* _http_start(): start http server.
*/
static void
_http_start(u2_http* htp_u)
{
  struct sockaddr_in add_u;

  uv_tcp_init(u2L, &htp_u->wax_u);

  memset(&add_u, 0, sizeof(add_u));
  add_u.sin_family = AF_INET;
  add_u.sin_addr.s_addr = INADDR_ANY;

  /*  Try ascending ports.
  */
  while ( 1 ) {
    add_u.sin_port = htons(htp_u->por_w);

    if ( 0 != uv_tcp_bind(&htp_u->wax_u, add_u)  ) {
      uv_err_t las_u = uv_last_error(u2L);

      if ( UV_EADDRINUSE == las_u.code ) {
        htp_u->por_w++;
        continue;
      }
      else {
        uL(fprintf(uH, "http: bind: %s\n", uv_strerror(las_u)));
      }
    }
    if ( 0 != uv_listen((uv_stream_t*)&htp_u->wax_u, 16, _http_listen_cb) ) {
      uv_err_t las_u = uv_last_error(u2L);

      if ( UV_EADDRINUSE == las_u.code ) {
        htp_u->por_w++;
        continue;
      }
      else {
        uL(fprintf(uH, "http: listen: %s\n", uv_strerror(las_u)));
      }
    }
    uL(fprintf(uH, "http: live (%s) on %d\n",
                   (u2_yes == htp_u->sec) ? "\"secure\"" : "insecure",
                   htp_u->por_w));
    break;
  }
}
示例#17
0
static void connect_to_remote_cb(uv_connect_t* req, int status)
{
	server_ctx *ctx = (server_ctx *)req->data;
	if (status) {
		if (uv_last_error(req->handle->loop).code != UV_ECANCELED) {
			SHOW_UV_ERROR(ctx->client.loop);
			uv_close((uv_handle_t*)(void *)&ctx->remote, remote_established_close_cb);
			free(ctx->handshake_buffer);
			free(req);
		}
		return;
	}

	free(req);

	LOGCONN(&ctx->remote, "Connected to %s");

	uv_buf_t buf;
	buf.base = (char *)ctx->handshake_buffer;
	buf.len = HANDSHAKE_BUFFER_SIZE;

	if (!ctx->buffer_len) {
		free(ctx->handshake_buffer);
	} else {
		uv_write_t *wreq = (uv_write_t *)malloc(sizeof(uv_write_t));
		if (!wreq) {
			uv_close((uv_handle_t*)(void *)&ctx->client, client_established_close_cb);
			FATAL("malloc() failed!");
		}
		wreq->data = buf.base;
		buf.len = ctx->buffer_len;
		int n = uv_write(wreq, (uv_stream_t *)(void *)&ctx->remote, &buf, 1, after_write_cb);
		if (n) {
			LOGE("Write to remote failed!");
			free(wreq);
			uv_close((uv_handle_t*)(void *)&ctx->remote, remote_established_close_cb);
			return;
		}
	}

	ctx->handshake_buffer = NULL;
	ctx->buffer_len = 0;
	
	int n = uv_read_start((uv_stream_t *)(void *)&ctx->client, established_alloc_cb, client_established_read_cb);
	if (n) {
		SHOW_UV_ERROR(ctx->client.loop);
		uv_close((uv_handle_t*)(void *)&ctx->client, client_established_close_cb);
		return;
	}
	n = uv_read_start((uv_stream_t *)(void *)&ctx->remote, established_alloc_cb, remote_established_read_cb);
	if (n) {
		SHOW_UV_ERROR(ctx->client.loop);
		uv_close((uv_handle_t*)(void *)&ctx->remote, remote_established_close_cb);
		return;
	}
}
示例#18
0
文件: forza.c 项目: revington/forza
int main(int argc, char *argv[]) {
  char** hosts;
  char* hostname;
  char* user;
  char* name;
  int i, c = 0;
  uv_interface_address_t* addresses;
  uv_err_t err;

  srand(time(NULL));

  atexit(forza__kill);
  signal(SIGTERM, forza__on_sigterm);

  loop = uv_default_loop();

#ifdef FORZA_VERSION_HASH
  printf("forza "FORZA_VERSION_HASH"\n");
#else
  printf("forza\n");
#endif

  opt = saneopt_init(argc - 1, argv + 1);
  saneopt_alias(opt, "host", "h");
  hosts = saneopt_get_all(opt, "host");
  hostname = saneopt_get(opt, "hostname");
  user = saneopt_get(opt, "app-user");
  name = saneopt_get(opt, "app-name");
  arguments = saneopt_arguments(opt);

  if (hostname == NULL) {
    hostname = malloc(256 * sizeof(*hostname));
    err = uv_interface_addresses(&addresses, &c);
    if (err.code != UV_OK) {
      fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop)));
      return 1;
    }
    for (i = 0; i < c; i++) {
      /* For now, only grab the first non-internal, non 0.0.0.0 interface.
       * TODO: Make this smarter.
       */
      if (addresses[i].is_internal) continue;
      uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname));
      if (strcmp(hostname, "0.0.0.0") != 0) break;
    }
    uv_free_interface_addresses(addresses, c);
  }

  forza_connect(hosts, hostname, user, name, on_connect);

  uv_run(loop, UV_RUN_DEFAULT);

  free(hosts);

  return 0;
}
static void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  uv_err_t err = uv_last_error(uv_default_loop());

  if (nread > 0) {
    output_used += nread;
  } else if (nread < 0) {
    ASSERT(err.code == UV_EOF);
    uv_close((uv_handle_t*)tcp, close_cb);
  }
}
示例#20
0
int luv_execpath(lua_State* L) {
  size_t size = 2*PATH_MAX;
  char exec_path[size];
  if (uv_exepath(exec_path, &size)) {
    uv_err_t err = uv_last_error(uv_default_loop());
    return luaL_error(L, "tcp_bind6: %s", uv_strerror(err));
  }
  lua_pushlstring(L, exec_path, size);
  return 1;
}
示例#21
0
文件: main.c 项目: AndrewTsao/uvbook
void on_connect(uv_connect_t *req, int status) {
    if (status == -1) {
        fprintf(stderr, "connect failed error %s\n", uv_err_name(uv_last_error(loop)));
        free(req);
        return;
    }

    uv_read_start((uv_stream_t*) req->data, alloc_buffer, on_read);
    free(req);
}
示例#22
0
文件: net.c 项目: flybird119/uv-mongo
void
net_resolve_cb(uv_getaddrinfo_t *rv, int stat, net_ai * ai) {
  net_t * net = (net_t*) rv->data;
  err_t err;
  socketPair_t dest;
  char addr[INET6_ADDRSTRLEN];
  int ret;

  if (stat != 0) {
    err = uv_last_error(net->loop);
    if (net->error_cb) {
      net->error_cb(net, err, (char *) uv_strerror(err));
    } else {
      printf("error(%s:%d) %s", net->hostname, net->port, (char *) uv_strerror(err));
      net_free(net);
    }
    return;
  }

  uv_ip4_name((socketPair_t *) ai->ai_addr, addr, INET6_ADDRSTRLEN);
  dest = uv_ip4_addr(addr, net->port);

  /*
   * create tcp instance.
   */
  uv_tcp_init(net->loop, net->handle);
  ret = uv_tcp_connect(net->conn, net->handle, dest, net_connect_cb);
  if (ret != NET_OK) {
    err = uv_last_error(net->loop);
    if (net->error_cb) {
      net->error_cb(net, err, (char *) uv_strerror(err));
    } else {
      printf("error(%s:%d) %s", net->hostname, net->port, (char *) uv_strerror(err));
      net_free(net);
    }
    return;
  }

  /*
   * free
   */
  uv_freeaddrinfo(ai);
}
示例#23
0
static void connect_cb(uv_req_t* req, int status) {
  if (status) LOG(uv_strerror(uv_last_error()));
  ASSERT(status == 0);

  write_sockets++;
  req_free(req);

  maybe_connect_some();
  maybe_start_writing();
}
示例#24
0
int luv_execpath(lua_State* L) {
  size_t size = 2*PATH_MAX;
  char exec_path[2*PATH_MAX];
  if (uv_exepath(exec_path, &size)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "uv_exepath: %s", uv_strerror(err));
  }
  lua_pushlstring(L, exec_path, size);
  return 1;
}
示例#25
0
int luv_tcp_nodelay (lua_State* L) {
  uv_tcp_t* handle = (uv_tcp_t*)luv_checkudata(L, 1, "tcp");
  int enable = lua_toboolean(L, 2);

  if (uv_tcp_nodelay(handle, enable)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "tcp_nodelay: %s", uv_strerror(err));
  }
  return 0;
}
示例#26
0
static void _send_cb(uv_udp_send_t* req, int status) {
  assert(req->handle);
  assert(req->data);
  assert(req->handle->data);
  UDPCallbacks* cb = reinterpret_cast<UDPCallbacks*>(req->handle->data);
  ContextHolder* req_data = reinterpret_cast<ContextHolder*>(req->data);
  cb->on_send(status, status < 0 ? uv_last_error(req->handle->loop).code : 0, req_data->data(), req_data->context());
  delete req_data;
  delete req;
}
示例#27
0
VALUE ae_get_last_uv_error(void)
{
  AE_TRACE();
  VALUE ae_uv_error;

  if (NIL_P(ae_uv_error = rb_hash_aref(AE_UV_ERRORS, INT2FIX(uv_last_error(AE_uv_loop).code))))
    ae_uv_error = rb_hash_aref(AE_UV_ERRORS, INT2FIX(-1));

  return ae_uv_error;
}
示例#28
0
void Connection::on_read_ssl(uv_stream_t* client, ssize_t nread, uv_buf_t buf) {
#else
void Connection::on_read_ssl(uv_stream_t* client, ssize_t nread, const uv_buf_t* buf) {
#endif
  Connection* connection = static_cast<Connection*>(client->data);

  SslSession* ssl_session = connection->ssl_session_.get();
  assert(ssl_session != NULL);

  if (nread < 0) {
#if UV_VERSION_MAJOR == 0
    if (uv_last_error(connection->loop_).code != UV_EOF) {
#else
    if (nread != UV_EOF) {
#endif
      connection->notify_error("Read error '" +
                               std::string(UV_ERRSTR(nread, connection->loop_)) +
                               "'");
    } else {
      connection->defunct();
    }
    return;
  }

  ssl_session->incoming().commit(nread);

  if (ssl_session->is_handshake_done()) {
    char buf[SSL_READ_SIZE];
    int rc =  0;
    while ((rc = ssl_session->decrypt(buf, sizeof(buf))) > 0) {
      connection->consume(buf, rc);
    }
    if (rc <= 0 && ssl_session->has_error()) {
      connection->notify_error("Unable to decrypt data: " + ssl_session->error_message(),
                               CONNECTION_ERROR_SSL);
    }
  } else {
    connection->ssl_handshake();
  }
}

void Connection::on_timeout(Timer* timer) {
  Handler* handler = static_cast<Handler*>(timer->data());
  Connection* connection = handler->connection();
  LOG_INFO("Request timed out to host %s on connection(%p)",
           connection->host_->address_string().c_str(),
           static_cast<void*>(connection));
  // TODO (mpenick): We need to handle the case where we have too many
  // timeout requests and we run out of stream ids. The java-driver
  // uses a threshold to defunct the connection.
  handler->set_state(Handler::REQUEST_STATE_TIMEOUT);
  handler->on_timeout();

  connection->metrics_->request_timeouts.inc();
}
示例#29
0
/* _term_write_cb(): general write callback.
*/
static void
_term_write_cb(uv_write_t* wri_u, c3_i sas_i)
{
  _u2_write_t* ruq_u = (void *)wri_u;

  if ( 0 != sas_i ) {
    uL(fprintf(uH, "term: write: %s\n", uv_strerror(uv_last_error(u2L))));
  }
  free(ruq_u->buf_y);
  free(ruq_u);
}
示例#30
0
void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  uv_err_t err = uv_last_error();

  if (nread > 0) {
    output_used += nread;
  } else if (nread < 0) {
    if (err.code == UV_EOF) {
      uv_close((uv_handle_t*)tcp, close_cb);
    }
  }
}