1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* Copyright (c) 1997-1998 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Default netio object implementation.
* Besides providing a (slightly) useful service,
* this module serves as an example of how to implement a netio object.
*/
#include <oskit/io/netio.h>
#include <oskit/c/string.h>
#include <oskit/c/stdlib.h>
#include <oskit/c/malloc.h>
#include <oskit/c/assert.h>
struct client_netio {
oskit_netio_t ioi; /* COM I/O Interface */
unsigned count; /* reference count */
oskit_error_t (*func)(void *data, oskit_bufio_t *b, oskit_size_t size);
void *data;
void (*cleanup)(void *data); /* cleanup destructor */
};
typedef struct client_netio client_netio_t;
/*
* Query a net I/O object for its interfaces.
* This is extremely easy because we only export one interface
* (plus its base type, IUnknown).
*/
static OSKIT_COMDECL
net_query(oskit_netio_t *io, const oskit_iid_t *iid, void **out_ihandle)
{
struct client_netio *po = (struct client_netio *)io;
assert(po != NULL);
assert(po->count != 0);
if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
memcmp(iid, &oskit_netio_iid, sizeof(*iid)) == 0) {
*out_ihandle = &po->ioi;
++po->count;
return 0;
}
*out_ihandle = NULL;
return OSKIT_E_NOINTERFACE;
}
/*
* Clone a reference to a netio object
*/
static OSKIT_COMDECL_U
net_addref(oskit_netio_t *io)
{
struct client_netio *po = (struct client_netio *)io;
assert(po != NULL);
assert(po->count != 0);
return ++po->count;
}
/*
* Close ("release") a netio object.
*/
static OSKIT_COMDECL_U
net_release(oskit_netio_t *io)
{
struct client_netio *po = (struct client_netio *)io;
unsigned newcount;
assert(po != NULL);
assert(po->count != 0);
newcount = po->count - 1;
if (newcount == 0) {
if (po->cleanup)
po->cleanup(po->data);
free(po);
return 0;
}
return po->count = newcount;
}
/*
* Receive a packet and forward it on to the callback function.
*/
static OSKIT_COMDECL
net_push(oskit_netio_t *ioi, oskit_bufio_t *b, oskit_size_t pkt_size)
{
struct client_netio *po = (struct client_netio *)ioi;
assert(po != NULL);
assert(po->count != 0);
return po->func(po->data, b, pkt_size);
}
static
struct oskit_netio_ops client_netio_ops = {
net_query,
net_addref,
net_release,
net_push
};
oskit_netio_t *
oskit_netio_create_cleanup(oskit_error_t (*func)(void *data, oskit_bufio_t *b,
oskit_size_t pkt_size),
void *data, void (*destructor)(void *data))
{
struct client_netio *c;
c = (struct client_netio *)malloc(sizeof(*c));
if (c == NULL)
return NULL;
c->ioi.ops = &client_netio_ops;
c->count = 1;
c->func = func;
c->data = data;
c->cleanup = destructor;
return &c->ioi;
}
oskit_netio_t *
oskit_netio_create(oskit_error_t (*func)(void *data, oskit_bufio_t *b,
oskit_size_t pkt_size),
void *data)
{
return oskit_netio_create_cleanup(func, data, NULL);
}
|