forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinger.h
107 lines (94 loc) · 3.77 KB
/
Pinger.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
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
/* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef Pinger_H
#define Pinger_H
#include "benc/String.h"
#include "memory/Allocator.h"
#include "util/log/Log.h"
#include "crypto/random/Random.h"
#include "util/events/EventBase.h"
#include "util/Linker.h"
Linker_require("util/Pinger.c");
/**
* On pong received callback.
* This is called when a response to the ping comes in.
*
* @param data the response content or NULL if the ping timed out, if Pinger_ping() was called with
* null, this will be an empty string.
* @param milliseconds the number of milliseconds between the sending of the ping and this event.
* @param context the context which is set in the Pinger_Ping struct returned by Pinger_ping().
*/
#define Pinger_ON_RESPONSE(x) \
void (* x)(String* data, uint32_t milliseconds, void* context)
/**
* Sender callback.
* The pinger uses this user provided function to send the ping.
* This function does not return, in the event of error, it is the user's responsibility to
* communicate the error through the context back to the function which called Pinger_ping().
*
* @param data the content of the ping to send.
* @param context the context which is set in the Pinger_Ping struct returned by Pinger_ping().
*/
#define Pinger_SEND_PING(x) void (* x)(String* data, void* context)
struct Pinger;
struct Pinger_Ping
{
/**
* The allocator which is set by Pinger_ping() and can be used to
* allocate space which will be freed when the ping completes.
*/
struct Allocator* pingAlloc;
/** How the ping will be identified on the wire. */
uint32_t handle;
/**
* This is NULL by default and is set by the caller of Pinger_ping(),
* when sendPing() and onResponse() are called, whatever this is, will be passed to them.
*/
void* context;
};
/**
* @param data this is the bytes that you want to be reflected back to you.
* @param onResponse this function will be called when the ping is responded to or times out.
* @param sendPing the function which will be called to send the ping out to the other node.
* @param timeoutMilliseconds the number of milliseconds to wait before timeout.
* @param allocator cancel the ping by freeing this allocator.
* @param pinger
* @return a new Pinger_Ping if all goes well, NULL if there is no space.
*/
struct Pinger_Ping* Pinger_newPing(String* data,
Pinger_ON_RESPONSE(onResponse),
Pinger_SEND_PING(sendPing),
uint32_t timeoutMilliseconds,
struct Allocator* allocator,
struct Pinger* pinger);
/**
* Function to call when data comes in which appears to be a ping response.
*
* @param data the data as it comes in.
* @param pinger the pinger context.
*/
void Pinger_pongReceived(String* data, struct Pinger* pinger);
/**
* Create a new pinger.
*
* @param eventBase
* @param logger
* @param alloc
*/
struct Pinger* Pinger_new(struct EventBase* eventBase,
struct Random* rand,
struct Log* logger,
struct Allocator* alloc);
#endif