aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver/client-list.h
blob: 4d2bfa798b20e3380b14bde8e1192aff9edb0045 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 * mds — A micro-display server
 * Copyright © 2014, 2015, 2016, 2017  Mattias Andrée (maandree@kth.se)
 * 
 * This program is free software: you can redistribute it 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 MDS_LIBMDSSERVER_CLIENT_LIST_H
#define MDS_LIBMDSSERVER_CLIENT_LIST_H


#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>



#define CLIENT_LIST_T_VERSION 0

/**
 * Dynamic array of client ID:s
 */
typedef struct client_list
{
	/**
	 * The size of the array
	 */
	size_t capacity;

	/**
	 * The index after the last used index
	 */
	size_t size;

	/**
	 * Stored client ID:s
	 */
	uint64_t *clients;

} client_list_t;



/**
 * Create a client list
 * 
 * @param   this      Memory slot in which to store the new client list
 * @param   capacity  The minimum initial capacity of the client list, 0 for default
 * @return            Non-zero on error, `errno` will have been set accordingly
 */
__attribute__((nonnull))
int client_list_create(client_list_t *restrict this, size_t capacity);

/**
 * Release all resources in a client list, should
 * be done even if `client_list_create` fails
 * 
 * @param  this  The client list
 */
__attribute__((nonnull))
void client_list_destroy(client_list_t *restrict this);

/**
 * Clone a client list
 * 
 * @param   this  The client list to clone
 * @param   out   Memory slot in which to store the new client list
 * @return        Non-zero on error, `errno` will have been set accordingly
 */
__attribute__((nonnull))
int client_list_clone(const client_list_t *restrict this, client_list_t *restrict out);

/**
 * Add a client to the list
 * 
 * @param   this    The list
 * @param   client  The client to add
 * @return          Non-zero on error, `errno` will be set accordingly
 */
__attribute__((nonnull))
int client_list_add(client_list_t *restrict this, uint64_t client);

/**
 * Remove a client from the list, once
 * 
 * @param  this    The list
 * @param  client  The client to remove
 */
__attribute__((nonnull))
void client_list_remove(client_list_t *restrict this, uint64_t client);

/**
 * Calculate the buffer size need to marshal a client list
 * 
 * @param   this  The list
 * @return        The number of bytes to allocate to the output buffer
 */
__attribute__((pure, nonnull))
size_t client_list_marshal_size(const client_list_t *restrict this);

/**
 * Marshals a client list
 * 
 * @param  this  The list
 * @param  data  Output buffer for the marshalled data
 */
__attribute__((nonnull))
void client_list_marshal(const client_list_t *restrict this, char *restrict data);

/**
 * Unmarshals a client list
 * 
 * @param   this  Memory slot in which to store the new client list
 * @param   data  In buffer with the marshalled data
 * @return        Non-zero on error, `errno` will be set accordingly.
 *                Destroy the list on error.
 */
__attribute__((nonnull))
int client_list_unmarshal(client_list_t *restrict this, char *restrict data);


#endif