void CListener::_listencb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa, int socklen, void *user_data) { printf("on accept\n"); CListener* pThis = (CListener*)user_data; pThis->SetState(eListened); pThis->OnAccept(fd); }
void CEventManager::FireEvent(EventQueueItem_t *pEI) { CListener *pListener; if (pEI->Flags & ET_BROADCAST) { int i; CSimpleArray<CListener *> FireOrder; // firing an event can cause a listener to be removed from the list // so we make a copy of the list, and then fire each one. for (i = 0 ; i < m_ListenerList.GetSize() ; i++) FireOrder.Add(m_ListenerList[i]); for (i = 0 ; i < FireOrder.GetSize() ; i++) { pListener = FireOrder[i]; // only fire the event if it's still in the list! (if it's not then pListener is invalid..) if (m_ListenerList.Find(pListener) >= 0) pListener->OnEvent(pEI->EventID,pEI->pData); } } else { pEI->pFrom->OnEvent(pEI->EventID,pEI->pData); } }
/* ----------------------------------------------------------------------------------------- */ int main(int argc, char ** argv) { int infile = 0, outfile = 0; double filter = 0; parse_argv(argc, argv, &infile, &outfile, &filter); if(infile != 0) { play(infile); return 0; } // Create a sample listener and controller CListener listener; Controller controller; if(outfile != 0) listener.set_robot_output(outfile); listener.set_filter(filter); // Have the sample listener receive events from the controller controller.addListener(listener); // Keep this process running until Enter is pressed std::cout << "Press Enter to quit..." << std::endl; std::cin.get(); // Remove the sample listener when done controller.removeListener(listener); return 0; }
TInt32 CConnectionMgr::StopListening(void *pListeningPointer) { CListener *pListener = m_pListeners; CListener *pLast=NULL; while(pListener) { if (pListener == (pListeningPointer)) { if(pLast) { pLast->AttachToList(pListener->GetNext()); delete pListener; return SUCCESS; } else { m_pListeners = pListener->GetNext(); delete pListener; return SUCCESS; } } else { pLast = pListener; pListener = pListener->GetNext(); } } return OUT_OF_RANGE; }
CListener* CListener::NewL(CRequestHandler& aRqstHndlr) { CListener* self = new(ELeave) CListener(aRqstHndlr); CleanupStack::PushL(self); self->ConstructL(); CleanupStack::Pop(self); return self; }
// Worker process for connection listening UINT __cdecl CListener::ListenerWorker(LPVOID v) { CListener * Listener = (CListener *) v; // See _beginthread call for parameter definition SetThreadName("Listener"); Listener->Listen(); return 0; }
int main(int argc, char *argv[]) { signal(SIGCHLD, sigchld_hander); CListener listener = CListener(CHandler::Handler); int ret = listener.Listen(9999); if( ret < 0 ) { return 1; } listener.Run(); return 0; }
TInt32 CConnectionMgr::Run(TUInt32 runCnt) { TInt32 usedCnt = 0; CListener *pList = m_pListeners; m_nTimeNow = time(NULL); while (pList) { usedCnt += pList->Run(runCnt-usedCnt); if (usedCnt >= runCnt) { return usedCnt; } pList = pList->GetNext(); } usedCnt += m_connector.Run((runCnt-usedCnt)); TIOEvent *pEvent = m_netEventQueues.GetNetEvent(); while(pEvent) { TIOEvent event = *pEvent; m_netEventQueues.ConfirmHandleNetEvent(pEvent); CConnection *pConnection = m_conncectionPool.GetConectionByIdx(pEvent->m_connectionIdx); if (pConnection) { //先确认处理了消息,这样网络层如果有新事件,也会发事件通知. pConnection->OnAppRecved(); //由应用层调用,如果返回-1,则表示需要释放连接,把链接close ,并且放回connectionPool TInt32 ret = pConnection->AppRoutine(m_pBuff,m_buffSize); //不能先处理,再确认,因为,可能在处理完了,即上句执行完了,网络层又有了新事件,就会丢失. //pConnection->OnAppRecved(); pConnection->OnAppHandled(); if (ret < SUCCESS) { //pConnection->CloseConnection(); m_conncectionPool.ReleaseItem(pConnection); } } ++ usedCnt; if(usedCnt > runCnt) { break; } pEvent = m_netEventQueues.GetNetEvent(); } return usedCnt; }
void *CConnectionMgr::Listen(TUInt32 nIp,TUInt16 nPort,TUInt16 nMaxConnection,void *pIfCallBack) { CListener *p = new CListener(); if (NULL == p) { return 0; } TInt32 ret = p->Init(m_hCompletionPort,nIp, nPort, nMaxConnection,(IfListenerCallBack*)pIfCallBack,&m_conncectionPool,m_pParserFactory,m_pCryptorFactory); if (SUCCESS > ret) { p->Final(); delete p; return 0; } p->AttachToList(m_pListeners); m_pListeners = p; return p; }
void *CConnectionMgr::Listen(const TChar *pIp,TUInt16 port,TUInt16 maxConnection,void *pIfCallBack) { CListener *p = new CListener(); if (NULL == p) { return 0; } TUInt32 myIp = 0; if (pIp != NULL) { myIp = inet_addr(pIp); } TInt32 ret = p->Init(m_hCompletionPort,myIp, port, maxConnection,(IfListenerCallBack*)pIfCallBack,&m_conncectionPool,m_pParserFactory,m_pCryptorFactory); if (SUCCESS > ret) { p->Final(); delete p; return NULL; } p->AttachToList(m_pListeners); m_pListeners = p; return p; }
virtual bool OnLoad(const CString& sArgStr, CString& sMessage) { if (sArgStr.empty()) return true; // We don't accept any arguments, but for backwards // compatibility we have to do some magic here. sMessage = "Arguments converted to new syntax"; bool bSSL = false; bool bIPv6 = false; bool bShareIRCPorts = true; unsigned short uPort = 8080; CString sArgs(sArgStr); CString sPort; CString sListenHost; while (sArgs.Left(1) == "-") { CString sOpt = sArgs.Token(0); sArgs = sArgs.Token(1, true); if (sOpt.Equals("-IPV6")) { bIPv6 = true; } else if (sOpt.Equals("-IPV4")) { bIPv6 = false; } else if (sOpt.Equals("-noircport")) { bShareIRCPorts = false; } else { // Uhm... Unknown option? Let's just ignore all // arguments, older versions would have returned // an error and denied loading return true; } } // No arguments left: Only port sharing if (sArgs.empty() && bShareIRCPorts) return true; if (sArgs.find(" ") != CString::npos) { sListenHost = sArgs.Token(0); sPort = sArgs.Token(1, true); } else { sPort = sArgs; } if (sPort.Left(1) == "+") { sPort.TrimLeft("+"); bSSL = true; } if (!sPort.empty()) { uPort = sPort.ToUShort(); } if (!bShareIRCPorts) { // Make all existing listeners IRC-only const vector<CListener*>& vListeners = CZNC::Get().GetListeners(); vector<CListener*>::const_iterator it; for (it = vListeners.begin(); it != vListeners.end(); ++it) { (*it)->SetAcceptType(CListener::ACCEPT_IRC); } } // Now turn that into a listener instance CListener *pListener = new CListener(uPort, sListenHost, bSSL, (!bIPv6 ? ADDR_IPV4ONLY : ADDR_ALL), CListener::ACCEPT_HTTP); if (!pListener->Listen()) { sMessage = "Failed to add backwards-compatible listener"; return false; } CZNC::Get().AddListener(pListener); SetArgs(""); return true; }
void CClient::UserPortCommand(CString& sLine) { const CString sCommand = sLine.Token(0); if (sCommand.Equals("LISTPORTS")) { CTable Table; Table.AddColumn("Port"); Table.AddColumn("BindHost"); Table.AddColumn("SSL"); Table.AddColumn("Proto"); Table.AddColumn("IRC/Web"); vector<CListener*>::const_iterator it; const vector<CListener*>& vpListeners = CZNC::Get().GetListeners(); for (it = vpListeners.begin(); it < vpListeners.end(); ++it) { Table.AddRow(); Table.SetCell("Port", CString((*it)->GetPort())); Table.SetCell("BindHost", ((*it)->GetBindHost().empty() ? CString("*") : (*it)->GetBindHost())); Table.SetCell("SSL", CString((*it)->IsSSL())); EAddrType eAddr = (*it)->GetAddrType(); Table.SetCell("Proto", (eAddr == ADDR_ALL ? "All" : (eAddr == ADDR_IPV4ONLY ? "IPv4" : "IPv6"))); CListener::EAcceptType eAccept = (*it)->GetAcceptType(); Table.SetCell("IRC/Web", (eAccept == CListener::ACCEPT_ALL ? "All" : (eAccept == CListener::ACCEPT_IRC ? "IRC" : "Web"))); } PutStatus(Table); return; } CString sPort = sLine.Token(1); CString sAddr = sLine.Token(2); EAddrType eAddr = ADDR_ALL; if (sAddr.Equals("IPV4")) { eAddr = ADDR_IPV4ONLY; } else if (sAddr.Equals("IPV6")) { eAddr = ADDR_IPV6ONLY; } else if (sAddr.Equals("ALL")) { eAddr = ADDR_ALL; } else { sAddr.clear(); } unsigned short uPort = sPort.ToUShort(); if (sCommand.Equals("ADDPORT")) { CListener::EAcceptType eAccept = CListener::ACCEPT_ALL; CString sAccept = sLine.Token(3); if (sAccept.Equals("WEB")) { eAccept = CListener::ACCEPT_HTTP; } else if (sAccept.Equals("IRC")) { eAccept = CListener::ACCEPT_IRC; } else if (sAccept.Equals("ALL")) { eAccept = CListener::ACCEPT_ALL; } else { sAccept.clear(); } if (sPort.empty() || sAddr.empty() || sAccept.empty()) { PutStatus("Usage: AddPort <[+]port> <ipv4|ipv6|all> <web|irc|all> [bindhost]"); } else { bool bSSL = (sPort.Left(1).Equals("+")); const CString sBindHost = sLine.Token(4); CListener* pListener = new CListener(uPort, sBindHost, bSSL, eAddr, eAccept); if (!pListener->Listen()) { delete pListener; PutStatus("Unable to bind [" + CString(strerror(errno)) + "]"); } else { if (CZNC::Get().AddListener(pListener)) PutStatus("Port Added"); else PutStatus("Error?!"); } } } else if (sCommand.Equals("DELPORT")) { if (sPort.empty() || sAddr.empty()) { PutStatus("Usage: DelPort <port> <ipv4|ipv6|all> [bindhost]"); } else { const CString sBindHost = sLine.Token(3); CListener* pListener = CZNC::Get().FindListener(uPort, sBindHost, eAddr); if (pListener) { CZNC::Get().DelListener(pListener); PutStatus("Deleted Port"); } else { PutStatus("Unable to find a matching port"); } } } }