lwIP TCP/UDP parallel test
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@0:197ec1eccf49, 2013-07-10 (annotated)
- Committer:
- tass
- Date:
- Wed Jul 10 12:24:46 2013 +0000
- Revision:
- 0:197ec1eccf49
lwIP TCP/UDP parallel connections test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tass | 0:197ec1eccf49 | 1 | #include "mbed.h" |
tass | 0:197ec1eccf49 | 2 | #include "rtos.h" |
tass | 0:197ec1eccf49 | 3 | #include "EthernetInterface.h" |
tass | 0:197ec1eccf49 | 4 | |
tass | 0:197ec1eccf49 | 5 | #define ECHO_SERVER_PORT 7 |
tass | 0:197ec1eccf49 | 6 | #define UDP_SERVER_PORT 2327 |
tass | 0:197ec1eccf49 | 7 | #define BUFFER_QUANTITY (1024*1024) |
tass | 0:197ec1eccf49 | 8 | #define NUMBER_OF_THREADS 2u |
tass | 0:197ec1eccf49 | 9 | |
tass | 0:197ec1eccf49 | 10 | Queue<void,NUMBER_OF_THREADS> clientList; |
tass | 0:197ec1eccf49 | 11 | |
tass | 0:197ec1eccf49 | 12 | |
tass | 0:197ec1eccf49 | 13 | /****** UDP Connection ******/ |
tass | 0:197ec1eccf49 | 14 | void UDPThread(const void * arg); |
tass | 0:197ec1eccf49 | 15 | void startUDPThread(void) |
tass | 0:197ec1eccf49 | 16 | { |
tass | 0:197ec1eccf49 | 17 | Thread * threadUDPEcho = new Thread(UDPThread); |
tass | 0:197ec1eccf49 | 18 | } |
tass | 0:197ec1eccf49 | 19 | |
tass | 0:197ec1eccf49 | 20 | // thread is sending out data on a rate of 1KB at each 10ms |
tass | 0:197ec1eccf49 | 21 | void UDPThread(const void * arg) |
tass | 0:197ec1eccf49 | 22 | { |
tass | 0:197ec1eccf49 | 23 | |
tass | 0:197ec1eccf49 | 24 | UDPSocket server; |
tass | 0:197ec1eccf49 | 25 | Endpoint endp; |
tass | 0:197ec1eccf49 | 26 | char buffer[1024]; |
tass | 0:197ec1eccf49 | 27 | |
tass | 0:197ec1eccf49 | 28 | endp.set_address("255.255.255.255",UDP_SERVER_PORT); |
tass | 0:197ec1eccf49 | 29 | printf("Binding result :%d\n", server.bind(UDP_SERVER_PORT)); |
tass | 0:197ec1eccf49 | 30 | |
tass | 0:197ec1eccf49 | 31 | server.set_blocking(false,1500); |
tass | 0:197ec1eccf49 | 32 | printf("Started UDP Broadcast server...\n"); |
tass | 0:197ec1eccf49 | 33 | server.set_broadcasting(); |
tass | 0:197ec1eccf49 | 34 | |
tass | 0:197ec1eccf49 | 35 | while(true) |
tass | 0:197ec1eccf49 | 36 | { |
tass | 0:197ec1eccf49 | 37 | server.sendTo(endp,buffer,sizeof(buffer)); |
tass | 0:197ec1eccf49 | 38 | Thread::wait(10); |
tass | 0:197ec1eccf49 | 39 | } |
tass | 0:197ec1eccf49 | 40 | |
tass | 0:197ec1eccf49 | 41 | server.close(); |
tass | 0:197ec1eccf49 | 42 | } |
tass | 0:197ec1eccf49 | 43 | |
tass | 0:197ec1eccf49 | 44 | /****** TCP Connection ******/ |
tass | 0:197ec1eccf49 | 45 | |
tass | 0:197ec1eccf49 | 46 | void TCPClientThread(const void * arg); |
tass | 0:197ec1eccf49 | 47 | void acceptNewClient(TCPSocketServer & server) |
tass | 0:197ec1eccf49 | 48 | { |
tass | 0:197ec1eccf49 | 49 | TCPSocketConnection *client =new TCPSocketConnection(); |
tass | 0:197ec1eccf49 | 50 | server.accept(*client); |
tass | 0:197ec1eccf49 | 51 | client->set_blocking(false, 1500); // Timeout after (1.5)s |
tass | 0:197ec1eccf49 | 52 | printf("Connection from: %s\n", client->get_address()); |
tass | 0:197ec1eccf49 | 53 | if(clientList.put((void *)client) != osOK) // queue is full? |
tass | 0:197ec1eccf49 | 54 | delete(client); |
tass | 0:197ec1eccf49 | 55 | } |
tass | 0:197ec1eccf49 | 56 | |
tass | 0:197ec1eccf49 | 57 | void TCPClientThread(const void * arg) |
tass | 0:197ec1eccf49 | 58 | { |
tass | 0:197ec1eccf49 | 59 | |
tass | 0:197ec1eccf49 | 60 | while(true) |
tass | 0:197ec1eccf49 | 61 | { |
tass | 0:197ec1eccf49 | 62 | TCPSocketConnection * client = NULL; |
tass | 0:197ec1eccf49 | 63 | char buffer[1024]; |
tass | 0:197ec1eccf49 | 64 | int dataReceived = 0; |
tass | 0:197ec1eccf49 | 65 | int dataSent = 0; |
tass | 0:197ec1eccf49 | 66 | |
tass | 0:197ec1eccf49 | 67 | // client stream |
tass | 0:197ec1eccf49 | 68 | osEvent evt = clientList.get(); |
tass | 0:197ec1eccf49 | 69 | if(evt.status == osEventMessage) |
tass | 0:197ec1eccf49 | 70 | client = (TCPSocketConnection *)evt.value.p; |
tass | 0:197ec1eccf49 | 71 | else |
tass | 0:197ec1eccf49 | 72 | continue; |
tass | 0:197ec1eccf49 | 73 | |
tass | 0:197ec1eccf49 | 74 | while(true) |
tass | 0:197ec1eccf49 | 75 | { |
tass | 0:197ec1eccf49 | 76 | printf("\n\n\nStarting the receiving part...\n"); |
tass | 0:197ec1eccf49 | 77 | while(dataReceived < BUFFER_QUANTITY) |
tass | 0:197ec1eccf49 | 78 | { |
tass | 0:197ec1eccf49 | 79 | int n = client->receive(buffer, sizeof(buffer)); |
tass | 0:197ec1eccf49 | 80 | if (n <= 0) { |
tass | 0:197ec1eccf49 | 81 | printf("Receive error\n"); |
tass | 0:197ec1eccf49 | 82 | break; |
tass | 0:197ec1eccf49 | 83 | } |
tass | 0:197ec1eccf49 | 84 | dataReceived += n; |
tass | 0:197ec1eccf49 | 85 | } |
tass | 0:197ec1eccf49 | 86 | |
tass | 0:197ec1eccf49 | 87 | |
tass | 0:197ec1eccf49 | 88 | printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY); |
tass | 0:197ec1eccf49 | 89 | if(dataReceived < BUFFER_QUANTITY) |
tass | 0:197ec1eccf49 | 90 | { |
tass | 0:197ec1eccf49 | 91 | printf("Receiving part of the test has failed. Exiting connection.\n"); |
tass | 0:197ec1eccf49 | 92 | break; |
tass | 0:197ec1eccf49 | 93 | } |
tass | 0:197ec1eccf49 | 94 | else{ |
tass | 0:197ec1eccf49 | 95 | printf("Receiving has passed...\n"); |
tass | 0:197ec1eccf49 | 96 | } |
tass | 0:197ec1eccf49 | 97 | |
tass | 0:197ec1eccf49 | 98 | printf("\n\n\nStarting the sending part...\n"); |
tass | 0:197ec1eccf49 | 99 | while(dataSent < BUFFER_QUANTITY) |
tass | 0:197ec1eccf49 | 100 | { |
tass | 0:197ec1eccf49 | 101 | int n = client->send_all(buffer, sizeof(buffer)); |
tass | 0:197ec1eccf49 | 102 | if (n <= 0) { |
tass | 0:197ec1eccf49 | 103 | printf("Send error : %d\n",n); |
tass | 0:197ec1eccf49 | 104 | break; |
tass | 0:197ec1eccf49 | 105 | } |
tass | 0:197ec1eccf49 | 106 | dataSent += n; |
tass | 0:197ec1eccf49 | 107 | } |
tass | 0:197ec1eccf49 | 108 | |
tass | 0:197ec1eccf49 | 109 | printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY); |
tass | 0:197ec1eccf49 | 110 | if(dataSent < BUFFER_QUANTITY) |
tass | 0:197ec1eccf49 | 111 | { |
tass | 0:197ec1eccf49 | 112 | printf("Sending part of the test has failed. Exiting connection.\n"); |
tass | 0:197ec1eccf49 | 113 | break; |
tass | 0:197ec1eccf49 | 114 | } |
tass | 0:197ec1eccf49 | 115 | else |
tass | 0:197ec1eccf49 | 116 | { |
tass | 0:197ec1eccf49 | 117 | printf("Sending test has passed...\n"); |
tass | 0:197ec1eccf49 | 118 | } |
tass | 0:197ec1eccf49 | 119 | |
tass | 0:197ec1eccf49 | 120 | |
tass | 0:197ec1eccf49 | 121 | printf("\n\n\nStarting echo part...\n"); |
tass | 0:197ec1eccf49 | 122 | dataReceived = dataSent = 0; |
tass | 0:197ec1eccf49 | 123 | while((dataReceived+dataSent) < 2*BUFFER_QUANTITY) |
tass | 0:197ec1eccf49 | 124 | { |
tass | 0:197ec1eccf49 | 125 | int n = client->receive(buffer, sizeof(buffer)); |
tass | 0:197ec1eccf49 | 126 | if (n <= 0) { |
tass | 0:197ec1eccf49 | 127 | printf("Receive error\n"); |
tass | 0:197ec1eccf49 | 128 | break; |
tass | 0:197ec1eccf49 | 129 | } |
tass | 0:197ec1eccf49 | 130 | dataReceived += n; |
tass | 0:197ec1eccf49 | 131 | |
tass | 0:197ec1eccf49 | 132 | n = client->send_all(buffer, n); |
tass | 0:197ec1eccf49 | 133 | if (n <= 0) { |
tass | 0:197ec1eccf49 | 134 | printf("Send error\n"); |
tass | 0:197ec1eccf49 | 135 | break; |
tass | 0:197ec1eccf49 | 136 | } |
tass | 0:197ec1eccf49 | 137 | dataSent += n; |
tass | 0:197ec1eccf49 | 138 | } |
tass | 0:197ec1eccf49 | 139 | |
tass | 0:197ec1eccf49 | 140 | printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY); |
tass | 0:197ec1eccf49 | 141 | if((dataReceived+dataSent) < 2*BUFFER_QUANTITY) |
tass | 0:197ec1eccf49 | 142 | { |
tass | 0:197ec1eccf49 | 143 | printf("Echo test has failed.Exiting connection...\n"); |
tass | 0:197ec1eccf49 | 144 | } |
tass | 0:197ec1eccf49 | 145 | else |
tass | 0:197ec1eccf49 | 146 | { |
tass | 0:197ec1eccf49 | 147 | printf("Echo test has passed...\n"); |
tass | 0:197ec1eccf49 | 148 | } |
tass | 0:197ec1eccf49 | 149 | |
tass | 0:197ec1eccf49 | 150 | |
tass | 0:197ec1eccf49 | 151 | printf("Test was finished...\n"); |
tass | 0:197ec1eccf49 | 152 | } |
tass | 0:197ec1eccf49 | 153 | // droping out client |
tass | 0:197ec1eccf49 | 154 | client->close(); |
tass | 0:197ec1eccf49 | 155 | delete(client); |
tass | 0:197ec1eccf49 | 156 | break; |
tass | 0:197ec1eccf49 | 157 | } |
tass | 0:197ec1eccf49 | 158 | } |
tass | 0:197ec1eccf49 | 159 | |
tass | 0:197ec1eccf49 | 160 | int main() |
tass | 0:197ec1eccf49 | 161 | { |
tass | 0:197ec1eccf49 | 162 | EthernetInterface eth; |
tass | 0:197ec1eccf49 | 163 | eth.init(); //Use DHCP |
tass | 0:197ec1eccf49 | 164 | eth.connect(); |
tass | 0:197ec1eccf49 | 165 | printf("IP Address %s\n", eth.getIPAddress()); |
tass | 0:197ec1eccf49 | 166 | |
tass | 0:197ec1eccf49 | 167 | // startUDP Thread |
tass | 0:197ec1eccf49 | 168 | startUDPThread(); |
tass | 0:197ec1eccf49 | 169 | |
tass | 0:197ec1eccf49 | 170 | |
tass | 0:197ec1eccf49 | 171 | TCPSocketServer server; |
tass | 0:197ec1eccf49 | 172 | server.bind(ECHO_SERVER_PORT); |
tass | 0:197ec1eccf49 | 173 | server.listen(); |
tass | 0:197ec1eccf49 | 174 | // start client threads |
tass | 0:197ec1eccf49 | 175 | for(int i=0;i<NUMBER_OF_THREADS;i++) |
tass | 0:197ec1eccf49 | 176 | { |
tass | 0:197ec1eccf49 | 177 | new Thread(TCPClientThread); |
tass | 0:197ec1eccf49 | 178 | } |
tass | 0:197ec1eccf49 | 179 | printf("Started TCP server...\n"); |
tass | 0:197ec1eccf49 | 180 | |
tass | 0:197ec1eccf49 | 181 | while (true) |
tass | 0:197ec1eccf49 | 182 | { |
tass | 0:197ec1eccf49 | 183 | acceptNewClient(server); |
tass | 0:197ec1eccf49 | 184 | } |
tass | 0:197ec1eccf49 | 185 | } |