Ejemplo n.º 1
0
static void spawn_handling_thread(struct ns_connection *nc) {
  struct ns_mgr dummy = {};
  sock_t sp[2];
  struct ns_connection *c[2];

  /*
   * Create a socket pair, and wrap each socket into the connection with
   * dummy event manager.
   * c[0] stays in this thread, c[1] goes to another thread.
   */
  ns_socketpair(sp, SOCK_STREAM);
  c[0] = ns_add_sock(&dummy, sp[0], forwarder_ev_handler);
  c[1] = ns_add_sock(&dummy, sp[1], nc->listener->priv_1);

  /* Interlink client connection with c[0] */
  link_conns(c[0], nc);

  /*
   * Switch c[0] manager from the dummy one to the real one. c[1] manager
   * will be set in another thread, allocated on stack of that thread.
   */
  ns_add_conn(nc->mgr, c[0]);

  /*
   * Dress c[1] as nc.
   * TODO(lsm): code in accept_conn() looks similar. Refactor.
   */
  c[1]->listener = nc->listener;
  c[1]->proto_handler = nc->proto_handler;
  c[1]->proto_data = nc->proto_data;
  c[1]->user_data = nc->user_data;

  ns_start_thread(per_connection_thread_function, c[1]);
}
Ejemplo n.º 2
0
static struct ns_connection *accept_conn(struct ns_connection *ls) {
  struct ns_connection *c = NULL;
  union socket_address sa;
  socklen_t len = sizeof(sa);
  sock_t sock = INVALID_SOCKET;

  /* NOTE(lsm): on Windows, sock is always > FD_SETSIZE */
  if ((sock = accept(ls->sock, &sa.sa, &len)) == INVALID_SOCKET) {
  } else if ((c = ns_add_sock(ls->mgr, sock, ls->handler)) == NULL) {
    closesocket(sock);
#ifdef NS_ENABLE_SSL
  } else if (ls->ssl_ctx != NULL &&
             ((c->ssl = SSL_new(ls->ssl_ctx)) == NULL ||
              SSL_set_fd(c->ssl, sock) != 1)) {
    DBG(("SSL error"));
    ns_close_conn(c);
    c = NULL;
#endif
  } else {
    c->listener = ls;
    c->proto_data = ls->proto_data;
    c->proto_handler = ls->proto_handler;
    c->user_data = ls->user_data;
    ns_call(c, NS_ACCEPT, &sa);
    DBG(("%p %d %p %p", c, c->sock, c->ssl_ctx, c->ssl));
  }

  return c;
}
Ejemplo n.º 3
0
void sj_prompt_init_hal() {
  int fds[2];
  if (!ns_socketpair(fds, SOCK_STREAM)) {
    printf("cannot create a socketpair\n");
    exit(1);
  }
  ns_start_thread(stdin_thread, (void *) (uintptr_t) fds[1]);
  ns_add_sock(&sj_mgr, fds[0], prompt_handler);
}
Ejemplo n.º 4
0
static const char *test_thread(void) {
  struct ns_mgr mgr;
  struct ns_connection *nc;
  sock_t sp[2];
  char buf[20];

  ASSERT(ns_socketpair(sp) == 1);
  ns_start_thread(thread_func, &sp[1]);

  ns_mgr_init(&mgr, NULL);
  ASSERT((nc = ns_add_sock(&mgr, sp[0], eh2)) != NULL);
  nc->user_data = buf;
  poll_mgr(&mgr, 50);
  ASSERT(strcmp(buf, ":-)") == 0);
  ns_mgr_free(&mgr);
  closesocket(sp[1]);

  return NULL;
}
Ejemplo n.º 5
0
int main(int argc, char *argv[]) {
    struct ns_mgr mgr;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <port> <client|server>\n", argv[0]);
        exit(EXIT_FAILURE);
    } else if (strcmp(argv[2], "client") == 0) {
        int fds[2];
        struct ns_connection *ioconn, *server_conn;

        ns_mgr_init(&mgr, NULL, client_handler);

        // Connect to the pubsub server
        server_conn = ns_connect(&mgr, argv[1], NULL);
        if (server_conn == NULL) {
            fprintf(stderr, "Cannot connect to port %s\n", argv[1]);
            exit(EXIT_FAILURE);
        }

        // Create a socketpair and give one end to the thread that reads stdin
        ns_socketpair(fds);
        ns_start_thread(stdin_thread, &fds[1]);

        // The other end of a pair goes inside the server
        ioconn = ns_add_sock(&mgr, fds[0], NULL);
        ioconn->flags |= NSF_USER_1;    // Mark this so we know this is a stdin
        ioconn->user_data = server_conn;

    } else {
        // Server code path
        ns_mgr_init(&mgr, NULL, server_handler);
        ns_bind(&mgr, argv[1], NULL);
        printf("Starting pubsub server on port %s\n", argv[1]);
    }

    for (;;) {
        ns_mgr_poll(&mgr, 1000);
    }
    ns_mgr_free(&mgr);

    return EXIT_SUCCESS;
}