void closeConnection() { if (cSocket.Close() != 0) { CCLog("ODSocket :closeConnection Close Error,errCode = %d!",cSocket.GetError()); } if (cSocket.Clean() != 0) { CCLog("ODSocket :closeConnection Clean Error,errCode = %d!",cSocket.GetError()); } // g_NetworkStat = NetworkStat_NotConnect; CCLog(" -----Network closed-----"); // g_NetworkStat = NetworkStat_NotConnect; MessageAutoLock lock; RequestMessage* temp; int count = 0; while(!requestMsgQueue.empty()) { count ++; temp = requestMsgQueue.front(); requestMsgQueue.pop(); delete temp; } CCLog("%s --> throwed %d request packet(s)",__FUNCTION__,count); // 这也不需要,因为ODSocket::Clean()有调用这个函数 //#ifdef WIN32 // WSACleanup(); //#endif }
void netSocketManger::endSocket() { if (!threadSend.joinable()) {//线程已启动 threadSend.~thread(); delete m_sendEvent; m_sendEvent = NULL; } if (!threadRecive.joinable()) { threadRecive.~thread(); } while (!m_sendQueue.IsEmpty()) { SocketData *sendData = m_sendQueue.Pop(); //CC_SAFE_FREE(sendData->body); free(sendData); } while (!m_mainQueue.IsEmpty()) { SocketData *mainData = m_mainQueue.Pop(); CC_SAFE_FREE(mainData); //CC_SAFE_FREE(mainData->addContent); } if (isConnect) { cSocket.Close(); cSocket.Clean(); isConnect = false; } m_isFirst = true; m_sendDelegateList.clear(); }
void* TigerThread::thread_funcation(void *arg) { ODSocket *pSocket = ODSocket::sharedODSocket(); pSocket->Init(); pSocket->Create(AF_INET, SOCK_STREAM,0); /* ///////////////////////////////////////////// char ipaddr[20]; cSocket.DnsParse("www.baidu.com", ipaddr); //192.168.22.154 //aliyun-027.btcsky.com /////////////////////////////////////////////--DNS解析 www->ip */ if (pSocket->Connect("58.83.190.220", 6621)) { char recvBuf[1024] = "\0"; // std::string testmsg= |login|[email protected],e10adc3949ba59abbe56e057f20f883e,68:A8:6D:10:66:DC|$\r\n std::string testmsg= pSocket->msg_To_Sever; pSocket->Send(testmsg.data(),testmsg.length(),0); pSocket->Recv(recvBuf,1024,0); std::string rec_msg=std::string(recvBuf); printf("%s",rec_msg.c_str()); } else { CCLog("服务器连接失败"); } pSocket->Close(); pSocket->Clean(); return NULL; }
//主线程更新 void netSocketManger::update(float dt) { if (!m_mainQueue.IsEmpty()) { SocketData *data = m_mainQueue.Pop(); if (data->eventType == REQUEST) { if (m_pushDelegate) { m_pushDelegate->pushHandler(data); } }else if (data->eventType == RESPONSE) { SendDelegateMap::iterator it = m_sendDelegateList.find(data->sn); if (it != m_sendDelegateList.end()) { SendDataDelegate *delegate = it->second; m_sendDelegateList.erase(it); delegate->sendHandler(data); } }else if (data->eventType == CONNECT_SUCCEED || data->eventType == CONNECT_FAIL || data->eventType == DISCONNECT) { if (data->eventType != CONNECT_SUCCEED) { if (!threadSend.joinable()) {//线程已启动 threadSend.~thread(); delete m_sendEvent; m_sendEvent = NULL; } if (!threadRecive.joinable()) { threadRecive.~thread(); } while (!m_sendQueue.IsEmpty()) { SocketData *sendData = m_sendQueue.Pop(); //litaoming update //CC_SAFE_FREE(data->body); //CC_SAFE_FREE(sendData->reciveBody); free(sendData); } while (!m_mainQueue.IsEmpty()) { SocketData *mainData = m_mainQueue.Pop(); if (mainData != data) { //CC_SAFE_FREE(mainData->body); //CC_SAFE_FREE(mainData->addContent); } } if (isConnect) { cSocket.Close(); cSocket.Clean(); isConnect = false; } m_isFirst = true; m_sendDelegateList.clear(); } if (m_statusDelegate) { m_statusDelegate->statusHandler(data->eventType); } } //CC_SAFE_FREE(data->body); //CC_SAFE_FREE(data->addContent); free(data); } }
void MapScene::clientR() { ODSocket cSocketR; cSocketR.Init(); cSocketR.Create(AF_INET,SOCK_STREAM,0); while(!cSocketR.Connect(ip.c_str(),portW)) { #ifdef WIN32 Sleep(1000); #else usleep(1000 * 1000); #endif } //因为是client 所以用的是portR; char buf[BLOCK_SIZE]; for (;;) { if (!start) return ; int len = cSocketR.Recv(buf,BLOCK_SIZE ,0); if (len <= 0 ) { heartBeat = false; break; } if (!status) break; int pAngle; int x; int y; int avatarId; int type = 0; int number; bool flag[MAX][4][20]; int t_life = 0; int state = 0; avatarId = getIntFromStr(buf, 1); state = getIntFromStr(buf+1, 1); t_life = getIntFromStr(buf+2, 2); pAngle = getIntFromStr(buf+4, 4); x = getIntFromStr(buf+8, 4); y = getIntFromStr(buf+12, 4); avatar[avatarId]->pAngle = pAngle; avatar[avatarId]->getsprite()->setRotation(pAngle); avatar[avatarId]->getsprite()->setPosition(ccp(x,y)); if (!avatar[avatarId]->getsprite()->getIsVisible() && status) avatar[avatarId]->getsprite()->setIsVisible(true); len = 16; if (state == 1 && !player->isFrozen) { player->isFrozen = true; schedule(schedule_selector(MapScene::freezeplayer), 0.1f); schedule(schedule_selector(MapScene::revertplayer), 2.0f); } if (t_life < player->life) { if (player->life == t_life + 5) { schedule(schedule_selector(MapScene::attackByBomb),0.1f); } reduceLife(t_life); player->life = t_life; schedule(schedule_selector(MapScene::syncLife), 0.1f); } for (int i = 0 ; i < MAX ; ++i) for (int j = 0 ; j <= 2; ++j) for (int k = 0 ; k < avatarBullets[i][j]->capacity(); ++k) flag[i][j][k] = false; while(1) { type = getIntFromStr(buf+len, 1); if (type == 9) break; len += 1; number = getIntFromStr(buf+len, 2); len += 2; pAngle = getIntFromStr(buf+len, 4); len += 4; x = getIntFromStr(buf+len, 4); len += 4; y = getIntFromStr(buf+len, 4); len += 4; GameBullet * mybullet = (GameBullet *)(avatarBullets[avatarId][type]->objectAtIndex(number)); mybullet->vAngle = pAngle; mybullet->getsprite()->setRotation(pAngle); mybullet->getsprite()->setPosition(ccp(x,y)); if (!mybullet->getsprite()->getIsVisible()) mybullet->getsprite()->setIsVisible(true); flag[avatarId][type][number] = true; } for (int i = 0 ; i < MAX ; ++i) for (int j = 0 ; j <= 2; ++j) for (int k = 0 ; k < avatarBullets[i][j]->capacity(); ++k) { if (!flag[i][j][k]) { ((GameBullet *)(avatarBullets[i][j]->objectAtIndex(k)))->getsprite()->setIsVisible(false); } } if (t_life == 0) break; } cSocketR.Close(); cSocketR.Clean(); }
void MapScene::clientW() { ODSocket cSocketW; cSocketW.Init(); cSocketW.Create(AF_INET,SOCK_STREAM,0); while(!cSocketW.Connect(ip.c_str(),portR)) { #ifdef WIN32 Sleep(1000); #else usleep(1000 * 1000); #endif } ; //因为是client 所以用的是portR; char buf[BLOCK_SIZE]; sprintf(buf,"%1d",id); int aid = -1; int state = 0; while(1) { if (!start) break; for(int i = 0 ; i < MAX; ++i) { if (avatar[i]->getsprite()->getIsVisible()) { aid = i; break; } } state = 0; if(avatar[aid]->isFrozen) { state = 1; } sprintf(buf+1,"%1d",state); sprintf(buf+2,"%2d",avatarLife); CCPoint herop = (player->getsprite())->getPosition(); int dy = herop.y; int dx = herop.x; int angle = (int)player->pAngle; sprintf(buf+4, "%4d", angle); sprintf(buf+8, "%4d", dx); sprintf(buf+12, "%4d", dy); int len = 16; for (int i = 0; i <= 2; ++i) for (int j = 0; j < bullets[i]->capacity(); ++j) { GameBullet * mybullet = (GameBullet *)(bullets[i]->objectAtIndex(j)); if (mybullet->getsprite()->getIsVisible()) { CCPoint p = (mybullet->getsprite()->getPosition()); sprintf(buf+len, "%1d", (int)i); len += 1; sprintf(buf+len, "%2d", (int)j); len += 2; sprintf(buf+len, "%4d", (int)mybullet->vAngle); len += 4; sprintf(buf+len, "%4d", (int)p.x); len += 4; sprintf(buf+len, "%4d", (int)p.y); len += 4; } } buf[len] = '9'; //if (demo > 3 || temp != 0) //{ if (cSocketW.Send(buf, BLOCK_SIZE ,0)== -1) break; // } #ifdef WIN32 Sleep(30); #else usleep(30 * 1000); #endif } cSocketW.Close(); cSocketW.Clean(); }