/** * @brief Performs connection throw a SpotChannel. * @param GSpotTestUT The test case name. * @param TestSpotChannel The test name. * @since 1.0.0 */ TEST(GSpotTestUT, TestSpotChannel) { std::cout << std::endl << std::endl; std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; std::cout << "////// TestSpotChannel ////////" << std::endl; std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; EchoServer echoServer; ChannelListenerWorker listenerOne("Listener One"); SpotChannel spotChannel("localhost", echoServer.port()); spotChannel.start(); spotChannel.subscribe( listenerOne ); listenerOne.setChannel(spotChannel); EXPECT_EQ(spotChannel.getError(), SpotChannel::SpotChannelError::CNoError); if( spotChannel.getStatus() == SpotChannel::SpotChannelStatus::CStatusConnected ) { std::string str = "hello"; gvr::communication::IChannel::BufferType data(str.begin(), str.end()); spotChannel.send(data); EXPECT_EQ(spotChannel.getError(), SpotChannel::SpotChannelError::CNoError); // Check the condition in a maximum elased time ... /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Timestamp initialTime; const unsigned long usTimeout = 800000; // timeout in microseconds... 1000 microseconds are 1 millisecond. while ( ( listenerOne.getLastBufferReceived().empty() ) && ( Timestamp::TimeDiff((Timestamp()-initialTime)) < usTimeout ) ) { Thread::sleep(1); } std::cout << "Elapsed time ...[" << std::dec << Timestamp::TimeDiff( (Timestamp()-initialTime) ) << " us]." << std::endl; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// EXPECT_EQ(listenerOne.getLastBufferReceived(), "hello"); } spotChannel.stop(); std::cout << "SpotChannel was stopped..." << std::endl; std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; std::cout << std::endl << std::endl; }
std::string FileChannelTest::filename() const { std::string name = "log_"; name.append(DateTimeFormatter::format(Timestamp(), "%Y%m%d%H%M%S")); name.append(".log"); return name; }
//用户帐号,密码登入 void MeetingConnImpl::Login(const char* strUserName, const char* strPassword,uint32_t clientType,bool bEncrypt) { /* std::string strTest="[18621066138,18979080001]"; std::string strTest2 = strTest; Parser parser; Var result; try { result = parser.parse(strTest2); Poco::JSON::Array::Ptr array =result.extract<Poco::JSON::Array::Ptr>(); for(Poco::Int32 i = 0; i<array->size();i++) { Var item = array->get(i); unsigned long long phone = item; cout<<phone; } } catch(JSONException& jsone) { //printf("%s \n",buffer); std::cout << jsone.message() << std::endl; return; } */ //md5 m_loginType = 1; m_userRole = 2; m_tryLoginCount = 0; m_lastLoginTime = Timestamp(); if(bEncrypt) { MD5Engine engine; engine.update(strPassword); strcpy(m_strUserAccount, strUserName); strcpy(m_strPassword, DigestEngine::digestToHex(engine.digest()).data()); } else { strcpy(m_strUserAccount, strUserName); strcpy(m_strPassword,strPassword); } m_clientType = clientType; if(m_bHasLogined == true) return; m_bHasLogined = false; char utfStr[1024]={0}; sprintf(utfStr, "{\"cmd\":\"login\",\"account\":\"%s\",\"password\":\"%s\",\"client_type\":%u}\r\n",m_strUserAccount,m_strPassword,clientType); strcpy(m_strLogin,utfStr); }
//用房间号登录,呢称,房间密码,终端类型 void MeetingConnImpl::LoginWithRoomNo(const char* strUserName, const char* strRoomPassword,uint32_t clientType,uint32_t u32RoomID) { //md5 m_tryLoginCount = 0; m_lastLoginTime = Timestamp(); m_loginType = 2; m_userRole = 2; m_ulRoomID = u32RoomID; //MD5Engine engine; //engine.update(strRoomPassword); strcpy(m_strUserAccount, strUserName); strcpy(m_strUserName, strUserName); strcpy(m_strPassword,strRoomPassword); m_clientType = clientType; if(m_bHasLogined == true) return; m_bHasLogined = false; char utfStr[1024]={0}; sprintf(utfStr, "{\"cmd\":\"loginWithRoomNo\",\"roomID\":%u,\"password\":\"%s\",\"client_type\":%u}\r\n",u32RoomID,m_strPassword,clientType); strcpy(m_strLogin,utfStr); }
/** * @brief Performs a simulation of a subscription to a particular message type. * @param GSpotTestUT The test case name. * @param TestSpotSessionSpecialMessageSubscribe The test name. * @since 1.0.0 */ TEST(GSpotTestUT, TestSpotSessionSpecialMessageSubscribe) { std::cout << std::endl << std::endl; std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; std::cout << "////// TestSpotSessionSpecialMessageSubscribe ////////" << std::endl; std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; EchoServer echoServer; SpotSessionListenerWorker workerOne("Worker One"); SpotSessionListenerWorker workerTwo("Worker Two"); SpotChannel spotChannel("localhost", echoServer.port()); MockSpotSessionCancelTimers spotSession; SpotMessagePing firstSpotPingMessageRq; SpotMessageLogin firstSpotLoginMessageRq; SpotMessagePingResponse firstSpotPingMessageRs; SpotMessagePing firstSpotPingMessageRq2; spotSession.subscribe(workerOne, firstSpotPingMessageRq.getTypeId()); spotSession.subscribe(workerOne, firstSpotPingMessageRq2.getTypeId()); spotSession.subscribe(workerTwo, firstSpotLoginMessageRq.getTypeId()); spotSession.subscribe(workerTwo, firstSpotPingMessageRs.getTypeId()); EXPECT_EQ(firstSpotPingMessageRq.getTypeId(), SpotMessagePing::GetTypeId()); workerOne.setSession(spotSession); workerTwo.setSession(spotSession); std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; spotSession.start(spotChannel); EXPECT_EQ(spotSession.isConnected(), true); // First asynchronous send and response evaluation ... ////////////////////////////////////////////////////////////////////// { spotSession.send(firstSpotPingMessageRq); EXPECT_EQ(spotSession.getError(), SpotSession::SpotSessionError::CNoError); // Check the condition in a maximum elapsed time ... { Timestamp initialTime; const unsigned long usTimeout = 800000; // timeout in microseconds... 1000 microseconds are 1 millisecond. while ( ( workerOne.getTypeId() != firstSpotPingMessageRq.getTypeId() ) && ( Timestamp::TimeDiff( (Timestamp()-initialTime) ) < usTimeout ) ) { Thread::sleep(1); } std::cout << "Elapsed time ...[" << std::dec << Timestamp::TimeDiff( (Timestamp()-initialTime) ) << " us]." << std::endl; } EXPECT_EQ(workerOne.getTypeId(), firstSpotPingMessageRq.getTypeId()); } ////////////////////////////////////////////////////////////////////// // Second asynchronous send and response evaluation ... ////////////////////////////////////////////////////////////////////// { spotSession.send(firstSpotLoginMessageRq); EXPECT_EQ(spotSession.getError(), SpotSession::SpotSessionError::CNoError); // Check the condition in a maximum elapsed time ... { Timestamp initialTime; const unsigned long usTimeout = 800000; // timeout in microseconds... 1000 microseconds are 1 millisecond. while ( ( workerTwo.getTypeId() != firstSpotLoginMessageRq.getTypeId() ) && ( Timestamp::TimeDiff( (Timestamp()-initialTime) ) < usTimeout ) ) { Thread::sleep(1); } std::cout << "Elapsed time ...[" << std::dec << Timestamp::TimeDiff( (Timestamp()-initialTime) ) << " us]." << std::endl; } EXPECT_EQ(workerTwo.getTypeId(), firstSpotLoginMessageRq.getTypeId()); SpotMessagePing spotPingMessageRq; spotPingMessageRq.parse(workerOne.getBuffer()); EXPECT_EQ(spotPingMessageRq.getTypeId(), firstSpotPingMessageRq.getTypeId()); } ////////////////////////////////////////////////////////////////////// std::cout << "Stopping the SpotSession..." << std::endl; spotSession.stop(); EXPECT_EQ(spotSession.isConnected(), false); std::cout << "SpotSession was stopped..." << std::endl; std::cout << "//////////////////////////////////////////////////////////////////////" << std::endl; std::cout << std::endl << std::endl; }
void TimedNotificationQueueTest::testDequeue() { TimedNotificationQueue queue; assert (queue.empty()); assert (queue.size() == 0); Notification* pNf = queue.dequeueNotification(); assertNullPtr(pNf); queue.enqueueNotification(new Notification, Timestamp()); assert (!queue.empty()); assert (queue.size() == 1); pNf = queue.dequeueNotification(); assertNotNullPtr(pNf); assert (queue.empty()); assert (queue.size() == 0); pNf->release(); Poco::Timestamp ts1; ts1 += 100000; Poco::Timestamp ts2; ts2 += 200000; Poco::Timestamp ts3; ts3 += 300000; Poco::Timestamp ts4; ts4 += 400000; queue.enqueueNotification(new QTestNotification("first"), ts1); queue.enqueueNotification(new QTestNotification("fourth"), ts4); queue.enqueueNotification(new QTestNotification("third"), ts3); queue.enqueueNotification(new QTestNotification("second"), ts2); assert (!queue.empty()); assert (queue.size() == 4); QTestNotification* pTNf = 0; while (!pTNf) { pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); } assertNotNullPtr(pTNf); assert (pTNf->data() == "first"); pTNf->release(); assert (ts1.elapsed() >= 0); assert (!queue.empty()); assert (queue.size() == 3); pTNf = 0; while (!pTNf) { pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); } assertNotNullPtr(pTNf); assert (pTNf->data() == "second"); pTNf->release(); assert (ts2.elapsed() >= 0); assert (!queue.empty()); assert (queue.size() == 2); pTNf = 0; while (!pTNf) { pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); } assertNotNullPtr(pTNf); assert (pTNf->data() == "third"); pTNf->release(); assert (ts3.elapsed() >= 0); assert (!queue.empty()); assert (queue.size() == 1); pTNf = 0; while (!pTNf) { pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); } assertNotNullPtr(pTNf); assert (pTNf->data() == "fourth"); pTNf->release(); assert (ts4.elapsed() >= 0); assert (queue.empty()); assert (queue.size() == 0); pNf = queue.dequeueNotification(); assertNullPtr(pNf); }
void MeetingConnImpl::run() { Poco::Timespan span(2*1000); char buffer[1500*10] = {0}; SocketAddress sender; Poco::Int32 n; m_recvLen = 0; memset(m_TempRecvBuf,0,sizeof(m_TempRecvBuf)); //探测是否连接成功; Poco::Timespan spanTimeOut(10,0); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList errList; Timestamp timeHeart; Timestamp timeTimeout; Timespan errorSpan(1000); span = Timespan(1000*100); writeList.push_back(*m_pSock); errList.push_back(*m_pSock); if(Socket::select(readList,writeList,errList,spanTimeOut)) { if(writeList.size()>0) { m_bRawSockConnected = true; m_pIMeetingEvent->OnNetEvent(NET_EVENT_CONNECT_SUCCESS,"connect success"); } else { //连接不上 m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"connect login server time out"); goto OnError; } } else { m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"connect login server time out"); goto OnError; } if(m_bRawSockConnected) { //发送握手协议1 char msg[512]; sprintf(msg,"{\"cmd\":\"handSharke1\",\"cmdVar\":%d}\r\n",MeetingFrameImpl::GetInstance()->GetVersion()); try { if(m_bRawSockConnected) m_pSock->sendBytes(msg,strlen(msg)); } catch(NetException&) { m_bRawSockConnected = false; m_bStop = true; m_pIMeetingEvent->OnNetEvent(NET_EVENT_LOST_CONNECTION,"server has a error"); goto OnError; } } m_recvLen = 0; m_lastTime = 0; while(!m_bStop &&m_bRawSockConnected) { char c = '\r'; char c2 = '\n'; if(!m_bStop && m_pSock->poll(errorSpan,Socket::SELECT_ERROR)) { if(m_bHasLogined) { m_bRawSockConnected = false; m_pIMeetingEvent->OnNetEvent(NET_EVENT_LOST_CONNECTION,"Proxy Server Error"); } goto OnError; } if(!m_bStop && m_pSock->poll(span, Socket::SELECT_READ)) { try { if(m_pSock->available()<=0) { if(m_bHasLogined) { m_bRawSockConnected = false; m_pIMeetingEvent->OnNetEvent(NET_EVENT_LOST_CONNECTION,"Proxy Server Error"); } goto OnError; } if(m_bStop) { m_bHasLogined = m_bLogicServerConnected = m_bRawSockConnected = false; return; } memset(buffer,0,sizeof(buffer)); n = m_pSock->receiveBytes(buffer,sizeof(buffer)); m_lastTime = Timestamp(); if(n>0) { //切包,拼包 ProcessRawCommand(buffer,n); } else { if(!m_bStop) m_pIMeetingEvent->OnNetEvent(NET_EVENT_LOST_CONNECTION,"lost connection"); goto OnError; } } catch (Poco::Exception& exc) { std::cerr << "AA_proxy_Server: " << exc.displayText() << std::endl; if(!m_bStop) m_pIMeetingEvent->OnNetEvent(NET_EVENT_LOST_CONNECTION,"proxy server error"); goto OnError; return; } } //一分钟发一次心跳 if(timeHeart.elapsed()>30*1000*1000) { char *msg2="{\"cmd\":\"ping\"}\r\n"; if(m_bLogicServerConnected) m_pSock->sendBytes(msg2,strlen(msg2)); timeHeart = Timestamp(); } // 70秒没有收到数据,掉线了; if(timeTimeout.elapsed() >30*1000*1000) { if(m_lastTime.elapsed()>35*1000*1000) { m_pIMeetingEvent->OnNetEvent(NET_EVENT_LOST_CONNECTION,"lost connection"); goto OnError; } timeTimeout = Timestamp(); } if(m_bHasLogined == true && m_bLogicServerConnected == true) { //如果已经登录成功,检测缓冲区有没有要发送的数据,发送数据 { FastMutex::ScopedLock autoLock(m_lock); while(m_sendBufList.size()>0) { char * strSend = m_sendBufList.front(); if(m_pSock!=NULL) { m_pSock->sendBytes(strSend,strlen(strSend)); m_sendBufList.pop(); delete strSend; } else { break; } } } } else if(m_tryLoginCount<5) { //发送登录数据 if(m_bLogicServerConnected == true) { if(m_tryLoginCount == 0) { m_pSock->sendBytes(m_strLogin,strlen(m_strLogin)); m_tryLoginCount++; m_lastLoginTime = Timestamp(); } else { if(m_lastLoginTime.elapsed() >1000*5*1000) { m_pSock->sendBytes(m_strLogin,strlen(m_strLogin)); m_tryLoginCount++; m_lastLoginTime = Timestamp(); } } } } }//end while //把没有发送的数据发送完 { FastMutex::ScopedLock autoLock(m_lock); while(m_sendBufList.size()>0) { char * strSend = m_sendBufList.front(); if(m_pSock!=NULL) { m_pSock->sendBytes(strSend,strlen(strSend)); m_sendBufList.pop(); delete strSend; } else { break; } } } OnError: if(m_bLogicServerConnected && NULL != m_pSock) { m_pSock->shutdown(); } m_bStop = true; m_bHasLogined = false; m_bLogicServerConnected = m_bRawSockConnected= false; delete m_pSock; m_pSock = NULL; }