Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ThingPlug_Ethernet_Example
Fork of GMMP_mbed by
Network.cpp
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 g_socket = -1; 00021 00022 return; 00023 } 00024 00025 int CheckSocket() 00026 { 00027 return 0; 00028 } 00029 00030 int Connect() 00031 { 00032 int ret = GMMP_SUCCESS; 00033 00034 //delay(2000); 00035 infoln("Connecting..."); 00036 INFO("server ip: %d.%d.%d.%d:%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3], g_nServerPort); 00037 00038 char szServerIp[24]; 00039 sprintf(szServerIp, "%d.%d.%d.%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3]); 00040 00041 if(g_pClient == NULL) { 00042 ERR("pClient is NULL!!!"); 00043 return LIB_PARAM_ERROR; 00044 } 00045 00046 // if (client.connect((char *)szServerIp, g_nServerPort)) { 00047 /* 00048 if (g_pClient->connect((char *)szServerIp, g_nServerPort)) { 00049 infoln("Connected!!"); 00050 } else { 00051 infoln("connection failed!"); 00052 ret = SERVER_CONNECT_ERROR; 00053 } 00054 */ 00055 int retry = 30; 00056 do 00057 { 00058 if(retry <= 0) { 00059 ret = SERVER_CONNECT_ERROR; 00060 break; 00061 } 00062 00063 infoln("Trying to connect to Server..."); 00064 if (g_pClient->connect((char *)szServerIp, g_nServerPort)) { 00065 infoln("Connected!"); 00066 break; 00067 } 00068 00069 infoln("Connection failed!"); 00070 } while(retry--); 00071 00072 // ## 20150812 debug 00073 //if(g_pClient->connected()) 00074 //{ 00075 // printf("Debug >> Send Hello\r\n"); 00076 // g_pClient->write("hello", 6); // data sending success 00077 //} 00078 00079 return ret; 00080 } 00081 00082 int WriteTCP(char* pBuf, int nLen) 00083 { 00084 DBG("WriteTCP(): %d", nLen); 00085 INFO("Connection Status = %s", g_pClient->connected() ? "Connected":"Disconnected"); 00086 00087 if (pBuf == NULL || nLen <= 0) return LIB_PARAM_ERROR; 00088 00089 if(g_pClient == NULL) { 00090 ERR("pClient is NULL!!!"); 00091 return LIB_PARAM_ERROR; 00092 } 00093 // client.write(pBuf, nLen); 00094 g_pClient->write(pBuf, nLen); 00095 00096 return GMMP_SUCCESS; 00097 } 00098 00099 int ReadTCP(char* pBuf, const int nMaxlen) 00100 { 00101 return ReadTCP2(pBuf, nMaxlen, 1); 00102 } 00103 00104 int ReadTCPAsync(char* pBuf, const int nMaxlen); 00105 00106 int ReadTCP2(char* pBuf, const int nMaxlen, byte blocking) 00107 { 00108 //blocking = 0; 00109 00110 if (!blocking) { 00111 return ReadTCPAsync(pBuf, nMaxlen); 00112 } 00113 00114 if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR; 00115 00116 if(g_pClient == NULL) { 00117 ERR("pClient is NULL!!!"); 00118 return LIB_PARAM_ERROR; 00119 } 00120 00121 int idx = 0; 00122 memset(pBuf, 0, nMaxlen); 00123 00124 while(idx < nMaxlen) 00125 { 00126 // if (client.available()) { 00127 if (g_pClient->available()) { 00128 // pBuf[idx] = client.read(); 00129 pBuf[idx] = g_pClient->read(); 00130 DBG("%d", idx); 00131 DBG(" %c",(byte)pBuf[idx]); 00132 idx++; 00133 } else { 00134 //debugln("E_WOULDBLOCK"); 00135 return E_WOULDBLOCK; 00136 } 00137 } 00138 00139 return GMMP_SUCCESS; 00140 } 00141 00142 int ReadTCPAsync(char* pBuf, const int nMaxlen) 00143 { 00144 if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR; 00145 00146 if(g_pClient == NULL) { 00147 ERR("pClient is NULL!!!"); 00148 return LIB_PARAM_ERROR; 00149 } 00150 00151 int idx = 0; 00152 char val = -1; 00153 00154 memset(pBuf, 0, nMaxlen); 00155 00156 while(idx < nMaxlen) 00157 { 00158 // val = client.read(); 00159 val = g_pClient->read(); 00160 00161 if (val != -1) { 00162 pBuf[idx++] = (byte)val; 00163 00164 /* 00165 pBuf[idx] = (byte)val; 00166 debug(idx); 00167 debug(" "); 00168 debugln((byte)pBuf[idx]); 00169 idx++; 00170 */ 00171 } else { 00172 return E_WOULDBLOCK; 00173 } 00174 } 00175 00176 return GMMP_SUCCESS; 00177 }
Generated on Tue Jul 12 2022 21:35:52 by
