~ubuntu-branches/ubuntu/karmic/gtk-gnutella/karmic

« back to all changes in this revision

Viewing changes to src/core/tx.c

  • Committer: Bazaar Package Importer
  • Author(s): Anand Kumria
  • Date: 2005-08-04 11:32:05 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050804113205-q746i4lgo3rtlegn
Tags: 0.95.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: tx.c,v 1.9 2005/05/29 08:48:36 graaff Exp $
 
3
 *
 
4
 * Copyright (c) 2002-2003, Raphael Manfredi
 
5
 *
 
6
 *----------------------------------------------------------------------
 
7
 * This file is part of gtk-gnutella.
 
8
 *
 
9
 *  gtk-gnutella is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  gtk-gnutella is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with gtk-gnutella; if not, write to the Free Software
 
21
 *  Foundation, Inc.:
 
22
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 *----------------------------------------------------------------------
 
24
 */
 
25
 
 
26
/**
 
27
 * @ingroup core
 
28
 * @file
 
29
 *
 
30
 * Network TX drivers.
 
31
 *
 
32
 * This file is the "ancestor" class of all TX drivers, and therefore only
 
33
 * implements general routines that are mostly common, as well as provides
 
34
 * type-checked entry points for dynamically dispatched routines, such
 
35
 * as tx_write().
 
36
 *
 
37
 * @author Raphael Manfredi
 
38
 * @date 2002-2003
 
39
 */
 
40
 
 
41
#include "common.h"
 
42
 
 
43
RCSID("$Id: tx.c,v 1.9 2005/05/29 08:48:36 graaff Exp $");
 
44
 
 
45
#include "tx.h"
 
46
#include "lib/override.h"       /* Must be the last header included */
 
47
 
 
48
/*
 
49
 * Dynamic dispatch of polymorphic routines.
 
50
 */
 
51
 
 
52
#define TX_INIT(o,a)            ((o)->ops->init((o), (a)))
 
53
#define TX_DESTROY(o)           ((o)->ops->destroy((o)))
 
54
#define TX_WRITE(o,d,l)         ((o)->ops->write((o), (d), (l)))
 
55
#define TX_WRITEV(o,i,c)        ((o)->ops->writev((o), (i), (c)))
 
56
#define TX_SENDTO(o,t,d,l)      ((o)->ops->sendto((o), (t), (d), (l)))
 
57
#define TX_ENABLE(o)            ((o)->ops->enable((o)))
 
58
#define TX_DISABLE(o)           ((o)->ops->disable((o)))
 
59
#define TX_PENDING(o)           ((o)->ops->pending((o)))
 
60
#define TX_BIO_SOURCE(o)        ((o)->ops->bio_source((o)))
 
61
#define TX_FLUSH(o)                     ((o)->ops->flush((o)))
 
62
 
 
63
/**
 
64
 * Create a new network driver, equipped with the `ops' operations and
 
65
 * initialize its specific parameters by calling the init routine with `args'.
 
66
 *
 
67
 * @return NULL if there is an initialization problem.
 
68
 */
 
69
txdrv_t *
 
70
tx_make(struct gnutella_node *n, const struct txdrv_ops *ops, gpointer args)
 
71
{
 
72
        txdrv_t *tx;
 
73
 
 
74
        g_assert(n);
 
75
        g_assert(ops);
 
76
 
 
77
        tx = g_malloc0(sizeof(*tx));
 
78
 
 
79
        tx->node = n;
 
80
        tx->ops = ops;
 
81
 
 
82
        if (NULL == TX_INIT(tx, args))          /* Let the heir class initialize */
 
83
                return NULL;
 
84
 
 
85
        return tx;
 
86
}
 
87
 
 
88
/**
 
89
 * Dispose of the driver resources.
 
90
 */
 
91
void
 
92
tx_free(txdrv_t *tx)
 
93
{
 
94
        g_assert(tx);
 
95
 
 
96
        TX_DESTROY(tx);
 
97
        g_free(tx);
 
98
}
 
99
 
 
100
/**
 
101
 * Write `len' bytes starting at `data'.
 
102
 *
 
103
 * @return the amount of bytes written, or -1 with errno set on error.
 
104
 */
 
105
ssize_t
 
106
tx_write(txdrv_t *tx, gpointer data, size_t len)
 
107
{
 
108
        return TX_WRITE(tx, data, len);
 
109
}
 
110
 
 
111
/**
 
112
 * Write I/O vector.
 
113
 *
 
114
 * @return amount of bytes written, or -1 on error with errno set.
 
115
 */
 
116
ssize_t
 
117
tx_writev(txdrv_t *tx, struct iovec *iov, gint iovcnt)
 
118
{
 
119
        return TX_WRITEV(tx, iov, iovcnt);
 
120
}
 
121
 
 
122
/**
 
123
 * Send buffer datagram to specified destination `to'.
 
124
 *
 
125
 * @return amount of bytes written, or -1 on error with errno set.
 
126
 */
 
127
ssize_t
 
128
tx_sendto(txdrv_t *tx, gnet_host_t *to, gpointer data, size_t len)
 
129
{
 
130
        return TX_SENDTO(tx, to, data, len);
 
131
}
 
132
 
 
133
/**
 
134
 * Register service routine from upper TX layer.
 
135
 */
 
136
void
 
137
tx_srv_register(txdrv_t *tx, tx_service_t srv_fn, gpointer srv_arg)
 
138
{
 
139
        g_assert(tx);
 
140
        g_assert(srv_fn);
 
141
 
 
142
        tx->srv_routine = srv_fn;
 
143
        tx->srv_arg = srv_arg;
 
144
}
 
145
 
 
146
/**
 
147
 * Record that upper layer wants its service routine enabled.
 
148
 */
 
149
void
 
150
tx_srv_enable(txdrv_t *tx)
 
151
{
 
152
        if (tx->flags & TX_SERVICE)             /* Already enabled */
 
153
                return;
 
154
 
 
155
        TX_ENABLE(tx);
 
156
        tx->flags |= TX_SERVICE;
 
157
}
 
158
 
 
159
/**
 
160
 * Record that upper layer wants its service routine disabled.
 
161
 */
 
162
void
 
163
tx_srv_disable(txdrv_t *tx)
 
164
{
 
165
        g_assert(tx->flags & TX_SERVICE);
 
166
 
 
167
        TX_DISABLE(tx);
 
168
        tx->flags &= ~TX_SERVICE;
 
169
}
 
170
 
 
171
/**
 
172
 * @return amount of data pending in the whole stack.
 
173
 */
 
174
size_t
 
175
tx_pending(txdrv_t *tx)
 
176
{
 
177
        g_assert(tx);
 
178
 
 
179
        return TX_PENDING(tx);
 
180
}
 
181
 
 
182
/**
 
183
 * The I/O source of the lowest layer (link) that physically sends
 
184
 * the information.
 
185
 */
 
186
struct bio_source *
 
187
tx_bio_source(txdrv_t *tx)
 
188
{
 
189
        g_assert(tx);
 
190
 
 
191
        return TX_BIO_SOURCE(tx);
 
192
}
 
193
 
 
194
/**
 
195
 * Request that data be sent immediately.
 
196
 */
 
197
void
 
198
tx_flush(txdrv_t *tx)
 
199
{
 
200
        g_assert(tx);
 
201
 
 
202
        TX_FLUSH(tx);
 
203
}
 
204
 
 
205
/**
 
206
 * The write() operation is forbidden.
 
207
 */
 
208
ssize_t
 
209
tx_no_write(txdrv_t *unused_tx, gpointer unused_data, size_t unused_len)
 
210
{
 
211
        (void) unused_tx;
 
212
        (void) unused_data;
 
213
        (void) unused_len;
 
214
        g_error("no write() operation allowed");
 
215
        errno = ENOENT;
 
216
        return -1;
 
217
}
 
218
 
 
219
/**
 
220
 * The writev() operation is forbidden.
 
221
 */
 
222
ssize_t
 
223
tx_no_writev(txdrv_t *unused_tx, struct iovec *unused_iov, gint unused_iovcnt)
 
224
{
 
225
        (void) unused_tx;
 
226
        (void) unused_iov;
 
227
        (void) unused_iovcnt;
 
228
        g_error("no writev() operation allowed");
 
229
        errno = ENOENT;
 
230
        return -1;
 
231
}
 
232
 
 
233
/**
 
234
 * The sendto() operation is forbidden.
 
235
 */
 
236
ssize_t
 
237
tx_no_sendto(txdrv_t *unused_tx, gnet_host_t *unused_to,
 
238
                gpointer unused_data, size_t unused_len)
 
239
{
 
240
        (void) unused_tx;
 
241
        (void) unused_to;
 
242
        (void) unused_data;
 
243
        (void) unused_len;
 
244
        g_error("no sendto() operation allowed");
 
245
        errno = ENOENT;
 
246
        return -1;
 
247
}
 
248
 
 
249
/* vi: set ts=4 sw=4 cindent: */