Application of Gtalk jingle (3) signal/Slot Mechanism

Source: Internet
Author: User

The signal/slot mechanism is widely used in libjingle (using the sigslot library written by Sarah Thompson ).

 

Sigslot separates the previously called functions to a certain extent, and then freely binds (CONNECT) or disconnect) their call relationships during the program running. In this way, they are relatively loosely separated, but there is still a certain degree of coupling, such as the type and number of parameters of signal and slot must be consistent.

 

For example, the following line of code in relayserver:

  1. Void relayserver: addinternalsocket (talk_base: asyncpacketsocket * socket ){
  2. ...
  3. Socket-> signalreadpacket. Connect (this, & relayserver: oninternalpacket );
  4. }

After adding a UDP socket for internal connection, call signalreadpacket Of The UDP socket and call oninternalpacket () of the relayserver () (its definition is a common member function like relayserver, but the parameter must be consistent with the associated signal, and relayserver must inherit has_slots <>. In this way, when signalreadpacket is executed, oninternalpacket () is automatically called ().

 

Signal definition:

  1. Class asyncpacketsocket ...{
  2. Public:
  3. ...
  4. // Emitted each time a packet is read.
  5. Sigslot: signal4 <const char *, size_t, const socketaddress &, asyncpacketsocket *> signalreadpacket;
  6. ...
  7. }

Slot definition:

 

  1. Class relayserver: Public sigslot: has_slots <> {
  2. ...
  3. PRIVATE:
  4. ...
  5. Void oninternalpacket (
  6. Const char * bytes, size_t size, const talk_base: socketaddress & remote_addr,
  7. Talk_base: asyncpacketsocket * socket );
  8. ...
  9. }

 

Note:

The above oninternalpacket () is defined as private in the relayserver class, and can be executed outside the relayserver class through the signal/slot mechanism. This is determined by the use of function pointers in the sigslot library. It seems that C ++'s permission protection for private members is broken. Of course, this is authorized inside the slot class (relayserver: addinternalsocket (); so it can be said that the slot class knows that its private slot method will be executed outside the class, is informed and authorized. From this perspective, we can say that flexibility is provided: authorization can be executed, and private methods cannot be directly called without authorization.

 

-Qianli

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.