src/sock_internal.h
branchnew-transport
changeset 155 c59d3eaff0fb
parent 139 55b9dcc2b73a
equal deleted inserted replaced
154:f4472119de3b 155:c59d3eaff0fb
     5  * @file
     5  * @file
     6  *
     6  *
     7  * internal sock_* interface
     7  * internal sock_* interface
     8  */
     8  */
     9 #include "sock.h"
     9 #include "sock.h"
    10 #include <stdbool.h>
       
    11 
       
    12 /**
       
    13  * General state for all sock_stream's
       
    14  */
       
    15 struct sock_stream_ctx {
       
    16     /** libevent core */
       
    17     struct event_base *ev_base;
       
    18 
       
    19 };
       
    20 
    10 
    21 /**
    11 /**
    22  * Global sock_stream_ctx used for sock_init() and all sock_stream's
    12  * Global sock_stream_ctx used for sock_init() and all sock_stream's
    23  */
    13  */
    24 extern struct sock_stream_ctx _sock_stream_ctx;
    14 extern struct sock_stream_ctx {
       
    15     /** libevent core */
       
    16     struct event_base *ev_base;
    25 
    17 
    26 /**
    18 } _sock_stream_ctx;
    27  * Socket implementation type methods
       
    28  */
       
    29 struct sock_stream_methods {
       
    30     /** As read(2) */
       
    31     err_t (*read) (struct sock_stream *sock, void *buf, size_t *len, struct error_info *err);
       
    32 
       
    33     /** As write(2) */
       
    34     err_t (*write) (struct sock_stream *sock, const void *buf, size_t *len, struct error_info *err);
       
    35 
       
    36     /** Initialize events. cb_info/cb_arg are already updated */
       
    37     err_t (*event_init) (struct sock_stream *sock);
       
    38 
       
    39     /** Enable events as specified */
       
    40     err_t (*event_enable) (struct sock_stream *sock, short mask);
       
    41     
       
    42     /** Release all resources and free the sock_stream */
       
    43     void (*release) (struct sock_stream *sock);
       
    44 
       
    45     /** The type's connect_cb handler, defaults to just invoke conn_cb_func */
       
    46     void (*_conn_cb) (struct sock_stream *sock, struct error_info *err);
       
    47 };
       
    48 
       
    49 /**
       
    50  * The base type struct, which defines the method table.
       
    51  */
       
    52 struct sock_stream_type {
       
    53     /** Method table */
       
    54     struct sock_stream_methods methods;
       
    55 };
       
    56 
       
    57 /**
       
    58  * The base sock_stream type, as used by the sock_stream_* functions.
       
    59  *
       
    60  * The specific implementations should embed this at the start of their type-specific struct, and then cast around
       
    61  * as appropriate.
       
    62  */
       
    63 struct sock_stream {
       
    64     /** The sock_stream_type for this socket */
       
    65     struct sock_stream_type *type;
       
    66 
       
    67     /** Last error info, XXX: remove this */
       
    68     struct error_info err;
       
    69 
       
    70     /** Callbacks */
       
    71     const struct sock_stream_callbacks *cb_info;
       
    72 
       
    73     /** Callback arg */
       
    74     void *cb_arg;
       
    75 
       
    76     /** Connection callback function */
       
    77     sock_stream_connect_cb conn_cb_func;
       
    78 
       
    79     /** Connection callback context argument */
       
    80     void *conn_cb_arg;
       
    81 };
       
    82 
       
    83 /**
       
    84  * Convert a `struct sock_stream*` to the given type.
       
    85  */
       
    86 #define SOCK_FROM_BASE(sock, type) ((type*) sock)
       
    87 
       
    88 /**
       
    89  * Get a pointer to the sock_stream's error_info field.
       
    90  */
       
    91 #define SOCK_ERR(sock) (&(sock)->err)
       
    92 
       
    93 /**
       
    94  * Initialize a sock_stream with the given sock_stream_type.
       
    95  *
       
    96  * The sock_stream should be initialized to zero. It is a bug to call this twice.
       
    97  *
       
    98  * @param sock the new sock_stream
       
    99  * @param type the sock_stream_type defining the implementation used
       
   100  * @param cb_func the optional connect_async callback function
       
   101  * @param cb_arg the optional context argument for cb_func
       
   102  */
       
   103 void sock_stream_init (struct sock_stream *sock, struct sock_stream_type *type, sock_stream_connect_cb cb_func, void *cb_arg);
       
   104 
       
   105 /**
       
   106  * Invoke the appropriate callbacks for the given EV_* bitmask.
       
   107  *
       
   108  * @param sock the sock_stream
       
   109  * @param what combination of EV_* bits describing what callbacks to invoke
       
   110  */
       
   111 void sock_stream_invoke_callbacks (struct sock_stream *sock, short what);
       
   112 
       
   113 /**
       
   114  * Invoke the sock_stream_conn_cb callback with the given error param.
       
   115  *
       
   116  * This invokes the sock_stream_methods::_conn_cb if present and \a direct is not given, otherwise the callback directly
       
   117  *
       
   118  * @param direct force the conn_cb to be called directly
       
   119  */
       
   120 void sock_stream_invoke_conn_cb (struct sock_stream *sock, struct error_info *err, bool direct);
       
   121 
    19 
   122 #endif /* SOCK_INTERNAL_H */
    20 #endif /* SOCK_INTERNAL_H */