XBMC研究之web server

来源:互联网 发布:北京网络工程公司 编辑:程序博客网 时间:2024/06/17 12:11
  1. 转载自http://blog.sina.com.cn/s/blog_6c14c17e0100lny0.html

上篇博文中讲到GUI上用JSON脚本发送一个请求时,会调用AnswerToConnection函数来进行应答。其实此函数是在呼叫MHD_start_daemon时,作为callback函数传入libmicrohttpd的,MHD_start_daemon是libmicrohttpd的守护入口。
    那么这篇博文将介绍XBMC的Web Server,首先我们看看Web Server的启动:
    bool CWebServer::Start(const char *ip, int port)
    {
        if (!m_running)
        {
            // To stream perfectly we should probably have MHD_USE_THREAD_PER_CONNECTION instead of MHD_USE_SELECT_INTERNALLY as it provides multiple clients concurrently
            m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv6, port, NULL, NULL, &CWebServer::AnswerToConnection, this, MHD_OPTION_END);
            if (!m_daemon) //try IPv4
                m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, this, &CWebServer::AnswerToConnection, this, MHD_OPTION_END);
            m_running = m_daemon != NULL;
            if (m_running)
                CLog::Log(LOGNOTICE, "WebServer: Started the webserver");
            else
                CLog::Log(LOGERROR, "WebServer: Failed to start the webserver");
        }
        return m_running;
    }
    关闭web server守护:
    bool CWebServer::Stop()
    {
        if (m_running)
        {
            MHD_stop_daemon(m_daemon);
            m_running = false;
            CLog::Log(LOGNOTICE, "WebServer: Stopped the webserver");
        }
        return !m_running;
    }