BOOL CNetPTCPConnection::StartWork() { //PrintNetLog(0xffffffff,"(%d)Connection开始工作",GetID()); m_pEpollEventRouter->Init(this); m_Socket.SetState(CNetSocket::SS_CONNECTED); if(!m_Socket.EnableBlocking(FALSE)) { OnConnection(FALSE); PrintNetLog(0xffffffff,"(%d)Connection开始非阻塞模式失败!",GetID()); m_Socket.Close(); return FALSE; } if(!GetServer()->BindSocket(m_Socket.GetSocket(),m_pEpollEventRouter)) { OnConnection(FALSE); PrintNetLog(0xffffffff,"(%d)Connection绑定Epoll失败!",GetID()); m_Socket.Close(); return FALSE; } //CEasyString ConnectionName; //ConnectionName.Format("[%s:%d]",GetRemoteAddress().GetIPString(),GetRemoteAddress().GetPort()); //SetName(ConnectionName);; //PrintNetLog(0xffffffff,"%s连接建立[%u]",GetName(),(UINT)m_Socket.GetSocket()); OnConnection(TRUE); return TRUE; }
void InternalHandleCommand(SRobotCommand const& cmd) { switch(cmd.m_ecmd) { case ecmdCONNECT: OnConnection(); break; case ecmdRESET: OnDisconnection(); break; case ecmdMOVE: { bool bReverse = false; for(unsigned int i=0; i<countof(g_amotors); ++i) { int const nSpeed = i%2==0 ? cmd.m_nSpeedLeft : cmd.m_nSpeedRight; bReverse = bReverse || ((nSpeed < 0) != g_amotors[i].m_bReverse); } if(bReverse) { // stop all motors and reset PID // Serial.println("STOP Motors"); for(unsigned int i=0; i<countof(g_amotors); ++i) { g_amotors[i].Stop(g_apid[i]); } delay(200); } for(unsigned int i=0; i<countof(g_amotors); ++i) { int const nSpeed = i%2==0 ? cmd.m_nSpeedLeft : cmd.m_nSpeedRight; if(0==nSpeed) { g_amotors[i].Stop(g_apid[i]); } else { g_amotors[i].SetSpeed(constrain(nSpeed, -MAX_SPEED, MAX_SPEED)); } } } } }
ClientSocket* ServerSocket::Accept() { ClientSocket* client = AllocClient(); uv_tcp_init(getSocket()->loop, client->getSocket()); if(uv_accept((uv_stream_t*)getSocket(), (uv_stream_t*) client->getSocket()) == 0) { client->getSocket()->data = client; OnConnection(client); } else { OnError("Accepting Client"); client->Close(); } return client; }
bool CHttpServer::Listen(int Port) { QTcpServer* pListener = new QTcpServer(this); pListener->listen(QHostAddress::Any, Port); if (!pListener->isListening()) { delete pListener; return false; } connect(pListener, SIGNAL(newConnection()), this, SLOT(OnConnection())); m_Listners.insert(Port, pListener); return true; }
int CNetConnection::Update(int ProcessPacketLimit) { int PacketCount=0; //处理Connect if(m_Socket.GetState()==CNetSocket::SS_CONNECTING) { m_Socket.Connected(); if(m_Socket.GetState()==CNetSocket::SS_CONNECTED) { StartWork(); } if(m_Socket.GetState()==CNetSocket::SS_UNUSED) { OnConnection(FALSE); } } else { CEpollEventObject * pEpollEventObject; while(m_RecvQueue.PopFront(pEpollEventObject)) { OnRecvData(*(pEpollEventObject->GetDataBuff())); GetServer()->DeleteEventObject(pEpollEventObject); PacketCount++; if(PacketCount>=ProcessPacketLimit) break; } } //处理关闭 if(m_WantClose) { if(m_UseSafeDisconnect) { if(m_SendQueue.GetObjectCount()<=0) { Disconnect(); } } else { Disconnect(); } } return PacketCount; }
int CNetPTCPConnection::Update(int ProcessPacketLimit) { int PacketCount=0; //处理Connect if(m_Socket.GetState()==CNetSocket::SS_CONNECTING) { m_Socket.Connected(); if(m_Socket.GetState()==CNetSocket::SS_CONNECTED) { StartWork(); } if(m_Socket.GetState()==CNetSocket::SS_UNUSED) { OnConnection(FALSE); } } return PacketCount; }
void ConnectorInterface::ThreadFunc() { #if BEHAVIAC_COMPILER_MSVC //printf("ThreadFunc gs_threadFlag = %d\n", (int)gs_threadFlag.value()); BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); #endif { ScopedInt_t scopedInt(&gs_threadFlag); Log("behaviac: Socket Thread Starting\n"); #if BEHAVIAC_COMPILER_MSVC BEHAVIAC_ASSERT(t_packetBufferIndex != TLS_OUT_OF_INDEXES); #else //printf("ThreadFunc t_packetBufferIndex = %d\n", t_packetBufferIndex); //BEHAVIAC_ASSERT(t_packetBufferIndex != (unsigned int)-1); #endif// } namespace Socket = behaviac::Socket; const bool blockingSocket = true; behaviac::Socket::Handle serverSocket = 0; { ScopedInt_t scopedInt(&gs_threadFlag); serverSocket = Socket::Create(blockingSocket); if (!serverSocket) { Log("behaviac: Couldn't create server socket.\n"); return; } char bufferTemp[64]; string_sprintf(bufferTemp, "behaviac: Listening at port %d...\n", m_port); Log(bufferTemp); // max connections: 1, don't allow multiple clients? if (!Socket::Listen(serverSocket, m_port, 1)) { Log("behaviac: Couldn't configure server socket.\n"); Socket::Close(serverSocket); return; } } #if BEHAVIAC_COMPILER_MSVC BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); #endif this->ReserveThreadPacketBuffer(); while (!m_terminating) { #if BEHAVIAC_COMPILER_MSVC //wait for connecting while (!m_terminating) { //Log("Socket::TestConnection.\n"); if (Socket::TestConnection(serverSocket)) { break; } behaviac::Thread::Sleep(100); } #endif if (!m_terminating) { BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); Log("behaviac: accepting...\n"); { ScopedInt_t scopedInt(&gs_threadFlag); m_writeSocket = Socket::Accept(serverSocket, kSocketBufferSize); if (!m_writeSocket) { Log("behaviac: Couldn't create write socket.\n"); Socket::Close(serverSocket); return; } Log("behaviac: connection accepted\n"); } BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); { ScopedInt_t scopedInt(&gs_threadFlag); AtomicInc(m_isConnected); behaviac::Thread::Sleep(1); OnConnection(); AtomicInc(m_isConnectedFinished); behaviac::Thread::Sleep(1); //this->OnConnectionFinished(); Log("behaviac: after Connected.\n"); } BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); while (!m_terminating && this->m_writeSocket) { behaviac::Thread::Sleep(1); SendAllPackets(); ReceivePackets(); } BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); // One last time, to send any outstanding packets out there. if (this->m_writeSocket) { SendAllPackets(); Socket::Close(m_writeSocket); } this->Clear(); Log("behaviac: disconnected. \n"); } }//while (!m_terminating) Socket::Close(serverSocket); this->Clear(); BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); Log("behaviac: ThreadFunc exited. \n"); }