Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
EchoServer.cpp@0:b6a536e40b5d, 2012-06-20 (annotated)
- Committer:
- dfukumori
- Date:
- Wed Jun 20 22:16:31 2012 +0000
- Revision:
- 0:b6a536e40b5d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dfukumori | 0:b6a536e40b5d | 1 | #include "EchoServer.h" |
dfukumori | 0:b6a536e40b5d | 2 | |
dfukumori | 0:b6a536e40b5d | 3 | char* screenBufA; |
dfukumori | 0:b6a536e40b5d | 4 | |
dfukumori | 0:b6a536e40b5d | 5 | EchoServer::EchoServer(char* scrbuf) { |
dfukumori | 0:b6a536e40b5d | 6 | // Create the sockets and set the callbacks |
dfukumori | 0:b6a536e40b5d | 7 | tcpSock = new TCPSocket; |
dfukumori | 0:b6a536e40b5d | 8 | tcpSock->setOnEvent(this, &EchoServer::onNetTcpSocketEvent); |
dfukumori | 0:b6a536e40b5d | 9 | udpSock = new UDPSocket; |
dfukumori | 0:b6a536e40b5d | 10 | udpSock->setOnEvent(this, &EchoServer::onNetUdpSocketEvent); |
dfukumori | 0:b6a536e40b5d | 11 | screenBufA = scrbuf; |
dfukumori | 0:b6a536e40b5d | 12 | } |
dfukumori | 0:b6a536e40b5d | 13 | |
dfukumori | 0:b6a536e40b5d | 14 | EchoServer::~EchoServer() { |
dfukumori | 0:b6a536e40b5d | 15 | // Delete the sockets on destruction |
dfukumori | 0:b6a536e40b5d | 16 | delete tcpSock; |
dfukumori | 0:b6a536e40b5d | 17 | delete udpSock; |
dfukumori | 0:b6a536e40b5d | 18 | } |
dfukumori | 0:b6a536e40b5d | 19 | |
dfukumori | 0:b6a536e40b5d | 20 | void EchoServer::bind(int tcpPort, int udpPort) { |
dfukumori | 0:b6a536e40b5d | 21 | // bind and listen on TCP |
dfukumori | 0:b6a536e40b5d | 22 | tcpSock->bind(Host(IpAddr(192,168,1,128), tcpPort)); |
dfukumori | 0:b6a536e40b5d | 23 | tcpSock->listen(); |
dfukumori | 0:b6a536e40b5d | 24 | // bind UDP |
dfukumori | 0:b6a536e40b5d | 25 | udpSock->bind(Host(IpAddr(192,168,1,128), udpPort)); |
dfukumori | 0:b6a536e40b5d | 26 | } |
dfukumori | 0:b6a536e40b5d | 27 | |
dfukumori | 0:b6a536e40b5d | 28 | void EchoServer::onNetTcpSocketEvent(TCPSocketEvent e) { |
dfukumori | 0:b6a536e40b5d | 29 | // We're only interested in the ACCEPT event where we need to accept |
dfukumori | 0:b6a536e40b5d | 30 | // the incoming connection |
dfukumori | 0:b6a536e40b5d | 31 | if ( e == TCPSOCKET_ACCEPT ) { |
dfukumori | 0:b6a536e40b5d | 32 | TCPSocket* tcpClientSocket; |
dfukumori | 0:b6a536e40b5d | 33 | Host client; |
dfukumori | 0:b6a536e40b5d | 34 | if ( tcpSock->accept(&client, &tcpClientSocket) ) { |
dfukumori | 0:b6a536e40b5d | 35 | printf("onNetTcpSocketEvent : Could not accept connection.\r\n"); |
dfukumori | 0:b6a536e40b5d | 36 | return; //Error in accept, discard connection |
dfukumori | 0:b6a536e40b5d | 37 | } |
dfukumori | 0:b6a536e40b5d | 38 | // We can find out from where the connection is coming by looking at the |
dfukumori | 0:b6a536e40b5d | 39 | // Host parameter of the accept() method |
dfukumori | 0:b6a536e40b5d | 40 | IpAddr clientIp = client.getIp(); |
dfukumori | 0:b6a536e40b5d | 41 | printf("Incoming TCP connection from %d.%d.%d.%d\r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
dfukumori | 0:b6a536e40b5d | 42 | // Create TCPEchoHandler and pass client socket |
dfukumori | 0:b6a536e40b5d | 43 | TCPEchoHandler* tcpHandler = new TCPEchoHandler(tcpClientSocket, screenBufA); //TCPSocket ownership is passed to handler |
dfukumori | 0:b6a536e40b5d | 44 | // The handler object will destroy itself when done, or will be destroyed on Server destruction |
dfukumori | 0:b6a536e40b5d | 45 | } |
dfukumori | 0:b6a536e40b5d | 46 | } |
dfukumori | 0:b6a536e40b5d | 47 | |
dfukumori | 0:b6a536e40b5d | 48 | void EchoServer::onNetUdpSocketEvent(UDPSocketEvent e) { |
dfukumori | 0:b6a536e40b5d | 49 | // We're only interested in the READABLE event (it's the only one) |
dfukumori | 0:b6a536e40b5d | 50 | if ( e == UDPSOCKET_READABLE ) { |
dfukumori | 0:b6a536e40b5d | 51 | // No need for a handler for UDP - set up a buffer and check client |
dfukumori | 0:b6a536e40b5d | 52 | char buff[128]; |
dfukumori | 0:b6a536e40b5d | 53 | Host client; |
dfukumori | 0:b6a536e40b5d | 54 | IpAddr clientIp = client.getIp(); |
dfukumori | 0:b6a536e40b5d | 55 | printf("Incoming UDP connection from %d.%d.%d.%d\r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
dfukumori | 0:b6a536e40b5d | 56 | // Keep reading while there's data to be read |
dfukumori | 0:b6a536e40b5d | 57 | while ( int len = udpSock->recvfrom(buff, 128, &client) ) { |
dfukumori | 0:b6a536e40b5d | 58 | if ( len > 0 ) |
dfukumori | 0:b6a536e40b5d | 59 | // If there's data, send it straight back out |
dfukumori | 0:b6a536e40b5d | 60 | udpSock->sendto(buff, len, &client); |
dfukumori | 0:b6a536e40b5d | 61 | } |
dfukumori | 0:b6a536e40b5d | 62 | } |
dfukumori | 0:b6a536e40b5d | 63 | } |