void HTTPSClientSessionTest::testConnectNB() { SecureStreamSocket sock; sock.connectNB(SocketAddress("server.com", 443)); char buf[512]; std::string msg("GET / HTTP/1.0\r\n\r\n"); sock.sendBytes(msg.c_str(), (int)msg.length()); Socket::SocketList read; Socket::SocketList write; Socket::SocketList exec; read.push_back(sock); Socket::select(read, write, exec, Poco::Timespan(30, 0) ); int rc = sock.receiveBytes(buf, 512); assert (rc > 0); }
void SocketTest::testSelect2() { Timespan timeout(100000); EchoServer echoServer1; EchoServer echoServer2; StreamSocket ss1(SocketAddress("localhost", echoServer1.port())); StreamSocket ss2(SocketAddress("localhost", echoServer2.port())); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList exceptList; readList.push_back(ss1); readList.push_back(ss2); assert (Socket::select(readList, writeList, exceptList, timeout) == 0); assert (readList.empty()); assert (writeList.empty()); assert (exceptList.empty()); ss1.sendBytes("hello", 5); ss1.poll(timeout, Socket::SELECT_READ); readList.push_back(ss1); readList.push_back(ss2); assert (Socket::select(readList, writeList, exceptList, timeout) == 1); assert (readList.size() == 1); assert (readList[0] == ss1); assert (writeList.empty()); assert (exceptList.empty()); char buffer[256]; int n = ss1.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); readList.clear(); writeList.clear(); exceptList.clear(); writeList.push_back(ss1); writeList.push_back(ss2); assert (Socket::select(readList, writeList, exceptList, timeout) == 2); assert (readList.empty()); assert (writeList.size() == 2); assert (writeList[0] == ss1); assert (writeList[1] == ss2); assert (exceptList.empty()); ss1.close(); ss2.close(); }
void SocketTest::testSelect() { Timespan timeout(250000); EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList exceptList; readList.push_back(ss); assert (Socket::select(readList, writeList, exceptList, timeout) == 0); assert (readList.empty()); assert (writeList.empty()); assert (exceptList.empty()); ss.sendBytes("hello", 5); ss.poll(timeout, Socket::SELECT_READ); readList.push_back(ss); writeList.push_back(ss); assert (Socket::select(readList, writeList, exceptList, timeout) == 2); assert (!readList.empty()); assert (!writeList.empty()); assert (exceptList.empty()); char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void SocketReactor::run() { _pThread = Thread::current(); Socket::SocketList readable; Socket::SocketList writable; Socket::SocketList except; while (!_stop) { try { readable.clear(); writable.clear(); except.clear(); int nSockets = 0; { FastMutex::ScopedLock lock(_mutex); for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it) { if (it->second->accepts(_pReadableNotification)) { readable.push_back(it->first); nSockets++; } if (it->second->accepts(_pWritableNotification)) { writable.push_back(it->first); nSockets++; } if (it->second->accepts(_pErrorNotification)) { except.push_back(it->first); nSockets++; } } } if (nSockets == 0) { onIdle(); Thread::trySleep(static_cast<long>(_timeout.totalMilliseconds())); } else if (Socket::select(readable, writable, except, _timeout)) { onBusy(); for (Socket::SocketList::iterator it = readable.begin(); it != readable.end(); ++it) dispatch(*it, _pReadableNotification); for (Socket::SocketList::iterator it = writable.begin(); it != writable.end(); ++it) dispatch(*it, _pWritableNotification); for (Socket::SocketList::iterator it = except.begin(); it != except.end(); ++it) dispatch(*it, _pErrorNotification); } else onTimeout(); } catch (Exception& exc) { ErrorHandler::handle(exc); } catch (std::exception& exc) { ErrorHandler::handle(exc); } catch (...) { ErrorHandler::handle(); } } onShutdown(); }
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; }
void LocalSocketTest::doSelectOrPoll2(SelectPtr pAction) { Timespan timeout(100000); SocketAddress sas1("/tmp/poco.server1.tcp.sock"); EchoServer echoServer1(sas1); SocketAddress sac1("/tmp/poco.client1.tcp.sock"); StreamSocket ss1(sas1, &sac1); SocketAddress sas2("/tmp/poco.server2.tcp.sock"); EchoServer echoServer2(sas2); SocketAddress sac2("/tmp/poco.client2.tcp.sock"); StreamSocket ss2(sas2, &sac2); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList exceptList; readList.push_back(ss1); readList.push_back(ss2); assert (pAction(readList, writeList, exceptList, timeout) == 0); assert (readList.empty()); assert (writeList.empty()); assert (exceptList.empty()); ss1.sendBytes("hello", 5); ss1.poll(timeout, Socket::SELECT_READ); readList.push_back(ss1); readList.push_back(ss2); assert (pAction(readList, writeList, exceptList, timeout) == 1); assert (readList.size() == 1); assert (readList[0] == ss1); assert (writeList.empty()); assert (exceptList.empty()); char buffer[256]; int n = ss1.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); readList.clear(); writeList.clear(); exceptList.clear(); writeList.push_back(ss1); writeList.push_back(ss2); assert (pAction(readList, writeList, exceptList, timeout) == 2); assert (readList.empty()); assert (writeList.size() == 2); assert (writeList[0] == ss1); assert (writeList[1] == ss2); assert (exceptList.empty()); ss1.close(); ss2.close(); }
void LocalSocketTest::doSelectOrPoll1(SelectPtr pAction) { Timespan timeout(250000); SocketAddress sas("/tmp/poco.server.tcp.sock"); EchoServer echoServer(sas); SocketAddress sac("/tmp/poco.client.tcp.sock"); StreamSocket ss(sas, &sac); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList exceptList; readList.push_back(ss); assert (pAction(readList, writeList, exceptList, timeout) == 0); assert (readList.empty()); assert (writeList.empty()); assert (exceptList.empty()); ss.sendBytes("hello", 5); ss.poll(timeout, Socket::SELECT_READ); readList.push_back(ss); writeList.push_back(ss); assert (pAction(readList, writeList, exceptList, timeout) == 2); assert (!readList.empty()); assert (!writeList.empty()); assert (exceptList.empty()); char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
//---------------------------------------------------------------------------- void ServerPoll::_ReadWriteRun() { Socket::SocketList readable; Socket::SocketList writable; Socket::SocketList except; std::map<px2_socket_t, ClientContext*> contextMap; int nSockets = 0; { ScopedCS cs(&mContextMapMutex); std::map<unsigned int, ClientContext *>::iterator it = mClientMap.begin(); for (; it != mClientMap.end(); it++) { contextMap[it->second->TheSocket.GetSocket()] = it->second; readable.push_back(it->second->TheSocket); writable.push_back(it->second->TheSocket); except.push_back(it->second->TheSocket); nSockets++; } } Timespan timeout(250000); if (nSockets == 0) { // odle /*_*/ } else if (Socket::Select(readable, writable, except, timeout)) { // busy for (Socket::SocketList::iterator it = readable.begin(); it != readable.end(); ++it) { ClientContext *context = contextMap[(*it).GetSocket()]; if (_OnRead(context) < 0) { DisconnectClient(context->ClientID); } } for (Socket::SocketList::iterator it = writable.begin(); it != writable.end(); ++it) { ClientContext *context = contextMap[(*it).GetSocket()]; if (_OnWrite(context) < 0) { DisconnectClient(context->ClientID); } } for (Socket::SocketList::iterator it = except.begin(); it != except.end(); ++it) { } } else { // time out /*_*/ } }