Socket server test

main.cpp

Committer:
Colin Hogben
Date:
2016-08-25
Revision:
4:19dd8a25cc8a
Parent:
0:d2e0685698cc
Child:
5:2c2f993df16b

File content as of revision 4:19dd8a25cc8a:

#include "mbed.h"
#include "EthernetInterface.h"
#include "lwip/stats.h"

#ifndef SRV_PORT
#define SRV_PORT 8084
#endif

Serial pc(USBTX, USBRX);
EthernetInterface eth;

Thread stats_thread;

void stats_main(void)
{
    int i;
    while (true) {
        int c = pc.getc();
        switch (c) {
        default: printf("lwip statistics: *=all"
                        " l=LINK a=ETHARP"
                        " i=IP f=IPFRAG c=ICMP g=IGMP"
                        " u=UDP t=TCP"
                        " h=HEAP m=MEMP* p=PCB n=NETBUF/NETCONN"
                        " s=SYS\n"); break;
        case '*': stats_display(); break;
        case 'l': LINK_STATS_DISPLAY(); break;
        case 'a': ETHARP_STATS_DISPLAY(); break;
        case 'f': IPFRAG_STATS_DISPLAY(); break;
        case 'i': IP_STATS_DISPLAY(); break;
        case 'c': ICMP_STATS_DISPLAY(); break;
        case 'g': IGMP_STATS_DISPLAY(); break;
        case 'u': UDP_STATS_DISPLAY(); break;
        case 't': TCP_STATS_DISPLAY(); break;
        case 'h': MEM_STATS_DISPLAY(); break;
        case 'm': for (i=0; i < MEMP_MAX; i++) MEMP_STATS_DISPLAY(i); break;
        case 'p':
            MEMP_STATS_DISPLAY(MEMP_UDP_PCB);
            MEMP_STATS_DISPLAY(MEMP_TCP_PCB);
            MEMP_STATS_DISPLAY(MEMP_TCP_PCB_LISTEN);
            break;
        case 'n':
            MEMP_STATS_DISPLAY(MEMP_NETBUF);
            MEMP_STATS_DISPLAY(MEMP_NETCONN);
            break;
        case 's': SYS_STATS_DISPLAY(); break;
        }
    }
}

void tcp_echo_conn(TCPSocket *sock)
{
    static char buf[5000];
    int ret;
    while (true) {
        ret = sock->recv(buf, sizeof(buf));
        if (ret < 0) {
            error("recv: %d", ret);
        } else if (ret == 0) {
            printf("recv: EOF\r\n");
            delete sock;
            break;
        } else {
            ret = sock->send(buf, ret);
            if (ret < 0) {
                error("send: %d", ret);
            }
        }
    }
}

void tcp_echo(TCPServer *srv)
{
    int err;
    while (true) {
        TCPSocket *sock = new TCPSocket;
        err = srv->accept(sock);
        if (err) error("accept: %d\r\n", err);
        printf("accepted connection\r\n");
        Thread *conn_thread = new Thread;
        conn_thread->start(Callback<void()>(sock, tcp_echo_conn));
    }
}

int main(void)
{
    int err;
    printf("\r\n== socket-test %s ==\r\n", __DATE__);
    stats_init();
    stats_thread.start(stats_main);
    err = eth.connect();
    if (err) error("connect: %d\n", err);
    printf("ip %s\r\n", eth.get_ip_address());
    TCPServer srv;
    err = srv.open(&eth);
    if (err) error("open: %d\r\n", err);
    err = srv.bind(SRV_PORT);
    if (err) error("bind: %d\r\n", err);
    err = srv.listen(0);
    if (err) error("listen: %d\r\n", err);
    Thread srv_thread;
    srv_thread.start(Callback<void()>(&srv, tcp_echo));

    while (true) {
        wait(1.0);
    }
}