以下是由Socket接收到数据,然后再对数据进行解析。
NormalText Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
ComSimuData.h
class ComSimuData { public: ComSimuData(); ~ComSimuData(); VOID SetCarARRouteData(COM_CarARRouteData &routeData); ///AR用ルート道塗情報取得 eCOM_ERROR_CODE GetCarARRouteData( COM_CarARRouteData& out_cararrtdt ///AR用ルート道塗情報 【OUT】 ); private: COM_CarARRouteData *m_pRouteData; NP_BOOL bRouteData; }; ComSimuData.cpp #ifndef CXX_COMSIMUDATA_H # include "ComSimuData.h" #endif ComSimuData::ComSimuData() :bRouteData(NP_FALSE) { m_pRouteData = new COM_CarARRouteData(); } ComSimuData::~ComSimuData() { if(m_pRouteData != NULL) { delete m_pRouteData; m_pRouteData = NULL; } } VOID ComSimuData::SetCarARRouteData(COM_CarARRouteData &routeData) { m_pRouteData->routeID = routeData.routeID; m_pRouteData->numOfARRtData = routeData.numOfARRtData; memcpy(&(m_pRouteData->carARRoute),routeData.carARRoute,sizeof(routeData.carARRoute)); } eCOM_ERROR_CODE ComSimuData::GetCarARRouteData(COM_CarARRouteData &out_cararrtdt) { if((&out_cararrtdt) != NULL) { out_cararrtdt.routeID = m_pRouteData->routeID; out_cararrtdt.numOfARRtData = m_pRouteData->numOfARRtData; memcpy(&(out_cararrtdt.carARRoute),&(m_pRouteData->carARRoute),sizeof(m_pRouteData->carARRoute)); return COM_ERROR_CODE_NON; } else { return COM_ERROR_CODE_INITIALIZE_FAILED; } } ComSimuListener使用scoket接收数据 #include "stdafx.h" #ifndef CXX_COMSIMULISTENER_H # include "ComSimuListener.h" #endif #ifndef CXX_AL_COMMONFILE_H # include "AL_CommonFile.h" #endif #ifndef CXX_AL_LOG_H # include "AL_Log.h" #endif #ifndef CXX_COMCALLBACK_H # include "ComCallBack.h" #endif #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #define COMDEBUG #include "ComSimuData.h" #include "COM_EventDef.h" const char COM_HOST_IP_PORT_CONF_FILE[] = "/sdcard/Simu_Phone_Conf.xml"; const SHORT COM_HOST_PORT = 2345; const int COM_RECV_BUFFER_LEN = 400 * 1024; const BYTE* s_buf = new BYTE[COM_RECV_BUFFER_LEN]; const char COM_DATA_HEAD[] = "COMDATAHEAD"; const char COM_DATA_ENDING[] = "COMDATAENDING"; ComSimuListener::ComSimuListener() : m_bStop(NP_FALSE) , m_iSockFD(-1) , m_pComCallBack() { } ComSimuListener::~ComSimuListener() { if(-1 != m_iSockFD) { StopListen(); } if(s_buf) { delete[] s_buf; } } VOID ComSimuListener::SetComCallBack(ComCallBack* pCallBack) { m_pComCallBack = pCallBack; } NP_BOOL ComSimuListener::StartListen() { m_bStop = NP_FALSE; DWORD ip; SHORT port; if(NP_TRUE == GetServIPPort(ip, port)) { Listen(ip, COM_HOST_PORT); } return NP_TRUE; } VOID ComSimuListener::StopListen() { OutputDebugLog("[Com] ComSimuListener->StopListen()"); m_bStop = NP_TRUE; shutdown(m_iSockFD, 2); close(m_iSockFD); m_iSockFD = -1; OutputDebugLog("[Com] awake"); } NP_BOOL ComSimuListener::GetServIPPort(DWORD& ip, SHORT& port) { AL_CommonFile conff; if(NP_TRUE == conff.Open(COM_HOST_IP_PORT_CONF_FILE)) { char buf[101]; memset(&buf, 0x00, 1001); DWORD read = 0; if(NP_TRUE == conff.Read(buf, 100, (DWORD*)NULL )) { AL_String content(buf); int colon = content.Find(":"); if(colon != -1) { in_addr in; if(inet_aton(content.Left(colon).GetString(), &in)) { ip = in.s_addr; } port = atoi(content.Right(content.GetLength() - colon - 1).GetString()); } } } NP_BOOL ret = NP_TRUE; if(port == 0 || ip == 0) { ret = NP_FALSE; } return ret; } VOID ComSimuListener::Listen(DWORD ip, SHORT port) { int client_fd; struct sockaddr_in my_addr; struct sockaddr_in remote_addr; if ((m_iSockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1) { AL_Log::Output(AL_LOG_KIND_COM, "[Com] Create socket error"); // temp: Log id return; } OutputDebugLog("[Com] Client prepare to connect!"); BOOL bReuseaddr = TRUE; setsockopt(m_iSockFD,SOL_SOCKET,15,(const char*)&bReuseaddr,sizeof(BOOL)); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(port); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero),8); if (bind(m_iSockFD, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { AL_Log::Output(AL_LOG_KIND_COM, "[Com] bind socket error"); // temp: Log id return; } if (listen(m_iSockFD, 20) == -1) { AL_Log::Output(AL_LOG_KIND_COM, "[Com] listen socket error"); // temp: Log id return; } while(1) { if(m_bStop) { break; } int sin_size = sizeof(struct sockaddr_in); if ((client_fd = accept(m_iSockFD, (struct sockaddr *)&remote_addr, (socklen_t*)&sin_size)) == -1) { AL_Log::Output(AL_LOG_KIND_COM, "[Com] accpet socket error"); // temp: Log id continue; } OutputDebugLog("[Com] Client accepted"); while(1){ memset((void*)s_buf, 0, COM_RECV_BUFFER_LEN); int recvSize = recv(client_fd, (void*)s_buf, COM_RECV_BUFFER_LEN, 0); OutputDebugLog("[Com] Received bytes: %d", recvSize); if(recvSize == -1) { AL_Log::Output(AL_LOG_KIND_COM, "[Com] recv socket error"); } if(recvSize <=0 ) { break; } #ifdef COMDEBUG FILE* fp = fopen("/sdcard/comdebug", "a+"); if(fp) { fwrite(s_buf, 1, recvSize, fp); fclose(fp); } #endif OutputDebugLog((char*) s_buf); ProcessRecvData(recvSize); } AL_Log::Output(AL_LOG_KIND_COM, "received a connection from %s \n", inet_ntoa(remote_addr.sin_addr)); // temp: Log id close(client_fd); } } VOID ComSimuListener::ProcessRecvData(int recvSize) { if(!s_buf || recvSize <= strlen(COM_DATA_HEAD)) { return; } if(!IsComData()) { OutputDebugLog("[COM] ComSimuListener ProcessRecvData IsComData FALSE"); AL_Log::Output(AL_LOG_KIND_COM, "[Com] received data is no of com"); return; } for(int offset = strlen(COM_DATA_HEAD); offset < recvSize; ) { if(IsEnd(offset)) { break; } int32_t length = *((int32_t*)(s_buf + offset)); offset += 4; OutputDebugLog("[COM] Receive data's length is %d", length); sDDS_CallBackArgument arg; memset(&arg, 0x00, sizeof(sDDS_CallBackArgument)); memcpy(&arg, s_buf + offset, length); OutputDebugLog("[COM] Arg0 : %02X",((BYTE*)(&arg))[0]); OutputDebugLog("[COM] Arg1 : %02X",((BYTE*)(&arg))[1]); OutputDebugLog("[COM] Arg2 : %02X",((BYTE*)(&arg))[2]); OutputDebugLog("[COM] Arg3 : %02X",((BYTE*)(&arg))[3]); OutputDebugLog("[COM] ComSimuListener ProcessRecvData Notify"); int eventKind = *(int32_t*)(s_buf + offset); offset += 4; int eventID = *(int32_t*)(s_buf + offset); offset += 4; // COM_CALL_BACK_EVENT_ID_CAR_AR_ROUTE if(eventID == COM_CALL_BACK_EVENT_ID_CAR_AR_ROUTE) { COM_CarARRouteData routeData; //类的结构在下面 routeData.routeID = *(DWORD*)(s_buf + offset); offset += sizeof(DWORD); routeData.numOfARRtData = *(WORD*)(s_buf + offset); offset += sizeof(WORD); memcpy(&(routeData.carARRoute), (s_buf + offset), sizeof(sCOM_CarARRoute)); ComSimuData simuData; simuData.SetCarARRouteData(routeData); } m_pComCallBack->Notify(arg); offset += length; } } NP_BOOL ComSimuListener::IsComData() { NP_BOOL ret = NP_FALSE; if(s_buf) { char* pHeader = new char[strlen(COM_DATA_HEAD) + 1]; memset(pHeader, 0, strlen(COM_DATA_HEAD)+ 1); memcpy(pHeader, s_buf, strlen(COM_DATA_HEAD)); ret = strcmp(pHeader, COM_DATA_HEAD) == 0 ? NP_TRUE : NP_FALSE; } return ret; } NP_BOOL ComSimuListener::IsEnd(int offset) { NP_BOOL ret = FALSE; if(s_buf) { char* pHeader = new char[strlen(COM_DATA_ENDING) + 1]; memset(pHeader, 0, strlen(COM_DATA_ENDING)+ 1); memcpy(pHeader, s_buf + offset, strlen(COM_DATA_ENDING)); ret = strcmp(pHeader, COM_DATA_ENDING) == 0 ? NP_TRUE : NP_FALSE; } return ret; } /*NP_BOOL saveMapRouteData(AL_String fileName, DWORD size) { AL_CommonFile file; NP_BOOL flag = NP_FALSE; if(TRUE == file.Open(fileName, AL_FILE_OPEN_MODE_A)) { flag = file.Write(s_buf, size, (DWORD *)NULL); } file.Close(); return flag; }*/ 类COM_CarARRouteData的结构 class COM_CarARRouteData { public: void clearData(); public: DWORD routeID; //DWORD 是unsigned long WORD numOfARRtData; //WORD 是unsigned short sCOM_CarARRoute carARRoute[ 256 ]; }; 结构体:sCOM_CarARRoute的结构 struct sCOM_CarARRoute { SHORT latStart; SHORT lonStart; SHORT latEnd; SHORT lonEnd; //SHORT是short eCOM_CAR_ROAD_TYPE roadType; //布尔值 DWORD parcelID[ 2 ]; //DWORD 是unsigned long } ; |