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