src/transport.c
branchnew-transport
changeset 155 c59d3eaff0fb
parent 154 f4472119de3b
child 156 6534a4ac957b
equal deleted inserted replaced
154:f4472119de3b 155:c59d3eaff0fb
     1 #include "transport_internal.h"
     1 #include "transport_internal.h"
     2 
     2 
       
     3 #include <assert.h>
     3 
     4 
     4 void transport_bind (transport_t *transport, const struct transport_type *type, const struct transport_info *info)
     5 void transport_init (transport_t *transport, const struct transport_type *type, const struct transport_info *info)
     5 {
     6 {
     6     // not already bound
     7     // not already bound
     7     assert(!transport->type && !transport->info);
     8     assert(!transport->type);
     8 
     9 
     9     // store
    10     // store
    10     transport->type = type;
    11     transport->type = type;
    11     *transport->info = info;
    12     transport->info = *info;
    12 }
    13 }
    13 
    14 
    14 void* transport_check (transport_t *transport, const struct transport_type *type)
    15 void* transport_check (transport_t *transport, const struct transport_type *type)
    15 {
    16 {
    16     // sanity check
    17     // sanity check
    17     assert(type && transport->type == type);
    18     assert(type && transport->type == type);
    18 
    19 
    19     // ok
    20     // ok
    20     return transport;
    21     return transport;
    21 }
    22 }
       
    23 
       
    24 void transport_connected (transport_t *transport, const error_t *err, bool direct)
       
    25 {
       
    26     // update state
       
    27     transport->connected = true;
       
    28 
       
    29     if (direct || !transport->type->methods._connected) {
       
    30         // user callback
       
    31         if (err)
       
    32             // connect failed
       
    33             transport->info.cb_tbl->on_error(transport, err, transport->info.cb_arg);
       
    34         else
       
    35             // connect succesfull
       
    36             transport->info.cb_tbl->on_connect(transport, transport->info.cb_arg);
       
    37 
       
    38     } else {
       
    39         // wrapper method
       
    40         transport->type->methods._connected(transport, err);
       
    41     }
       
    42 }
       
    43 
       
    44 void transport_invoke (transport_t *transport, short what)
       
    45 {
       
    46     // on_ready
       
    47     if (what & TRANSPORT_READ && transport->info.cb_tbl->on_read)
       
    48         transport->info.cb_tbl->on_read(transport, transport->info.cb_arg);
       
    49 
       
    50     // on_write
       
    51     if (what & TRANSPORT_WRITE && transport->info.cb_tbl->on_write)
       
    52         transport->info.cb_tbl->on_write(transport, transport->info.cb_arg);
       
    53 
       
    54 }
       
    55 
       
    56 void transport_error (transport_t *transport, const error_t *err)
       
    57 {
       
    58     // update state
       
    59     transport->connected = false;
       
    60 
       
    61     // invoke callback
       
    62     transport->info.cb_tbl->on_error(transport, err, transport->info.cb_arg);
       
    63 }