Ejemplo n.º 1
0
//-----------------------------------------------------------------------------
// 描述: 服务器开始运行后,主线程进行后台守护工作
//-----------------------------------------------------------------------------
void IseMainServer::runBackground()
{
    int adjustThreadInterval = iseApp().iseOptions().getUdpAdjustThreadInterval();
    int secondCount = 0;

    while (!iseApp().isTerminated())
    try
    {
        try
        {
            // 每隔 adjustThreadInterval 秒执行一次
            if ((secondCount % adjustThreadInterval) == 0)
            {
#ifdef ISE_LINUX
                // 暂时屏蔽退出信号
                SignalMasker sigMasker(true);
                sigMasker.setSignals(1, SIGTERM);
                sigMasker.block();
#endif

                // 维护工作者线程的数量
                if (udpServer_) udpServer_->adjustWorkerThreadCount();
            }
        }
        catch (...)
        {}

        secondCount++;
        sleepSeconds(1, true);  // 1秒
    }
    catch (...)
    {}
}
Ejemplo n.º 2
0
//-----------------------------------------------------------------------------
// 描述: 服务器初始化 (若初始化失败则抛出异常)
// 备注: 由 iseApp().initialize() 调用
//-----------------------------------------------------------------------------
void IseMainServer::initialize()
{
    // 定时器管理器
    timerManager_ = new TimerManager();

    // 初始化 UDP 服务器
    if (iseApp().iseOptions().getServerType() & ST_UDP)
    {
        udpServer_ = new MainUdpServer();
        udpServer_->setLocalPort(static_cast<WORD>(iseApp().iseOptions().getUdpServerPort()));
        udpServer_->setListenerThreadCount(iseApp().iseOptions().getUdpListenerThreadCount());
        udpServer_->open();
    }

    // 初始化 TCP 服务器
    if (iseApp().iseOptions().getServerType() & ST_TCP)
    {
        tcpServer_ = new MainTcpServer();
        tcpServer_->open();
    }

    // 初始化辅助服务器
    assistorServer_ = new AssistorServer();
    assistorServer_->open();

    // TCP连接器
    tcpConnector_ = new TcpConnector();

    // 初始化系统线程管理器
    sysThreadMgr_ = new SysThreadMgr();
    sysThreadMgr_->initialize();
}
Ejemplo n.º 3
0
void AppBusiness::afterInit()
{
    string ip = iseApp().getArgString(0);
    int port = 10003;

    iseApp().tcpConnector().connect(InetAddress(ip, port),
        boost::bind(&AppBusiness::onConnectComplete, this, _1, _2, _3, _4));
}
Ejemplo n.º 4
0
//-----------------------------------------------------------------------------
// 描述: 构造函数
// 参数:
//   ownGroup - 指定所属组别
//-----------------------------------------------------------------------------
UdpRequestQueue::UdpRequestQueue(UdpRequestGroup *ownGroup) :
    condition_(mutex_)
{
    int groupIndex;

    ownGroup_ = ownGroup;
    groupIndex = ownGroup->getGroupIndex();
    capacity_ = iseApp().iseOptions().getUdpRequestQueueCapacity(groupIndex);
    maxWaitTime_ = iseApp().iseOptions().getUdpRequestMaxWaitTime();
    packetCount_ = 0;
}
Ejemplo n.º 5
0
//-----------------------------------------------------------------------------
// 描述: 根据负载情况动态调整线程数量
//-----------------------------------------------------------------------------
void UdpWorkerThreadPool::AdjustThreadCount()
{
    int packetCount, threadCount;
    int minThreads, maxThreads, deltaThreads;
    int packetAlertLine;

    // 取得线程数量上下限
    iseApp().iseOptions().getUdpWorkerThreadCount(
        ownGroup_->getGroupIndex(), minThreads, maxThreads);
    // 取得请求队列中数据包数量警戒线
    packetAlertLine = iseApp().iseOptions().getUdpRequestQueueAlertLine();

    // 检测线程是否工作超时
    checkThreadTimeout();
    // 强行杀死僵死的线程
    killZombieThreads();

    // 取得数据包数量和线程数量
    packetCount = ownGroup_->getRequestQueue().getCount();
    threadCount = threadList_.getCount();

    // 保证线程数量在上下限范围之内
    if (threadCount < minThreads)
    {
        createThreads(minThreads - threadCount);
        threadCount = minThreads;
    }
    if (threadCount > maxThreads)
    {
        terminateThreads(threadCount - maxThreads);
        threadCount = maxThreads;
    }

    // 如果请求队列中的数量超过警戒线,则尝试增加线程数量
    if (threadCount < maxThreads && packetCount >= packetAlertLine)
    {
        deltaThreads = ise::min(maxThreads - threadCount, 3);
        createThreads(deltaThreads);
    }

    // 如果请求队列为空,则尝试减少线程数量
    if (threadCount > minThreads && packetCount == 0)
    {
        deltaThreads = 1;
        terminateThreads(deltaThreads);
    }
}
Ejemplo n.º 6
0
UdpWorkerThread::UdpWorkerThread(UdpWorkerThreadPool *threadPool) :
    ownPool_(threadPool),
    timeoutChecker_(this)
{
    setAutoDelete(true);
    // 启用超时检测
    timeoutChecker_.setTimeoutSecs(iseApp().iseOptions().getUdpWorkerThreadTimeout());

    ownPool_->registerThread(this);
}
Ejemplo n.º 7
0
//-----------------------------------------------------------------------------
// 描述: 用户信号处理器
//-----------------------------------------------------------------------------
void userSignalHandler(int sigNo)
{
    const CallbackList<UserSignalHandlerCallback>& callbackList = iseApp().onUserSignal_;

    for (int i = 0; i < callbackList.getCount(); i++)
    {
        const UserSignalHandlerCallback& callback = callbackList.getItem(i);
        if (callback)
            callback(sigNo);
    }
}
Ejemplo n.º 8
0
//-----------------------------------------------------------------------------
// 描述: 致命非法操作 信号处理器
//-----------------------------------------------------------------------------
void fatalErrorSignalHandler(int sigNo)
{
    static bool isInHandler = false;
    if (isInHandler) return;
    isInHandler = true;

    // 停止主线程循环
    iseApp().setTerminated(true);

    logger().writeFmt(SEM_SIG_FATAL_ERROR, sigNo);
    abort();
}
Ejemplo n.º 9
0
//-----------------------------------------------------------------------------
// 描述: 初始化请求组别列表
//-----------------------------------------------------------------------------
void MainUdpServer::initRequestGroupList()
{
    clearRequestGroupList();

    requestGroupCount_ = iseApp().iseOptions().getUdpRequestGroupCount();
    for (int groupIndex = 0; groupIndex < requestGroupCount_; groupIndex++)
    {
        UdpRequestGroup *p;
        p = new UdpRequestGroup(this, groupIndex);
        requestGroupList_.push_back(p);
    }
}
Ejemplo n.º 10
0
//-----------------------------------------------------------------------------
// 描述: 正常退出程序 信号处理器
//-----------------------------------------------------------------------------
void exitProgramSignalHandler(int sigNo)
{
    static bool isInHandler = false;
    if (isInHandler) return;
    isInHandler = true;

    // 停止主线程循环
    iseApp().setTerminated(true);

    logger().writeFmt(SEM_SIG_TERM, sigNo);

    siglongjmp(procExitJmpBuf, 1);
}
Ejemplo n.º 11
0
//-----------------------------------------------------------------------------
// 描述: 更新ISE配置
//-----------------------------------------------------------------------------
void IseSvrModBusiness::updateIseOptions()
{
    IseOptions& options = iseApp().iseOptions();

    // 设置UDP请求组别的总数量
    options.setUdpRequestGroupCount(getUdpGroupCount());
    // 设置TCP服务器的总数量
    options.setTcpServerCount(getTcpServerCount());

    // UDP请求组别相关设置
    int globalGroupIndex = 0;
    for (int modIndex = 0; modIndex < serverModuleMgr_.getCount(); modIndex++)
    {
        IseServerModule& serverModule = serverModuleMgr_.getItem(modIndex);
        for (int groupIndex = 0; groupIndex < serverModule.getUdpGroupCount(); groupIndex++)
        {
            UdpGroupOptions grpOpt;
            serverModule.getUdpGroupOptions(groupIndex, grpOpt);
            options.setUdpRequestQueueCapacity(globalGroupIndex, grpOpt.requestQueueCapacity);
            options.setUdpWorkerThreadCount(globalGroupIndex, grpOpt.minWorkerThreads, grpOpt.maxWorkerThreads);
            globalGroupIndex++;
        }
    }

    // TCP服务器相关设置
    int globalServerIndex = 0;
    for (int modIndex = 0; modIndex < serverModuleMgr_.getCount(); modIndex++)
    {
        IseServerModule& serverModule = serverModuleMgr_.getItem(modIndex);
        for (int serverIndex = 0; serverIndex < serverModule.getTcpServerCount(); serverIndex++)
        {
            TcpServerOptions svrOpt;
            serverModule.getTcpServerOptions(serverIndex, svrOpt);
            options.setTcpServerPort(globalServerIndex, svrOpt.serverPort);
            options.setTcpServerEventLoopCount(globalServerIndex, svrOpt.eventLoopCount);
            globalServerIndex++;
        }
    }

    // 设置辅助服务线程的数量
    int assistorCount = 0;
    for (int i = 0; i < serverModuleMgr_.getCount(); i++)
        assistorCount += serverModuleMgr_.getItem(i).getAssistorThreadCount();
    options.setAssistorThreadCount(assistorCount);
}
Ejemplo n.º 12
0
//-----------------------------------------------------------------------------
// 描述: 收到数据包
//-----------------------------------------------------------------------------
void MainUdpServer::onRecvData(void *packetBuffer, int packetSize, const InetAddress& peerAddr)
{
    int groupIndex;

    // 先进行数据包分类,得到组别号
    iseApp().iseBusiness().classifyUdpPacket(packetBuffer, packetSize, groupIndex);

    // 如果组别号合法
    if (groupIndex >= 0 && groupIndex < requestGroupCount_)
    {
        UdpPacket *p = new UdpPacket();
        if (p)
        {
            p->recvTimestamp_ = time(NULL);
            p->peerAddr_ = peerAddr;
            p->packetSize_ = packetSize;
            p->setPacketBuffer(packetBuffer, packetSize);

            // 添加到请求队列中
            requestGroupList_[groupIndex]->getRequestQueue().addPacket(p);
        }
    }
}
Ejemplo n.º 13
0
//-----------------------------------------------------------------------------
// 描述: 线程的执行函数
//-----------------------------------------------------------------------------
void UdpWorkerThread::execute()
{
    int groupIndex;
    UdpRequestQueue *requestQueue;

    groupIndex = ownPool_->getRequestGroup().getGroupIndex();
    requestQueue = &(ownPool_->getRequestGroup().getRequestQueue());

    while (!isTerminated())
    try
    {
        boost::scoped_ptr<UdpPacket> packet(requestQueue->extractPacket());
        if (packet)
        {
            AutoInvoker autoInvoker(timeoutChecker_);

            // 分派数据包
            if (!isTerminated())
                iseApp().iseBusiness().onRecvedUdpPacket(*this, groupIndex, *packet);
        }
    }
    catch (Exception&)
    {}
}
Ejemplo n.º 14
0
void IseServerModule::broadcastMessage(BaseSvrModMessage& message)
{
    (static_cast<IseSvrModBusiness*>(&iseApp().iseBusiness()))->broadcastMessage(message);
}