src/sock_tcp.c
changeset 11 14e79683c48c
parent 10 9fe218576d13
child 12 4147fae232d9
equal deleted inserted replaced
10:9fe218576d13 11:14e79683c48c
     9 #include <fcntl.h>
     9 #include <fcntl.h>
    10 #include <string.h>
    10 #include <string.h>
    11 #include <assert.h>
    11 #include <assert.h>
    12 
    12 
    13 /*
    13 /*
       
    14  * Our basic socket event handler for driving our callbacks
       
    15  */
       
    16 static void sock_tcp_event_handler (evutil_socket_t fd, short what, void *arg) 
       
    17 {
       
    18     struct sock_tcp *sock = arg;
       
    19 
       
    20     // invoke appropriate callback
       
    21     if (what & EV_READ && SOCK_TCP_BASE(sock)->cb_info->on_read)
       
    22         SOCK_TCP_BASE(sock)->cb_info->on_read(SOCK_TCP_BASE(sock), SOCK_TCP_BASE(sock)->cb_arg);
       
    23 
       
    24     if (what & EV_WRITE && SOCK_TCP_BASE(sock)->cb_info->on_write)
       
    25         SOCK_TCP_BASE(sock)->cb_info->on_read(SOCK_TCP_BASE(sock), SOCK_TCP_BASE(sock)->cb_arg);
       
    26 }
       
    27 
       
    28 /*
    14  * Our sock_stream_methods.read method
    29  * Our sock_stream_methods.read method
    15  */
    30  */
    16 static err_t sock_tcp_read (struct sock_stream *base_sock, void *buf, size_t *len)
    31 static err_t sock_tcp_read (struct sock_stream *base_sock, void *buf, size_t *len)
    17 {
    32 {
    18     struct sock_tcp *sock = SOCK_FROM_BASE(base_sock, struct sock_tcp);
    33     struct sock_tcp *sock = SOCK_FROM_BASE(base_sock, struct sock_tcp);
    49 }
    64 }
    50 
    65 
    51 static err_t sock_tcp_event_init (struct sock_stream *base_sock)
    66 static err_t sock_tcp_event_init (struct sock_stream *base_sock)
    52 {
    67 {
    53     struct sock_tcp *sock = SOCK_FROM_BASE(base_sock, struct sock_tcp);
    68     struct sock_tcp *sock = SOCK_FROM_BASE(base_sock, struct sock_tcp);
    54     
    69     err_t err;
       
    70 
       
    71     // set nonblocking
       
    72     if ((err = sock_tcp_set_nonblock(sock, 1)))
       
    73         return err;
       
    74 
       
    75     // add ourselves as the event handler
       
    76     if ((err = sock_tcp_init_ev(sock, &sock_tcp_event_handler, sock)))
       
    77         return err;
       
    78     
       
    79     // done
    55     return SUCCESS;
    80     return SUCCESS;
    56 }
    81 }
    57 
    82 
    58 static err_t sock_tcp_event_enable (struct sock_stream *base_sock, short mask)
    83 static err_t sock_tcp_event_enable (struct sock_stream *base_sock, short mask)
    59 {
    84 {
    60     struct sock_tcp *sock = SOCK_FROM_BASE(base_sock, struct sock_tcp);
    85     struct sock_tcp *sock = SOCK_FROM_BASE(base_sock, struct sock_tcp);
    61     
    86 
       
    87     // just add the appropraite events
       
    88     if (mask & EV_READ && event_add(sock->ev_read, NULL))
       
    89         return SET_ERROR(SOCK_TCP_ERR(sock), ERR_EVENT_ADD);
       
    90  
       
    91     if (mask & EV_WRITE && event_add(sock->ev_write, NULL))
       
    92         return SET_ERROR(SOCK_TCP_ERR(sock), ERR_EVENT_ADD);
       
    93     
       
    94     // done
    62     return SUCCESS;
    95     return SUCCESS;
    63 }
    96 }
    64 
    97 
    65 /*
    98 /*
    66  * Our sock_stream_type
    99  * Our sock_stream_type
    70     .methods.write          = &sock_tcp_write,
   103     .methods.write          = &sock_tcp_write,
    71     .methods.event_init     = &sock_tcp_event_init,
   104     .methods.event_init     = &sock_tcp_event_init,
    72     .methods.event_enable   = &sock_tcp_event_enable,
   105     .methods.event_enable   = &sock_tcp_event_enable,
    73 };
   106 };
    74 
   107 
    75 /*
       
    76  * Our basic socket event handler for driving our callbacks
       
    77  */
       
    78 static void sock_tcp_event (evutil_socket_t fd, short what, void *arg) 
       
    79 {
       
    80     struct sock_tcp *sock = arg;
       
    81 
       
    82     // invoke appropriate callback
       
    83     if (what & EV_READ && SOCK_TCP_BASE(sock)->cb_info->on_read)
       
    84         SOCK_TCP_BASE(sock)->cb_info->on_read(SOCK_TCP_BASE(sock), SOCK_TCP_BASE(sock)->cb_arg);
       
    85 
       
    86     if (what & EV_WRITE && SOCK_TCP_BASE(sock)->cb_info->on_write)
       
    87         SOCK_TCP_BASE(sock)->cb_info->on_read(SOCK_TCP_BASE(sock), SOCK_TCP_BASE(sock)->cb_arg);
       
    88 }
       
    89 
       
    90 err_t sock_tcp_alloc (struct sock_tcp **sock_ptr)
   108 err_t sock_tcp_alloc (struct sock_tcp **sock_ptr)
    91 {
   109 {
    92     // alloc
   110     // alloc
    93     if ((*sock_ptr = calloc(1, sizeof(**sock_ptr))) == NULL)
   111     if ((*sock_ptr = calloc(1, sizeof(**sock_ptr))) == NULL)
    94         return ERR_CALLOC;
   112         return ERR_CALLOC;
   114 
   132 
   115 err_t sock_tcp_init_ev (struct sock_tcp *sock, void (*ev_cb)(evutil_socket_t, short, void *), void *cb_arg)
   133 err_t sock_tcp_init_ev (struct sock_tcp *sock, void (*ev_cb)(evutil_socket_t, short, void *), void *cb_arg)
   116 {
   134 {
   117     // require valid fd
   135     // require valid fd
   118     assert(sock->fd >= 0);
   136     assert(sock->fd >= 0);
       
   137 
       
   138     // this is initialization
       
   139     assert(sock->ev_read == NULL && sock->ev_write == NULL);
   119     
   140     
   120     // create new event
   141     // create new event
   121     if ((sock->ev = event_new(_sock_stream_ctx.ev_base, sock->fd, EV_READ, ev_cb, cb_arg)) == NULL)
   142     if ((sock->ev_read = event_new(_sock_stream_ctx.ev_base, sock->fd, EV_READ, ev_cb, cb_arg)) == NULL)
       
   143         return SET_ERROR(SOCK_TCP_ERR(sock), ERR_EVENT_NEW);
       
   144 
       
   145     if ((sock->ev_write = event_new(_sock_stream_ctx.ev_base, sock->fd, EV_WRITE, ev_cb, cb_arg)) == NULL)
   122         return SET_ERROR(SOCK_TCP_ERR(sock), ERR_EVENT_NEW);
   146         return SET_ERROR(SOCK_TCP_ERR(sock), ERR_EVENT_NEW);
   123 
   147 
   124     // ok
   148     // ok
   125     return SUCCESS;
   149     return SUCCESS;
   126 }
   150 }