コード例 #1
0
ファイル: multithreading.c プロジェクト: LiKun-8/fossa
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]);
}
コード例 #2
0
ファイル: net.c プロジェクト: xuezaijiongtu/fossa
void ns_mgr_init(struct ns_mgr *s, void *user_data) {
  memset(s, 0, sizeof(*s));
  s->ctl[0] = s->ctl[1] = INVALID_SOCKET;
  s->user_data = user_data;

#ifdef _WIN32
  {
    WSADATA data;
    WSAStartup(MAKEWORD(2, 2), &data);
  }
#elif !defined(AVR_LIBC)
  /* Ignore SIGPIPE signal, so if client cancels the request, it
   * won't kill the whole process. */
  signal(SIGPIPE, SIG_IGN);
#endif

#ifndef NS_DISABLE_SOCKETPAIR
  do {
    ns_socketpair(s->ctl, SOCK_DGRAM);
  } while (s->ctl[0] == INVALID_SOCKET);
#endif

#ifdef NS_ENABLE_SSL
  {
    static int init_done;
    if (!init_done) {
      SSL_library_init();
      init_done++;
    }
  }
#endif
  ns_ev_mgr_init(s);
  DBG(("=================================="));
  DBG(("init mgr=%p", s));
}
コード例 #3
0
ファイル: posix_hal.c プロジェクト: nikhilkotian22/smart.js
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);
}
コード例 #4
0
ファイル: unit_test.c プロジェクト: hosseinian-malay/fossa
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;
}
コード例 #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;
}