webrtc sigslot usage and source code analysis

background

When we use webrtc, we will see sigslot. What is sigslot is what we call signal and slot, and qt is used. Sigslot in webrtc is a C++ event processing framework designed and implemented by Sarah Thompson. This framework is very lightweight. The whole code has only one sigslot.h file, and its design is excellent. It decouples events and processing mechanisms to the maximum extent and ensures thread safety.
In WebRTC, sigslot is the basic event processing framework, which is used in message notification and response processing of multiple modules.

connect

In the whole header file, the slot mainly inherits the class has_slots, the class has_slots mainly saves all signals, and the sender sets the signal sets.

When signal calls connect, slots save its signal

swh.clicked.connect(&lit1, &Light::turn_on);

Call signal's connect, and then call signal_connect method to save the signal

   template<class desttype>
            void connect(desttype* pclass, void (desttype::*pmemfun)())
        {
            lock_block<mt_policy> lock(this);
            _connection0<desttype, mt_policy>* conn =
                new _connection0<desttype, mt_policy>(pclass, pmemfun);//Create a connect object
            m_connected_slots.push_back(conn); //Put it in the connect list
            //Call the signal_connect function of the object, which must inherit from has_slots


            //has_slots function
            pclass->signal_connect(this); //Insert this-signal into the sender set of the object
        }

_ Connection 0 is a list of no parameters. In the code _Connection 1 ~_Connection 8 are the corresponding parameters. This sum
Signal 1 to signal 8 are the same. Here, new_connection 0 is the address of the class address and the class method of the slot. Here, we should pay attention to it.
- The return value must be void
- Slot parameters range from 0 to 8
- Classes that implement slots must inherit from has_slots<>

m_connected_slots stores all the slots

emit

When the signal is triggered, there are two ways to call it:

swh.clicked();
swh.clicked.emit();
  • The first is that we override the operator that invokes it by default.
  • emit method for the second call
lock_block<mt_policy> lock(this);
typename connections_list::const_iterator itNext, it = m_connected_slots.begin();
typename connections_list::const_iterator itEnd = m_connected_slots.end();

while(it != itEnd)
{
    itNext = it;
    ++itNext;

    //call connect0Of emit
    (*it)->emit();

    it = itNext;
}

Here are m_pobject and m_pmemfun from connect

//Send a message, that is, call the callback function registered in connect (the member function of the object)
virtual void emit()
{
    (m_pobject->*m_pmemfun)();
}

release

At the end of the program, the disconnect_all method is used when the signal signal signal inherits _signal_base 0 ~_signal_base 8.

lock_block<mt_policy> lock(this);
typename connections_list::const_iterator it = m_connected_slots.begin();
typename connections_list::const_iterator itEnd = m_connected_slots.end();

while(it != itEnd)
{
    //Traversal in turn connection Of list, call has_slots Of signal_disconnect
    (*it)->getdest()->signal_disconnect(this);
    //delete connections
    delete *it;

    ++it;
}

//delete list All the elements in it
m_connected_slots.erase(m_connected_slots.begin(), m_connected_slots.end());

class diagram

contact

Web RTC sharing learning and research qq q group 438078394

Author: clzhan 690759587

Keywords: Qt

Added by beesgirl713 on Fri, 14 Jun 2019 22:36:59 +0300