Libjingle source code parsing (1)-what can we learn from libjingle?

Source: Internet
Author: User

Recently boring, I opened the source code and found that libjingle is a good thing and decided to study it.

:

Http://code.google.com/p/libjingle/

SVN checkout can be used:

SVN checkoutHTTP: // Libjingle.googlecode.com/svn/trunk/
Libjingle-read-only

I used myjingle for debugging:

Http://www.bluehands.de/software/beat/myjingle/

Debugging through myjingle has the following advantages:

1. myjingle has helped us configure the Vc solution and setup package projects.

2. myjingle can connect to Google Talk, so that you can connect to the Google server through the Gmail account, without building your own XMPP server.

3. Through living instance code, you can compile and debug the libjingle implementation method and XMPP protocol.


After analyzing the source code of libjingle over the past two days, we have learned the following points that can be used in our own code:

1. Signal-Slot Mechanism (here is the introduction: http://blog.csdn.net/smallcraft/article/details/2237802)

Sigslot, in short, indicates that signal is the event source, while slot is the event processing method. The sigslot mechanism associates the event sources and event processing methods of two types of objects through weak coupling.

The usage of sigslot in libjingle is as follows:

First, define the event source, that is, the xmppclient class, which contains the signalstatechange member variable of Type sigslog: signal1 <xmppengine: State>. The xmppengine here :: the functions of the State template type will be introduced.

class XmppClient{public:    sigslot::signal1<XmppEngine::State> SignalStateChange;}

Next, define another class event processing method, that is, testclient, inherited from sigslog: has_slots <> (this class has the event processing function ).

Then, use the Connect Method of the signalstatechange member variable of the xmppclient object just defined to associate the signalstatechange event with the onstatechange method of testclient. Once signalstatechange is triggered, the onstatechange method is called immediately.

class TestClient: public sigslot::has_slots<>{    TestClient(buzz::XmppClient* xmpp_client):xmpp_client_(xmpp_client){xmpp_client_->SignalStateChange.connect(this, &TestClient::OnStateChange);        }void OnStateChange(buzz::XmppEngine::State state){if(state == buzz::XmppEngine::STATE_CLOSED){printf("Xmpp Closed\r\n");cricket::Thread::Current()->Stop();}else{switch (state){case buzz::XmppEngine::STATE_START:printf("Xmpp START\r\n");break;case buzz::XmppEngine::STATE_OPENING:printf("Xmpp OPENING\r\n");break;case buzz::XmppEngine::STATE_OPEN:printf("Xmpp OPENED\r\n");InitPresence();break;}}}};

So how to trigger the event?

It is easy to call the following statement in any place:

In the xmppclient class method:

voidXmppClient::EnsureClosed() {  if (!d_->signal_closed_) {    d_->signal_closed_ = true;    delivering_signal_ = true;    SignalStateChange(XmppEngine::STATE_CLOSED);    delivering_signal_ = false;  }}

Instead of xmppclient, the method is as follows:

voidXmppClient::Private::OnStateChange(int state) {  if (state == XmppEngine::STATE_CLOSED) {    client_->EnsureClosed();  }  else {    client_->SignalStateChange((XmppEngine::State)state);  }  client_->Wake();}

The above two trigger events show that the xmppengine: State mentioned above is the parameter of the trigger event. This parameter is finally called to the onstatechange parameter. This mechanism is similar to sendmessage in Windows window processing, but more like a broadcast message method, because sigslot can support single-source triggering and multiple methods are called, that is, the Connect Method of signalstatechange can be called multiple times.

* In addition, up to eight parameters can be supported in the sigslot of ligjingle.

2. synchronous/asynchronous multi-thread processing.

If you have read the source code of chrome, we can know that all the chrome thread models are in asynchronous command mode. posttask and postdelayedtask can be seen everywhere.

Ligjingle not only supports asynchronous mode, but also synchronization mode. like Windows window message processing mechanism, it supports both postmessage and sendmessage.

3. When dealing with network-related operations, a large number of adapter modes are used.

In computer programming, the adapter mode (sometimes called packaging style or packaging) adapts a class interface to what the user expects. An adaptation allows a class that cannot work together because the interface is not compatible, by encapsulating its own interface in an existing class. Ligjingle needs to support multiple platforms. The use of interfaces in Windows and Linux is similar, but this small difference will make the codes of the two unable to adapt to each other, therefore, you need to wrap the Code related to this platform through the adapter. In addition, when SSL is supported, libjingle uses Schannel for Windows.
In Linux, OpenSSL is used.


4. Drilling Technology.

As shown in, relayserver is needed for communication between computers in Nat or between clients with firewalls.

Libjingle supports holes and P2P transmission through the XMPP protocol.

Through the XMPP protocol, libjingle holes, we can:

1. Multi-User speech.

2. Multi-User video.

3. Multi-User File Sharing.

4. One-to-one remote desktop assistance.

5. Multi-user online music stream synchronization.

6. There are many other things we can do.

5. Understand how XMPP and Gtalk are implemented.

What is XMPP? Http://baike.baidu.com/view/189676.htm)

XMPP is an XML-based protocol that inherits the flexible development in the XML environment. Therefore, XMPP-based applications are highly scalable. After the extension, XMPP can send extended information to meet user requirements, and create application processes such as the content publishing system and address-based services at the top of XMPP. In addition, XMPP includes a software protocol for the server to enable communication with the other, making it easier for developers to build customer applications or add functions to a system.

Gtalk and MSN can all enjoy chatting through XMPP protocol.

6. Follow-up, need more in-depth understanding of XMPP and libjingle, if there are like-minded, you can contact: lihe21327@gmail.com

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.