안 병학 / GMMP

Fork of GMMP 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 Client *pClient = NULL;
00016 
00017 void CloseSocket()
00018 {
00019     if (g_socket <= 0) return;
00020 
00021     g_socket = -1;
00022     
00023     delete pClient;
00024     pClient = NULL;
00025 
00026     return;
00027 }
00028 
00029 int CheckSocket()
00030 {
00031     return 0;
00032 }
00033 
00034 int Connect()
00035 {
00036   int ret = GMMP_SUCCESS;
00037 
00038   if(pClient == NULL)
00039     pClient = new Client;
00040 
00041   //delay(2000);
00042   infoln("Connecting...");
00043   INFO("server ip: %d.%d.%d.%d:%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3], g_nServerPort);
00044 
00045   char szServerIp[24];
00046   sprintf(szServerIp, "%d.%d.%d.%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3]);
00047 
00048   if (/*client.)*/pClient->connect((char *)szServerIp, g_nServerPort)) {
00049     infoln("Connected!!");
00050   } else {
00051     infoln("connection failed!");
00052     ret = SERVER_CONNECT_ERROR;
00053   }
00054 
00055     return ret;
00056 }
00057 
00058 int WriteTCP(char* pBuf, int nLen)
00059 {
00060     DBG("WriteTCP(): %d", nLen);
00061 
00062     if (pBuf == NULL || nLen <= 0) return LIB_PARAM_ERROR;
00063 
00064   /*client.*/pClient->write(pBuf, nLen);
00065 
00066     return GMMP_SUCCESS;
00067 }
00068 
00069 int ReadTCP(char* pBuf, const int nMaxlen)
00070 {
00071   return ReadTCP2(pBuf, nMaxlen, 1);
00072 }
00073 
00074 int ReadTCPAsync(char* pBuf, const int nMaxlen);
00075 
00076 int ReadTCP2(char* pBuf, const int nMaxlen, byte blocking)
00077 {
00078   //blocking = 0;
00079 
00080   if (!blocking) {
00081     return ReadTCPAsync(pBuf, nMaxlen);
00082   }
00083 
00084     if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
00085 
00086     int idx = 0;
00087 
00088     memset(pBuf,  0, nMaxlen);
00089 
00090     while(idx < nMaxlen)
00091     {
00092     if (/*client.*/pClient->available()) {
00093           pBuf[idx] = /*client.*/pClient->read();
00094       DBG("%d", idx);
00095       DBG(" %c",(byte)pBuf[idx]);
00096       idx++;
00097     } else {
00098       debugln("E_WOULDBLOCK");
00099       return E_WOULDBLOCK;
00100     }
00101     }
00102 
00103     return GMMP_SUCCESS;
00104 }
00105 
00106 int ReadTCPAsync(char* pBuf, const int nMaxlen)
00107 {
00108     if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
00109 
00110     int idx = 0;
00111   char val = -1;
00112 
00113     memset(pBuf,  0, nMaxlen);
00114 
00115     while(idx < nMaxlen)
00116     {
00117     val = /*client.*/pClient->read();
00118 
00119     if (val != -1) {
00120           pBuf[idx++] = (byte)val;
00121       
00122       /*
00123           pBuf[idx] = (byte)val;
00124       debug(idx);
00125       debug(" ");
00126       debugln((byte)pBuf[idx]);
00127       idx++;
00128       */
00129     } else {
00130       return E_WOULDBLOCK;
00131     }
00132     }
00133 
00134     return GMMP_SUCCESS;
00135 }