Simple coroutine library integrated with IO event loop (libevent) / objects in plain C Snapshot
Classes | Typedefs | Functions
TCPACCEPTOR

listener for tcp connections, a usermode thread is created for each new connection. More...

Classes

struct  tagTCPACCEPTOR

Typedefs

typedef int(* EVTHREAD_FACTORY )(int fd, EVTHREAD_PROC *proc, void **ctx, void *factory_ctx)
typedef struct tagTCPACCEPTOR EVTCPACCEPTOR

Functions

EVTCPACCEPTOREVTCPACCEPTOR_init_ex (EVLOOP *loop, SOCKADDR *addr, int listener_backlog, EVTHREAD_FACTORY factory, int read_buffer_size, int write_buffer_size, void *ctx)
EVTCPACCEPTOREVTCPACCEPTOR_init (EVLOOP *loop, int listener_fd, EVTHREAD_FACTORY factory, int read_buffer_size, int write_buffer_size, void *ctx)
void EVTCPACCEPTOR_close (EVTCPACCEPTOR *)

Detailed Description

listener for tcp connections, a usermode thread is created for each new connection.


Typedef Documentation

typedef struct tagTCPACCEPTOR EVTCPACCEPTOR
typedef int(* EVTHREAD_FACTORY)(int fd, EVTHREAD_PROC *proc, void **ctx, void *factory_ctx)

Definition at line 233 of file evthread.h.


Function Documentation

void EVTCPACCEPTOR_close ( EVTCPACCEPTOR )

Definition at line 659 of file evthread.c.

{
  event_del(&acceptor->read_event);
  if (acceptor->fd != -1) {
    close(acceptor->fd);
  }
  free(acceptor);
}
EVTCPACCEPTOR* EVTCPACCEPTOR_init ( EVLOOP loop,
int  listener_fd,
EVTHREAD_FACTORY  factory,
int  read_buffer_size,
int  write_buffer_size,
void *  ctx 
)

Definition at line 630 of file evthread.c.

{
  EVTCPACCEPTOR *acceptor;

  acceptor = (EVTCPACCEPTOR *) malloc( sizeof( EVTCPACCEPTOR ) );
  if (!acceptor) {
    return 0;
  }
  
  if (fd_set_blocking( fd, 0 )) {
    free(acceptor);
    return  0;
  }

  acceptor->loop = loop;
  acceptor->factory = factory;
  acceptor->fd = fd;
  acceptor->ctx = ctx;

  acceptor->read_buffer_size = read_buffer_size;
  acceptor->send_buffer_size = send_buffer_size;

  event_set( &acceptor->read_event, fd, EV_READ | EV_PERSIST, socket_listener_cb, (void *) acceptor );
  event_base_set( loop->ev_base, &acceptor->read_event );
  event_add( &acceptor->read_event, 0 );

  return acceptor;
}
EVTCPACCEPTOR* EVTCPACCEPTOR_init_ex ( EVLOOP loop,
SOCKADDR *  addr,
int  listener_backlog,
EVTHREAD_FACTORY  factory,
int  read_buffer_size,
int  write_buffer_size,
void *  ctx 
)

Definition at line 615 of file evthread.c.

{
  int listener_fd;

  listener_fd = fd_make_tcp_listener( addr, listener_backlog );
  if (listener_fd == -1) {
    MLOG_ERROR( "Can't listen on address %s. error errno %d", SOCKADDR_to_string( addr ), errno );
    return 0; 
  }

  return EVTCPACCEPTOR_init( loop, listener_fd, factory, read_buffer_size, send_buffer_size, ctx );
 

}