echo_server.h
Go to the documentation of this file.
1 /**
2  * @file echo_server.h
3  * @brief Echo server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _ECHO_SERVER_H
32 #define _ECHO_SERVER_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/socket.h"
37 
38 //Echo server support
39 #ifndef ECHO_SERVER_SUPPORT
40  #define ECHO_SERVER_SUPPORT DISABLED
41 #elif (ECHO_SERVER_SUPPORT != ENABLED && ECHO_SERVER_SUPPORT != DISABLED)
42  #error ECHO_SERVER_SUPPORT parameter is not valid
43 #endif
44 
45 //Stack size required to run the Echo server
46 #ifndef ECHO_SERVER_STACK_SIZE
47  #define ECHO_SERVER_STACK_SIZE 500
48 #elif (ECHO_SERVER_STACK_SIZE < 1)
49  #error ECHO_SERVER_STACK_SIZE parameter is not valid
50 #endif
51 
52 //Priority at which the Echo server should run
53 #ifndef ECHO_SERVER_PRIORITY
54  #define ECHO_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
55 #endif
56 
57 //TCP Echo service support
58 #ifndef ECHO_SERVER_TCP_SUPPORT
59  #define ECHO_SERVER_TCP_SUPPORT ENABLED
60 #elif (ECHO_SERVER_TCP_SUPPORT != ENABLED && ECHO_SERVER_TCP_SUPPORT != DISABLED)
61  #error ECHO_SERVER_TCP_SUPPORT parameter is not valid
62 #endif
63 
64 //Maximum number of simultaneous TCP connections
65 #ifndef ECHO_SERVER_MAX_TCP_CONNECTIONS
66  #define ECHO_SERVER_MAX_TCP_CONNECTIONS 2
67 #elif (ECHO_SERVER_MAX_TCP_CONNECTIONS < 1)
68  #error ECHO_SERVER_MAX_TCP_CONNECTIONS parameter is not valid
69 #endif
70 
71 //Size of the buffer for input/output operations (TCP)
72 #ifndef ECHO_SERVER_TCP_BUFFER_SIZE
73  #define ECHO_SERVER_TCP_BUFFER_SIZE 512
74 #elif (ECHO_SERVER_TCP_BUFFER_SIZE < 1)
75  #error ECHO_SERVER_TCP_BUFFER_SIZE parameter is not valid
76 #endif
77 
78 //UDP Echo service support
79 #ifndef ECHO_SERVER_UDP_SUPPORT
80  #define ECHO_SERVER_UDP_SUPPORT ENABLED
81 #elif (ECHO_SERVER_UDP_SUPPORT != ENABLED && ECHO_SERVER_UDP_SUPPORT != DISABLED)
82  #error ECHO_SERVER_UDP_SUPPORT parameter is not valid
83 #endif
84 
85 //Size of the buffer for input/output operations (UDP)
86 #ifndef ECHO_SERVER_UDP_BUFFER_SIZE
87  #define ECHO_SERVER_UDP_BUFFER_SIZE 1472
88 #elif (ECHO_SERVER_UDP_BUFFER_SIZE < 1)
89  #error ECHO_SERVER_UDP_BUFFER_SIZE parameter is not valid
90 #endif
91 
92 //Idle connection timeout
93 #ifndef ECHO_SERVER_TIMEOUT
94  #define ECHO_SERVER_TIMEOUT 30000
95 #elif (ECHO_SERVER_TIMEOUT < 1)
96  #error ECHO_SERVER_TIMEOUT parameter is not valid
97 #endif
98 
99 //Echo server tick interval
100 #ifndef ECHO_SERVER_TICK_INTERVAL
101  #define ECHO_SERVER_TICK_INTERVAL 1000
102 #elif (ECHO_SERVER_TICK_INTERVAL < 100)
103  #error ECHO_SERVER_TICK_INTERVAL parameter is not valid
104 #endif
105 
106 //Application specific context
107 #ifndef ECHO_SERVER_PRIVATE_CONTEXT
108  #define ECHO_SERVER_PRIVATE_CONTEXT
109 #endif
110 
111 //Echo service port number
112 #define ECHO_PORT 7
113 
114 //C++ guard
115 #ifdef __cplusplus
116 extern "C" {
117 #endif
118 
119 
120 /**
121  * @brief TCP connection state
122  **/
123 
124 typedef enum
125 {
129 
130 
131 /**
132  * @brief Echo server settings
133  **/
134 
135 typedef struct
136 {
137  OsTaskParameters task; ///<Task parameters
138  NetInterface *interface; ///<Underlying network interface
139  uint16_t port; ///<Echo server port number
141 
142 
143 /**
144  * @brief Echo TCP connection
145  **/
146 
147 typedef struct
148 {
149  EchoTcpConnectionState state; ///<Connection state
150  Socket *socket; ///<Underlying TCP socket
151  systime_t timestamp; ///<Time stamp
152  char_t buffer[ECHO_SERVER_TCP_BUFFER_SIZE]; ///<Memory buffer for input/output operations (TCP)
153  size_t bufferLen; ///<Length of the buffer, in bytes
154  size_t bufferPos; ///<Current position in the buffer
156 
157 
158 /**
159  * @brief Echo server context
160  **/
161 
162 typedef struct
163 {
164  EchoServerSettings settings; ///<User settings
165  bool_t running; ///<Operational state of the Echo server
166  bool_t stop; ///<Stop request
167  OsEvent event; ///<Event object used to poll the sockets
168  OsTaskParameters taskParams; ///<Task parameters
169  OsTaskId taskId; ///<Task identifier
170 #if (ECHO_SERVER_TCP_SUPPORT == ENABLED)
171  Socket *tcpSocket; ///<Listening TCP socket
172  EchoTcpConnection tcpConnection[ECHO_SERVER_MAX_TCP_CONNECTIONS]; ///<TCP connections
173 #endif
174 #if (ECHO_SERVER_UDP_SUPPORT == ENABLED)
175  Socket *udpSocket; ///<UDP socket
176  char_t udpBuffer[ECHO_SERVER_UDP_BUFFER_SIZE]; ///<Memory buffer for input/output operations (UDP)
177 #endif
178  ECHO_SERVER_PRIVATE_CONTEXT ///<Application specific context
180 
181 
182 //Echo server related functions
184 
186  const EchoServerSettings *settings);
187 
190 
191 void echoServerTask(EchoServerContext *context);
192 
193 void echoServerDeinit(EchoServerContext *context);
194 
195 //C++ guard
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif
#define ECHO_SERVER_UDP_BUFFER_SIZE
Definition: echo_server.h:87
error_t echoServerStart(EchoServerContext *context)
Start Echo server.
Definition: echo_server.c:126
int bool_t
Definition: compiler_port.h:53
void echoServerTask(EchoServerContext *context)
Echo server task.
Definition: echo_server.c:322
error_t echoServerInit(EchoServerContext *context, const EchoServerSettings *settings)
Initialize Echo server context.
Definition: echo_server.c:76
OsTaskParameters task
Task parameters.
Definition: echo_server.h:137
Event object.
void echoServerDeinit(EchoServerContext *context)
Release Echo server context.
Definition: echo_server.c:455
error_t echoServerStop(EchoServerContext *context)
Stop Echo server.
Definition: echo_server.c:263
Echo TCP connection.
Definition: echo_server.h:148
Socket * socket
Underlying TCP socket.
Definition: echo_server.h:150
@ ECHO_TCP_CONNECTION_STATE_CLOSED
Definition: echo_server.h:126
@ ECHO_TCP_CONNECTION_STATE_OPEN
Definition: echo_server.h:127
error_t
Error codes.
Definition: error.h:43
Echo server settings.
Definition: echo_server.h:136
NetInterface * interface
Underlying network interface.
Definition: echo_server.h:138
EchoTcpConnectionState
TCP connection state.
Definition: echo_server.h:125
size_t bufferPos
Current position in the buffer.
Definition: echo_server.h:154
#define NetInterface
Definition: net.h:36
Socket * tcpSocket
Listening TCP socket.
Definition: echo_server.h:171
#define ECHO_SERVER_PRIVATE_CONTEXT
Definition: echo_server.h:108
Task parameters.
#define ECHO_SERVER_MAX_TCP_CONNECTIONS
Definition: echo_server.h:66
void echoServerGetDefaultSettings(EchoServerSettings *settings)
Initialize settings with default values.
Definition: echo_server.c:54
bool_t running
Operational state of the Echo server.
Definition: echo_server.h:165
uint16_t port
Echo server port number.
Definition: echo_server.h:139
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:48
Socket * udpSocket
UDP socket.
Definition: echo_server.h:175
Echo server context.
Definition: echo_server.h:163
#define ECHO_SERVER_TCP_BUFFER_SIZE
Definition: echo_server.h:73
OsTaskId taskId
Task identifier.
Definition: echo_server.h:169
#define Socket
Definition: socket.h:36
Socket API.
OsEvent event
Event object used to poll the sockets.
Definition: echo_server.h:167
thread_t * OsTaskId
Task identifier.
EchoServerSettings settings
User settings.
Definition: echo_server.h:164
TCP/IP stack core.
systime_t timestamp
Time stamp.
Definition: echo_server.h:151
EchoTcpConnectionState state
Connection state.
Definition: echo_server.h:149
bool_t stop
Stop request.
Definition: echo_server.h:166
size_t bufferLen
Length of the buffer, in bytes.
Definition: echo_server.h:153
OsTaskParameters taskParams
Task parameters.
Definition: echo_server.h:168