예제 #1
0
파일: asev.c 프로젝트: dpersson/libasev
int 
main(void)
{
  picoev_loop* loop;
  int listen_sock, flag;
  
  /* listen to port */
  assert((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) != -1);
  flag = 1;
  assert(setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) == 0);
  struct sockaddr_in listen_addr;
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_port = htons(PORT);
  listen_addr.sin_addr.s_addr = htonl(HOST);
  assert(bind(listen_sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr)) == 0);
  assert(listen(listen_sock, 5) == 0);
  setup_sock(listen_sock);
  
  /* init picoev */
  picoev_init(MAX_FDS);
  /* create loop */
  loop = picoev_create_loop(60);
  /* add listen socket */
  picoev_add(loop, listen_sock, PICOEV_READ, 0, accept_callback, NULL);

  /* Seccomp sandbox setup */
  sandbox_init();
  sandbox_setup();
  sandbox_lockdown();

  /* loop */
  while (1)
  {
    fputc('.', stdout);
    fflush(stdout);
    picoev_loop_once(loop, 10);
  }
  /* cleanup */
  picoev_destroy_loop(loop);
  picoev_deinit();
  
  return 0;
}
예제 #2
0
int main(int argc, char** argv)
{
  int ch, r, flag;
  struct sockaddr_in listen_addr;
  picoev_loop* loop;
  
  while ((ch = getopt(argc, argv, "p:")) != -1) {
    switch (ch) {
    case 'p':
      assert(sscanf(optarg, "%hu", &port) == 1);
      break;
    default:
      exit(1);
    }
  }
  
  listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  assert(listen_sock != -1);
  flag = 1;
  r = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
  assert(r == 0);
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_port = htons(port);
  listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  r = bind(listen_sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr));
  assert(r == 0);
  setup_sock(listen_sock);
  r = listen(listen_sock, SOMAXCONN);
  assert(r == 0);
  
  picoev_init(1048576 + 10);
  loop = picoev_create_loop(60);
  picoev_add(loop, listen_sock, PICOEV_READ, 0, accept_cb, NULL);
  while (1) {
    picoev_loop_once(loop, 10);
  }
  
  return 0;
}
예제 #3
0
파일: bossan_ext.c 프로젝트: henteko/bossan
static VALUE
bossan_run_loop(int argc, VALUE *argv, VALUE self)
{
  int ret;
  VALUE args1, args2, args3;

  rb_scan_args(argc, argv, "21", &args1, &args2, &args3);

  if(listen_sock > 0){
    rb_raise(rb_eException, "already set listen socket");
  }

  if (argc == 3){
    server_name = StringValuePtr(args1);
    server_port = NUM2INT(args2);

    long _port = NUM2INT(args2);

    if (_port <= 0 || _port >= 65536) {
      // out of range
      rb_raise(rb_eArgError, "port number outside valid range");
    }

    server_port = (short)_port;

    ret = inet_listen();
    rack_app = args3;
  } else {
    Check_Type(args1, T_STRING);
    ret = unix_listen(StringValuePtr(args1));
    rack_app = args2;
  }

  if(ret < 0){
    //error
    listen_sock = -1;
  }

  if(listen_sock <= 0){
    rb_raise(rb_eTypeError, "not found listen socket");
  }
    
  /* init picoev */
  picoev_init(MAX_FDS);
  /* create loop */
  main_loop = picoev_create_loop(60);
  loop_done = 1;
  
  setsig(SIGPIPE, sigpipe_cb);
  setsig(SIGINT, sigint_cb);
  setsig(SIGTERM, sigint_cb);
    
  picoev_add(main_loop, listen_sock, PICOEV_READ, ACCEPT_TIMEOUT_SECS, accept_callback, NULL);
    
  /* loop */
  while (loop_done) {
    picoev_loop_once(main_loop, 10);
  }
  picoev_destroy_loop(main_loop);
  picoev_deinit();

  printf("Bye.\n");
  return Qnil;
}
예제 #4
0
int main(int argc, char** argv)
{
  int ch, i, r;
  picoev_loop* loop;
  struct timeval start_at, end_at;
  double elapsed;
  
  host.s_addr = htonl(0x7f000001);
  
  while ((ch = getopt(argc, argv, "a:c:n:fh:p:")) != -1) {
    switch (ch) {
    case 'a':
      assert(sscanf(optarg, "%d", &active_connections) == 1);
      break;
    case 'c':
      assert(sscanf(optarg, "%d", &num_connections) == 1);
      break;
    case 'n':
      assert(sscanf(optarg, "%d", &num_requests) == 1);
      break;
    case 'f':
      fix_clients = 1;
      break;
    case 'h':
      if (inet_aton(optarg, &host) == 0) {
	struct hostent* h = gethostbyname(optarg);
	assert(h != NULL && "host not found");
	assert(h->h_addrtype == AF_INET);
	assert(h->h_length == sizeof(host));
	memcpy(&host, h->h_addr_list[0], sizeof(host));
      }
      break;
    case 'p':
      assert(sscanf(optarg, "%hu", &port) == 1);
      break;
    default:
      exit(1);
    }
  }
  
  picoev_init(num_connections + 10);
  loop = picoev_create_loop(60);
  
  /* setup connections */
  for (i = 0; i < num_connections; ++i) {
    int on;
    struct sockaddr_in addr;
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    assert(fd != -1 && "socket(2) failed");
    on = 1;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    memcpy(&addr.sin_addr, &host, sizeof(host));
    printf("try to connect\n");
    r = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
    if (r == -1) {
      perror("could not connect to server");
      exit(2);
    }
    r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
    assert(r == 0);
    r = fcntl(fd, F_SETFL, O_NONBLOCK);
    assert(r == 0);
    picoev_add(loop, fd, PICOEV_READ, 0, read_cb, NULL);
    fds[i] = fd;
    usleep(1000);
  }
  
  gettimeofday(&start_at, NULL);
  
  /* fire first active_connections */
  printf("fire!\n");
  for (i = 0; i < active_connections; ++i) {
    r = write(fds[next_fd_idx], "hello\n", 6);
    assert(r == 6);
    next_fd_idx = (next_fd_idx + 1) % num_connections;
  }
  /* the main loop */
  while (num_responses < num_requests) {
    picoev_loop_once(loop, 10);
  }
  
  gettimeofday(&end_at, NULL);
  
  elapsed = end_at.tv_sec + end_at.tv_usec / 1000000.0
    - (start_at.tv_sec + start_at.tv_usec / 1000000.0);
  printf("%f reqs./sec. (%d in %f seconds)\n", num_responses / elapsed,
	 num_responses, elapsed);
  
  return 0;
}