#include "transport_internal.h"
#include <assert.h>
/*
* Internal API
*/
void transport_init (transport_t *transport, const struct transport_type *type, const struct transport_info *info)
{
// not already bound
assert(!transport->type);
// store
transport->type = type;
if (info)
transport->info = *info;
}
void* transport_check (transport_t *transport, const struct transport_type *type)
{
const struct transport_type *tp_type;
// sanity check
assert(type);
// look for a matching type in the transport's type list
for (tp_type = transport->type; tp_type; tp_type = tp_type->parent)
if (tp_type == type)
break;
// make sure we found one
assert(tp_type);
// ok
return transport;
}
void transport_connected (transport_t *transport, const error_t *err, bool direct)
{
// update state
transport->connected = true;
if (direct || !transport->type->methods._connected) {
// user callback
if (err)
// connect failed
transport->info.cb_tbl->on_error(transport, err, transport->info.cb_arg);
else
// connect succesfull
transport->info.cb_tbl->on_connect(transport, transport->info.cb_arg);
} else {
// wrapper method
transport->type->methods._connected(transport, err);
}
}
void transport_invoke (transport_t *transport, short what)
{
// on_ready
if (what & TRANSPORT_READ && transport->info.cb_tbl->on_read)
transport->info.cb_tbl->on_read(transport, transport->info.cb_arg);
// on_write
if (what & TRANSPORT_WRITE && transport->info.cb_tbl->on_write)
transport->info.cb_tbl->on_write(transport, transport->info.cb_arg);
}
void transport_error (transport_t *transport, const error_t *err)
{
// update state
transport->connected = false;
// invoke callback
transport->info.cb_tbl->on_error(transport, err, transport->info.cb_arg);
}
/*
* Public API
*/
int transport_read (transport_t *transport, void *buf, size_t len, error_t *err)
{
// not readable
if (!transport->type->methods.read)
return SET_ERROR(err, -1);
// proxy off to method handler
if (transport->type->methods.read(transport, buf, &len, err))
return -ERROR_CODE(err);
// return updated 'bytes-read' len
return len;
}
int transport_write (transport_t *transport, const void *buf, size_t len, error_t *err)
{
// XXX: not writeable
if (!transport->type->methods.write)
return SET_ERROR(err, -1);
// proxy off to method handler
if (transport->type->methods.write(transport, buf, &len, err))
return -ERROR_CODE(err);
// return updated 'bytes-written' len
return len;
}
err_t transport_events (transport_t *transport, short mask)
{
error_t err;
// notify transport
if (transport->type->methods.events) {
if (transport->type->methods.events(transport, mask, &err))
goto error;
}
// update the event mask
transport->info.ev_mask = mask;
// ok
return SUCCESS;
error:
return ERROR_CODE(&err);
}
void transport_set_callbacks (transport_t *transport, const struct transport_callbacks *cb_tbl, void *cb_arg)
{
transport->info.cb_tbl = cb_tbl;
transport->info.cb_arg = cb_arg;
}
void transport_destroy (transport_t *transport)
{
// destroy the transport-specific stuff
if (transport->type->methods.destroy)
transport->type->methods.destroy(transport);
// then the transport itself
free(transport);
}