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();
}
int main()
{
	//往空的队列里添加和删除元素
	CQueue cq;
	cq.Push(4);
	cq.Push(5);
	cout << cq.Pop();
	cout << cq.Pop();
	cout << cq.Pop();
	
	return 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
}
Beispiel #3
0
void CHtSuUnitEx::RecvCallback(Ht::ERecvType recvType)
{
	switch (recvType) {
	case Ht::eRecvHostData:
	{
		if (recvDataCnt == 0) {
			assert(!dataCntQue.Empty());
			recvDataCnt = dataCntQue.Front();
			dataCntQue.Pop();
			printf("eRecvHostData - recvDataCnt = %d\n", recvDataCnt);
		}

		assert(recvDataCnt > 1);
		recvDataCnt -= RecvHostData(recvDataCnt - 1, recvArr);
	}
	break;
	case Ht::eRecvHostDataMarker:
	{
		if (recvDataCnt == 0) {
			assert(!dataCntQue.Empty());
			recvDataCnt = dataCntQue.Front();
			dataCntQue.Pop();
			printf("eRecvDataMarker - recvDataCnt = %d\n", recvDataCnt);
		}

		assert(recvDataCnt == 1);
		bool bMarker = RecvHostDataMarker();
		assert(bMarker);
		recvDataCnt = 0;
		markerCnt += 1;
	}
	break;
	case Ht::eRecvReturn:
	{
		uint16_t recvDataCnt;
		RecvReturn_echo(recvDataCnt);
		rtnCnt += 1;
	}
	break;
	default:
		assert(0);
	}
}
//发送数据线程
void netSocketManger::sendThread()
{

    //bool connect = cSocket.Connect(kServerIP,kServerPort);
    bool connect = cSocket.Connect(netSocketManger::sharednetSocketManger()->server.IP.c_str(),netSocketManger::sharednetSocketManger()->server.port);
   // cSocket.Send("bbb",strlen("bbb")+1,0);

    if (connect) {
        isConnect = true;
        SocketData *errorData = newSocketData();
        errorData->eventType = CONNECT_SUCCEED;
        m_mainQueue.Push(errorData);
        //创建接收线程
        m_mutexx.lock();
        netSocketManger::sharednetSocketManger()->createReciveThread();
        m_mutexx.unlock();
        m_sendEvent->Lock();
        while (true) {
            while (!m_sendQueue.IsEmpty()) {
                SocketData *data = m_sendQueue.Pop();
                uLong comprLen = data->bodyLen;
                const char *compr = data->sendData;
                
                T_MSGHEAD_T msgHead;
                
                msgHead.cmd = (unsigned short)data->module.cmd;
                msgHead.com = 0;
                msgHead.enc = 0;
                msgHead.eno = 0;
                msgHead.idx = (uInt)data->sn;
                msgHead.len = (unsigned short)comprLen;
                msgHead.tea = 0;//s_tea;
                
                unsigned char *sendData = (unsigned char*)malloc(comprLen);
                unsigned int pos = 0;
                
                memcpy(&sendData[pos], compr, comprLen);//body
                pos += comprLen;
                
                int ret = cSocket.Send((char*)sendData,pos,0);
                log("发送:%s",compr);
                if (ret <= 0) {
                    m_sendEvent->Unlock();
                    free(sendData);
                    SocketData *errorData = newSocketData();
                    errorData->eventType = DISCONNECT;
                    m_mainQueue.Push(errorData);
                    return;
                }

                free(data->sendData);
                free(data);
                free(sendData);

                log("-----发送数据长度len:%d------",msgHead.len);
                log("-----------");
                
            }
            
            m_sendEvent->Wait();
        }
        m_sendEvent->Unlock();
    }else {
        isConnect = false;
        SocketData *errorData = newSocketData();
        errorData->eventType = CONNECT_FAIL;
        m_mainQueue.Push(errorData);
    }
}
//主线程更新
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);
    }
}