Eric Jung / GMMP_mbed

Fork of GMMP_mbed by SKTelecom_ThingPlug

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Network.cpp Source File

Network.cpp

Go to the documentation of this file.
00001 /** TCP Socket 통신을 위한 모듈
00002  * @file Network.cpp
00003  * @date 2015/07/20
00004  * @version 0.0.1.0
00005  **/
00006 
00007 #include "Client.h"
00008 
00009 #include "Network.h"
00010 #include "GMMP_Operation.h"
00011 
00012 int g_socket = -1;
00013 
00014 //Client client;
00015 extern Client *g_pClient; //lesmin
00016 
00017 void CloseSocket()
00018 {
00019     if (g_socket <= 0) return;
00020 
00021     g_socket = -1;
00022     
00023     return;
00024 }
00025 
00026 int CheckSocket()
00027 {
00028     return 0;
00029 }
00030 
00031 int Connect()
00032 {  
00033   int ret = GMMP_SUCCESS;  
00034 
00035   //delay(2000);
00036   infoln("Connecting...");
00037   INFO("server ip: %d.%d.%d.%d:%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3], g_nServerPort);
00038 
00039   char szServerIp[24];
00040   sprintf(szServerIp, "%d.%d.%d.%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3]);
00041 
00042     if(g_pClient == NULL) {
00043         ERR("pClient is NULL!!!");
00044         return LIB_PARAM_ERROR;
00045     }
00046 
00047 //  if (client.connect((char *)szServerIp, g_nServerPort)) {
00048     /*
00049     if (g_pClient->connect((char *)szServerIp, g_nServerPort)) {
00050         infoln("Connected!!");
00051     } else {
00052         infoln("connection failed!");
00053         ret = SERVER_CONNECT_ERROR;
00054     }
00055     */
00056     int retry = 30;
00057     do
00058     {       
00059         if(retry <= 0) {
00060             ret = SERVER_CONNECT_ERROR;
00061             break;
00062         }
00063         
00064         infoln("Trying to connect to Server...");
00065         if (g_pClient->connect((char *)szServerIp, g_nServerPort)) {
00066             infoln("Connected!");
00067             break;
00068         }   
00069         
00070         infoln("Connection failed!");
00071     } while(retry--);
00072 
00073     return ret;
00074 }
00075 
00076 int WriteTCP(char* pBuf, int nLen)
00077 {
00078     DBG("WriteTCP(): %d", nLen);
00079 
00080     if (pBuf == NULL || nLen <= 0) return LIB_PARAM_ERROR;
00081 
00082     if(g_pClient == NULL) {
00083         ERR("pClient is NULL!!!");
00084         return LIB_PARAM_ERROR;
00085     }
00086 //  client.write(pBuf, nLen);
00087     g_pClient->write(pBuf, nLen);
00088 
00089     return GMMP_SUCCESS;
00090 }
00091 
00092 int ReadTCP(char* pBuf, const int nMaxlen)
00093 {
00094   return ReadTCP2(pBuf, nMaxlen, 1);
00095 }
00096 
00097 int ReadTCPAsync(char* pBuf, const int nMaxlen);
00098 
00099 int ReadTCP2(char* pBuf, const int nMaxlen, byte blocking)
00100 {
00101   //blocking = 0;
00102 
00103   if (!blocking) {
00104     return ReadTCPAsync(pBuf, nMaxlen);
00105   }
00106 
00107     if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
00108 
00109     if(g_pClient == NULL) {
00110         ERR("pClient is NULL!!!");
00111         return LIB_PARAM_ERROR;
00112     }
00113 
00114     int idx = 0;
00115     memset(pBuf,  0, nMaxlen);
00116 
00117     while(idx < nMaxlen)
00118     {
00119 //    if (client.available()) {
00120     if (g_pClient->available()) {
00121 //        pBuf[idx] = client.read();
00122           pBuf[idx] = g_pClient->read();
00123       DBG("%d", idx);
00124       DBG(" %c",(byte)pBuf[idx]);
00125       idx++;
00126     } else {
00127       //debugln("E_WOULDBLOCK");
00128       return E_WOULDBLOCK;
00129     }
00130     }
00131 
00132     return GMMP_SUCCESS;
00133 }
00134 
00135 int ReadTCPAsync(char* pBuf, const int nMaxlen)
00136 {
00137     if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
00138 
00139     if(g_pClient == NULL) {
00140         ERR("pClient is NULL!!!");
00141         return LIB_PARAM_ERROR;
00142     }
00143 
00144     int idx = 0;
00145   char val = -1;
00146 
00147     memset(pBuf,  0, nMaxlen);
00148 
00149     while(idx < nMaxlen)
00150     {
00151 //    val = client.read();
00152     val = g_pClient->read();
00153 
00154     if (val != -1) {
00155           pBuf[idx++] = (byte)val;
00156       
00157       /*
00158           pBuf[idx] = (byte)val;
00159       debug(idx);
00160       debug(" ");
00161       debugln((byte)pBuf[idx]);
00162       idx++;
00163       */
00164     } else {
00165       return E_WOULDBLOCK;
00166     }
00167     }
00168 
00169     return GMMP_SUCCESS;
00170 }