server/client
Dependencies: mbed NetServicesMin
tcp_client.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_error.h" |
recotana | 0:432b571e11a3 | 5 | #include "tcp_client.h" |
recotana | 0:432b571e11a3 | 6 | |
recotana | 1:6e61ee662fd3 | 7 | #include "leds.h" |
recotana | 1:6e61ee662fd3 | 8 | |
recotana | 0:432b571e11a3 | 9 | extern IpAddr inCommingIp; |
recotana | 0:432b571e11a3 | 10 | |
recotana | 1:6e61ee662fd3 | 11 | |
recotana | 0:432b571e11a3 | 12 | Host server; |
recotana | 0:432b571e11a3 | 13 | TCPSocket sendSock; |
recotana | 0:432b571e11a3 | 14 | ConnectStatus clientStatus; |
recotana | 0:432b571e11a3 | 15 | Timer checkTimer; |
recotana | 0:432b571e11a3 | 16 | Ticker clientCheck; |
recotana | 0:432b571e11a3 | 17 | |
recotana | 0:432b571e11a3 | 18 | IpAddr clientIp; |
recotana | 0:432b571e11a3 | 19 | int clientPort; |
recotana | 0:432b571e11a3 | 20 | |
recotana | 0:432b571e11a3 | 21 | void onSendEvent (TCPSocketEvent e) { |
recotana | 1:6e61ee662fd3 | 22 | DBGF("... client event arrival!\r\n"); |
recotana | 0:432b571e11a3 | 23 | switch(e) |
recotana | 0:432b571e11a3 | 24 | { |
recotana | 1:6e61ee662fd3 | 25 | case TCPSOCKET_CONNECTED: |
recotana | 0:432b571e11a3 | 26 | { |
recotana | 1:6e61ee662fd3 | 27 | ledTcpConnect(true); |
recotana | 1:6e61ee662fd3 | 28 | |
recotana | 1:6e61ee662fd3 | 29 | char str[]="wellcome! server connected!"; |
recotana | 1:6e61ee662fd3 | 30 | |
recotana | 1:6e61ee662fd3 | 31 | sendSock.send(str,strlen(str) ); |
recotana | 1:6e61ee662fd3 | 32 | |
recotana | 0:432b571e11a3 | 33 | clientStatus=connected; |
recotana | 1:6e61ee662fd3 | 34 | DBGF("TCPSOCKET_CONNECTED!!\r\n"); |
recotana | 0:432b571e11a3 | 35 | break; |
recotana | 0:432b571e11a3 | 36 | } |
recotana | 0:432b571e11a3 | 37 | |
recotana | 1:6e61ee662fd3 | 38 | case TCPSOCKET_WRITEABLE: |
recotana | 0:432b571e11a3 | 39 | { |
recotana | 1:6e61ee662fd3 | 40 | DBGF("TCPSOCKET_WRITEABLE!!\r\n"); |
recotana | 0:432b571e11a3 | 41 | clientStatus=connected; |
recotana | 1:6e61ee662fd3 | 42 | ledTcpConnect(true); |
recotana | 0:432b571e11a3 | 43 | break; |
recotana | 0:432b571e11a3 | 44 | } |
recotana | 0:432b571e11a3 | 45 | |
recotana | 1:6e61ee662fd3 | 46 | case TCPSOCKET_READABLE: |
recotana | 1:6e61ee662fd3 | 47 | { DBGF("TCPSOCKET_READABLE!!\r\n"); |
recotana | 0:432b571e11a3 | 48 | clientStatus=connected; |
recotana | 1:6e61ee662fd3 | 49 | ledTcpConnect(true); |
recotana | 0:432b571e11a3 | 50 | break; |
recotana | 0:432b571e11a3 | 51 | } |
recotana | 0:432b571e11a3 | 52 | case TCPSOCKET_CONTIMEOUT: |
recotana | 0:432b571e11a3 | 53 | { |
recotana | 1:6e61ee662fd3 | 54 | DBGF("TCPSOCKET_CONTIMEOUT!!\r\n"); |
recotana | 0:432b571e11a3 | 55 | socketClose(); |
recotana | 1:6e61ee662fd3 | 56 | ledTcpConnect(false); |
recotana | 0:432b571e11a3 | 57 | break; |
recotana | 0:432b571e11a3 | 58 | } |
recotana | 0:432b571e11a3 | 59 | case TCPSOCKET_CONRST: |
recotana | 0:432b571e11a3 | 60 | { |
recotana | 1:6e61ee662fd3 | 61 | DBGF("TCPSOCKET_CONRST!!\r\n"); |
recotana | 0:432b571e11a3 | 62 | socketClose(); |
recotana | 1:6e61ee662fd3 | 63 | ledTcpConnect(false); |
recotana | 0:432b571e11a3 | 64 | break; |
recotana | 0:432b571e11a3 | 65 | } |
recotana | 0:432b571e11a3 | 66 | case TCPSOCKET_CONABRT: |
recotana | 0:432b571e11a3 | 67 | { |
recotana | 1:6e61ee662fd3 | 68 | DBGF("TCPSOCKET_CONABRT!!\r\n"); |
recotana | 0:432b571e11a3 | 69 | socketClose(); |
recotana | 1:6e61ee662fd3 | 70 | ledTcpConnect(false); |
recotana | 0:432b571e11a3 | 71 | break; |
recotana | 0:432b571e11a3 | 72 | } |
recotana | 0:432b571e11a3 | 73 | case TCPSOCKET_ERROR: |
recotana | 1:6e61ee662fd3 | 74 | { DBGF("TCPSOCKET_ERROR\r\n"); |
recotana | 0:432b571e11a3 | 75 | socketClose(); |
recotana | 1:6e61ee662fd3 | 76 | ledTcpConnect(false); |
recotana | 0:432b571e11a3 | 77 | break; |
recotana | 0:432b571e11a3 | 78 | |
recotana | 0:432b571e11a3 | 79 | } |
recotana | 0:432b571e11a3 | 80 | case TCPSOCKET_DISCONNECTED: |
recotana | 0:432b571e11a3 | 81 | { |
recotana | 1:6e61ee662fd3 | 82 | DBGF("TCPSOCKET_DISCONNECTED...\r\n"); |
recotana | 0:432b571e11a3 | 83 | socketClose(); |
recotana | 1:6e61ee662fd3 | 84 | ledTcpConnect(false); |
recotana | 0:432b571e11a3 | 85 | break; |
recotana | 0:432b571e11a3 | 86 | } |
recotana | 0:432b571e11a3 | 87 | } |
recotana | 1:6e61ee662fd3 | 88 | DBGF("...client event out\r\n"); |
recotana | 0:432b571e11a3 | 89 | } |
recotana | 0:432b571e11a3 | 90 | |
recotana | 0:432b571e11a3 | 91 | void clientInit(void){ |
recotana | 0:432b571e11a3 | 92 | sendSock.setOnEvent(&onSendEvent); |
recotana | 0:432b571e11a3 | 93 | clientStatus=close; |
recotana | 0:432b571e11a3 | 94 | } |
recotana | 0:432b571e11a3 | 95 | |
recotana | 0:432b571e11a3 | 96 | int clientConnect(IpAddr ip , int port) |
recotana | 0:432b571e11a3 | 97 | { |
recotana | 0:432b571e11a3 | 98 | if(clientStatus == connecting){ |
recotana | 1:6e61ee662fd3 | 99 | DBGF("socket already connecting..\r\n"); |
recotana | 0:432b571e11a3 | 100 | return 0; |
recotana | 0:432b571e11a3 | 101 | } |
recotana | 0:432b571e11a3 | 102 | else if(clientStatus == connected){ |
recotana | 1:6e61ee662fd3 | 103 | DBGF("socket already connected..\r\n"); |
recotana | 0:432b571e11a3 | 104 | return 0; |
recotana | 0:432b571e11a3 | 105 | } |
recotana | 0:432b571e11a3 | 106 | |
recotana | 0:432b571e11a3 | 107 | Host server(ip, port); |
recotana | 0:432b571e11a3 | 108 | |
recotana | 0:432b571e11a3 | 109 | TCPSocketErr err= sendSock.connect(server); |
recotana | 0:432b571e11a3 | 110 | if (err != TCPSOCKET_OK) { |
recotana | 1:6e61ee662fd3 | 111 | DBGF("connect Error. "); |
recotana | 0:432b571e11a3 | 112 | dispTcpError(err); |
recotana | 0:432b571e11a3 | 113 | clientStatus=close; |
recotana | 0:432b571e11a3 | 114 | return -1; |
recotana | 0:432b571e11a3 | 115 | } |
recotana | 0:432b571e11a3 | 116 | clientStatus=connecting; |
recotana | 0:432b571e11a3 | 117 | return 0; |
recotana | 0:432b571e11a3 | 118 | } |
recotana | 0:432b571e11a3 | 119 | |
recotana | 0:432b571e11a3 | 120 | int tcpSend(char *data,int length){ |
recotana | 1:6e61ee662fd3 | 121 | |
recotana | 1:6e61ee662fd3 | 122 | ledEther(); |
recotana | 1:6e61ee662fd3 | 123 | int ret = sendSock.send(data, length); |
recotana | 1:6e61ee662fd3 | 124 | |
recotana | 1:6e61ee662fd3 | 125 | return ret; |
recotana | 0:432b571e11a3 | 126 | |
recotana | 0:432b571e11a3 | 127 | } |
recotana | 0:432b571e11a3 | 128 | |
recotana | 0:432b571e11a3 | 129 | void checkConnect(void){ |
recotana | 0:432b571e11a3 | 130 | |
recotana | 0:432b571e11a3 | 131 | if(clientStatus != close) return; |
recotana | 0:432b571e11a3 | 132 | |
recotana | 0:432b571e11a3 | 133 | clientConnect( clientIp,clientPort ); |
recotana | 0:432b571e11a3 | 134 | return; |
recotana | 0:432b571e11a3 | 135 | } |
recotana | 0:432b571e11a3 | 136 | |
recotana | 0:432b571e11a3 | 137 | void socketClose(void){ |
recotana | 0:432b571e11a3 | 138 | sendSock.close(); |
recotana | 0:432b571e11a3 | 139 | clientStatus=close; |
recotana | 0:432b571e11a3 | 140 | } |
recotana | 0:432b571e11a3 | 141 | |
recotana | 0:432b571e11a3 | 142 | |
recotana | 0:432b571e11a3 | 143 | |
recotana | 0:432b571e11a3 | 144 | void connectCheckStart(IpAddr ip , int port) |
recotana | 0:432b571e11a3 | 145 | { |
recotana | 0:432b571e11a3 | 146 | clientIp=ip; |
recotana | 0:432b571e11a3 | 147 | clientPort=port; |
recotana | 1:6e61ee662fd3 | 148 | clientCheck.attach(&checkConnect,3); |
recotana | 0:432b571e11a3 | 149 | } |
recotana | 0:432b571e11a3 | 150 | |
recotana | 1:6e61ee662fd3 | 151 | int tcpSendPayload(Payload *_payload) |
recotana | 1:6e61ee662fd3 | 152 | { |
recotana | 1:6e61ee662fd3 | 153 | uint8_t tmp[_payload->size]; |
recotana | 1:6e61ee662fd3 | 154 | |
recotana | 1:6e61ee662fd3 | 155 | memcpy(tmp,(uint8_t*)_payload,_payload->size); |
recotana | 1:6e61ee662fd3 | 156 | |
recotana | 1:6e61ee662fd3 | 157 | payloadToNetwork((Payload *)tmp); |
recotana | 1:6e61ee662fd3 | 158 | |
recotana | 1:6e61ee662fd3 | 159 | return tcpSend((char*)tmp,_payload->size); |
recotana | 1:6e61ee662fd3 | 160 | } |
recotana | 1:6e61ee662fd3 | 161 |