//接收数据线程
void netSocketManger::reciveThread()
{
    char buff[MAX_BUFF];
    int recv_len = 0;
    bool hasMorePackets = false;
 
    while(true)
    {
        
        if( !hasMorePackets ) {
            
            int ret = cSocket.Recv(buff, MAX_BUFF - recv_len, 0);
            
            if (ret <= 0) {
                isConnect = false;
                SocketData *errorData = newSocketData();
                errorData->eventType = DISCONNECT;
                m_mainQueue.Push(errorData);
                break;
            }
            if(ret < 1)
                break;
        }
       
            T_MSGHEAD_T reciveCmd;
            reciveCmd.len = 1;        //接收缓存中的前64个字节数据
            reciveCmd.enc = 0;        //数据是否加密标志
            reciveCmd.com = 0;        //数据是否压缩标志,这两个我们都设置为否,因为我们的数据本身只有一位
            reciveCmd.idx = (uInt)m_sn;      //包索引也设置为固定值,因为我们只有一个接口,这里发arduino马上就会回.
            reciveCmd.tea = 0;
            unsigned int pos = 0;
            
            SocketData *data = newSocketData();
            data->module.cmd = 100;   //先将100设置为灯的协议号
            data->module.eno = 0;     //主动将错误码设置为0,即正常
            data->eventType = RESPONSE;
            data->sn = reciveCmd.idx;
            std::string datax(&buff[pos],reciveCmd.len);
            data->body = std::string(datax);
            pos += reciveCmd.len;
            data->bodyLen = reciveCmd.len;
        
        log("接收:%s",data->body.c_str());
            log("*************************************************");
            log("*************************************************");
            
            m_mainQueue.Push(data);
            
            hasMorePackets = false;
        
    }
    
}
int netSocketManger::send(NetWorkCommandStruct module, std::string &body, SendDataDelegate *delegate)
{
    if (!delegate ||  !isConnect) {
        return -1;
    }
    
    m_sn++;
    
    m_sendDelegateList[m_sn] = delegate;
    
    SocketData *data = newSocketData();
    data->sn = m_sn;
    
    if (body.empty()) {
        data->body = "";
        data->bodyLen = 0;
    }else {
        int bodyStrLen = (int)body.size();
        //data->body = (char*)malloc(bodyStrLen);
        const char* tmp = body.data();
        data->sendData = (char*)malloc(bodyStrLen);
        memcpy(data->sendData, tmp, bodyStrLen);
        data->bodyLen = bodyStrLen;
    }
    
    data->module.cmd = module.cmd;
    
    m_sendQueue.Push(data);
    
    m_sendEvent->Post();
    
    return (int)m_sn;
}
int main()
{
	//往空的队列里添加和删除元素
	CQueue cq;
	cq.Push(4);
	cq.Push(5);
	cout << cq.Pop();
	cout << cq.Pop();
	cout << cq.Pop();
	
	return 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
}
示例#4
0
int main(int argc, char **argv)
{
	CHtHif *pHtHif = new CHtHif();
	CHtSuUnitEx *pSuUnit = new CHtSuUnitEx(pHtHif);

	pSuUnit->SetUsingCallback(true);

	int err_cnt = 0;

	uint16_t callCnt = 0;
	int sendDataCnt = 0;

	for (int i = 0; i < ARR_SIZE; i++) {
		memArr[i] = 0x0300 | i;
		sendArr[i] = 0x04000000 | (i << 16);
	}

	pSuUnit->SendHostMsg(SU_ARRAY_ADDR, (uint64_t)&memArr);

	while (callCnt < TEST_CNT || markerCnt < TEST_CNT || rtnCnt < TEST_CNT) {
		if (callCnt < TEST_CNT && sendDataCnt == 0 && pSuUnit->SendCall_echo(callCnt)) {
			sendDataCnt = callCnt++ + 1;
			dataCntQue.Push(sendDataCnt);
			continue;
		}

		if (sendDataCnt > 1) {
			sendDataCnt -= pSuUnit->SendHostData(sendDataCnt - 1, sendArr);
			continue;
		}

		if (sendDataCnt == 1 && pSuUnit->SendHostDataMarker()) {
			sendDataCnt = 0;
			continue;
		}

		if (pSuUnit->RecvPoll() == Ht::eRecvIdle)
			usleep(1000);
	}

	delete pHtHif;

	if (err_cnt)
		printf("FAILED: detected %d issues!\n", err_cnt);
	else
		printf("PASSED\n");

	return err_cnt;
}
//发送数据线程
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);
    }
}