Esempio n. 1
0
File: test-ipc.c Progetto: clibs/uv
int ipc_helper(int listen_after_write) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted.
   */
  struct sockaddr_in addr;
  uv_write_t write_req;
  int r;
  uv_buf_t buf;
  uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);

  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, stdin_handle);

  ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
  ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
  ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

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

  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  ASSERT(r == 0);

  if (!listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection);
    ASSERT(r == 0);
  }

  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
      (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);

  if (listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection);
    ASSERT(r == 0);
  }

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  ASSERT(connection_accepted == 1);
  ASSERT(close_cb_called == 3);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Esempio n. 2
0
static int ipc_helper(int listen_after_write) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted. In this initial version only
   * data is transfered over the channel. XXX edit this comment after handle
   * transfer is added.
   */
  
  uv_write_t write_req;
  int r;
  uv_buf_t buf;

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, 0);

  ASSERT(uv_is_readable(&channel));
  ASSERT(uv_is_writable(&channel));

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

  r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  ASSERT(r == 0);

  if (!listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
      (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);

  if (listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  r = uv_run(uv_default_loop());
  ASSERT(r == 0);

  ASSERT(connection_accepted == 1);
  ASSERT(close_cb_called == 3);

  return 0;
}
Esempio n. 3
0
int ipc_helper(int listen_after_write) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted.
   */

  uv_write_t write_req;
  int r;
  uv_buf_t buf;

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, 0);

  ASSERT(uv_is_readable((uv_stream_t*) &channel));
  ASSERT(uv_is_writable((uv_stream_t*) &channel));
  ASSERT(!uv_is_closing((uv_handle_t*) &channel));

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

  r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  ASSERT(r == 0);

  if (!listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
      (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);

  if (listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  r = uv_run(uv_default_loop());
  ASSERT(r == 0);

  ASSERT(connection_accepted == 1);
  ASSERT(close_cb_called == 3);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Esempio n. 4
0
File: main.cpp Progetto: No9/luvdbie
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);
}
Esempio n. 5
0
static int dns_start(int port) {
  struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port);
  int r;

  r = uv_tcp_init(&server);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Socket creation error\n");
    return 1;
  }

  r = uv_tcp_bind(&server, addr);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Bind error\n");
    return 1;
  }

  r = uv_listen((uv_network_stream_t*)&server, 128, on_connection);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Listen error\n");
    return 1;
  }

  return 0;
}
Esempio n. 6
0
/* Set up an IPC pipe server that hands out listen sockets to the worker
 * threads. It's kind of cumbersome for such a simple operation, maybe we
 * should revive uv_import() and uv_export().
 */
void start_connection_dispatching(uv_handle_type type, unsigned int num_servers, struct server_ctx* servers, char* listen_address, int listen_port)
{
    int rc;
    struct ipc_server_ctx ctx;
    uv_loop_t* loop;
    unsigned int i;

    loop = uv_default_loop();
    ctx.num_connects = num_servers;

    if (type == UV_TCP)
    {
        uv_ip4_addr(listen_address, listen_port, &listen_addr);

        rc = uv_tcp_init(loop, (uv_tcp_t*) &ctx.server_handle);
        rc = uv_tcp_bind((uv_tcp_t*) &ctx.server_handle, (const struct sockaddr*)&listen_addr, 0);
        printf("Listening on %s:%d\n", listen_address, listen_port);
    }

    rc = uv_pipe_init(loop, &ctx.ipc_pipe, 1);
    rc = uv_pipe_bind(&ctx.ipc_pipe, "HAYWIRE_CONNECTION_DISPATCH_PIPE_NAME");
    rc = uv_listen((uv_stream_t*) &ctx.ipc_pipe, 128, ipc_connection_cb);

    for (i = 0; i < num_servers; i++)
        uv_sem_post(&servers[i].semaphore);

    rc = uv_run(loop, UV_RUN_DEFAULT);
    uv_close((uv_handle_t*) &ctx.server_handle, NULL);
    rc = uv_run(loop, UV_RUN_DEFAULT);

    for (i = 0; i < num_servers; i++)
        uv_sem_wait(&servers[i].semaphore);
}
Esempio n. 7
0
uv_stream_t* createTcpServer(uv_loop_t* pLoop, const std::string& host,
  int port, WebApplication* pWebApplication) {

  // We own pWebApplication. It will be destroyed by the socket but if in
  // the future we have failure cases that stop execution before we get
  // that far, we MUST delete pWebApplication ourselves.

  // Deletes itself when destroy() is called, which occurs in freeServer()
  Socket* pSocket = new Socket();
  // TODO: Handle error
  uv_tcp_init(pLoop, &pSocket->handle.tcp);
  pSocket->handle.isTcp = true;
  pSocket->handle.stream.data = pSocket;
  pSocket->pWebApplication = pWebApplication;

  struct sockaddr_in address = uv_ip4_addr(host.c_str(), port);
  int r = uv_tcp_bind(&pSocket->handle.tcp, address);
  if (r) {
    pSocket->destroy();
    return NULL;
  }
  r = uv_listen((uv_stream_t*)&pSocket->handle.stream, 128, &on_request);
  if (r) {
    pSocket->destroy();
    return NULL;
  }

  return &pSocket->handle.stream;
}
Esempio n. 8
0
void TCPSocket::listen(int backlog) 
{
    TraceLS(this) << "Listening" << endl;
    init();
    int r = uv_listen(ptr<uv_stream_t>(), backlog, internal::onAcceptConnection);
    if (r) setAndThrowError("TCP listen failed", r);
}
Esempio n. 9
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);
    }
}
Esempio n. 10
0
File: server.c Progetto: torque/reki
int Server_listen( Server *server ) {
	struct sockaddr_storage address;
	checkFunction( Server_AddressInfo( server, &address ) );
	server->ipFamily = address.ss_family;

	unsigned int flags = 0;
	if ( address.ss_family == AF_INET6 )
		flags |= IPV6_V6ONLY;

	switch ( server->protocol ) {
		case ServerProtocol_TCP: {
			checkFunction( uv_tcp_bind( server->handle.tcpHandle, (struct sockaddr*)&address, flags ) );
			break;
		}
		case ServerProtocol_UDP: {
			checkFunction( uv_udp_bind( server->handle.udpHandle, (struct sockaddr*)&address, flags ) );
			log_err( "UDP actually isn't supported yet." );
			return 1;
			// break;
		}
		default: {
			log_err( "An unknown server protocol happened." );
			return 1;
		}
	}

	// e = uv_udp_recv_start( server->handle.udpHandle, allocCB, readCB );
	checkFunction( uv_listen( server->handle.stream, 128, Server_newTCPConnection ) );

	char namebuf[INET6_ADDRSTRLEN];
	checkFunction( getnameinfo( (struct sockaddr *)&address, sizeof(address), namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST ) );
	dbg_info( "Listening on [%s]:%d.", namebuf, ntohs(((struct sockaddr_in*)&address)->sin_port) );

	return 0;
}
Esempio n. 11
0
File: uvc.c Progetto: whtc123/libuvc
int uvc_listen(uvc_io *io,int backlog){
	io->handle->data=io;
	uv_listen((uv_stream_t *)io->handle,backlog,uvc_connection_cb);
	io->cur =uvc_self();
	uvc_yield( );
	return io->return_status;
}
Esempio n. 12
0
int main() {

    uv_loop_t *loop = uv_default_loop();

    resbuf.base = RESPONSE;
    resbuf.len = sizeof(RESPONSE);

    settings.on_headers_complete = on_headers_complete;

    uv_tcp_init(loop, &server);
    struct sockaddr_in addr;

    int r;
    r = uv_ip4_addr("0.0.0.0", 8001, &addr);

    if (r < 0)
        fprintf(stderr, "ip4_addr error %d\n", r);

    r = uv_tcp_bind(&server, (const struct sockaddr *) &addr, 0);

    if (r < 0) {
        fprintf(stderr, "bind: %s\n", uv_strerror(r));
        // return -1;
    }

    r = uv_listen((uv_stream_t *) &server, 128, on_connected);

    if (r < 0) {
        fprintf(stderr, "listen: %s\n", uv_strerror(r));
        return -1;
    }

    return uv_run(loop, UV_RUN_DEFAULT);
}
Esempio n. 13
0
/**
 * Executed when calling from shell.
 */
int main(int argc, const char** argv) {
    /* transforms an ip4 addr to a libuv usable struct */
    struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", 3000);
    
    loop = uv_default_loop();

    /* set the http response to the buffer */
    resp_buf.base = RESPONSE;
    resp_buf.len = sizeof(RESPONSE);

    /* assign our parser handlers */
    settings.on_url = http_url_cb;
    settings.on_body = http_body_cb;
    settings.on_header_field = http_header_field_cb;
    settings.on_header_value = http_header_value_cb;
    settings.on_headers_complete = http_headers_complete_cb;
    settings.on_message_begin = http_message_begin_cb;
    settings.on_message_complete = http_message_complete_cb;

    /* create tcp server and bind it to the address */
    uv_tcp_init(loop, &server);
    uv_tcp_bind(&server, addr);

    /* let the tcp server handle incoming connections */
    int r = uv_listen((uv_stream_t*) &server, 128, tcp_new_connection_cb);

    if (r) {
        fprintf(stderr, "Error on tcp listen: %s.\n", 
                uv_strerror(uv_last_error(loop)));
    }

    /* start the event loop */
    return uv_run(loop, UV_RUN_DEFAULT);
}
Esempio n. 14
0
void
ircd_start(ircd_t *ircd) {
    struct addrinfo hints;
    struct addrinfo *result;

    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_PASSIVE;
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    // rest 0

    int status = getaddrinfo(NULL, "6999", &hints, &result);
    if (status) {
        printf("getaddrinfo: ");
        puts(gai_strerror(status));
        return;
    }

    if (uv_tcp_bind(&ircd->handle, result->ai_addr, 0) < 0) {
        perror("bind");
        freeaddrinfo(result);
        return;
    }

    if (uv_listen((uv_stream_t*)&ircd->handle, IRCD_BACKLOG, do_accept) > 0) {
        perror("listen");
        return;
    }

    printf("ircd listening on %s\n", sprint_addrport(result->ai_addr));

    freeaddrinfo(result);
}
Esempio n. 15
0
static int run_worker(uv_loop_t *loop, struct engine *engine)
{
	/* Control sockets or TTY */
	auto_free char *sock_file = NULL;
	uv_pipe_t pipe;
	uv_pipe_init(loop, &pipe, 0);
	pipe.data = engine;
	if (g_interactive) {
		printf("[system] interactive mode\n> ");
		fflush(stdout);
		uv_pipe_open(&pipe, 0);
		uv_read_start((uv_stream_t*) &pipe, tty_alloc, tty_read);
	} else {
		(void) mkdir("tty", S_IRWXU|S_IRWXG);
		sock_file = afmt("tty/%ld", getpid());
		if (sock_file) {
			uv_pipe_bind(&pipe, sock_file);
			uv_listen((uv_stream_t *) &pipe, 16, tty_accept);
		}
	}
	/* Run event loop */
	uv_run(loop, UV_RUN_DEFAULT);
	if (sock_file) {
		unlink(sock_file);
	}
	return kr_ok();
}
Esempio n. 16
0
int main(int argc, char **argv) {
  uv_tcp_t handle;
  struct sockaddr_in addr;
  int r;
  
  uv_loop_init(&loop);
  uv_tcp_init(&loop, &handle);
  
  r = uv_ip4_addr("0.0.0.0", 30100, &addr);
  uv_tcp_bind(&handle, (struct sockaddr *)&addr, 0);
  if (r) {
    fprintf(stderr, "Listen: bind failed\n");
    return -1;
  }

  // handle.data = this;
  r = uv_listen((uv_stream_t *)&handle, 8192,
                on_new_connection);
  if (r) {
    fprintf(stderr, "InitTCPServer uv_listen failed\n");
    return -1;
  }

  return uv_run(&loop, UV_RUN_DEFAULT);
}
Esempio n. 17
0
void connection_consumer_start(void *arg)
{
    int rc;
    struct server_ctx *ctx;
    uv_loop_t* loop;
    
    ctx = arg;
    loop = uv_loop_new();
    listener_event_loops[ctx->index] = *loop;
    
    http_request_cache_configure_listener(loop, &listener_async_handles[ctx->index]);
    uv_barrier_wait(listeners_created_barrier);
    
    rc = uv_async_init(loop, &ctx->async_handle, connection_consumer_close);
    uv_unref((uv_handle_t*) &ctx->async_handle);
    
    /* Wait until the main thread is ready. */
    uv_sem_wait(&ctx->semaphore);
    get_listen_handle(loop, (uv_stream_t*) &ctx->server_handle);
    uv_sem_post(&ctx->semaphore);
    
    rc = uv_listen((uv_stream_t*)&ctx->server_handle, 128, connection_consumer_new_connection);
    rc = uv_run(loop, UV_RUN_DEFAULT);
    
    uv_loop_delete(loop);
}
Esempio n. 18
0
int run_ipc_send_recv_helper(uv_loop_t* loop, int inprocess) {
  int r;

  is_in_process = inprocess;

  memset(&ctx2, 0, sizeof(ctx2));

  r = uv_pipe_init(loop, &ctx2.listen, 0);
  ASSERT(r == 0);

  r = uv_pipe_init(loop, &ctx2.channel, 1);
  ASSERT(r == 0);

  if (inprocess) {
    r = uv_pipe_bind(&ctx2.listen, TEST_PIPENAME_3);
    ASSERT(r == 0);

    r = uv_listen((uv_stream_t*)&ctx2.listen, SOMAXCONN, listen_cb);
    ASSERT(r == 0);
  } else {
    r = uv_pipe_open(&ctx2.channel, 0);
    ASSERT(r == 0);

    send_recv_start();
  }

  r = uv_run(loop, UV_RUN_DEFAULT);
  ASSERT(r == 0);

  return 0;
}
Esempio n. 19
0
int main(int argc, char const *argv[])
{
	LOGI(WELCOME_MESSAGE);
	make_tables((unsigned char *)PASSWORD, encrypt_table, decrypt_table);
	LOGI("Encrypt and decrypt table generated");
	
	int n;
	uv_loop_t *loop = uv_default_loop();
	uv_tcp_t listener;

	struct sockaddr_in addr = uv_ip4_addr(SERVER_LISTEN, SERVER_PORT);

	n = uv_tcp_init(loop, &listener);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(loop);

	n = uv_tcp_bind(&listener, addr);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(loop);

	n = uv_listen((uv_stream_t*)(void *)&listener, 5, connect_cb);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(loop);
	LOGI("Listening on " SERVER_LISTEN ":" TOSTR(SERVER_PORT));

	return uv_run(loop);
}
Esempio n. 20
0
static int _tcp_bind(uv_tcp_t *handle, struct sockaddr *addr, uv_connection_cb connection)
{
	unsigned flags = 0;
	if (addr->sa_family == AF_INET6) {
		flags |= UV_TCP_IPV6ONLY;
	}

	int ret = uv_tcp_bind(handle, addr, flags);
	if (ret != 0) {
		return ret;
	}

	/* TCP_DEFER_ACCEPT delays accepting connections until there is readable data. */
#ifdef TCP_DEFER_ACCEPT
	if (set_tcp_option((uv_handle_t *)handle, TCP_DEFER_ACCEPT, KR_CONN_RTT_MAX/1000) != 0) {
		kr_log_info("[ io ] tcp_bind (defer_accept): %s\n", strerror(errno));
	}
#endif

	ret = uv_listen((uv_stream_t *)handle, 16, connection);
	if (ret != 0) {
		return ret;
	}

	return tcp_bind_finalize((uv_handle_t *)handle);
}
Esempio n. 21
0
int main() {
  int r = 0;
  uv_loop_t *loop = uv_default_loop();

  /* 1. Initialize TCP server */
  r = uv_tcp_init(loop, &tcp_server);
  CHECK(r, "uv_tcp_init");

  /* 2. Bind to localhost:7000 */
  struct sockaddr_in addr;
  r = uv_ip4_addr(HOST, PORT, &addr);
  CHECK(r, "uv_ip4_addr");

  r = uv_tcp_bind(&tcp_server, (struct sockaddr*) &addr, AF_INET);
  CHECK(r, "uv_tcp_bind");

  /* 3. Start listening */
  /* uv_tcp_t inherits uv_stream_t so casting is ok */
  r = uv_listen((uv_stream_t*) &tcp_server, SOMAXCONN, onconnection);
  CHECK(r, "uv_listen");
  log_info("Listening on %s:%d", HOST, PORT);

  uv_run(loop, UV_RUN_DEFAULT);

  MAKE_VALGRIND_HAPPY();

  return 0;
}
Esempio n. 22
0
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;
}
Esempio n. 23
0
static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
    uv_handle_type pending) {
  int r;
  uv_buf_t outbuf;
  uv_err_t err;

  if (nread == 0) {
    /* Everything OK, but nothing read. */
    free(buf.base);
    return;
  }

  if (nread < 0) {
    err = uv_last_error(pipe->loop);
    if (err.code == UV_EOF) {
      free(buf.base);
      return;
    }

    printf("error recving on channel: %s\n", uv_strerror(err));
    abort();
  }

  fprintf(stderr, "got %d bytes\n", (int)nread);

  if (!tcp_server_listening) {
    ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE);
    read2_cb_called++;

    /* Accept the pending TCP server, and start listening on it. */
    ASSERT(pending == UV_TCP);
    r = uv_tcp_init(uv_default_loop(), &tcp_server);
    ASSERT(r == 0);

    r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
    ASSERT(r == 0);

    r = uv_listen((uv_stream_t*)&tcp_server, 12, on_connection);
    ASSERT(r == 0);

    tcp_server_listening = 1;

    /* Make sure that the expected data is correctly multiplexed. */
    ASSERT(memcmp("hello\n", buf.base, nread) == 0);

    outbuf = uv_buf_init("world\n", 6);
    r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL);
    ASSERT(r == 0);

    /* Create a bunch of connections to get both servers to accept. */
    make_many_connections();
  } else if (memcmp("accepted_connection\n", buf.base, nread) == 0) {
    /* Remote server has accepted a connection.  Close the channel. */
    ASSERT(pending == UV_UNKNOWN_HANDLE);
    remote_conn_accepted = 1;
    uv_close((uv_handle_t*)&channel, NULL);
  }

  free(buf.base);
}
Esempio n. 24
0
static int pipe_echo_start(char* pipeName) {
  int r;

#ifndef _WIN32
  {
    uv_fs_t req;
    uv_fs_unlink(uv_default_loop(), &req, pipeName, NULL);
    uv_fs_req_cleanup(&req);
  }
#endif

  server = (uv_handle_t*)&pipeServer;
  serverType = PIPE;

  r = uv_pipe_init(loop, &pipeServer, 0);
  if (r) {
    fprintf(stderr, "uv_pipe_init: %s\n", uv_strerror(r));
    return 1;
  }

  r = uv_pipe_bind(&pipeServer, pipeName);
  if (r) {
    fprintf(stderr, "uv_pipe_bind: %s\n", uv_strerror(r));
    return 1;
  }

  r = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, on_connection);
  if (r) {
    fprintf(stderr, "uv_pipe_listen: %s\n", uv_strerror(r));
    return 1;
  }

  return 0;
}
Esempio n. 25
0
int main(int argc, char **argv)
{
    uv_loop_t *loop = uv_default_loop();
    uv_tcp_t listener;
    struct sockaddr_in sockaddr;
    h2o_handler_t ws_handler;
    int r;

    if ((r = uv_tcp_init(loop, &listener)) != 0) {
        fprintf(stderr, "uv_tcp_init:%s\n", uv_strerror(r));
        goto Error;
    }
    uv_ip4_addr("127.0.0.1", 7890, &sockaddr);
    if ((r = uv_tcp_bind(&listener, (struct sockaddr*)&sockaddr, sizeof(sockaddr))) != 0) {
        fprintf(stderr, "uv_tcp_bind:%s\n", uv_strerror(r));
        goto Error;
    }
    if ((r = uv_listen((uv_stream_t*)&listener, 128, on_connect)) != 0) {
        fprintf(stderr, "uv_listen:%s\n", uv_strerror(r));
        goto Error;
    }

    h2o_config_init(&config);
    memset(&ws_handler, 0, sizeof(ws_handler));
    ws_handler.on_req = on_req;
    h2o_linklist_insert(&config.default_host.handlers, &ws_handler._link);
    h2o_context_init(&ctx, loop, &config);
    //ssl_ctx = h2o_ssl_new_server_context("server.crt", "server.key", NULL);

    return uv_run(loop, UV_RUN_DEFAULT);

Error:
    return 1;
}
Esempio n. 26
0
uv_stream_t* createPipeServer(uv_loop_t* pLoop, const std::string& name,
  int mask, WebApplication* pWebApplication) {

  // We own pWebApplication. It will be destroyed by the socket but if in
  // the future we have failure cases that stop execution before we get
  // that far, we MUST delete pWebApplication ourselves.

  // Deletes itself when destroy() is called, which occurs in freeServer()
  Socket* pSocket = new Socket();
  // TODO: Handle error
  uv_pipe_init(pLoop, &pSocket->handle.pipe, true);
  pSocket->handle.isTcp = false;
  pSocket->handle.stream.data = pSocket;
  pSocket->pWebApplication = pWebApplication;

  mode_t oldMask = 0;
  if (mask >= 0)
    oldMask = umask(mask);
  int r = uv_pipe_bind(&pSocket->handle.pipe, name.c_str());
  if (mask >= 0)
    umask(oldMask);

  if (r) {
    pSocket->destroy();
    return NULL;
  }
  r = uv_listen((uv_stream_t*)&pSocket->handle.stream, 128, &on_request);
  if (r) {
    pSocket->destroy();
    return NULL;
  }

  return &pSocket->handle.stream;
}
Esempio n. 27
0
static int tcp6_echo_start(int port) {
  struct sockaddr_in6 addr6;
  int r;

  ASSERT(0 == uv_ip6_addr("::1", port, &addr6));

  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;
  }

  /* IPv6 is optional as not all platforms support it */
  r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr6, 0);
  if (r) {
    /* show message but return OK */
    fprintf(stderr, "IPv6 not supported\n");
    return 0;
  }

  r = uv_listen((uv_stream_t*)&tcpServer, SOMAXCONN, on_connection);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Listen error\n");
    return 1;
  }

  return 0;
}
Esempio n. 28
0
static int echo_start(int port) {
  struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port);
  int r;

  r = uv_tcp_init(&server, on_server_close, NULL);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Socket creation error\n");
    return 1;
  }

  r = uv_bind(&server, (struct sockaddr*) &addr);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Bind error\n");
    return 1;
  }

  r = uv_listen(&server, 128, on_accept);
  if (r) {
    /* TODO: Error codes */
    fprintf(stderr, "Listen error\n");
    return 1;
  }

  return 0;
}
Esempio n. 29
0
static void socket_bind(MVMThreadContext *tc, MVMOSHandle *h, MVMString *host, MVMint64 port, MVMint32 backlog) {
    MVMIOSyncSocketData *data = (MVMIOSyncSocketData *)h->body.data;
    if (!data->ss.handle) {
        struct sockaddr *dest    = MVM_io_resolve_host_name(tc, host, port);
        uv_tcp_t        *socket  = MVM_malloc(sizeof(uv_tcp_t));
        int r;

        if ((r = uv_tcp_init(tc->loop, socket)) != 0 ||
                (r = uv_tcp_bind(socket, dest, 0)) != 0) {
            MVM_free(socket);
            MVM_free(dest);
            MVM_exception_throw_adhoc(tc, "Failed to bind: %s", uv_strerror(r));
        }
        MVM_free(dest);

        /* Start listening, but unref the socket so it won't get in the way of
         * other things we want to do on this event loop. */
        socket->data = data;
        if ((r = uv_listen((uv_stream_t *)socket, backlog, on_connection)) != 0) {
            MVM_free(socket);
            MVM_exception_throw_adhoc(tc, "Failed to listen: %s", uv_strerror(r));
        }
        uv_unref((uv_handle_t *)socket);

        data->ss.handle = (uv_stream_t *)socket;
    }
    else {
        MVM_exception_throw_adhoc(tc, "Socket is already bound or connected");
    }
}
Esempio n. 30
0
static int pipe_echo_start(char* pipeName) {
  int r;

  server = (uv_handle_t*)&pipeServer;
  serverType = PIPE;

  r = uv_pipe_init(loop, &pipeServer);
  if (r) {
    fprintf(stderr, "uv_pipe_init: %s\n",
        uv_strerror(uv_last_error(loop)));
    return 1;
  }

  r = uv_pipe_bind(&pipeServer, pipeName);
  if (r) {
    fprintf(stderr, "uv_pipe_bind: %s\n",
        uv_strerror(uv_last_error(loop)));
    return 1;
  }

  r = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, on_connection);
  if (r) {
    fprintf(stderr, "uv_pipe_listen: %s\n",
        uv_strerror(uv_last_error(loop)));
    return 1;
  }

  return 0;
}