author | Tero Marttila <terom@fixme.fi> |
Sun, 03 May 2009 17:18:16 +0300 | |
branch | new-transport |
changeset 165 | b3e95108c884 |
parent 159 | d3e253d7281a |
child 176 | 6750d50ee8cd |
permissions | -rw-r--r-- |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#ifndef TRANSPORT_INTERNAL_H |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
#define TRANSPORT_INTERNAL_H |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
/** |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
* @file |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
* |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
* The internal interface for transport implementations. |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
*/ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
#include "transport.h" |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
|
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
11 |
#include <stdbool.h> |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
12 |
|
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
/** |
156
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
14 |
* Method table for implementation stuff. |
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
15 |
* |
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
16 |
* Note that it is the transport's resposibility to implement the behaviour described in transport.h |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
*/ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
struct transport_methods { |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
/** For transport_read() */ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
err_t (*read) (transport_t *transport, void *buf, size_t *len, error_t *err); |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
/** For transport_write() */ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
err_t (*write) (transport_t *transport, const void *buf, size_t *len, error_t *err); |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
|
156
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
25 |
/** |
165 | 26 |
* The mask of event flags will be set to the given mask if this method is succesfull. |
156
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
27 |
* |
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
28 |
* The old mask is still available in transport::info::ev_mask. |
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
29 |
*/ |
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
30 |
err_t (*events) (transport_t *transport, short mask, error_t *err); |
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
31 |
|
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
32 |
/** |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
33 |
* Release the transport's internal state, but not the transport itself. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
34 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
35 |
* In other words, this should release everything inside the transport_t, but not free() the transport_t itself. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
36 |
*/ |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
void (*destroy) (transport_t *transport); |
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
38 |
|
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
39 |
/** |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
40 |
* Used by layered transports to handle transport_connected. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
41 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
42 |
* If this is NULL, transport_connected will call the user callback directly, otherwise, it will proxy through this. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
43 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
44 |
* The \a err param follows the same rules as for transport_connected() - NULL for success, error info otherwise. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
45 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
46 |
* @param transport the transport state |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
47 |
* @param err error info if the connect failed |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
48 |
*/ |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
49 |
void (*_connected) (transport_t *transport, const error_t *err); |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
}; |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
/** |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
* The definition of a transport type |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
*/ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
struct transport_type { |
159
d3e253d7281a
implement heirarchial type-checking for transport_check
Tero Marttila <terom@fixme.fi>
parents:
157
diff
changeset
|
56 |
/** Parent type */ |
d3e253d7281a
implement heirarchial type-checking for transport_check
Tero Marttila <terom@fixme.fi>
parents:
157
diff
changeset
|
57 |
const struct transport_type *parent; |
d3e253d7281a
implement heirarchial type-checking for transport_check
Tero Marttila <terom@fixme.fi>
parents:
157
diff
changeset
|
58 |
|
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
/** Method table */ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
struct transport_methods methods; |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
}; |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
/** |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
* The base transport type |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
*/ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
struct transport { |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
/** The type info, or NULL if not yet bound */ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
const struct transport_type *type; |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
/** User info */ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
struct transport_info info; |
156
6534a4ac957b
add transport/sock/line_proto/etc code compiles
Tero Marttila <terom@fixme.fi>
parents:
155
diff
changeset
|
72 |
|
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
73 |
/** Are we connected? */ |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
74 |
bool connected; |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
}; |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
/** |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
* Bind the given transport to the given type with the given user info. |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
* |
157 | 80 |
* \a info may be given as NULL to not have any callbacks, but this will crash if any transport_* is called before |
81 |
* transport_set_callbacks(). |
|
82 |
* |
|
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
* It is a bug to call this with a transport that is already bound. |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
*/ |
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
85 |
void transport_init (transport_t *transport, const struct transport_type *type, const struct transport_info *info); |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
/** |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
* Check the type of the transport, and return the transport as a void* suitable for casting to the appropriate struct |
159
d3e253d7281a
implement heirarchial type-checking for transport_check
Tero Marttila <terom@fixme.fi>
parents:
157
diff
changeset
|
89 |
* for the type, or any of its children. |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
* |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
* It is a bug to call this with a transport of a different type. |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
*/ |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
void* transport_check (transport_t *transport, const struct transport_type *type); |
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
|
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
95 |
/** |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
96 |
* Mark the transport as connected, calling transport_methods::_connected if it exists and \a direct is not given, |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
97 |
* transport_callbacks::on_connected/transport_callbacks::on_error otherwise. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
98 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
99 |
* If the connect succeeded, \a err should be given as NULL. If the connect failed, \a err should contain the error |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
100 |
* info. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
101 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
102 |
* If called from the transport_methods::_connected method, pass in direct to avoid recursion. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
103 |
* |
165 | 104 |
* XXX: This sets the transport::connected flag, regardless of which callback it invokes. |
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
105 |
* |
165 | 106 |
* XXX: implement proper layering of types by taking a transport_type arg and chaining down from there. |
155
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
107 |
* |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
108 |
* @param transport the transport state |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
109 |
* @param err NULL for success, otherwise connect error code |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
110 |
* @param direct call the user callback directly, ignoring any method |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
111 |
*/ |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
112 |
void transport_connected (transport_t *transport, const error_t *err, bool direct); |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
113 |
|
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
114 |
/** |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
115 |
* Invoke the user callbacks based on the given TRANSPORT_* flags |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
116 |
*/ |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
117 |
void transport_invoke (transport_t *transport, short what); |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
118 |
|
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
119 |
/** |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
120 |
* Mark the transport as failed, calling transport_methods::on_error with the given error code. |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
121 |
*/ |
c59d3eaff0fb
most of the new transport/sock code compiles, but things are still missing
Tero Marttila <terom@fixme.fi>
parents:
154
diff
changeset
|
122 |
void transport_error (transport_t *transport, const error_t *err); |
154
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
|
f4472119de3b
initial code towards transport implementation, doesn't compile
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
#endif /* TRANSPORT_INTERNAL_H */ |