static void luv_poll_cb(uv_poll_t* handle, int status, int events) { lua_State* L = luv_state(handle->loop); luv_handle_t* data = (luv_handle_t*)handle->data; const char* evtstr; if (status < 0) { fprintf(stderr, "%s: %s\n", uv_err_name(status), uv_strerror(status)); lua_pushstring(L, uv_err_name(status)); } else { lua_pushnil(L); } switch (events) { case UV_READABLE: evtstr = "r"; break; case UV_WRITABLE: evtstr = "w"; break; case UV_READABLE|UV_WRITABLE: evtstr = "rw"; break; case UV_DISCONNECT: evtstr = "d"; break; case UV_READABLE|UV_DISCONNECT: evtstr = "rd"; break; case UV_WRITABLE|UV_DISCONNECT: evtstr = "wd"; break; case UV_READABLE|UV_WRITABLE|UV_DISCONNECT: evtstr = "rwd"; break; default: evtstr = ""; break; } lua_pushstring(L, evtstr); luv_call_callback(L, data, LUV_POLL, 2); }
void client::onResolved(uv_getaddrinfo_t* resolver_, int status_, struct addrinfo* res_) { if (status_ == -1) { throw std::runtime_error(uv_err_name(status_)); } int r = 0; data_t* data = (data_t*)resolver_->data; //char addr[17] = {'\0'}; //uv_ip4_name((struct sockaddr_in*)res_->ai_addr, addr, 16); //@TODO add ip6 support //std::cout << "Resolved ip : " << addr << std::endl; uv_tcp_init(data->loop, &data->sock); data->connect_req.data = data; r = uv_tcp_connect( &(data->connect_req), &(data->sock), (const sockaddr*)res_->ai_addr, onConnect ); if (r) { throw std::runtime_error(uv_err_name(r)); } uv_freeaddrinfo(res_); }
// ## on connection static void on_connection(uv_stream_t* server, int status) { int r; req_res_t *rr; uv_stream_t* stream; DEBUG("on_connection\n"); if (status != 0) { fprintf(stderr, "Connect error %s\n", uv_err_name(status)); //return; } else { // Create a new stream. stream = malloc(sizeof(uv_tcp_t)); if (stream == NULL) { fprintf(stderr, "malloc error\n"); //return; } else { r = uv_tcp_init(loop, (uv_tcp_t*)stream); if (0 != r) { fprintf(stderr, "uv_tcp_init error %s\n", uv_err_name(r)); //return; } else { DEBUG("creating new req_res\n"); rr = req_res_init(stream); /* Implicit parameter check.*/ if (rr == NULL) { fprintf(stderr, "res_init error on stream\n"); //return; } else { // Associate the state with stream. stream->data = rr; // Accept the connection r = uv_accept(server, stream); if (0 != r) { fprintf(stderr, "uv_accept error %s\n", uv_err_name(r)); //return; } else { // Start reading on the stream and register the [on_tcp_read](#on-tcp-read) // handler. Libuv will call [request_alloc](#request-alloc) to allocate memory for // the request buffer. r = uv_read_start(stream, request_alloc, on_tcp_read); if (0 != r) { fprintf(stderr, "uv_read_start error %s\n", uv_err_name(r)); } } } } } } }
// ----------- // #main int main(int argc, char *argv[]) { struct sockaddr_in addr; int port, return_value; if (argc != 4) { fprintf(stderr, "usage: %s <ip> <port> <web-root>\n", argv[0]); return_value = -1; } else { // Create the libuv event loop. loop = uv_default_loop(); web_root = argv[3]; /* ToDo: Add check for argv ! */ web_root_len = strlen(web_root); port = (int)strtol(argv[2], NULL, 0); return_value = uv_ip4_addr(argv[1], port, &addr); if (0 != return_value) { fprintf(stderr, "Listen error %s\n", uv_err_name(return_value)); return_value = 2; } else { // Create a new TCP socket. return_value = uv_tcp_init(loop, &tcp_server); if (return_value) { fprintf(stderr, "Socket creation error %s\n", uv_err_name(return_value)); return_value = 3; } // Bind the socket to the IPv4 address and port. return_value = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0); if (0 != return_value) { fprintf(stderr, "Bind error %s\n", uv_err_name(return_value)); return_value = 4; } // Register the [on_connection](#on-connection) handler for connections on // the socket. return_value = uv_listen((uv_stream_t*)&tcp_server, SOMAXCONN, on_connection); if (0 != return_value) { fprintf(stderr, "Listen error %s\n", uv_err_name(return_value)); return_value = 5; } /* ToDo: Add check for argv ! */ DEBUG("Server listen on %s:%d serving %s\n", argv[1], port, web_root); // Start the event loop. uv_run(loop, UV_RUN_DEFAULT); return_value = 0; } } return return_value; }
void TCPServer::IncomingData(uv_stream_t * client, ssize_t bytes_read, const uv_buf_t * buffer) { if (bytes_read < 0) { LogError(LOG_NETWORKING, "Receiving data - failure, reason: %s", uv_err_name(bytes_read)); if (buffer->base) free(buffer->base); uv_close((uv_handle_t *) client, ClosedCallback); return; } if (bytes_read == 0) { // everything is ok, but nothing was read } else { NetworkManager::ReceiveData(*client, buffer->base, bytes_read); LogInfo(LOG_NETWORKING, "Receiving data - success {length: %d}", bytes_read); } free(buffer->base); }
static void tcp_make_connect(conn_rec* p) { struct sockaddr_in addr; tcp_conn_rec* tp; int r; tp = (tcp_conn_rec*) p; r = uv_tcp_init(loop, (uv_tcp_t*)&p->stream); ASSERT(r == 0); ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); r = uv_tcp_connect(&tp->conn_req, (uv_tcp_t*) &p->stream, (const struct sockaddr*) &addr, connect_cb); if (r) { fprintf(stderr, "uv_tcp_connect error %s\n", uv_err_name(r)); ASSERT(0); } #if DEBUG printf("make connect %d\n", p->i); #endif p->conn_req.data = p; p->write_req.data = p; p->stream.data = p; }
void parse_dns_cb(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res) { LOG_DEBUG("status: %d", status); void **ud_t = resolver->data; PARSE_CB cb_t = ud_t[0]; void *user_data_t = ud_t[1]; free(ud_t); free(resolver); unsigned ip = 0; unsigned short port = 0; if (status <0) { LOG_DEBUG("error stop strerror: %s, err_name: %s", uv_strerror(status), uv_err_name(status)); } else { ip = ((struct sockaddr_in*) res->ai_addr)->sin_addr.s_addr; port = ((struct sockaddr_in*) res->ai_addr)->sin_port; } LOG_DEBUG("ip: %u, port: %u", ip, port); uv_freeaddrinfo(res); cb_t(status, ip, ntohs(port), user_data_t); }
void UDPClient::on_read(uv_udp_t *req, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { UDPClient *serverPtr = (UDPClient *) req->data; if (nread < 0) { if (nread == -4095) { logger.log("UDPClient", "Socket closed.", LogLevel::warning); //std::cerr << "socket closed"; serverPtr->disconnect(true); } fprintf(stderr, "Read error %s\n", uv_err_name(nread)); uv_close((uv_handle_t *) req, NULL); return; } else if (nread == 0) { //nothing } else { char sender[17] = {0}; uv_ip4_name((const struct sockaddr_in *) addr, sender, 16); if (serverPtr->_onMessage != NULL) { serverPtr->_onMessage(std::string(buf->base, nread), std::string(sender)); } if (serverPtr->onMessageBin != NULL) { serverPtr->onMessageBin(buf->base, nread, std::string(sender)); } } free(buf->base); }
LUALIB_API int luaopen_luv (lua_State *L) { uv_loop_t* loop; int ret; loop = lua_newuserdata(L, sizeof(*loop)); ret = uv_loop_init(loop); if (ret < 0) { return luaL_error(L, "%s: %s\n", uv_err_name(ret), uv_strerror(ret)); } // Tell the state how to find the loop. lua_pushstring(L, "uv_loop"); lua_insert(L, -2); lua_rawset(L, LUA_REGISTRYINDEX); // Tell the loop how to find the state. loop->data = L; luv_req_init(L); luv_handle_init(L); luaL_newlib(L, luv_functions); luv_constants(L); lua_setfield(L, -2, "constants"); return 1; }
void httpreq_on_connect(uv_connect_t* con, int status) { if(status == -1) { printf("ERROR: httpreq_on_connect, cannot connect. @todo clean memory\n"); return; } HTTPRequest* req = static_cast<HTTPRequest*>(con->data); int result = uv_read_start((uv_stream_t*)&req->tcp_req, httpreq_alloc, httpreq_on_read); if(result == -1) { printf("ERROR: uv_read_start() error: %s\n", uv_err_name(uv_last_error(req->loop))); return; } if(!req->is_secure) { req->sendData(&req->out_buffer[0], req->out_buffer.size()); } else { req->ssl = SSL_new(req->ssl_ctx); req->ssl_buffer.addApplicationData(&req->out_buffer[0],req->out_buffer.size()); SSL_set_connect_state(req->ssl); SSL_do_handshake(req->ssl); req->ssl_buffer.init(req->ssl, ssl_write_to_socket, req, ssl_read_decrypted, req); req->ssl_buffer.update(); } }
static void Server_newTCPConnection( uv_stream_t *server, int status ) { if ( status < 0 ) { log_warn( "Connection error: %s", uv_err_name( status ) ); return; } dbg_info( "Connection received." ); ClientConnection *client = Client_new( ); if ( !client ) goto badClient; client->handle.tcpHandle = malloc( sizeof(*client->handle.tcpHandle) ); if ( !client->handle.tcpHandle ) goto badHandle; uv_tcp_init( server->loop, client->handle.tcpHandle ); client->handle.tcpHandle->data = client; if ( uv_accept( server, client->handle.stream ) == 0 ) { client->server = server->data; #if defined(CLIENTTIMEINFO) client->startTime = uv_now( server->loop ); #endif Client_handleConnection( client ); } else { Client_terminate( client ); } return; badHandle: Client_free( client ); badClient: return; }
// Sync readfile using libuv APIs as an API function. static duk_ret_t duv_loadfile(duk_context *ctx) { const char* path = duk_require_string(ctx, 0); uv_fs_t req; int fd = 0; uint64_t size; char* chunk; uv_buf_t buf; if (uv_fs_open(&loop, &req, path, O_RDONLY, 0644, NULL) < 0) goto fail; fd = req.result; if (uv_fs_fstat(&loop, &req, fd, NULL) < 0) goto fail; size = req.statbuf.st_size; chunk = duk_alloc(ctx, size); buf = uv_buf_init(chunk, size); if (uv_fs_read(&loop, &req, fd, &buf, 1, 0, NULL) < 0) goto fail; duk_push_lstring(ctx, chunk, size); duk_free(ctx, chunk); uv_fs_close(&loop, &req, fd, NULL); uv_fs_req_cleanup(&req); return 1; fail: if (fd) uv_fs_close(&loop, &req, fd, NULL); uv_fs_req_cleanup(&req); duk_error(ctx, DUK_ERR_ERROR, "%s: %s: %s", uv_err_name(req.result), uv_strerror(req.result), path); }
static int tcp4_echo_start(int port) { struct sockaddr_in addr; int r; ASSERT(0 == uv_ip4_addr("0.0.0.0", port, &addr)); server = (uv_handle_t*)&tcpServer; serverType = TCP; r = uv_tcp_init(loop, &tcpServer); if (r) { /* TODO: Error codes */ fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr, 0); if (r) { /* TODO: Error codes */ fprintf(stderr, "Bind error\n"); return 1; } r = uv_listen((uv_stream_t*)&tcpServer, SOMAXCONN, on_connection); if (r) { /* TODO: Error codes */ fprintf(stderr, "Listen error %s\n", uv_err_name(r)); return 1; } return 0; }
void recv_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { void **ud_t = stream->data; LOG_DEBUG("nread: %d %s, EOF_VALUE: %d", (int)nread, buf->base, UV_EOF); if (nread <0) { LOG_DEBUG("error stop strerror: %s, err_name: %s", uv_strerror(nread), uv_err_name(nread)); uv_read_stop(stream); RECV_CB cb_t = ud_t[0]; void *user_data_t = ud_t[1]; char * buff = ud_t[2]; int len = (int)ud_t[3]; int recved = (int)ud_t[4]; free(ud_t); stream->data = 0; cb_t(stream, nread, buff, recved, user_data_t); return; } int tmp = (int)ud_t[4]; tmp += nread; ud_t[4] = (void*)tmp; }
static void connect_cb(uv_connect_t* req, int status) { conn_rec* conn; uv_buf_t buf; int r; if (status != 0) { #if DEBUG fprintf(stderr, "connect error %s\n", uv_err_name(uv_last_error(uv_default_loop()))); #endif uv_close((uv_handle_t*)req->handle, close_cb); conns_failed++; return; } ASSERT(req != NULL); ASSERT(status == 0); conn = (conn_rec*)req->data; ASSERT(conn != NULL); #if DEBUG printf("connect_cb %d\n", conn->i); #endif r = uv_read_start(&conn->stream, alloc_cb, read_cb); ASSERT(r == 0); buf.base = buffer; buf.len = sizeof(buffer) - 1; r = uv_write(&conn->write_req, &conn->stream, &buf, 1, after_write); ASSERT(r == 0); }
int main(int argc, char *argv[]) { // Open database char c; //for managing getopt while ((c = getopt(argc, argv, OPTIONS)) != -1){ switch (c) { case 'c': break; } } leveldb::Options options; options.create_if_missing = true; leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db); loop = uv_default_loop(); uv_tcp_t server; uv_tcp_init(loop, &server); struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 8085); uv_tcp_bind(&server, bind_addr); int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection); if (r) { fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop))); return 1; } return uv_run(loop, UV_RUN_DEFAULT); }
void TCPClient::send_inl(uv_write_t* req /*= NULL*/) { write_param* writep = (write_param*)req; if (writep) { if (writeparam_list_.size() > MAXLISTSIZE) { FreeWriteParam(writep); } else { writeparam_list_.push_back(writep); } } while (true) { uv_mutex_lock(&mutex_writebuf_); if (write_circularbuf_.empty()) { uv_mutex_unlock(&mutex_writebuf_); break; } if (writeparam_list_.empty()) { writep = AllocWriteParam(); writep->write_req_.data = this; } else { writep = writeparam_list_.front(); writeparam_list_.pop_front(); } writep->buf_.len = write_circularbuf_.read(writep->buf_.base, writep->buf_truelen_); uv_mutex_unlock(&mutex_writebuf_); int iret = uv_write((uv_write_t*)&writep->write_req_, (uv_stream_t*)&client_handle_->tcphandle, &writep->buf_, 1, AfterSend); if (iret) { writeparam_list_.push_back(writep);//failure not call AfterSend. so recycle req LOG(ERROR)<<"client(" << this << ") send error:" << GetUVError(iret); fprintf(stdout, "send error. %s-%s\n", uv_err_name(iret), uv_strerror(iret)); } } }
bool HTTPRequest::send() { // CREATE HTTP REQUEST std::string request = getAsString(); std::copy(request.begin(), request.end(), std::back_inserter(out_buffer)); // HINTS FOR SOCKET struct addrinfo hints; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = 0; // SECURE CONNECTION? std::string port = "80"; if(is_secure) { port = "443"; } int r = uv_getaddrinfo(loop, &resolver_req, httpreq_on_resolved, host.c_str(), port.c_str(), &hints); if(r) { printf("ERROR: cannot resolve addres: %s\n", uv_err_name(uv_last_error(loop))); return false; } return true; }
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { if (nread < 0) { if (nread != UV_EOF) fprintf(stderr, "Read error %s\n", uv_err_name(nread)); uv_close((uv_handle_t*) client, NULL); } else if (nread > 0) {1 uv_write_t *req = (uv_write_t *) malloc(sizeof(uv_write_t)); uv_buf_t wrbuf = uv_buf_init(buf->base, nread); // buf->base point the string that the client had sent. // such as the string "mail Alice helloworld" // seperate the mail to :0x02A5AliceAAhelloworld" msgpack_sbuffer sbuf; msgpack_sbuffer_init(&sbuf); // prepare(&sbuf, buf->base); unpack(sbuf.data, sbuf.size); // protocol process with msgpack lib_msgpack_process(buf->base); uv_write(req, client, &wrbuf, 1, echo_write); } if (buf->base) free(buf->base); }
void duv_push_status(duk_context *ctx, int status) { if (status < 0) { duk_push_error_object(ctx, DUK_ERR_ERROR, "%s: %s", uv_err_name(status), uv_strerror(status)); } else { duk_push_null(ctx); } }
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; } }
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); }
int luv_set_process_title(lua_State* L) { const char* title = luaL_checkstring(L, 1); uv_err_t err = uv_set_process_title(title); if (err.code) { return luaL_error(L, "uv_set_process_title: %s: %s", uv_err_name(err), uv_strerror(err)); } return 0; }
JL_DLLEXPORT void jl_uv_writecb(uv_write_t *req, int status) { free(req); if (status < 0) { jl_safe_printf("jl_uv_writecb() ERROR: %s %s\n", uv_strerror(status), uv_err_name(status)); } }
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); }
void stdin_fs_cb(uv_fs_t* req) { if (stdin_watcher.result > 0) { buffer[stdin_watcher.result] = '\0'; fprintf(stderr, "Typed %s\n", buffer); wait_for_next_input(); } else { fprintf(stderr, "error opening file: [%s: %s]\n", uv_err_name((req->result)), uv_strerror((req->result))); } }
int luv_get_process_title(lua_State* L) { char title[8192]; uv_err_t err = uv_get_process_title(title, 8192); if (err.code) { return luaL_error(L, "uv_get_process_title: %s: %s", uv_err_name(err), uv_strerror(err)); } lua_pushstring(L, title); return 1; }
void client::onWrite(uv_write_t* write_req_, int status_) { free(write_req_); if (status_) { throw std::runtime_error(uv_err_name(status_)); } }
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; }
void mn_error(duk_context *ctx, int status) { duk_error( ctx, DUK_ERR_ERROR, "%s: %s", uv_err_name(status), uv_strerror(status) ); }