server/client

Dependencies:   mbed NetServicesMin

Revision:
1:6e61ee662fd3
Parent:
0:432b571e11a3
--- a/tcp_client.cpp	Sun Feb 19 06:21:50 2012 +0000
+++ b/tcp_client.cpp	Thu Feb 23 02:38:05 2012 +0000
@@ -1,10 +1,14 @@
+#include "types.h"
 #include "mbed.h"
 #include "config.h"
 #include "tcp_error.h"
 #include "tcp_client.h"
 
+#include "leds.h"
+
 extern IpAddr inCommingIp;
 
+
 Host server;
 TCPSocket sendSock;
 ConnectStatus clientStatus;
@@ -15,62 +19,73 @@
 int clientPort;
 
 void onSendEvent (TCPSocketEvent e) {
-    printf("... client event arrival!\n");
+    DBGF("... client event arrival!\r\n");
     switch(e)
     {
-        case TCPSOCKET_CONNECTED: // Ú‘±Š®—¹
+        case TCPSOCKET_CONNECTED:
         {
-            char str[]="connect!";
-            sendSock.send(str,strlen(str) ); // ‘—M
+            ledTcpConnect(true);
+            
+            char str[]="wellcome! server connected!";
+            
+            sendSock.send(str,strlen(str) );
+            
             clientStatus=connected;
-            printf("TCPSOCKET_CONNECTED!!\n");
+            DBGF("TCPSOCKET_CONNECTED!!\r\n");
             break;
          }
 
-        case TCPSOCKET_WRITEABLE: // ‘—M‰Â”\i‘—MŠ®—¹j
+        case TCPSOCKET_WRITEABLE:
         {
-            printf("TCPSOCKET_WRITEABLE!!\n");
+            DBGF("TCPSOCKET_WRITEABLE!!\r\n");
             clientStatus=connected;
+            ledTcpConnect(true);
             break;
         }
 
-        case TCPSOCKET_READABLE: // ŽóMƒf[ƒ^‚ ‚è
-        {   printf("TCPSOCKET_READABLE!!\n");
+        case TCPSOCKET_READABLE:
+        {   DBGF("TCPSOCKET_READABLE!!\r\n");
             clientStatus=connected;
+            ledTcpConnect(true);
             break;
         }
         case TCPSOCKET_CONTIMEOUT:
         {
-            printf("TCPSOCKET_CONTIMEOUT!!\n");
+            DBGF("TCPSOCKET_CONTIMEOUT!!\r\n");
             socketClose();
+            ledTcpConnect(false);
             break;
         }
         case TCPSOCKET_CONRST:
         {
-             printf("TCPSOCKET_CONRST!!\n");
+             DBGF("TCPSOCKET_CONRST!!\r\n");
              socketClose();
+             ledTcpConnect(false);
             break;
         }
         case TCPSOCKET_CONABRT:
         {
-             printf("TCPSOCKET_CONABRT!!\n");
+             DBGF("TCPSOCKET_CONABRT!!\r\n");
              socketClose();
+             ledTcpConnect(false);
             break;
         }
         case TCPSOCKET_ERROR:
-        {   printf("TCPSOCKET_ERROR\n");
+        {   DBGF("TCPSOCKET_ERROR\r\n");
             socketClose();
+            ledTcpConnect(false);
             break;
             
         }
          case TCPSOCKET_DISCONNECTED:
         {
-            printf("TCPSOCKET_DISCONNECTED...\n");
+            DBGF("TCPSOCKET_DISCONNECTED...\r\n");
             socketClose();
+            ledTcpConnect(false);
             break;
         }
     }
-    printf("...client event out\n");
+    DBGF("...client event out\r\n");
 }
 
 void clientInit(void){
@@ -81,11 +96,11 @@
 int clientConnect(IpAddr ip , int port)
 {
     if(clientStatus == connecting){
-        printf("socket already connecting..\n");
+        DBGF("socket already connecting..\r\n");
         return 0;
     }
     else if(clientStatus == connected){
-        printf("socket already connected..\n");
+        DBGF("socket already connected..\r\n");
         return 0;
     }
 
@@ -93,7 +108,7 @@
     
     TCPSocketErr err= sendSock.connect(server);
     if (err != TCPSOCKET_OK) {
-        printf("connect Error. ");
+        DBGF("connect Error. ");
         dispTcpError(err);
         clientStatus=close;
         return -1;
@@ -103,8 +118,11 @@
 }
 
 int tcpSend(char *data,int length){
- 
-    return sendSock.send(data, length);
+
+    ledEther();
+    int ret = sendSock.send(data, length);
+
+    return ret;
     
 }
 
@@ -127,7 +145,17 @@
 {
     clientIp=ip;
     clientPort=port;
-    clientCheck.attach(&checkConnect,3);
-    
+    clientCheck.attach(&checkConnect,3);   
 }
 
+int tcpSendPayload(Payload *_payload)
+{
+    uint8_t tmp[_payload->size];
+    
+    memcpy(tmp,(uint8_t*)_payload,_payload->size);
+    
+    payloadToNetwork((Payload *)tmp);
+    
+    return tcpSend((char*)tmp,_payload->size);
+}
+