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