forked from cjdelisle/cjdns
/
Interface.h
80 lines (63 loc) · 2.61 KB
/
Interface.h
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
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Interface_H
#define Interface_H
#include <stdint.h>
#include "wire/Message.h"
#define Interface_ERROR_WRONG_STATE 256
#define Interface_CALLBACK(name) \
uint8_t (* name)(struct Message* message, struct Interface* thisInterface)
#define Interface_CONST_CALLBACK(name) \
uint8_t (* const name)(struct Message* message, struct Interface* thisInterface)
/**
* An interface.
* All interfaces are point-to-point, no addressing is done at this level.
* If you have multiple direct connections (eg nodes in an ethernet),
* you must register an interface for each.
*/
struct Interface
{
/** Arbitarary data which belongs to the wire side of this interface. */
void* const senderContext;
/** The maximum allowable length for a message. */
uint16_t maxMessageLength;
/**
* The number of bytes of padding which must exist *before* the beginning of the message.
* when you call sendMessage, there must be requiredPadding number of bytes of free space before
* the location pointed to by message->bytes.
*/
uint16_t requiredPadding;
/**
* Send a message through this interface.
*
* @return 0 If all goes well, non-zero in case of an error.
* See Error.h for more information about interface error codes.
*/
Interface_CONST_CALLBACK(sendMessage);
/** Used to allocate this interface, the interface will close when this allocator is freed. */
struct Allocator* const allocator;
/** Arbitrary data which belongs to the receiver side of this interface. */
void* receiverContext;
/**
* Recieve a message from this interface.
*
* @param received the incoming message from the other node.
* @param senderContext arbitrary data which must be passed to the receiver.
* @return 0 If all goes well, non-zero in case of an error.
* See Error.h for more information about interface error codes.
*/
Interface_CALLBACK(receiveMessage);
};
#endif