server/client
Dependencies: mbed NetServicesMin
tcp_server.cpp@1:6e61ee662fd3, 2012-02-23 (annotated)
- Committer:
- recotana
- Date:
- Thu Feb 23 02:38:05 2012 +0000
- Revision:
- 1:6e61ee662fd3
- Parent:
- 0:432b571e11a3
added standalone test mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
recotana | 1:6e61ee662fd3 | 1 | #include "types.h" |
recotana | 0:432b571e11a3 | 2 | #include "mbed.h" |
recotana | 0:432b571e11a3 | 3 | #include "config.h" |
recotana | 0:432b571e11a3 | 4 | #include "tcp_server.h" |
recotana | 0:432b571e11a3 | 5 | #include "tcp_client.h" |
recotana | 0:432b571e11a3 | 6 | |
recotana | 1:6e61ee662fd3 | 7 | #include "data_models.h" |
recotana | 1:6e61ee662fd3 | 8 | #include "xbee_lib.h" |
recotana | 1:6e61ee662fd3 | 9 | #include "net_routing.h" |
recotana | 1:6e61ee662fd3 | 10 | |
recotana | 1:6e61ee662fd3 | 11 | #include "leds.h" |
recotana | 1:6e61ee662fd3 | 12 | |
recotana | 0:432b571e11a3 | 13 | TCPSocket listenSock; |
recotana | 0:432b571e11a3 | 14 | TCPSocket* recieveSock; |
recotana | 0:432b571e11a3 | 15 | Host client; |
recotana | 0:432b571e11a3 | 16 | IpAddr inCommingIp; |
recotana | 0:432b571e11a3 | 17 | |
recotana | 1:6e61ee662fd3 | 18 | |
recotana | 1:6e61ee662fd3 | 19 | |
recotana | 0:432b571e11a3 | 20 | extern bool isHostConnected; |
recotana | 0:432b571e11a3 | 21 | int serverInit(void) |
recotana | 0:432b571e11a3 | 22 | { |
recotana | 0:432b571e11a3 | 23 | listenSock.setOnEvent(&onServerListnerEvent) ; |
recotana | 0:432b571e11a3 | 24 | |
recotana | 1:6e61ee662fd3 | 25 | DBGF("Bindding...\r\n") ; |
recotana | 0:432b571e11a3 | 26 | |
recotana | 0:432b571e11a3 | 27 | if ( listenSock.bind(Host(IpAddr(), kServerPort)) != ETH_OK ){ |
recotana | 1:6e61ee662fd3 | 28 | DBGF("Bindding Error.\r\n") ; |
recotana | 0:432b571e11a3 | 29 | return -1 ; |
recotana | 0:432b571e11a3 | 30 | } |
recotana | 0:432b571e11a3 | 31 | |
recotana | 0:432b571e11a3 | 32 | printf("Listen...\r\n"); |
recotana | 0:432b571e11a3 | 33 | |
recotana | 0:432b571e11a3 | 34 | if ( listenSock.listen() != ETH_OK ){ |
recotana | 1:6e61ee662fd3 | 35 | DBGF("Listen Error.\r\n") ; |
recotana | 0:432b571e11a3 | 36 | return -1 ; |
recotana | 0:432b571e11a3 | 37 | } |
recotana | 0:432b571e11a3 | 38 | return 0; |
recotana | 0:432b571e11a3 | 39 | } |
recotana | 0:432b571e11a3 | 40 | |
recotana | 0:432b571e11a3 | 41 | //Listner Event |
recotana | 0:432b571e11a3 | 42 | void onServerListnerEvent(TCPSocketEvent e) |
recotana | 0:432b571e11a3 | 43 | { |
recotana | 1:6e61ee662fd3 | 44 | DBGF("server ---IN TCPSocketEvent ---\r\n"); |
recotana | 0:432b571e11a3 | 45 | |
recotana | 0:432b571e11a3 | 46 | if ( e == TCPSOCKET_ACCEPT ){ |
recotana | 1:6e61ee662fd3 | 47 | DBGF("Listening: TCP Socket Accepted\r\n"); |
recotana | 0:432b571e11a3 | 48 | |
recotana | 0:432b571e11a3 | 49 | if ( listenSock.accept(&client, &recieveSock) != TCPSOCKET_OK ) { |
recotana | 1:6e61ee662fd3 | 50 | DBGF("onTcpSocketEvent Error \r\n"); |
recotana | 0:432b571e11a3 | 51 | return; |
recotana | 0:432b571e11a3 | 52 | } |
recotana | 0:432b571e11a3 | 53 | |
recotana | 0:432b571e11a3 | 54 | recieveSock->setOnEvent(&onServerRecievetEvent); |
recotana | 0:432b571e11a3 | 55 | |
recotana | 0:432b571e11a3 | 56 | //Get Client Side IP Address |
recotana | 0:432b571e11a3 | 57 | inCommingIp = client.getIp(); |
recotana | 1:6e61ee662fd3 | 58 | DBGV("Controler IP Address is [%d.%d.%d.%d].\r\n", inCommingIp[0], inCommingIp[1], inCommingIp[2], inCommingIp[3]); |
recotana | 0:432b571e11a3 | 59 | } |
recotana | 0:432b571e11a3 | 60 | |
recotana | 1:6e61ee662fd3 | 61 | DBGF("server --- OUT TCPSocketEvent ---\r\n\r\n") ; |
recotana | 0:432b571e11a3 | 62 | } |
recotana | 0:432b571e11a3 | 63 | |
recotana | 0:432b571e11a3 | 64 | void onServerRecievetEvent(TCPSocketEvent e) |
recotana | 0:432b571e11a3 | 65 | { |
recotana | 1:6e61ee662fd3 | 66 | DBGF("server --- IN ConnectEvent ---\r\n"); |
recotana | 0:432b571e11a3 | 67 | |
recotana | 0:432b571e11a3 | 68 | |
recotana | 0:432b571e11a3 | 69 | switch(e) |
recotana | 0:432b571e11a3 | 70 | { |
recotana | 0:432b571e11a3 | 71 | case TCPSOCKET_CONNECTED: |
recotana | 1:6e61ee662fd3 | 72 | DBGF("Connected to host.\r\n") ; |
recotana | 0:432b571e11a3 | 73 | break; |
recotana | 0:432b571e11a3 | 74 | |
recotana | 0:432b571e11a3 | 75 | case TCPSOCKET_WRITEABLE: |
recotana | 1:6e61ee662fd3 | 76 | DBGF("Can write data to buf.\r\n"); |
recotana | 0:432b571e11a3 | 77 | break; |
recotana | 0:432b571e11a3 | 78 | |
recotana | 0:432b571e11a3 | 79 | case TCPSOCKET_READABLE: |
recotana | 1:6e61ee662fd3 | 80 | DBGF("Data in buf.\r\n"); |
recotana | 1:6e61ee662fd3 | 81 | |
recotana | 1:6e61ee662fd3 | 82 | ledEther(); |
recotana | 1:6e61ee662fd3 | 83 | |
recotana | 1:6e61ee662fd3 | 84 | Payload pl; |
recotana | 1:6e61ee662fd3 | 85 | initPayload(&pl); |
recotana | 1:6e61ee662fd3 | 86 | |
recotana | 1:6e61ee662fd3 | 87 | while ( int len = recieveSock->recv((char*)&pl,256) ) { |
recotana | 0:432b571e11a3 | 88 | |
recotana | 1:6e61ee662fd3 | 89 | //raw data dump |
recotana | 1:6e61ee662fd3 | 90 | // dumpPayload(&payload); |
recotana | 1:6e61ee662fd3 | 91 | |
recotana | 1:6e61ee662fd3 | 92 | networkToPayload(&pl); |
recotana | 1:6e61ee662fd3 | 93 | |
recotana | 1:6e61ee662fd3 | 94 | netRouting(&pl); |
recotana | 1:6e61ee662fd3 | 95 | |
recotana | 0:432b571e11a3 | 96 | } |
recotana | 1:6e61ee662fd3 | 97 | ; |
recotana | 0:432b571e11a3 | 98 | break; |
recotana | 0:432b571e11a3 | 99 | |
recotana | 0:432b571e11a3 | 100 | case TCPSOCKET_CONTIMEOUT: |
recotana | 1:6e61ee662fd3 | 101 | DBGF("Connection timed out.\r\n"); |
recotana | 0:432b571e11a3 | 102 | break ; |
recotana | 0:432b571e11a3 | 103 | |
recotana | 0:432b571e11a3 | 104 | case TCPSOCKET_CONRST: |
recotana | 1:6e61ee662fd3 | 105 | DBGF("Connection was reset by remote host.\r\n"); |
recotana | 0:432b571e11a3 | 106 | break ; |
recotana | 0:432b571e11a3 | 107 | |
recotana | 0:432b571e11a3 | 108 | case TCPSOCKET_CONABRT: |
recotana | 1:6e61ee662fd3 | 109 | DBGF("Connection was aborted.\r\n") ; |
recotana | 0:432b571e11a3 | 110 | break ; |
recotana | 0:432b571e11a3 | 111 | |
recotana | 0:432b571e11a3 | 112 | case TCPSOCKET_ERROR: |
recotana | 1:6e61ee662fd3 | 113 | DBGF("Unknown error.\r\n") ; |
recotana | 0:432b571e11a3 | 114 | break ; |
recotana | 0:432b571e11a3 | 115 | |
recotana | 0:432b571e11a3 | 116 | case TCPSOCKET_DISCONNECTED: |
recotana | 1:6e61ee662fd3 | 117 | DBGF("Tcp Socket Disconnected\r\n") ; |
recotana | 0:432b571e11a3 | 118 | recieveSock->close() ; |
recotana | 0:432b571e11a3 | 119 | break; |
recotana | 0:432b571e11a3 | 120 | } |
recotana | 1:6e61ee662fd3 | 121 | DBGF("--- OUT ConnectEvent ---\r\n\r\n"); |
recotana | 0:432b571e11a3 | 122 | } |
recotana | 0:432b571e11a3 | 123 | |
recotana | 0:432b571e11a3 | 124 | void recieveDump(char *data , int length){ |
recotana | 1:6e61ee662fd3 | 125 | DBGV("size = %d\n -- -id\n",length); |
recotana | 0:432b571e11a3 | 126 | |
recotana | 0:432b571e11a3 | 127 | for(int i=0;i<16;i++) printf("%d:%0x ",i,data[i]); |
recotana | 1:6e61ee662fd3 | 128 | DBGF("\n--- data\n"); |
recotana | 0:432b571e11a3 | 129 | |
recotana | 0:432b571e11a3 | 130 | int count=0; |
recotana | 0:432b571e11a3 | 131 | for(int i=0;i<(length-16);i++){ |
recotana | 1:6e61ee662fd3 | 132 | DBGV("%02d:%02x ",i,data[16+i]); |
recotana | 0:432b571e11a3 | 133 | if( count==15 ) {count=0; printf("\n");} |
recotana | 0:432b571e11a3 | 134 | else count++; |
recotana | 0:432b571e11a3 | 135 | } |
recotana | 0:432b571e11a3 | 136 | |
recotana | 1:6e61ee662fd3 | 137 | DBGF("\n---frame end\n"); |
recotana | 0:432b571e11a3 | 138 | } |
recotana | 0:432b571e11a3 | 139 |