From bc1b5f4bbfafd75b50153f1e9360b705eb663665 Mon Sep 17 00:00:00 2001
From: winlin <winlin@vip.126.com>
Date: Wed, 12 Nov 2014 10:05:42 +0800
Subject: [PATCH] for bug #194, disable the srs fd poll.

---
 trunk/src/app/srs_app_poll.cpp      | 129 +---------------------------
 trunk/src/app/srs_app_poll.hpp      |  49 ++---------
 trunk/src/app/srs_app_rtmp_conn.cpp |   2 +-
 trunk/src/app/srs_app_server.cpp    |   9 --
 4 files changed, 10 insertions(+), 179 deletions(-)

diff --git a/trunk/src/app/srs_app_poll.cpp b/trunk/src/app/srs_app_poll.cpp
index 859e49e11..66f24efff 100644
--- a/trunk/src/app/srs_app_poll.cpp
+++ b/trunk/src/app/srs_app_poll.cpp
@@ -26,152 +26,31 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <srs_kernel_error.hpp>
 #include <srs_kernel_log.hpp>
 
-// the interval in us to refresh the poll for all fds.
-// for performance refine, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
-#define SRS_POLL_CYCLE_INTERVAL 10 * 1000 * 1000
-
 SrsPoll::SrsPoll()
-{
-    _pds = NULL;
-    pthread = new SrsThread(this, 0, false);
-}
-
-SrsPoll::~SrsPoll()
-{
-    srs_freep(_pds);
-    srs_freep(pthread);
-    fds.clear();
-}
-
-int SrsPoll::start()
-{
-    return pthread->start();
-}
-
-int SrsPoll::cycle()
-{
-    int ret = ERROR_SUCCESS;
-    
-    if (fds.size() == 0) {
-        st_usleep(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
-        return ret;
-    }
-    
-    int nb_pds = (int)fds.size();
-    
-    // TODO: FIXME: use more efficient way for the poll.
-    srs_freep(_pds);
-    _pds = new pollfd[nb_pds];
-    
-    if (true) {
-        int index = 0;
-
-        std::map<int, SrsPollFD*>::iterator it;
-        for (it = fds.begin(); it != fds.end(); ++it) {
-            int fd = it->first;
-            
-            pollfd& pfd = _pds[index++];
-            pfd.fd = fd;
-            pfd.events = POLLIN;
-            pfd.revents = 0;
-        }
-        
-        srs_assert(index == (int)fds.size());
-    }
-
-    // Upon successful completion, a non-negative value is returned. 
-    // A positive value indicates the total number of OS file descriptors in pds that have events. 
-    // A value of 0 indicates that the call timed out.
-    if (st_poll(_pds, nb_pds, SRS_POLL_CYCLE_INTERVAL) < 0) {
-        srs_warn("ignore st_poll failed, size=%d", nb_pds);
-        return ret;
-    }
-    
-    for (int i = 0; i < nb_pds; i++) {
-        if (!(_pds[i].revents & POLLIN)) {
-            continue;
-        }
-        
-        int fd = _pds[i].fd;
-        if (fds.find(fd) == fds.end()) {
-            continue;
-        }
-        
-        SrsPollFD* owner = fds[fd];
-        owner->set_active(true);
-    }
-    
-    return ret;
-}
-
-int SrsPoll::add(st_netfd_t stfd, SrsPollFD* owner)
-{
-    int ret = ERROR_SUCCESS;
-    
-    int fd = st_netfd_fileno(stfd);
-    if (fds.find(fd) != fds.end()) {
-        ret = ERROR_RTMP_POLL_FD_DUPLICATED;
-        srs_error("fd exists, fd=%d, ret=%d", fd, ret);
-        return ret;
-    }
-    
-    fds[fd] = owner;
-    
-    return ret;
-}
-
-void SrsPoll::remove(st_netfd_t stfd, SrsPollFD* owner)
-{
-    std::map<int, SrsPollFD*>::iterator it;
-    
-    int fd = st_netfd_fileno(stfd);
-    if ((it = fds.find(fd)) != fds.end()) {
-        fds.erase(it);
-    }
-}
-
-SrsPoll* SrsPoll::_instance = new SrsPoll();
-
-SrsPoll* SrsPoll::instance()
-{
-    return _instance;
-}
-
-SrsPollFD::SrsPollFD()
 {
     _stfd = NULL;
     _active = false;
 }
 
-SrsPollFD::~SrsPollFD()
+SrsPoll::~SrsPoll()
 {
-    if (_stfd) {
-        SrsPoll* poll = SrsPoll::instance();
-        poll->remove(_stfd, this);
-    }
 }
 
-int SrsPollFD::initialize(st_netfd_t stfd)
+int SrsPoll::initialize(st_netfd_t stfd)
 {
     int ret = ERROR_SUCCESS;
     
     _stfd = stfd;
     
-    SrsPoll* poll = SrsPoll::instance();
-    if ((ret = poll->add(stfd, this)) != ERROR_SUCCESS) {
-        srs_error("add fd to poll failed. ret=%d", ret);
-        return ret;
-    }
-    
     return ret;
 }
 
-bool SrsPollFD::active()
+bool SrsPoll::active()
 {
     return _active;
 }
 
-void SrsPollFD::set_active(bool v)
+void SrsPoll::set_active(bool v)
 {
     _active = v;
 }
diff --git a/trunk/src/app/srs_app_poll.hpp b/trunk/src/app/srs_app_poll.hpp
index 7a962da69..67888c3a8 100644
--- a/trunk/src/app/srs_app_poll.hpp
+++ b/trunk/src/app/srs_app_poll.hpp
@@ -35,59 +35,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <srs_app_st.hpp>
 #include <srs_app_thread.hpp>
 
-class SrsPollFD;
-
-/**
-* the poll for all play clients to finger the active fd out.
-* for performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
-* the poll is shared by all SrsPollFD, and we start an isolate thread to finger the active fds.
-*/
-class SrsPoll : public ISrsThreadHandler
-{
-private:
-    SrsThread* pthread;
-    pollfd* _pds;
-    std::map<int, SrsPollFD*> fds;
-public:
-    SrsPoll();
-    virtual ~SrsPoll();
-public:
-    /**
-    * start the poll thread.
-    */
-    virtual int start();
-    /**
-    * start an cycle thread.
-    */
-    virtual int cycle();
-public:
-    /**
-    * add the fd to poll.
-    */
-    virtual int add(st_netfd_t stfd, SrsPollFD* owner);
-    /**
-    * remove the fd to poll, ignore any error.
-    */
-    virtual void remove(st_netfd_t stfd, SrsPollFD* owner);
-// singleton
-private:
-    static SrsPoll* _instance;
-public:
-    static SrsPoll* instance();
-};
-
 /**
 * the poll fd to check whether the specified fd is active.
+* we start new thread to covert the fd status to async.
+* for performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
 */
-class SrsPollFD
+class SrsPoll
 {
 private:
     st_netfd_t _stfd;
     // whether current fd is active.
     bool _active;
 public:
-    SrsPollFD();
-    virtual ~SrsPollFD();
+    SrsPoll();
+    virtual ~SrsPoll();
 public:
     /**
     * initialize the poll.
diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp
index 7c91d06a1..6c289abea 100644
--- a/trunk/src/app/srs_app_rtmp_conn.cpp
+++ b/trunk/src/app/srs_app_rtmp_conn.cpp
@@ -518,7 +518,7 @@ int SrsRtmpConn::playing(SrsSource* source)
     srs_verbose("consumer created success.");
     
     // use poll fd to manage the connection, read when active.
-    SrsPollFD poll_fd;
+    SrsPoll poll_fd;
     if ((ret = poll_fd.initialize(stfd)) != ERROR_SUCCESS) {
         return ret;
     }
diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp
index 2678f44eb..49df43998 100644
--- a/trunk/src/app/srs_app_server.cpp
+++ b/trunk/src/app/srs_app_server.cpp
@@ -44,7 +44,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <srs_app_source.hpp>
 #include <srs_app_utility.hpp>
 #include <srs_app_heartbeat.hpp>
-#include <srs_app_poll.hpp>
 
 // signal defines.
 #define SIGNAL_RELOAD SIGHUP
@@ -665,14 +664,6 @@ int SrsServer::do_cycle()
 {
     int ret = ERROR_SUCCESS;
     
-    // start the poll for play clients.
-    // performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
-    SrsPoll* poll = SrsPoll::instance();
-    if ((ret = poll->start()) != ERROR_SUCCESS) {
-        srs_error("start poll failed. ret=%d", ret);
-        return ret;
-    }
-    
     // find the max loop
     int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES);