summaryrefslogtreecommitdiff
path: root/toxcore/ping_array.h
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-05-12 14:07:03 -0400
committerirungentoo <irungentoo@gmail.com>2014-05-12 14:07:03 -0400
commit10da970e0dfcb21fda4da96635b690065375daff (patch)
treef2459735e1d51906e063b3288b60f41cd287b47f /toxcore/ping_array.h
parent87cec792062a755675e53b5603049cdb067aa70f (diff)
Added ping_array, a special efficient array for use in operations
that require sending ping type packets. Made ping packets use it.
Diffstat (limited to 'toxcore/ping_array.h')
-rw-r--r--toxcore/ping_array.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/toxcore/ping_array.h b/toxcore/ping_array.h
new file mode 100644
index 00000000..b7fff1eb
--- /dev/null
+++ b/toxcore/ping_array.h
@@ -0,0 +1,75 @@
1/* ping_array.h
2 *
3 * Implementation of an efficient array to store that we pinged something.
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox 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 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox 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 Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23#ifndef PING_ARRAY_H
24#define PING_ARRAY_H
25
26#include "network.h"
27
28typedef struct {
29 void *data;
30 uint32_t length;
31 uint64_t time;
32 uint64_t ping_id;
33} Ping_Array_Entry;
34
35
36typedef struct {
37 Ping_Array_Entry *entries;
38
39 uint32_t last_deleted; /* number representing the next entry to be deleted. */
40 uint32_t last_added; /* number representing the last entry to be added. */
41 uint32_t total_size; /* The length of entries */
42 uint32_t timeout; /* The timeout after which entries are cleared. */
43} Ping_Array;
44
45
46/* Add a data with length to the Ping_Array list and return a ping_id.
47 *
48 * return ping_id on success.
49 * return 0 on failure.
50 */
51uint64_t ping_array_add(Ping_Array *array, uint8_t *data, uint32_t length);
52
53/* Check if ping_id is valid and not timed out.
54 *
55 * On success, copies the data into data of length,
56 *
57 * return length of data copied on success.
58 * return -1 on failure.
59 */
60int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t ping_id);
61
62/* Initialize a Ping_Array.
63 * size represents the total size of the array and should be a power of 2.
64 * timeout represents the maximum timeout in seconds for the entry.
65 *
66 * return 0 on success.
67 * return -1 on failure.
68 */
69int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout);
70
71/* Free all the allocated memory in a Ping_Array.
72 */
73void ping_array_free_all(Ping_Array *array);
74
75#endif