Echo Server based on the legacy EthernetNetIf libraries used for a performance comparison with the new networking libraries
Dependencies: EthernetNetIf mbed
Fork of EchoServer by
EchoServer.cpp@0:fcd581e3ad7d, 2011-06-13 (annotated)
- Committer:
- naegawa
- Date:
- Mon Jun 13 06:47:43 2011 +0000
- Revision:
- 0:fcd581e3ad7d
test1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
naegawa | 0:fcd581e3ad7d | 1 | #include "EchoServer.h" |
naegawa | 0:fcd581e3ad7d | 2 | #include "lwip/ip_addr.h" |
naegawa | 0:fcd581e3ad7d | 3 | EchoServer::EchoServer() { |
naegawa | 0:fcd581e3ad7d | 4 | // Create the sockets and set the callbacks |
naegawa | 0:fcd581e3ad7d | 5 | tcpSock = new TCPSocket; |
naegawa | 0:fcd581e3ad7d | 6 | tcpSock->setOnEvent(this, &EchoServer::onNetTcpSocketEvent); |
naegawa | 0:fcd581e3ad7d | 7 | udpSock = new UDPSocket; |
naegawa | 0:fcd581e3ad7d | 8 | udpSock->setOnEvent(this, &EchoServer::onNetUdpSocketEvent); |
naegawa | 0:fcd581e3ad7d | 9 | } |
naegawa | 0:fcd581e3ad7d | 10 | |
naegawa | 0:fcd581e3ad7d | 11 | EchoServer::~EchoServer() { |
naegawa | 0:fcd581e3ad7d | 12 | // Delete the sockets on destruction |
naegawa | 0:fcd581e3ad7d | 13 | delete tcpSock; |
naegawa | 0:fcd581e3ad7d | 14 | delete udpSock; |
naegawa | 0:fcd581e3ad7d | 15 | } |
naegawa | 0:fcd581e3ad7d | 16 | |
naegawa | 0:fcd581e3ad7d | 17 | void EchoServer::bind(int tcpPort, int udpPort) { |
naegawa | 0:fcd581e3ad7d | 18 | // bind and listen on TCP |
naegawa | 0:fcd581e3ad7d | 19 | tcpSock->bind(Host(IP_ADDR_ANY, tcpPort)); |
naegawa | 0:fcd581e3ad7d | 20 | tcpSock->listen(); |
naegawa | 0:fcd581e3ad7d | 21 | // bind UDP |
naegawa | 0:fcd581e3ad7d | 22 | udpSock->bind(Host(IP_ADDR_ANY, udpPort)); |
naegawa | 0:fcd581e3ad7d | 23 | } |
naegawa | 0:fcd581e3ad7d | 24 | |
naegawa | 0:fcd581e3ad7d | 25 | void EchoServer::onNetTcpSocketEvent(TCPSocketEvent e) { |
naegawa | 0:fcd581e3ad7d | 26 | // We're only interested in the ACCEPT event where we need to accept |
naegawa | 0:fcd581e3ad7d | 27 | // the incoming connection |
naegawa | 0:fcd581e3ad7d | 28 | if ( e == TCPSOCKET_ACCEPT ) { |
naegawa | 0:fcd581e3ad7d | 29 | TCPSocket* tcpClientSocket; |
naegawa | 0:fcd581e3ad7d | 30 | Host client; |
naegawa | 0:fcd581e3ad7d | 31 | if ( tcpSock->accept(&client, &tcpClientSocket) ) { |
naegawa | 0:fcd581e3ad7d | 32 | printf("onNetTcpSocketEvent : Could not accept connection.\r\n"); |
naegawa | 0:fcd581e3ad7d | 33 | return; //Error in accept, discard connection |
naegawa | 0:fcd581e3ad7d | 34 | } |
naegawa | 0:fcd581e3ad7d | 35 | // We can find out from where the connection is coming by looking at the |
naegawa | 0:fcd581e3ad7d | 36 | // Host parameter of the accept() method |
naegawa | 0:fcd581e3ad7d | 37 | IpAddr clientIp = client.getIp(); |
naegawa | 0:fcd581e3ad7d | 38 | printf("Incoming TCP connection from %d.%d.%d.%d\r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
naegawa | 0:fcd581e3ad7d | 39 | // Create TCPEchoHandler and pass client socket |
naegawa | 0:fcd581e3ad7d | 40 | TCPEchoHandler* tcpHandler = new TCPEchoHandler(tcpClientSocket); //TCPSocket ownership is passed to handler |
naegawa | 0:fcd581e3ad7d | 41 | // The handler object will destroy itself when done, or will be destroyed on Server destruction |
naegawa | 0:fcd581e3ad7d | 42 | } |
naegawa | 0:fcd581e3ad7d | 43 | } |
naegawa | 0:fcd581e3ad7d | 44 | |
naegawa | 0:fcd581e3ad7d | 45 | void EchoServer::onNetUdpSocketEvent(UDPSocketEvent e) { |
naegawa | 0:fcd581e3ad7d | 46 | // We're only interested in the READABLE event (it's the only one) |
naegawa | 0:fcd581e3ad7d | 47 | if ( e == UDPSOCKET_READABLE ) { |
naegawa | 0:fcd581e3ad7d | 48 | // No need for a handler for UDP - set up a buffer and check client |
naegawa | 0:fcd581e3ad7d | 49 | char buff[128]; |
naegawa | 0:fcd581e3ad7d | 50 | Host client; |
naegawa | 0:fcd581e3ad7d | 51 | IpAddr clientIp = client.getIp(); |
naegawa | 0:fcd581e3ad7d | 52 | printf("Incoming UDP connection from %d.%d.%d.%d\r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
naegawa | 0:fcd581e3ad7d | 53 | // Keep reading while there's data to be read |
naegawa | 0:fcd581e3ad7d | 54 | while ( int len = udpSock->recvfrom(buff, 128, &client) ) { |
naegawa | 0:fcd581e3ad7d | 55 | if ( len > 0 ) |
naegawa | 0:fcd581e3ad7d | 56 | // If there's data, send it straight back out |
naegawa | 0:fcd581e3ad7d | 57 | udpSock->sendto(buff, len, &client); |
naegawa | 0:fcd581e3ad7d | 58 | } |
naegawa | 0:fcd581e3ad7d | 59 | } |
naegawa | 0:fcd581e3ad7d | 60 | } |