Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bd72dede6acc1057deb681f6ddc194bbf3e0648 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2013 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 *
 *******************************************************************************/

/**
 * \file etTcpSockets.h
 *
 * abstraction of a TCP/IP socket client and server
 *
 * \author Henrik Rentz-Reichert
 */

#ifndef _ETTCPSOCKETS_H_
#define _ETTCPSOCKETS_H_

#include "etDatatypes.h"

ET_EXTERN_C_BEGIN

/** the maximum number of connections per server */
#define MAX_CONNECTIONS		32

/** function prototype for receiving data from a socket */
typedef int (*etSocketReceiver)(void* self, int channel, int size, const int8* data);
/** function prototype for a function returning a writable buffer */
typedef int8* (*etBufferProvider)(void* self, int* size);

/**
 * The public part of the server data.
 * Data passed to the API methods <b>must</b> be created using
 * {@link etCreateSocketServerData()}
 */
typedef struct etSocketServerData {
	etSocketReceiver receiver;
	etBufferProvider bufferProvider;
	void* userData;
	int maxConnections;
}
etSocketServerData;

/**
 * The public part of the connection data.
 * Data passed to the API methods <b>must</b> be created using
 * {@link etCreateSocketConnectionData()}
 */
typedef struct etSocketConnectionData {
	etSocketReceiver receiver;
	etBufferProvider bufferProvider;
	void* userData;
}
etSocketConnectionData;

/** error codes for API functions */
typedef enum {
	ETSOCKET_OK,
	ETSOCKET_ERROR
}
etSocketError;


/*
 * general
 */

/**
 * to be called before working with sockets.
 * can be called multiple times, but must be aligned with etCleanupSockets
 */
etSocketError etInitSockets();

/**
 * to be called once after working with sockets
 */
etSocketError etCleanupSockets();

/*
 * server side
 */

/**
 * @return a struct with the server data allocated from the heap
 */
etSocketServerData* etCreateSocketServerData();

/**
 * @param data the previously allocated data to be freed
 */
void etFreeSocketServerData(etSocketServerData* data);

/**
 * starts a thread listening to a port
 * @param self the server data
 * @param port which is listened to
 * @return an error code of type {@link etSocketError}
 */
etSocketError etStartListening(etSocketServerData* self, short port);

/**
 * stops listening on the socket but leaves connections untouched
 * @param self the server data
 * @return an error code of type {@link etSocketError}
 */
etSocketError etStopSocketServer(etSocketServerData* self);

/**
 * write to an established connection of the server side
 * @param self the server data
 * @param connection the connection to which the data should be sent
 * @param size the size of the data in bytes
 * @param data the data to be sent
 * @return an error code of type {@link etSocketError}
 */
etSocketError etWriteServerSocket(etSocketServerData* self, int connection, int size, const int8* data);

/**
 * close a server connection (closes the socket and stops its thread)
 * @param self the server data
 * @param connection the connection to be stopped
 * @return an error code of type {@link etSocketError}
 */
etSocketError etCloseServerSocket(etSocketServerData* self, int connection);

/**
 * close all server connections (closes the sockets and stops their threads)
 * @param self the server data
 * @return an error code of type {@link etSocketError}
 */
etSocketError etCloseAllServerSockets(etSocketServerData* self);

/*
 * client side
 */

/**
 * @return a struct with the client data allocated from the heap
 */
etSocketConnectionData* etCreateSocketConnectionData();

/**
 * @param data the previously allocated data to be freed
 */
void etFreeSocketConnectionData(etSocketConnectionData* data);

/**
 * connect a socket server
 * @param addr the internet address given as quadrupel, if <code>NULL</code> then local host is assumed
 * @param port the port to which the connection should be established
 * @param self the client data
 * @return an error code of type {@link etSocketError}
 */
etSocketError etConnectServer(etSocketConnectionData* self, const char* addr, short port);

/**
 * write to an established client connection
 * @param self the client data
 * @param size the size of the data in bytes
 * @param data the data to be sent
 * @return an error code of type {@link etSocketError}
 */
etSocketError etWriteSocket(etSocketConnectionData* self, int size, const int8* data);

/**
 * close a client connection (closes the socket and stops the thread)
 * @param self the client data
 * @return an error code of type {@link etSocketError}
 */
etSocketError etCloseSocket(etSocketConnectionData* self);

ET_EXTERN_C_END

#endif /* _ETTCPSOCKETS_H_ */

Back to the top