source: branches/1.4x/libtransmission/iobuf.h @ 7403

Last change on this file since 7403 was 7403, checked in by charles, 12 years ago

(1.4x libT) probable fix for the "greedy peer" bug

File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2002-2004 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
4 *
5 * Transmission modifications and new bugs by Charles Kerr.
6 * Source: libevent's "patches-1.4" branch, svn revision 949
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef __TRANSMISSION__
32#error only libtransmission should #include this header.
33#endif
34
35#ifndef TR_EVBUFFER_H
36#define TR_EVBUFFER_H
37
38/**
39 * tr_iobuf is an i/o convenience wrapper similar to libevent's "bufferevent".
40 * it provides two evbuffers, one for writing and one for reading, and handles
41 * pumping them to/from the given fd.
42 *
43 * this class differs from bufferevent in two major ways:
44 * 1. the action callbacks include the number of bytes transferred
45 * 2. the up/down speeds are directly constrained by our `bandwidth' object
46 * 3. the implementation is hidden in the .c file
47 *
48 * 4. a late addition nasty hack to read/write on demand, called from
49 *    bandwidth. this actually seems to make a lot of this class redundant
50 *    and probably should be refactored.
51 */
52struct tr_iobuf;
53
54struct evbuffer;
55struct tr_bandwidth;
56struct tr_session;
57
58/** @brief returns the input evbuffer to that we can read from */
59struct evbuffer* tr_iobuf_input( struct tr_iobuf * iobuf );
60
61/** @brief returns the output evbuffer that we can write to */
62struct evbuffer* tr_iobuf_output( struct tr_iobuf * iobuf );
63
64/** @brief prototype for the callbacks invoked when bytes have been read or written
65    @see tr_iobuf_new
66    @see tr_iobuf_setcb */
67typedef void (*tr_iobuf_cb)( struct tr_iobuf*, size_t bytes_transferred, void* );
68
69/** @brief prototype for the callback invoked on error
70    @see tr_iobuf_new
71    @see tr_iobuf_setcb */
72typedef void (*tr_iobuf_error_cb)( struct tr_iobuf*, short what, void* );
73
74/** @brief create a new tr_iobuf object. */
75struct tr_iobuf* tr_iobuf_new( struct tr_handle     * session,
76                               struct tr_bandwidth  * bandwidth, 
77                               int                    fd,
78                               short                  event,
79                               tr_iobuf_cb            readcb,
80                               tr_iobuf_cb            writecb,
81                               tr_iobuf_error_cb      errorcb,
82                               void                 * cbarg );
83
84/** @brief destroy a tr_iobuf object. */
85void tr_iobuf_free( struct tr_iobuf * iobuf );
86
87/** @brief change the number of seconds it takes for a read/write to timeout */
88void tr_iobuf_settimeout( struct tr_iobuf  * iobuf,
89                          int                timeout_read,
90                          int                timeout_write );
91
92/** @brief set the bandwidth object that limits this iobuf's read/write speeds */
93void tr_iobuf_set_bandwidth( struct tr_iobuf      * iobuf,
94                             struct tr_bandwidth  * bandwidth );
95
96/** @brief change the callbacks invoked by this iobuf on error and bytes transferred */
97void tr_iobuf_setcb( struct tr_iobuf    * iobuf,
98                     tr_iobuf_cb          readcb,
99                     tr_iobuf_cb          writecb,
100                     tr_iobuf_error_cb    errorcb,
101                     void               * cbarg );
102
103/** @brief tell the iobuf to poll for certain states.
104    @brief event may be EV_READ, EV_WRITE, or EV_READ|EV_WRITE */
105int tr_iobuf_enable( struct tr_iobuf * iobuf, short event );
106
107/** @brief tell the iobuf to stop polling for certain states.
108    @brief event may be EV_READ, EV_WRITE, or EV_READ|EV_WRITE */
109int tr_iobuf_disable( struct tr_iobuf * iobuf, short event );
110
111int tr_iobuf_flush_output_buffer( struct tr_iobuf * iobuf, size_t max );
112
113int tr_iobuf_tryread( struct tr_iobuf * iobuf, size_t max );
114
115#endif
Note: See TracBrowser for help on using the repository browser.