// [TCP]发送数据用完成端口 int CServerClient::Send(const void* lpBuf, int nBufLen, int nFlags) { if( IsShutDown() ) return true; if(m_SendMessages.size() == 0 ||//是否有数据 GetPendingWrBufNum() > m_lMaxPendingWrBufNum)//待发送的数据大小小于限定值 { return true; } //如果平均发送数据大小超过限定值,则发送0大小的数据 if(GetCurSendSizePerSecond() > m_lMaxSendSizePerSecond) { SendZeroByteData(); return true; } itMsg it = m_SendMessages.begin(); for(;it != m_SendMessages.end();) { //发送一个消息 if(!Send((*it),nFlags)) return false; it = m_SendMessages.erase(it); if(GetPendingWrBufNum() > m_lMaxPendingWrBufNum || GetCurSendSizePerSecond() > m_lMaxSendSizePerSecond ) break; } return true; }
bool EasyIocp::stop() { if(IsShutDown()) { print("EasyIocp::stop: iocp has been stopped."); return false; } SetShutFlag(); FreeIocp(); return true; }
bool EasyIocp::start(sockaddr_in *bindAddr/* = NULL*/) { if(!WindowsVerOk(6, 0)) { print("EasyIocp::start: the program needs Windows NT6.0 or higher."); return false; } if(!IsShutDown()) { print("EasyIocp::start: iocp has been started."); return false; } if(bindAddr) memcpy(&bindAddr_, bindAddr, sizeof(bindAddr_)); ClearShutFlag(); InitIocp(); InitListenSocket(); return true; }
// static void ImageBridgeChild::FlushAllImages(ImageClient* aClient, ImageContainer* aContainer) { if (!IsCreated() || IsShutDown()) { return; } MOZ_ASSERT(aClient); MOZ_ASSERT(!sImageBridgeChildSingleton->mShuttingDown); MOZ_ASSERT(!InImageBridgeChildThread()); if (InImageBridgeChildThread()) { NS_ERROR("ImageBridgeChild::FlushAllImages() is called on ImageBridge thread."); return; } RefPtr<AsyncTransactionWaiter> waiter = new AsyncTransactionWaiter(); // This increment is balanced by the decrement in FlushAllImagesSync waiter->IncrementWaitCount(); sImageBridgeChildSingleton->GetMessageLoop()->PostTask( FROM_HERE, NewRunnableFunction(&FlushAllImagesSync, aClient, aContainer, waiter)); waiter->WaitComplete(); }
///函数:线程池回调函数,作为IOCP的工人线程函数。 void NTAPI EasyIocp::TPCallBack(PTP_CALLBACK_INSTANCE Instance, PVOID pParam, PTP_WORK Work) { EasyIocpStruct::WORK_ARG *workArg = (EasyIocpStruct::WORK_ARG*)pParam; EasyIocp *iocpModel = workArg->iocpModel; HANDLE hCompPort = iocpModel->GetComletionPort(); print("EasyIocp::TPCallBack: thread %d start.", workArg->id); CompletionKey *compKey = NULL; LPOVERLAPPED pOv = NULL; DWORD transBytes = 0; while(!IsShutDown()) { compKey = NULL; pOv = NULL; if( !GetQueuedCompletionStatus(hCompPort, &transBytes, (PULONG_PTR)&compKey, &pOv, INFINITE) ) { if(EasyIocpError::DealCompletionError(iocpModel, compKey)) { print("EasyIocp::TPCallBack: found an error, but continue."); continue; }else { break; } } if(!compKey || !pOv) { //手工退出操作 print("EasyIocp::TPCallBack: thread %d has been canceled.", workArg->id); break; } //处理业务 DealCompletionPacket(iocpModel, compKey, pOv, transBytes); } //end while(TRUE) print("EasyIocp::TPCallBack: thread %d exit.", workArg->id); return; }
bool CServerClient::AddSendMsg(CBaseMessage* pMsg) { char strInfo[256]=""; if(NULL == pMsg) return false; if( IsShutDown() ) { if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); return false; } //如果阻塞的消息数量大于规定数量,则强制性断开该连接 if(m_SendMessages.size() > m_lMaxBlockSendMsnNum) { ShutDown(); if(pMsg->RemoveRefCount() == 0) { CBaseMessage::FreeMsg(pMsg); } PutErrorString(NET_MODULE,"%-15s the blocked send messge count(num:%d) greater the max count(num:%d)", __FUNCTION__,m_SendMessages.size(),m_lMaxBlockSendMsnNum); return false; } //OnTransferChange(); //ulong dwCurTime = timeGetTime(); //long lCurMsgPri = pMsg->GetPriorityValue(dwCurTime); ////该消息类型是否可以丢弃 //bool bDiscard = CBaseMessage::GetIsDiscard(pMsg->GetType()); ////进行优先级判断,确定插入的位置 //itMsg it = m_SendMessages.begin(); //for(;it != m_SendMessages.end();) //{ // CBaseMessage* pTemptMsg = (*it); // //如果消息类型一样,查看是否可以丢弃已存在的消息 // if(pMsg->GetType() == pTemptMsg->GetType()) // { // //如果丢弃标记一样,则丢弃已经存在的消息 // if(bDiscard && pMsg->IsDiscardFlagEqual(pTemptMsg) ) // { // //_snprintf(strInfo,256,"Discard a messge(type:%d)",pTemptMsg->GetType()); // //PutErrorString(NET_MODULE,"%-15s %s",__FUNCTION__,strInfo); // it = m_SendMessages.erase(it); // CBaseMessage::FreeMsg(pTemptMsg); // continue; // } // } // //类型不一样,判断优先级 // else // { // if( lCurMsgPri > pTemptMsg->GetPriorityValue(dwCurTime)) // { // _snprintf(strInfo,256,"the messge(type:%d) prio greater the msg(type:%d)", // pMsg->GetType(),pTemptMsg->GetType()); // PutErrorString(NET_MODULE,"%-15s %s",__FUNCTION__,strInfo); // break; // } // } // it++; //} // //m_SendMessages.insert(it,pMsg); m_SendMessages.push_back(pMsg); //如果已发送的数据小于设置大小,则发送数据 if( !Send(NULL,0,0) ) return false; return true; }