Esempio n. 1
0
/* Allocates and returns a new uv_ares_task_t */
static uv_ares_task_t* uv__ares_task_create(uv_loop_t* loop, int fd) {
  uv_ares_task_t* h = malloc(sizeof(uv_ares_task_t));

  if (h == NULL) {
    uv_fatal_error(ENOMEM, "malloc");
    return NULL;
  }

  h->loop = loop;
  h->sock = fd;

  ev_io_init(&h->read_watcher, uv__ares_io, fd, EV_READ);
  ev_io_init(&h->write_watcher, uv__ares_io, fd, EV_WRITE);

  h->read_watcher.data = h;
  h->write_watcher.data = h;

  return h;
}
Esempio n. 2
0
struct remote* new_remote(int fd, int timeout)
{
    struct remote *remote;
    remote = malloc(sizeof(struct remote));
    remote->buf = malloc(BUF_SIZE);
    remote->recv_ctx = malloc(sizeof(struct remote_ctx));
    remote->send_ctx = malloc(sizeof(struct remote_ctx));
    remote->fd = fd;
    ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
    ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
    ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, timeout, 0);
    remote->recv_ctx->remote = remote;
    remote->recv_ctx->connected = 0;
    remote->send_ctx->remote = remote;
    remote->send_ctx->connected = 0;
    remote->buf_len = 0;
    remote->buf_idx = 0;
    return remote;
}
Esempio n. 3
0
LWS_VISIBLE int
libwebsocket_initloop(
	struct libwebsocket_context *context,
	struct ev_loop *loop)
{
	int status = 0;
	int backend;
	const char * backend_name;
	struct ev_io *w_accept = (ev_io *)&context->w_accept;
	struct ev_signal *w_sigint = (ev_signal *)&context->w_sigint;

	if (!loop)
		loop = ev_default_loop(0);

	context->io_loop = loop;
   
	/*
	 * Initialize the accept w_accept with the listening socket
	 * and register a callback for read operations:
	 */
	ev_io_init(w_accept, libwebsocket_accept_cb,
					context->listen_service_fd, EV_READ);
	ev_io_start(context->io_loop,w_accept);
	ev_signal_init(w_sigint, libwebsocket_sigint_cb, SIGINT);
	ev_signal_start(context->io_loop,w_sigint);
	backend = ev_backend(loop);

	switch (backend) {
	case EVBACKEND_SELECT:
		backend_name = "select";
		break;
	case EVBACKEND_POLL:
		backend_name = "poll";
		break;
	case EVBACKEND_EPOLL:
		backend_name = "epoll";
		break;
	case EVBACKEND_KQUEUE:
		backend_name = "kqueue";
		break;
	case EVBACKEND_DEVPOLL:
		backend_name = "/dev/poll";
		break;
	case EVBACKEND_PORT:
		backend_name = "Solaris 10 \"port\"";
		break;
	default:
		backend_name = "Unknown libev backend";
		break;
	};

	lwsl_notice(" libev backend: %s\n", backend_name);

	return status;
}
Esempio n. 4
0
void snf_start(SNFConfig *conf) {
  struct libevio_ctx *actx;
  struct ev_loop *loop = ev_default_loop(0);
  int sock;
  int error;
  SNFListener *listener = conf->listeners;
  int opt = 1;

  // Bind all the listeners and register them in the libev loop
  while (listener) {
    listener->sock = -1;
    snflog(conf, TIO, LINFO, "Binding %s:%s", listener->ip_str, listener->port);
    sock = socket(listener->ss.ss_family, SOCK_STREAM, 0);
    if (sock < 0) {
      snflog(conf, TIO, LERROR, "Couldn't create socket: %s",
          strerror(errno));
      goto continue_loop;
    }
    // Let the listener rebind after a restart
    error = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    if (error) {
      snflog(conf, TIO, LERROR, "Couldn't set socket options");
      close(sock);
      goto continue_loop;
    }
    error = bind(sock, (struct sockaddr *)&listener->ss, listener->ss.ss_len);
    if (error) {
      snflog(conf, TIO, LERROR, "Couldn't bind to listener %s:%u: %s",
          listener->ip_str, listener->port, strerror(errno));
      close(sock);
      goto continue_loop;
    }
    error = listen(sock, 5);
    if (error) {
      snflog(conf, TIO, LERROR, "Couldn't listen: %s", strerror(errno));
      close(sock);
      goto continue_loop;
    }
    // Init the custom context for the listener callback
    actx = malloc(sizeof(*actx));
    actx->listener = listener;
    actx->conf = conf;
    // Initialize the watcher struct with our fd to watch
    ev_io_init((ev_io *)actx, libev_accept_cb, sock, EV_READ);
    // Register the watcher with our event loop
    ev_io_start(loop, (ev_io *)actx);
    listener->sock = sock;

continue_loop:
    listener = listener->next;
  }

  // Loop until unroll is called
  ev_loop(loop, 0);
}
Esempio n. 5
0
/**
 * Initializes the UDP Listener.
 * @arg netconf The network configuration
 * @return 0 on success.
 */
static int setup_udp_listener(statsite_networking *netconf) {
    if (netconf->config->udp_port == 0) {
        syslog(LOG_INFO, "UDP port is disabled");
        return 0;
    }
    struct sockaddr_in addr;
    struct in_addr bind_addr;
    bzero(&addr, sizeof(addr));
    bzero(&bind_addr, sizeof(bind_addr));
    addr.sin_family = PF_INET;
    addr.sin_port = htons(netconf->config->udp_port);

    int ret = inet_pton(AF_INET, netconf->config->bind_address, &bind_addr);
    if (ret != 1) {
        syslog(LOG_ERR, "Invalid IPv4 address '%s'!", netconf->config->bind_address);
        return 1;
    }
    addr.sin_addr = bind_addr;

    // Make the socket, bind and listen
    int udp_listener_fd = socket(PF_INET, SOCK_DGRAM, 0);
    int optval = 1;
    if (setsockopt(udp_listener_fd, SOL_SOCKET,
                SO_REUSEADDR, &optval, sizeof(optval))) {
        syslog(LOG_ERR, "Failed to set SO_REUSEADDR! Err: %s", strerror(errno));
        close(udp_listener_fd);
        return 1;
    }
    if (bind(udp_listener_fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
        syslog(LOG_ERR, "Failed to bind on UDP socket! Err: %s", strerror(errno));
        close(udp_listener_fd);
        return 1;
    }

    // Put the socket in non-blocking mode
    int flags = fcntl(udp_listener_fd, F_GETFL, 0);
    fcntl(udp_listener_fd, F_SETFL, flags | O_NONBLOCK);

    // Allocate a connection object for the UDP socket,
    // ensure a min-buffer size of 64K
    conn_info *conn = get_conn();
    while (circbuf_avail_buf(&conn->input) < 65536) {
        circbuf_grow_buf(&conn->input);
    }
    netconf->udp_client.data = conn;

    syslog(LOG_INFO, "Listening on udp '%s:%d'.",
           netconf->config->bind_address, netconf->config->udp_port);

    // Create the libev objects
    ev_io_init(&netconf->udp_client, handle_udp_message,
                udp_listener_fd, EV_READ);
    ev_io_start(&netconf->udp_client);
    return 0;
}
Esempio n. 6
0
int pkr_conn_accepted_cb(int sockfd, void* void_pkm)
{
  struct incoming_conn_state* ics;
  struct pk_manager* pkm = (struct pk_manager*) void_pkm;
  struct pk_backend_conn* pkb;
  socklen_t slen;
  char rbuf[128];
  char lbuf[128];

  PK_TRACE_FUNCTION;

  ics = malloc(sizeof(struct incoming_conn_state));
  ics->pkm = pkm;
  ics->pkb = NULL;
  ics->tunnel = NULL;
  ics->hostname = NULL;
  ics->parsed_as = PROTO_UNKNOWN;
  ics->parse_state = PARSE_UNDECIDED;
  ics->created = pk_time();
  ics->unparsed_data = NULL;

  slen = sizeof(ics->local_addr);
  getsockname(sockfd, (struct sockaddr*) &(ics->local_addr), &slen);

  slen = sizeof(ics->remote_addr);
  getpeername(sockfd, (struct sockaddr*) &(ics->remote_addr), &slen);

  pk_log(PK_LOG_TUNNEL_DATA,
         "Accepted; remote=%s; local=%s; fd=%d",
         in_addr_to_str((struct sockaddr*) &(ics->remote_addr), rbuf, 128),
         in_addr_to_str((struct sockaddr*) &(ics->local_addr), lbuf, 128),
         sockfd);

  /* Allocate a connection for this request or die... */
  sprintf(lbuf, "!NEW:%d", ics->remote_addr.sin_port);
  if (NULL == (pkb = pkm_alloc_be_conn(pkm, NULL, lbuf))) {
    _pkr_close(ics, PK_LOG_ERROR, "BE alloc failed");
    PKS_close(sockfd);
    return -1;
  }

  pkb->kite = NULL;
  pkb->conn.sockfd = sockfd;
  ics->pkb = pkb;

  set_non_blocking(sockfd);

  ev_io_init(&(pkb->conn.watch_r),
             pkr_new_conn_readable_cb,
             PKS_EV_FD(sockfd),
             EV_READ);
  pkb->conn.watch_r.data = (void *) ics;
  ev_io_start(pkm->loop, &(pkb->conn.watch_r));
  return 0;
}
Esempio n. 7
0
const char*
slave_run_cb(void)
{
    struct ev_loop *loop;
    ev_io           io_listen;
    ev_signal       sigint_listen;
    ev_signal       sigterm_listen;

    pthread_mutex_init(&srv.pending_lk, 0);
    pthread_mutex_init(&srv.clients_lk, 0);
    if (!(loop = ev_default_loop(EVFLAG_AUTO)))
        return 1;

    syslog(LOG_INFO, "listening on %s:%hd", inet_ntoa(srv.addr.sin_addr), ntohs(srv.addr.sin_port));
    ev_signal_init(&sigint_listen, &nol_s_ev_sigint, SIGINT);
    ev_signal_init(&sigterm_listen, &nol_s_ev_sigint, SIGTERM);
    ev_io_init(&io_listen, &nol_s_ev_conn_accept, srv.listen_sock, EV_READ);
    ev_io_init(&srv.master_io, &nol_s_ev_master, srv.master_sock, EV_READ);
    ev_async_init(&srv.client_status, &nol_s_ev_client_status);

    /* catch SIGINT so we can close connections and clean up properly */
    ev_signal_start(loop, &sigint_listen);
    ev_signal_start(loop, &sigterm_listen);
    ev_io_start(loop, &io_listen);
    ev_io_start(loop, &srv.master_io);
    ev_async_start(loop, &srv.client_status);

    /** 
     * This loop will listen for new connections and data from
     * the master.
     **/
    ev_loop(loop, 0);
    close(srv.listen_sock);

    ev_default_destroy();
    closelog();
    pthread_mutex_destroy(&srv.pending_lk);
    pthread_mutex_destroy(&srv.clients_lk);

    nol_s_hook_invoke(HOOK_CLEANUP);
    return 0;
}
Esempio n. 8
0
// TODO: How to close off the things?
// Pass them through?
void add_callbacks(Bar * const bar, FILE *in, FILE *out)
{
  bspwm_watcher.data = &bar->bspwm;
  ev_io_init(&bspwm_watcher, bspwm_cb, fileno(in), EV_READ);

  wifi_timer.data = &bar->wifi;
  ev_init(&wifi_timer, wifi_cb);
  wifi_timer.repeat = 3.;

  battery_watcher.data = &bar->battery;
  ev_init(&battery_watcher, battery_cb);
  battery_watcher.repeat = 1.;

  sound_watcher.data = &bar->sound;
  ev_init(&sound_watcher, sound_cb);
  sound_watcher.repeat = 1.;

  packages_io_watcher.data = &bar->packages;
  FILE *packages_file = get_packages_file();
  ev_io_init(&packages_io_watcher, packages_io_cb, fileno(packages_file), EV_READ);
  ev_init(&packages_timer, packages_cb);
  packages_timer.repeat = 5 * 60.;

  // include an offset so that we don't get the time before it changes.
  // TODO Maybe separate into date watcher and time watcher?
  clock_update(&bar->clock);
  clock_watcher.data = &bar->clock;
  ev_periodic_init(&clock_watcher, clock_cb, 1., 60., 0);

  // Easy way to shut down.
  signal_watcher.data = bar;
  ev_signal_init(&signal_watcher, shutdown_cb, SIGINT);

  // An ev prepare runs before ev_run collects events;
  // in other words, it's (almost) like it's after all the events that have been called.
  // So we add an update

  out_fp = out;
  prepare_display.data = bar;
  ev_prepare_init(&prepare_display, display_cb);

}
Esempio n. 9
0
void
write_callback(struct ev_loop *loop, ev_io *w, int revents)
{
    char buffer[1024] = { 0 };
    snprintf(buffer, 1023, "this is a libev server\n");
    write(w->fd, buffer, strlen(buffer), 0);
    int fd = w->fd;
    ev_io_stop(loop, w);
    ev_io_init(w, recv_callback, fd, EV_READ);
    ev_io_start(loop, w);
}
Esempio n. 10
0
void socks5_accept(int sock, void (*cb)(int, char *, char *))
{
	ctx_t *ctx = (ctx_t *)malloc(sizeof(ctx_t));
	if (ctx == NULL)
	{
		LOG("out of memory");
		close(sock);
		return;
	}
	ctx->sock = sock;
	ctx->cb = cb;
	ctx->state = CLOSED;

	ev_io_init(&(ctx->w_read), socks5_recv_cb, ctx->sock, EV_READ);
	ev_io_init(&(ctx->w_write), socks5_send_cb, ctx->sock, EV_WRITE);
	ctx->w_read.data = (void *)ctx;
	ctx->w_write.data = (void *)ctx;

	ev_io_start(EV_A_ &(ctx->w_read));
}
Esempio n. 11
0
int main(int argc ,char** argv)
{
 thread_init();
 int listen;
 listen=socket_init();
 ev_io_init(&(dispatcher_thread.accept_watcher), accept_callback,listen, EV_READ);
 ev_io_start(dispatcher_thread.loop,&(dispatcher_thread.accept_watcher)); 
 ev_loop(dispatcher_thread.loop,0);
 ev_loop_destroy(dispatcher_thread.loop);
 return 0;
}
Esempio n. 12
0
int echo_client_add(echo_server_t *s, int sd, struct sockaddr_in *addr)
{
	echo_client_t *c = malloc(sizeof(*c));
	if (NULL == c) return -1;

	c->addr.sin_family = addr->sin_family;
	c->addr.sin_addr.s_addr = addr->sin_addr.s_addr;
	c->addr.sin_port = addr->sin_port;

	ev_io_init(&c->wev_recv, echo_client_wcb_recv, sd, EV_READ);
	ev_io_init(&c->wev_send, echo_client_wcb_send, sd, EV_WRITE);
	ev_timer_init(&c->wev_timeout, echo_client_wcb_timeout, 0, SOCKET_TIMEOUT);
	ev_timer_again(loop, &c->wev_timeout);
	ev_io_start(loop, &c->wev_recv);

	c->to_send_beg = c->to_send;
	c->to_send_size = 0;

	return 0;
}
inline static struct sock_ev_client* client_new(int fd) {
  struct sock_ev_client* client;

  client = realloc(NULL, sizeof(struct sock_ev_client));
  client->fd = fd;
  //client->server = server;
  setnonblock(client->fd);
  ev_io_init(&client->io, client_cb, client->fd, EV_READ);

  return client;
}
Esempio n. 14
0
bool CBSocketDidConnectEvent(CBDepObject * eventID, CBDepObject loopID, CBDepObject socketID, void (*onDidConnect)(void *, void *), void * peer){
	CBIOEvent * event = malloc(sizeof(*event));
	event->loop = loopID.ptr;
	event->onEvent.ptr = onDidConnect;
	event->peer = peer;
	event->timerCallback = CBDidConnectTimeout;
	event->socket = socketID;
	ev_io_init((struct ev_io *)event, CBDidConnect, socketID.i, EV_WRITE);
	eventID->ptr = event;
	return true;
}
Esempio n. 15
0
struct remote_ctx *new_remote(int fd, struct server_ctx *server_ctx)
{
    struct remote_ctx *ctx = malloc(sizeof(struct remote_ctx));
    memset(ctx, 0, sizeof(struct remote_ctx));
    ctx->fd = fd;
    ctx->server_ctx = server_ctx;
    ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ);
    ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout,
                  server_ctx->timeout);
    return ctx;
}
Esempio n. 16
0
static void
server_cb (EV_P_ ev_io *w, int revents)
{
    int new_fd;
    client_t *my_client;

    /* NOTES: possible event bits are EV_READ and EV_ERROR
     * however, EV_ERROR shouldn't really happen, so if (EV_ERROR) fatal
     * otherwise assume EV_READ */
    if (revents & EV_ERROR)
    {
        fprintf(stderr, "FATAL: unexpected EV_ERROR on server event watcher\n");
        exit(1);
    }

    /* assume EV_READ */
    new_fd = unix_accept(server_fd);
    if (new_fd == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
        return;     /* nothing interesting happened */
    else if (new_fd == -1)
    {
        fprintf(stderr, "accept failed unexpectedly: %s\n", strerror(errno));
        return;
    }
    
    fprintf(stderr, "accepted connection: fd: %d\n", new_fd);

    /* register client connection in state */
    if (nr_clients <= IRCD_CLIENTS_MAX
        && (my_client = malloc(sizeof(*my_client))))
    {
        list_push((list_t **)&client_list, (list_t *)my_client);
        nr_clients++;

        /* save client info and init watcher */
        my_client->fd = new_fd;
        my_client->timestamp = time(NULL);
        my_client->type = CLIENT_UNREGISTERED;
        my_client->more = NULL;
        my_client->in_buf.index = 0;
        my_client->out_buf = NULL;
        ev_io_init(&my_client->w, &client_cb, new_fd, EV_READ);
        ev_io_start(EV_A_ &my_client->w);
        my_client->w.data = my_client; /* lol recursion */
        return;
    }
    else if (my_client)
        /* we hit max clients?? */
        fprintf(stderr, "too many clients, dropping connection %d\n", new_fd);
    else
        fprintf(stderr, "malloc: %s\n", strerror(errno));
    close(new_fd);
    free(my_client);
}
Esempio n. 17
0
server_ctx_t *new_server_ctx(int fd)
{
    server_ctx_t *ctx = ss_malloc(sizeof(server_ctx_t));
    memset(ctx, 0, sizeof(server_ctx_t));

    ctx->fd = fd;

    ev_io_init(&ctx->io, server_recv_cb, fd, EV_READ);

    return ctx;
}
Esempio n. 18
0
/** Send SCGI formatted request stream to Worker*/
static void wr_req_body_write_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
  LOG_FUNCTION
  wr_req_t *req = (wr_req_t*) w->data;
  wr_wkr_t *worker = req->wkr;
  ssize_t sent = 0;
  LOG_DEBUG(DEBUG, "Request %d",req->id);
  if(!(revents & EV_WRITE))
    return;

  if(req->upload_file) {
    char buffer[WR_REQ_BODY_MAX_SIZE];
    ssize_t read;
    int rv=fseek(req->upload_file,req->bytes_sent,SEEK_SET);
    if(rv<0) {
      LOG_ERROR(WARN,"Error reading file:%s",strerror(errno));
      return;
    }
    read = fread(buffer,1,WR_REQ_BODY_MAX_SIZE,req->upload_file);
    sent = send(w->fd, buffer, read, 0);
  }

  if(sent <= 0) {
    ev_io_stop(loop,w);
    LOG_ERROR(WARN,"Error sending request:%s",strerror(errno));
    worker->state += (WR_WKR_ERROR + WR_WKR_HANG);
    wr_ctl_free(worker->ctl);
    return;
  }

  req->bytes_sent += sent;
  LOG_DEBUG(DEBUG,"Request %d sent %d/%d",  req->id, req->bytes_sent, req->ebb_req->content_length);

  if(req->ebb_req->content_length == req->bytes_sent) {
    ev_io_stop(loop,w);

    LOG_DEBUG(DEBUG,"Sent request body for Request %d to worker %d", req->id, worker->id);
    if(req->upload_file) {
      fclose(req->upload_file);
      remove(req->upload_file_name->str);
      wr_buffer_free(req->upload_file_name);
      req->upload_file = NULL;
    }

    scgi_free(req->scgi);
    req->scgi = NULL;

    ev_io_init(w,wr_resp_len_read_cb,w->fd,EV_READ);
    ev_io_start(loop,w);
    //We are waiting for response from worker, start idle watcher for it
    LOG_DEBUG(DEBUG,"Idle watcher started for worker %d", worker->id);
    wr_wait_watcher_start(worker);
    //wr_wkr_idle_watch_reset(worker);
  }
}
Esempio n. 19
0
void lwqq_async_io_watch(LwqqAsyncIoHandle io,int fd,int action,LwqqAsyncIoCallback fun,void* data)
{
    ev_io_init(io,event_cb_wrap,fd,action);
    LwqqAsyncIoWrap* wrap = s_malloc0(sizeof(*wrap));
    wrap->callback = fun;
    wrap->data = data;
    io->data = wrap;
    ev_io_start(EV_DEFAULT,io);
    if(ev_thread_status!=THREAD_NOW_RUNNING)
        start_ev_thread();
}
Esempio n. 20
0
spx_private void *spx_nio_thread_listen(void *arg){
    struct spx_nio_thread_context *context = (struct spx_nio_thread_context *) arg;
    if(NULL == context){
        return NULL;
    }
    ev_io_init(&(context->watcher),context->thread_notify_handle,context->pipe[0],EV_READ);
    context->watcher.data = context;//libev not the set function
    ev_io_start(context->loop,&(context->watcher));
    ev_run(context->loop,0);
    return NULL;
}
Esempio n. 21
0
void stream_init(server *srv, stream *s1, stream *s2, int fd1, int fd2, ev_io_cb cb1, ev_io_cb cb2, void* data) {
	ev_io *w1, *w2;
	UNUSED(srv);

	fd_init(fd1);
	fd_init(fd2);
	s1->other = s2;
	s2->other = s1;
	s1->fd = fd1;
	s2->fd = fd2;

	w1 = &s1->watcher; w2 = &s2->watcher;
	ev_io_init(w1, cb1, fd1, 0);
	ev_io_init(w2, cb2, fd2, 0);
	w1->data = data;
	w2->data = data;

	s1->buffer = g_string_sized_new(0);
	s2->buffer = g_string_sized_new(0);
}
Esempio n. 22
0
static struct server * new_server(int fd, struct listen_ctx *listener)
{
    if (verbose) {
        server_conn++;
    }

    struct server *server;
    server = malloc(sizeof(struct server));
    server->buf = malloc(BUF_SIZE);
    server->recv_ctx = malloc(sizeof(struct server_ctx));
    server->send_ctx = malloc(sizeof(struct server_ctx));
    server->fd = fd;
    ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
    ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
    ev_timer_init(&server->recv_ctx->watcher, server_timeout_cb,
                  min(MAX_CONNECT_TIMEOUT,
                      listener->timeout), listener->timeout);
    server->recv_ctx->server = server;
    server->recv_ctx->connected = 0;
    server->send_ctx->server = server;
    server->send_ctx->connected = 0;
    server->stage = 0;
    server->query = NULL;
    server->listen_ctx = listener;
    if (listener->method) {
        server->e_ctx = malloc(sizeof(struct enc_ctx));
        server->d_ctx = malloc(sizeof(struct enc_ctx));
        enc_ctx_init(listener->method, server->e_ctx, 1);
        enc_ctx_init(listener->method, server->d_ctx, 0);
    } else {
        server->e_ctx = NULL;
        server->d_ctx = NULL;
    }
    server->buf_len = 0;
    server->buf_idx = 0;
    server->remote = NULL;

    cork_dllist_add(&connections, &server->entries);

    return server;
}
Esempio n. 23
0
int client_add()
{
	int sd, rc;

	client_t *c;
	
	if (NULL == (c = calloc(1, sizeof(*c))))
	{
		return -1;
	}

	if (0 > (sd = socket(AF_INET, SOCK_STREAM, 0)))
	{
		fprintf(stderr, "sock error\n");
		return -1;
	}

	if (0 > (rc = aux_set_nonblk(sd, 1)))
	{
		close(sd);
		return -1;
	}

	rc = connect(sd, (struct sockaddr *) &addr, sizeof(addr));

	if (0 > rc && EINPROGRESS != errno)
	{
		fprintf(stderr, "conn error fd=%d\n", sd);
		close(sd);
		return -1;
	}

	ev_io_init(&c->wev_recv, wcb_recv, sd, EV_READ);
	ev_io_init(&c->wev_send, wcb_send, sd, EV_WRITE);
	ev_io_init(&c->wev_connect, wcb_connect, sd, EV_READ | EV_WRITE);
	ev_timer_init(&c->wev_timeout, wcb_timeout, 0, RESEND_INTERVAL);
	ev_timer_again(loop, &c->wev_timeout);
	ev_io_start(loop, &c->wev_connect);

	return 0;
}
Esempio n. 24
0
/**
 * Invoked when a TCP listening socket fd is ready
 * to accept a new client. Accepts the client, initializes
 * the connection buffers, and prepares to start listening
 * for client data
 */
static void handle_new_client(ev_loop *lp, ev_io *watcher, int ready_events) {
    // Get the network configuration
    bloom_networking *netconf = ev_userdata(lp);

    // Accept the client connection
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int client_fd = accept(watcher->fd,
                        (struct sockaddr*)&client_addr,
                        &client_addr_len);

    // Check for an error
    if (client_fd == -1) {
        syslog(LOG_ERR, "Failed to accept() connection! %s.", strerror(errno));
        return;
    }

    // Setup the socket
    if (set_client_sockopts(client_fd)) {
        return;
    }

    // Debug info
    syslog(LOG_DEBUG, "Accepted client connection: %s %d [%d]",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_fd);

    // Get the associated conn object
    conn_info *conn = get_conn();

    // Initialize the libev stuff
    ev_io_init(&conn->client, invoke_event_handler, client_fd, EV_READ);
    ev_io_init(&conn->write_client, handle_client_writebuf, client_fd, EV_WRITE);

    // Dispatch this client to a worker thread
    int next_thread = netconf->last_assign++ % netconf->config->worker_threads;
    worker_ev_userdata *data = netconf->workers[next_thread];

    // Sent accept along with the connection
    write(data->pipefd[1], "a", 1);
    write(data->pipefd[1], &conn, sizeof(conn_info*));
}
Esempio n. 25
0
int main() {
  struct ev_loop *loop;
  ev_io ss_watcher;
  int ss;
  ss = setup_ss();
  loop = ev_default_loop(EVBACKEND_EPOLL);
  ev_io_init(&ss_watcher, ss_cb, ss, EV_READ);
  ev_io_start(loop, &ss_watcher);
  ev_loop(loop, 0);
  if (close(ss) < 0) die("close");
  return 0;
}
Esempio n. 26
0
void worker(int sv,int pid,const char * home_dir){
	//printf("worker %d, sv: %d\n",pid,sv);

	id=pid;
	home_directory=home_dir;

	struct ev_loop * loop=ev_loop_new(0);
	ev_io w_new_client;
	ev_io_init(&w_new_client,new_client_cb,sv,EV_READ);
	ev_io_start(loop,&w_new_client);
	ev_run(loop);
}
Esempio n. 27
0
static server_t *new_server(int fd)
{
	server_t *server;
	server = malloc(sizeof(server_t));
	
	server->recv_ctx = malloc(sizeof(server_ctx_t));
	server->send_ctx = malloc(sizeof(server_ctx_t));
	server->buf = malloc(sizeof(buffer_t));
	server->fd = fd;
	server->recv_ctx->connected = 0;
	server->recv_ctx->server = server;
	server->send_ctx->connected = 0;
	server->send_ctx->server = server;
	
	ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
	ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
	
	balloc(server->buf, BUF_SIZE);
	
	return server;
}
Esempio n. 28
0
spx_private void *spx_thread_listening(void *arg){/*{{{*/
    struct spx_thread_pending_transport *tpt = (struct spx_thread_pending_transport *) arg;
    size_t idx = tpt->idx;
    struct spx_module_context *mc = tpt->mc;
    SpxFree(tpt);//free the memory
    struct spx_receive_context *tc = spx_list_get(mc->receive_triggers,idx);
    struct spx_thread_context *stc = spx_list_get(mc->threadpool,idx);
    ev_io_init(&(tc->watcher),tc->receive_handler,stc->pipe[0],EV_READ);
     ev_io_start(stc->loop,&(tc->watcher));
    ev_run(stc->loop,0);
    return NULL;
}/*}}}*/
Esempio n. 29
0
struct remote* new_remote(int fd)
{
    if (verbose) remote_conn++;

    struct remote *remote;
    remote = malloc(sizeof(struct remote));
    remote->buf = malloc(BUF_SIZE);
    remote->recv_ctx = malloc(sizeof(struct remote_ctx));
    remote->send_ctx = malloc(sizeof(struct remote_ctx));
    remote->fd = fd;
    ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
    ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
    remote->recv_ctx->remote = remote;
    remote->recv_ctx->connected = 0;
    remote->send_ctx->remote = remote;
    remote->send_ctx->connected = 0;
    remote->buf_len = 0;
    remote->buf_idx = 0;
    remote->server = NULL;
    return remote;
}
Esempio n. 30
0
static void
zc_asynio_write_init(zcAsynIO *conn)
{
    ev_io_init(&conn->w, zc_asynio_ev_write, conn->sock->fd, EV_WRITE);
    conn->w.data = conn;
    
    if (conn->write_timeout > 0) {
        float tm = conn->write_timeout/1000.0;
        ev_timer_init(&conn->wtimer, zc_asynio_write_ev_timeout, tm, tm);
        conn->wtimer.data = conn;
    }
}