Netconn API
Netconn API is addon on top of existing connection module and allows sending and receiving data with sequential API calls, similar to POSIX socket API.
It can operate in client or server mode and uses operating system features, such as message queues and semaphore to link non-blocking callback API for connections with sequential API for application thread.
Note
Connection API does not directly allow receiving data with sequential and linear code execution. All is based on connection event system. Netconn adds this functionality as it is implemented on top of regular connection API.
Warning
Netconn API are designed to be called from application threads ONLY. It is not allowed to call any of netconn API functions from within interrupt or callback event functions.
Netconn client
Netconn API client block diagram
Above block diagram shows basic architecture of netconn client application. There is always one application thread (in green) which calls netconn API functions to interact with connection API in synchronous mode.
Every netconn connection uses dedicated structure to handle message queue for data received packet buffers. Each time new packet is received (red block, data received event), reference to it is written to message queue of netconn structure, while application thread reads new entries from the same queue to get packets.
1/*
2 * Netconn client demonstrates how to connect as a client to server
3 * using sequential API from separate thread.
4 *
5 * it does not use callbacks to obtain connection status.
6 *
7 * Demo connects to NETCONN_HOST at NETCONN_PORT and sends GET request header,
8 * then waits for respond and expects server to close the connection accordingly.
9 */
10#include "netconn_client.h"
11#include "lwesp/lwesp.h"
12#include "lwesp/lwesp_netconn.h"
13
14/**
15 * \brief Host and port settings
16 */
17#define NETCONN_HOST "example.com"
18#define NETCONN_PORT 80
19
20/**
21 * \brief Request header to send on successful connection
22 */
23static const char request_header[] = ""
24 "GET / HTTP/1.1\r\n"
25 "Host: " NETCONN_HOST "\r\n"
26 "Connection: close\r\n"
27 "\r\n";
28
29/**
30 * \brief Netconn client thread implementation
31 * \param[in] arg: User argument
32 */
33void
34netconn_client_thread(void const* arg) {
35 lwespr_t res;
36 lwesp_pbuf_p pbuf;
37 lwesp_netconn_p client;
38 lwesp_sys_sem_t* sem = (void*)arg;
39
40 /* Make sure we are connected to access point first */
41 while (!lwesp_sta_has_ip()) {
42 lwesp_delay(1000);
43 }
44
45 /*
46 * First create a new instance of netconn
47 * connection and initialize system message boxes
48 * to accept received packet buffers
49 */
50 client = lwesp_netconn_new(LWESP_NETCONN_TYPE_TCP);
51 if (client != NULL) {
52
53 /*
54 * Connect to external server as client
55 * with custom NETCONN_CONN_HOST and CONN_PORT values
56 *
57 * Function will block thread until we are successfully connected (or not) to server
58 */
59 res = lwesp_netconn_connect(client, NETCONN_HOST, NETCONN_PORT);
60 if (res == lwespOK) { /* Are we successfully connected? */
61 printf("Connected to " NETCONN_HOST "\r\n");
62 res = lwesp_netconn_write(client, request_header, sizeof(request_header) - 1); /* Send data to server */
63 if (res == lwespOK) {
64 res = lwesp_netconn_flush(client); /* Flush data to output */
65 }
66 if (res == lwespOK) { /* Were data sent? */
67 printf("Data were successfully sent to server\r\n");
68
69 /*
70 * Since we sent HTTP request,
71 * we are expecting some data from server
72 * or at least forced connection close from remote side
73 */
74 do {
75 /*
76 * Receive single packet of data
77 *
78 * Function will block thread until new packet
79 * is ready to be read from remote side
80 *
81 * After function returns, don't forgot the check value.
82 * Returned status will give you info in case connection
83 * was closed too early from remote side
84 */
85 res = lwesp_netconn_receive(client, &pbuf);
86 if (res
87 == lwespCLOSED) { /* Was the connection closed? This can be checked by return status of receive function */
88 printf("Connection closed by remote side...\r\n");
89 break;
90 } else if (res == lwespTIMEOUT) {
91 printf("Netconn timeout while receiving data. You may try multiple readings before deciding to "
92 "close manually\r\n");
93 }
94
95 if (res == lwespOK && pbuf != NULL) { /* Make sure we have valid packet buffer */
96 /*
97 * At this point, read and manipulate
98 * with received buffer and check if you expect more data
99 *
100 * After you are done using it, it is important
101 * you free the memory, or memory leaks will appear
102 */
103 printf("Received new data packet of %d bytes\r\n", (int)lwesp_pbuf_length(pbuf, 1));
104 lwesp_pbuf_free_s(&pbuf); /* Free the memory after usage */
105 }
106 } while (1);
107 } else {
108 printf("Error writing data to remote host!\r\n");
109 }
110
111 /*
112 * Check if connection was closed by remote server
113 * and in case it wasn't, close it manually
114 */
115 if (res != lwespCLOSED) {
116 lwesp_netconn_close(client);
117 }
118 } else {
119 printf("Cannot connect to remote host %s:%d!\r\n", NETCONN_HOST, NETCONN_PORT);
120 }
121 lwesp_netconn_delete(client); /* Delete netconn structure */
122 }
123
124 printf("Terminating thread\r\n");
125 if (lwesp_sys_sem_isvalid(sem)) {
126 lwesp_sys_sem_release(sem);
127 }
128 lwesp_sys_thread_terminate(NULL); /* Terminate current thread */
129}
Netconn server
Netconn API server block diagram
When netconn is configured in server mode, it is possible to accept new clients from remote side. Application creates netconn server connection, which can only accept clients and cannot send/receive any data. It configures server on dedicated port (selected by application) and listens on it.
When new client connects, server callback function is called with new active connection event. Newly accepted connection is then written to server structure netconn which is later read by application thread. At the same time, netconn connection structure (blue) is created to allow standard send/receive operation on active connection.
Note
Each connected client has its own netconn connection structure. When multiple clients connect to server at the same time, multiple entries are written to connection accept message queue and are ready to be processed by application thread.
From this point, program flow is the same as in case of netconn client.
This is basic example for netconn thread. It waits for client and processes it in blocking mode.
Warning
When multiple clients connect at the same time to netconn server, they are processed one-by-one, sequentially. This may introduce delay in response for other clients. Check netconn concurrency option to process multiple clients at the same time
1/*
2 * Netconn server example is based on single thread
3 * and it listens for single client only on port 23.
4 *
5 * When new client connects, application processes client in the same thread.
6 * When multiple clients get connected at the same time,
7 * each of them waits all previous to be processed first, hence it may
8 * introduce latency, in some cases even clearly visible in (for example) user browser
9 */
10#include "netconn_server_1thread.h"
11#include "lwesp/lwesp_netconn.h"
12#include "lwesp/lwesp.h"
13
14/**
15 * \brief Basic thread for netconn server to test connections
16 * \param[in] arg: User argument
17 */
18void
19netconn_server_1thread_thread(void* arg) {
20 lwespr_t res;
21 lwesp_netconn_p server, client;
22 lwesp_pbuf_p p;
23
24 LWESP_UNUSED(arg);
25
26 /* Create netconn for server */
27 server = lwesp_netconn_new(LWESP_NETCONN_TYPE_TCP);
28 if (server == NULL) {
29 printf("Cannot create server netconn!\r\n");
30 }
31
32 /* Bind it to port 23 */
33 res = lwesp_netconn_bind(server, 23);
34 if (res != lwespOK) {
35 printf("Cannot bind server\r\n");
36 goto out;
37 }
38
39 /* Start listening for incoming connections with maximal 1 client */
40 res = lwesp_netconn_listen_with_max_conn(server, 1);
41 if (res != lwespOK) {
42 goto out;
43 }
44
45 /* Unlimited loop */
46 while (1) {
47 /* Accept new client */
48 res = lwesp_netconn_accept(server, &client);
49 if (res != lwespOK) {
50 break;
51 }
52 printf("New client accepted!\r\n");
53 while (1) {
54 /* Receive data */
55 res = lwesp_netconn_receive(client, &p);
56 if (res == lwespOK) {
57 printf("Data received!\r\n");
58 lwesp_pbuf_free_s(&p);
59 } else {
60 printf("Netconn receive returned: %d\r\n", (int)res);
61 if (res == lwespCLOSED) {
62 printf("Connection closed by client\r\n");
63 break;
64 }
65 }
66 }
67 /* Delete client */
68 if (client != NULL) {
69 lwesp_netconn_delete(client);
70 client = NULL;
71 }
72 }
73 /* Delete client */
74 if (client != NULL) {
75 lwesp_netconn_delete(client);
76 client = NULL;
77 }
78
79out:
80 printf("Terminating netconn thread!\r\n");
81 if (server != NULL) {
82 lwesp_netconn_delete(server);
83 }
84 lwesp_sys_thread_terminate(NULL);
85}
Netconn server concurrency
Netconn API server concurrency block diagram
When compared to classic netconn server, concurrent netconn server mode allows multiple clients to be processed at the same time. This can drastically improve performance and response time on clients side, especially when many clients are connected to server at the same time.
Every time server application thread (green block) gets new client to process, it starts a new processing thread instead of doing it in accept thread.
Server thread is only dedicated to accept clients and start threads
Multiple processing thread can run in parallel to send/receive data from multiple clients
No delay when multi clients are active at the same time
Higher memory footprint is necessary as there are multiple threads active
1/*
2 * Netconn server example is based on single "user" thread
3 * which listens for new connections and accepts them.
4 *
5 * When a new client is accepted by server,
6 * a new thread gets spawned and processes client request
7 * separately. When multiple users are connected,
8 * they can be processed simultaneously, hence no such latency as in single thread mode.
9 *
10 * As a drawback, more memory is consumed for multiple parallel threads being potentially
11 * used at the same period of time.
12 */
13#include "netconn_server.h"
14#include "lwesp/lwesp.h"
15#include "lwesp/lwesp_netconn.h"
16
17static void netconn_server_processing_thread(void* const arg);
18
19/**
20 * \brief Main page response file
21 */
22static const uint8_t rlwesp_data_mainpage_top[] =
23 ""
24 "HTTP/1.1 200 OK\r\n"
25 "Content-Type: text/html\r\n"
26 "\r\n"
27 "<html>"
28 " <head>"
29 " <link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />"
30 " <meta http-equiv=\"refresh\" content=\"1\" />"
31 " </head>"
32 " <body>"
33 " <p>Netconn driven website!</p>"
34 " <p>Total system up time: <b>";
35
36/**
37 * \brief Bottom part of main page
38 */
39static const uint8_t rlwesp_data_mainpage_bottom[] = ""
40 " </b></p>"
41 " </body>"
42 "</html>";
43
44/**
45 * \brief Style file response
46 */
47static const uint8_t rlwesp_data_style[] = ""
48 "HTTP/1.1 200 OK\r\n"
49 "Content-Type: text/css\r\n"
50 "\r\n"
51 "body { color: red; font-family: Tahoma, Arial; };";
52
53/**
54 * \brief 404 error response
55 */
56static const uint8_t rlwesp_error_404[] = ""
57 "HTTP/1.1 404 Not Found\r\n"
58 "\r\n"
59 "Error 404";
60
61/**
62 * \brief Netconn server thread implementation
63 * \param[in] arg: User argument
64 */
65void
66netconn_server_thread(void const* arg) {
67 lwespr_t res;
68 lwesp_netconn_p server, client;
69
70 LWESP_UNUSED(arg);
71
72 /*
73 * First create a new instance of netconn
74 * connection and initialize system message boxes
75 * to accept clients and packet buffers
76 */
77 server = lwesp_netconn_new(LWESP_NETCONN_TYPE_TCP);
78 if (server != NULL) {
79 printf("Server netconn created\r\n");
80
81 /* Bind network connection to port 80 */
82 res = lwesp_netconn_bind(server, 80);
83 if (res == lwespOK) {
84 printf("Server netconn listens on port 80\r\n");
85 /*
86 * Start listening for incoming connections
87 * on previously binded port
88 */
89 res = lwesp_netconn_listen(server);
90
91 while (1) {
92 /*
93 * Wait and accept new client connection
94 *
95 * Function will block thread until
96 * new client is connected to server
97 */
98 res = lwesp_netconn_accept(server, &client);
99 if (res == lwespOK) {
100 printf("Netconn new client connected. Starting new thread...\r\n");
101 /*
102 * Start new thread for this request.
103 *
104 * Read and write back data to user in separated thread
105 * to allow processing of multiple requests at the same time
106 */
107 if (lwesp_sys_thread_create(NULL, "client", (lwesp_sys_thread_fn)netconn_server_processing_thread,
108 client, 512, LWESP_SYS_THREAD_PRIO)) {
109 printf("Netconn client thread created\r\n");
110 } else {
111 printf("Netconn client thread creation failed!\r\n");
112
113 /* Force close & delete */
114 lwesp_netconn_close(client);
115 lwesp_netconn_delete(client);
116 }
117 } else {
118 printf("Netconn connection accept error!\r\n");
119 break;
120 }
121 }
122 } else {
123 printf("Netconn server cannot bind to port\r\n");
124 }
125 } else {
126 printf("Cannot create server netconn\r\n");
127 }
128
129 printf("Terminating thread\r\n");
130 lwesp_netconn_delete(server); /* Delete netconn structure */
131 lwesp_sys_thread_terminate(NULL); /* Terminate current thread */
132}
133
134/**
135 * \brief Thread to process single active connection
136 * \param[in] arg: Thread argument
137 */
138static void
139netconn_server_processing_thread(void* const arg) {
140 lwesp_netconn_p client = arg;
141 lwesp_pbuf_p pbuf, p = NULL;
142 lwespr_t res;
143 char strt[20];
144
145 printf("A new connection accepted!\r\n"); /* Print simple message */
146
147 do {
148 /*
149 * Client was accepted, we are now
150 * expecting client will send to us some data
151 *
152 * Wait for data and block thread for that time
153 */
154 res = lwesp_netconn_receive(client, &pbuf);
155
156 if (res == lwespOK) {
157 printf("Netconn data received, %d bytes\r\n", (int)lwesp_pbuf_length(pbuf, 1));
158 /* Check reception of all header bytes */
159 if (p == NULL) {
160 p = pbuf; /* Set as first buffer */
161 } else {
162 lwesp_pbuf_cat(p, pbuf); /* Concatenate buffers together */
163 }
164 /*
165 * Search for end of request section, that is supposed
166 * to end with line, followed by another fully empty line.
167 */
168 if (lwesp_pbuf_strfind(pbuf, "\r\n\r\n", 0) != LWESP_SIZET_MAX) {
169 if (lwesp_pbuf_strfind(pbuf, "GET / ", 0) != LWESP_SIZET_MAX) {
170 uint32_t now;
171 printf("Main page request\r\n");
172 now = lwesp_sys_now(); /* Get current time */
173 sprintf(strt, "%u ms; %d s", (unsigned)now, (unsigned)(now / 1000));
174 lwesp_netconn_write(client, rlwesp_data_mainpage_top, sizeof(rlwesp_data_mainpage_top) - 1);
175 lwesp_netconn_write(client, strt, strlen(strt));
176 lwesp_netconn_write(client, rlwesp_data_mainpage_bottom, sizeof(rlwesp_data_mainpage_bottom) - 1);
177 } else if (lwesp_pbuf_strfind(pbuf, "GET /style.css ", 0) != LWESP_SIZET_MAX) {
178 printf("Style page request\r\n");
179 lwesp_netconn_write(client, rlwesp_data_style, sizeof(rlwesp_data_style) - 1);
180 } else {
181 printf("404 error not found\r\n");
182 lwesp_netconn_write(client, rlwesp_error_404, sizeof(rlwesp_error_404) - 1);
183 }
184 lwesp_netconn_close(client); /* Close netconn connection */
185 lwesp_pbuf_free_s(&p); /* Do not forget to free memory after usage! */
186 break;
187 }
188 }
189 } while (res == lwespOK);
190
191 if (p != NULL) { /* Free received data */
192 lwesp_pbuf_free_s(&p);
193 }
194 lwesp_netconn_delete(client); /* Destroy client memory */
195 lwesp_sys_thread_terminate(NULL); /* Terminate this thread */
196}
Non-blocking receive
By default, netconn API is written to only work in separate application thread, dedicated for network connection processing. Because of that, by default every function is fully blocking. It will wait until result is ready to be used by application.
It is, however, possible to enable timeout feature for receiving data only.
When this feature is enabled, lwesp_netconn_receive()
will block for maximal timeout set with
lwesp_netconn_set_receive_timeout()
function.
When enabled, if there is no received data for timeout amount of time, function will return with timeout status and application needs to process it accordingly.
Tip
LWESP_CFG_NETCONN_RECEIVE_TIMEOUT
must be set to 1
to use this feature.
- group LWESP_NETCONN
Network connection.
Defines
-
LWESP_NETCONN_RECEIVE_NO_WAIT
Receive data with no timeout.
Note
Used with lwesp_netconn_set_receive_timeout function
-
LWESP_NETCONN_FLAG_FLUSH
Immediate flush after netconn write
Typedefs
-
typedef struct lwesp_netconn *lwesp_netconn_p
Netconn object structure.
Enums
-
enum lwesp_netconn_type_t
Netconn connection type.
Values:
-
enumerator LWESP_NETCONN_TYPE_TCP = LWESP_CONN_TYPE_TCP
TCP connection
-
enumerator LWESP_NETCONN_TYPE_SSL = LWESP_CONN_TYPE_SSL
SSL connection
-
enumerator LWESP_NETCONN_TYPE_UDP = LWESP_CONN_TYPE_UDP
UDP connection
-
enumerator LWESP_NETCONN_TYPE_TCPV6 = LWESP_CONN_TYPE_TCPV6
TCP connection over IPv6
-
enumerator LWESP_NETCONN_TYPE_SSLV6 = LWESP_CONN_TYPE_SSLV6
SSL connection over IPv6
-
enumerator LWESP_NETCONN_TYPE_UDPV6 = LWESP_CONN_TYPE_UDPV6
UDP connection over IPv6
-
enumerator LWESP_NETCONN_TYPE_TCP = LWESP_CONN_TYPE_TCP
Functions
-
lwesp_netconn_p lwesp_netconn_new(lwesp_netconn_type_t type)
Create new netconn connection.
- Parameters
type – [in] Netconn connection type
- Returns
New netconn connection on success,
NULL
otherwise
-
lwespr_t lwesp_netconn_delete(lwesp_netconn_p nc)
Delete netconn connection.
-
lwespr_t lwesp_netconn_bind(lwesp_netconn_p nc, lwesp_port_t port)
Bind a connection to specific port, can be only used for server connections.
-
lwespr_t lwesp_netconn_connect(lwesp_netconn_p nc, const char *host, lwesp_port_t port)
Connect to server as client.
-
lwespr_t lwesp_netconn_receive(lwesp_netconn_p nc, lwesp_pbuf_p *pbuf)
Receive data from connection.
- Parameters
nc – [in] Netconn handle used to receive from
pbuf – [in] Pointer to pointer to save new receive buffer to. When function returns, user must check for valid pbuf value
pbuf != NULL
- Returns
lwespOK when new data ready
- Returns
lwespCLOSED when connection closed by remote side
- Returns
lwespTIMEOUT when receive timeout occurs
- Returns
Any other member of lwespr_t otherwise
-
lwespr_t lwesp_netconn_close(lwesp_netconn_p nc)
Close a netconn connection.
-
int8_t lwesp_netconn_get_connnum(lwesp_netconn_p nc)
Get connection number used for netconn.
- Parameters
nc – [in] Netconn handle
- Returns
-1
on failure, connection number between0
and LWESP_CFG_MAX_CONNS otherwise
-
lwesp_conn_p lwesp_netconn_get_conn(lwesp_netconn_p nc)
Get netconn connection handle.
- Parameters
nc – [in] Netconn handle
- Returns
ESP connection handle
-
lwesp_netconn_type_t lwesp_netconn_get_type(lwesp_netconn_p nc)
Get netconn connection type.
- Parameters
nc – [in] Netconn handle
- Returns
ESP connection type
-
void lwesp_netconn_set_receive_timeout(lwesp_netconn_p nc, uint32_t timeout)
Set timeout value for receiving data.
When enabled, lwesp_netconn_receive will only block for up to timeout value and will return if no new data within this time
- Parameters
nc – [in] Netconn handle
timeout – [in] Timeout in units of milliseconds. Set to
0
to disable timeout feature. Function blocks until data receive or connection closed Set to> 0
to set maximum milliseconds to wait before timeout Set to LWESP_NETCONN_RECEIVE_NO_WAIT to enable non-blocking receive
-
uint32_t lwesp_netconn_get_receive_timeout(lwesp_netconn_p nc)
Get netconn receive timeout value.
- Parameters
nc – [in] Netconn handle
- Returns
Timeout in units of milliseconds. If value is
0
, timeout is disabled (wait forever)
-
lwespr_t lwesp_netconn_connect_ex(lwesp_netconn_p nc, const char *host, lwesp_port_t port, uint16_t keep_alive, const char *local_ip, lwesp_port_t local_port, uint8_t mode)
Connect to server as client, allow keep-alive option.
- Parameters
nc – [in] Netconn handle
host – [in] Pointer to host, such as domain name or IP address in string format
port – [in] Target port to use
keep_alive – [in] Keep alive period seconds
local_ip – [in] Local ip in connected command
local_port – [in] Local port address
mode – [in] UDP mode
- Returns
lwespOK if successfully connected, member of lwespr_t otherwise
-
lwespr_t lwesp_netconn_listen(lwesp_netconn_p nc)
Listen on previously binded connection.
-
lwespr_t lwesp_netconn_listen_with_max_conn(lwesp_netconn_p nc, uint16_t max_connections)
Listen on previously binded connection with max allowed connections at a time.
- Parameters
nc – [in] Netconn handle used to listen for new connections
max_connections – [in] Maximal number of connections server can accept at a time This parameter may not be larger than LWESP_CFG_MAX_CONNS
- Returns
-
lwespr_t lwesp_netconn_set_listen_conn_timeout(lwesp_netconn_p nc, uint16_t timeout)
Set timeout value in units of seconds when connection is in listening mode If new connection is accepted, it will be automatically closed after
seconds
elapsed without any data exchange.Note
Call this function before you put connection to listen mode with lwesp_netconn_listen
-
lwespr_t lwesp_netconn_accept(lwesp_netconn_p nc, lwesp_netconn_p *client)
Accept a new connection.
-
lwespr_t lwesp_netconn_write(lwesp_netconn_p nc, const void *data, size_t btw)
Write data to connection output buffers.
Note
This function may only be used on TCP or SSL connections
-
lwespr_t lwesp_netconn_write_ex(lwesp_netconn_p nc, const void *data, size_t btw, uint16_t flags)
Extended version of lwesp_netconn_write with additional option to set custom flags.
Note
It is recommended to use this for full features support
-
lwespr_t lwesp_netconn_flush(lwesp_netconn_p nc)
Flush buffered data on netconn TCP/SSL connection.
Note
This function may only be used on TCP/SSL connection
-
lwespr_t lwesp_netconn_send(lwesp_netconn_p nc, const void *data, size_t btw)
Send data on UDP connection to default IP and port.
-
lwespr_t lwesp_netconn_sendto(lwesp_netconn_p nc, const lwesp_ip_t *ip, lwesp_port_t port, const void *data, size_t btw)
Send data on UDP connection to specific IP and port.
Note
Use this function in case of UDP type netconn
-
LWESP_NETCONN_RECEIVE_NO_WAIT