mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
tests/lwip: adapt test to use sock_async
This commit is contained in:
parent
fc67e6143a
commit
ba2dd2cf44
@ -20,6 +20,8 @@ endif
|
||||
USEMODULE += lwip lwip_sock_ip lwip_netdev
|
||||
USEMODULE += lwip_udp lwip_sock_udp
|
||||
USEMODULE += lwip_tcp lwip_sock_tcp
|
||||
USEMODULE += lwip_sock_async
|
||||
USEMODULE += sock_async_event
|
||||
USEMODULE += shell
|
||||
USEMODULE += shell_commands
|
||||
USEMODULE += ps
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "common.h"
|
||||
#include "od.h"
|
||||
#include "net/af.h"
|
||||
#include "net/sock/async/event.h"
|
||||
#include "net/sock/ip.h"
|
||||
#include "shell.h"
|
||||
#include "thread.h"
|
||||
@ -43,25 +44,14 @@ static sock_ip_t server_sock;
|
||||
static char server_stack[THREAD_STACKSIZE_DEFAULT];
|
||||
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
|
||||
|
||||
static void *_server_thread(void *args)
|
||||
static void _ip_recv(sock_ip_t *sock, sock_async_flags_t flags)
|
||||
{
|
||||
sock_ip_ep_t server_addr = SOCK_IP_EP_ANY;
|
||||
uint8_t protocol;
|
||||
|
||||
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
|
||||
/* parse protocol */
|
||||
protocol = atoi(args);
|
||||
if (sock_ip_create(&server_sock, &server_addr, NULL, protocol, 0) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
server_running = true;
|
||||
printf("Success: started IP server on protocol %u\n", protocol);
|
||||
while (1) {
|
||||
int res;
|
||||
if (flags & SOCK_ASYNC_MSG_RECV) {
|
||||
sock_ip_ep_t src;
|
||||
int res;
|
||||
|
||||
if ((res = sock_ip_recv(&server_sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
SOCK_NO_TIMEOUT, &src)) < 0) {
|
||||
if ((res = sock_ip_recv(sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
0, &src)) < 0) {
|
||||
puts("Error on receive");
|
||||
}
|
||||
else if (res == 0) {
|
||||
@ -82,6 +72,25 @@ static void *_server_thread(void *args)
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void *_server_thread(void *args)
|
||||
{
|
||||
event_queue_t queue;
|
||||
sock_ip_ep_t server_addr = SOCK_IP_EP_ANY;
|
||||
uint8_t protocol;
|
||||
|
||||
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
|
||||
/* parse protocol */
|
||||
protocol = atoi(args);
|
||||
if (sock_ip_create(&server_sock, &server_addr, NULL, protocol, 0) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
server_running = true;
|
||||
printf("Success: started IP server on protocol %u\n", protocol);
|
||||
event_queue_init(&queue);
|
||||
sock_ip_event_init(&server_sock, &queue, _ip_recv);
|
||||
event_loop(&queue);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
114
tests/lwip/tcp.c
114
tests/lwip/tcp.c
@ -23,6 +23,7 @@
|
||||
#include "common.h"
|
||||
#include "od.h"
|
||||
#include "net/af.h"
|
||||
#include "net/sock/async/event.h"
|
||||
#include "net/sock/tcp.h"
|
||||
#include "shell.h"
|
||||
#include "thread.h"
|
||||
@ -43,6 +44,72 @@ static sock_tcp_t server_sock, client_sock;
|
||||
static sock_tcp_queue_t server_queue;
|
||||
static char server_stack[THREAD_STACKSIZE_DEFAULT];
|
||||
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
|
||||
static char _addr_str[IPV6_ADDR_MAX_STR_LEN];
|
||||
static event_queue_t _ev_queue;
|
||||
|
||||
static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags)
|
||||
{
|
||||
sock_tcp_ep_t client;
|
||||
|
||||
if (sock_tcp_get_remote(sock, &client) < 0) {
|
||||
/* socket was disconnected between event firing and this handler */
|
||||
return;
|
||||
}
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
ipv6_addr_to_str(_addr_str, (ipv6_addr_t *)&client.addr.ipv6,
|
||||
sizeof(_addr_str));
|
||||
#else
|
||||
ipv4_addr_to_str(_addr_str, (ipv4_addr_t *)&client.addr.ipv4,
|
||||
sizeof(_addr_str));
|
||||
#endif
|
||||
if (flags & SOCK_ASYNC_MSG_RECV) {
|
||||
int res;
|
||||
|
||||
/* we don't use timeouts so all errors should be related to a lost
|
||||
* connection */
|
||||
while ((res = sock_tcp_read(sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
0)) >= 0) {
|
||||
printf("Received TCP data from client [%s]:%u:\n", _addr_str,
|
||||
client.port);
|
||||
if (res > 0) {
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
else {
|
||||
puts("(nul)");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flags & SOCK_ASYNC_CONN_FIN) {
|
||||
printf("TCP connection to [%s]:%u reset\n", _addr_str, client.port);
|
||||
sock_tcp_disconnect(sock);
|
||||
}
|
||||
}
|
||||
|
||||
static void _tcp_accept(sock_tcp_queue_t *queue, sock_async_flags_t flags)
|
||||
{
|
||||
if (flags & SOCK_ASYNC_CONN_RECV) {
|
||||
sock_tcp_t *sock = NULL;
|
||||
int res;
|
||||
|
||||
if ((res = sock_tcp_accept(queue, &sock, 0)) < 0) {
|
||||
printf("Error on TCP accept [%d]\n", res);
|
||||
}
|
||||
else {
|
||||
sock_tcp_ep_t client;
|
||||
|
||||
sock_tcp_event_init(sock, &_ev_queue, _tcp_recv);
|
||||
sock_tcp_get_remote(sock, &client);
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
ipv6_addr_to_str(_addr_str, (ipv6_addr_t *)&client.addr.ipv6,
|
||||
sizeof(_addr_str));
|
||||
#else
|
||||
ipv4_addr_to_str(_addr_str, (ipv4_addr_t *)&client.addr.ipv4,
|
||||
sizeof(_addr_str));
|
||||
#endif
|
||||
printf("TCP client [%s]:%u connected\n", _addr_str, client.port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void *_server_thread(void *args)
|
||||
{
|
||||
@ -61,48 +128,9 @@ static void *_server_thread(void *args)
|
||||
server_running = true;
|
||||
printf("Success: started TCP server on port %" PRIu16 "\n",
|
||||
server_addr.port);
|
||||
while (1) {
|
||||
char client_addr[IPV6_ADDR_MAX_STR_LEN];
|
||||
sock_tcp_t *sock = NULL;
|
||||
int res;
|
||||
unsigned client_port;
|
||||
|
||||
if ((res = sock_tcp_accept(&server_queue, &sock, SOCK_NO_TIMEOUT)) < 0) {
|
||||
puts("Error on TCP accept");
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
sock_tcp_ep_t client;
|
||||
|
||||
sock_tcp_get_remote(sock, &client);
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
ipv6_addr_to_str(client_addr, (ipv6_addr_t *)&client.addr.ipv6,
|
||||
sizeof(client_addr));
|
||||
#else
|
||||
ipv4_addr_to_str(client_addr, (ipv4_addr_t *)&client.addr.ipv4,
|
||||
sizeof(client_addr));
|
||||
#endif
|
||||
client_port = client.port;
|
||||
printf("TCP client [%s]:%u connected\n",
|
||||
client_addr, client_port);
|
||||
}
|
||||
/* we don't use timeouts so all errors should be related to a lost
|
||||
* connection */
|
||||
while ((res = sock_tcp_read(sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
SOCK_NO_TIMEOUT)) >= 0) {
|
||||
printf("Received TCP data from client [%s]:%u:\n",
|
||||
client_addr, client_port);
|
||||
if (res > 0) {
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
else {
|
||||
puts("(nul)");
|
||||
}
|
||||
}
|
||||
printf("TCP connection to [%s]:%u reset, starting to accept again\n",
|
||||
client_addr, client_port);
|
||||
sock_tcp_disconnect(sock);
|
||||
}
|
||||
event_queue_init(&_ev_queue);
|
||||
sock_tcp_queue_event_init(&server_queue, &_ev_queue, _tcp_accept);
|
||||
event_loop(&_ev_queue);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -112,7 +140,7 @@ static int tcp_connect(char *addr_str, char *port_str, char *local_port_str)
|
||||
uint16_t local_port = 0;
|
||||
|
||||
if (client_running) {
|
||||
puts("Cient already connected");
|
||||
puts("Client already connected");
|
||||
}
|
||||
|
||||
/* parse destination address */
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "common.h"
|
||||
#include "od.h"
|
||||
#include "net/af.h"
|
||||
#include "net/sock/async/event.h"
|
||||
#include "net/sock/udp.h"
|
||||
#include "shell.h"
|
||||
#include "thread.h"
|
||||
@ -43,28 +44,14 @@ static sock_udp_t server_sock;
|
||||
static char server_stack[THREAD_STACKSIZE_DEFAULT];
|
||||
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
|
||||
|
||||
static void *_server_thread(void *args)
|
||||
static void _udp_recv(sock_udp_t *sock, sock_async_flags_t flags)
|
||||
{
|
||||
sock_udp_ep_t server_addr = SOCK_IP_EP_ANY;
|
||||
int res;
|
||||
|
||||
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
|
||||
/* parse port */
|
||||
server_addr.port = atoi(args);
|
||||
if ((res = sock_udp_create(&server_sock, &server_addr, NULL, 0)) < 0) {
|
||||
printf("Unable to open UDP server on port %" PRIu16 " (error code %d)\n",
|
||||
server_addr.port, -res);
|
||||
return NULL;
|
||||
}
|
||||
server_running = true;
|
||||
printf("Success: started UDP server on port %" PRIu16 "\n",
|
||||
server_addr.port);
|
||||
while (1) {
|
||||
if (flags & SOCK_ASYNC_MSG_RECV) {
|
||||
sock_udp_ep_t src;
|
||||
int res;
|
||||
|
||||
if ((res = sock_udp_recv(&server_sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
SOCK_NO_TIMEOUT, &src)) < 0) {
|
||||
if ((res = sock_udp_recv(sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
0, &src)) < 0) {
|
||||
puts("Error on receive");
|
||||
}
|
||||
else if (res == 0) {
|
||||
@ -85,6 +72,28 @@ static void *_server_thread(void *args)
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void *_server_thread(void *args)
|
||||
{
|
||||
event_queue_t queue;
|
||||
sock_udp_ep_t server_addr = SOCK_IP_EP_ANY;
|
||||
int res;
|
||||
|
||||
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
|
||||
/* parse port */
|
||||
server_addr.port = atoi(args);
|
||||
if ((res = sock_udp_create(&server_sock, &server_addr, NULL, 0)) < 0) {
|
||||
printf("Unable to open UDP server on port %" PRIu16 " (error code %d)\n",
|
||||
server_addr.port, -res);
|
||||
return NULL;
|
||||
}
|
||||
server_running = true;
|
||||
printf("Success: started UDP server on port %" PRIu16 "\n",
|
||||
server_addr.port);
|
||||
event_queue_init(&queue);
|
||||
sock_udp_event_init(&server_sock, &queue, _udp_recv);
|
||||
event_loop(&queue);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user