ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:142.08KB ,
资源ID:8598532      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8598532.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(libjingle学习笔记Thread MessageQueue SocketServer.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

libjingle学习笔记Thread MessageQueue SocketServer.docx

1、libjingle学习笔记Thread MessageQueue SocketServerlibjingle中的重要概念关于libjingle你应当理解下面的重要概念:SignalsThreads and MessagesNaming Conventions SSL SupportConnectionsTransports, Channels, and ConnectionsCandidates Data Packets一、 Signalslibjingle uses the sigslot library to facilitate communication between objects

2、. sigslot is a generic framework that enables you to connect a calling member to a receiving function in any class (including the same class) very simply. The way it works is this:libjingle使用sigslot library实现对象间通信,sigslot是一个通用的的框架,使你能够很简单的连接任意类(包括同一个类)的一个调用成员与一个收个函数。它的工作方式如下:发送类声明一个成员变量,叫做signalN,使用

3、一个特殊的模版化的语法。这个信号定义了监听函数参数。监听类实现一个函数具有相同数量、类型和参数序列的信号。这有时也被称为接收机或信槽。(注意:这个甚至可以是作为声明信号的那个相同的类。)这个函数不能返回一个值(例如。,返回void)。接收者必须继承sigslot:has_slot。监听类通过调用信号的连接方法连接到信号,传递一个指向对象的实例和该监听类实现函数的地址。发送方调用它的信号成员,就好像它是一个函数,并传递声明时的适当参数类型。它可以通过值传递参数或引用。You can connect as many signals as you like to a common slot. lib

4、jingle sometimes assigns multiple signals to a single slot in order to consolidate its message handling. Conversely, several objects declare a signal object in order to broadcast commonly needed messages from a single point (for example, alerts sent when a connection state changes). sigslot takes ca

5、re of disconnecting callbacks and dereferencing when objects are destroyed. 你可以连接尽可能多的信号使用一个通用的槽。libjingle有时分配多个信号到一个槽为了巩固其消息处理。相反地,几个对象声明一个信号对象以广播通常需要的消息从一个信源(例如,当一个连接状态改变时发送的警报状态)。当对象被销毁时sigslot负责切断回调函数和解除关联。下面的代码演示了使用sigslot:/ Class that sends the notification.class Sender / 信号声明. /名字中的2标志参数数量,参数

6、类型 / 被声明为模板参数列表 sigslot:signal2 SignalDanger; /任何对Panic()的调用将会发送SignalDanger信号 void Panic() SignalDanger(Help!, std:time(0); /监听类必须继承自sigslotclass Receiver : public sigslot:has_slots/接收注册以得到SignalDanger信号。/当SignalDanger信号发送,它被OnDanger()捕获。/第二个参数给地址的侦听器函数类定义。/第一个参数指出这个类的实例来接收通知。 Receiver(Sender sende

7、r) sender-SignalDanger.connect(this, &Receiver.OnDanger); / 任何对Panic()的调用, 将使Receiver:OnDanger获得消息. / 注意,数量和类型的参数匹配 void OnDanger(string message, std:time_t time) if(message = Help!) / Call the police . .代码中的许多类发送信号以通知重要事件的监听器,例如,例如当你发送或收到一个连接请求时Call:SignalSessionState发送通知。你的应用程序必须连接这些信号与适当的处理过程。在li

8、bjingle代码中的一般惯例是在信号的名称前加Signal,例如SignalStateChange,SignalSessionState,SignalSessionCreate,监听方法如。,SignalStateChange,SignalSessionState,SignalSessionCreate。 用来连接到信号的监听器方法通常在前面加上On。例如:OnPortDestroyed(),OnOutgoingMessage(),OnSendPacket()。看到sigslot文档为更多的细节。二、 Threads为了提高应用程序的性能libjingle支持多线程。libjingle组件使

9、用一个或两个全局可用的线程:一、 signaling threadsignaling thread是一个用于创建所有基本组件的线程,例如Session Manager和控制以及XMPP消息传递组件。二、 worker threadworker thread(在代码中有时称为channel thread)被对等组件对象(Peer to Peer component objects)来处理更多的资源密集型处理,比如数据流。把这些放在一个单独的线程中可以防止数据流阻塞或被XMPP或用户接口组件阻塞。使用工作线程的类包括ChannelManager,SocketMonitor,P2PTransport

10、Channel,以及Port对象。 为了启用第二个线程,您必须创建一个新的线程对象并传递到SessionManager构造函数。(如果没有线程被传递,那个创建SessionManager的线程将被用作工作线程)。CallClient:InitPhone演示了如何创建一个工作线程的底层组件。此外,libjingle现在提供了一个叫做SignalThread的基类。扩展这个类能使一个类对象存在于自己的线程中,可以被实例化,开始,独自完成和在完成后删除自己。关于更多信息看signalthread.h/.cc。注意:尽管libjingle支持多个线程,只有某些方法通过验证调用线程支持线程安全,并且很少

11、有方法做任何锁。下面的代码片段演示了一个方法怎样验证它正在哪个线程中调用:/检查我们被调用的通道(如.,worker)线程。ASSERT(talk_base:Thread:Current() = channel_thread_);channel_thread_-Clear(this);ibjingle使用talk_base:Thread object(or a derived object)封装了所有线程,信号线程,工作线程,和任何其他线程,。所有线程对象由ThreadManager来管理, ThreadManager针对请求检索这些线程。SessionManager调用ThreadManag

12、er:CurrentThread以提供给它一个signaling thread(和一个工作线程,如果没有被提供)当它被实例化;XmppPump使用当前线程用于其信号线程。因此, 在创建一个SessionManager对象之前或者期望XmppPump开始工作之前您必须创建一个线程对象(或派生对象)信号线程并放入ThreadManager的线程池, (参见注册服务器例如代码。)有两种方法可以创建一个线程:三、 AutoThread(自动线程)This wraps the existing operating system thread with a libjingle Thread object

13、and makes it the current thread in the ThreadManager objects thread pool (that is, will return the thread if Thread:CurrentThread is called). 用一个libjingle线程对象封装现有的操作系统线程,并使它成为在ThreadManager线程池中的当前线程(即,如果Thread:CurrentThrad被调用将返回这个线程)。四、 ThreadThis creates and wraps a new thread to use, typically for

14、 a worker thread. In order to use this thread, you have to create a new Thread object, call ThreadManager:Add or ThreadManager:SetCurrent to add it to the pool, and call Run to start it in a blocking loop, or Start to start the thread listening.这将创建和封装一个新的线程,用于,通常为一个工作线程。为了使用这个线程,您必须创建一个新的线程对象,调用Thr

15、eadManager:Add 或 ThreadManager:SetCurrent将它添加到池中,并调用Run以启动它在一个阻塞循环中,或者Start以启动线程监听。Threads provide a conduit for messages between (or within) objects. For instance, SocketManager sends a message to itself on another thread to destroy a socket, or to SessionManager when connection candidates have bee

16、n generated. The Thread object inherits MessageQueue, and together they expose Send, Post, and other methods for sending messages synchronously and asynchronously. An object that will receive messages sent using MessageQueue must inherit and implement MessageHandler. MessageHandler defines the OnMes

17、sage method, which is called with the MessageQueue messages. 线程在对象之间(或对象内部)为消息提供了一个渠道。例如,SocketManager给它自己发送一个消息在另一个线程中摧毁一个套接字,或者当连接的候选人(candidates)被生成时给SessionManager发送消息。线程对象继承MessageQueue,和他们一起暴露Send、Post其他方法用于同步和异步发送消息。一个要收到MessageQueue发送的消息的对象必须继承和实现MessageHandler。MessageHandler定义了OnMessage方法,在

18、收到MessageQueue发送的消息时被调用。You can send messages to any object that inherits talk_base:MessageHandler over any thread. However, if sending a message to perform a resource-intensive thread, you should send the message over the worker thread. You can get a handle to the worker thread by calling SessionMa

19、nager:worker_thread(). You can get a handle to the signaling thread by calling SessionManager:signaling_thread().An object has several ways to access a specific thread: it can request and store a thread pointer as an input parameter; it can assume that the current thread when it is created (accessed

20、 by ThreadManager:CurrentThread in its constructor) is a particular thread and cache a member pointer to it; it can call SessionManger:signal_thread() or SessionManager:worker_thread() to retrieve threads. All three techniques are used in libjingle. 你能够给任何继承自talk_base:MessageHandler的对象跨越任何线程发送消息,然而,

21、如果发送一个消息给一个执行资源密集型的线程,你应当发送消息到工作线程。你能够通过调用SessionManager:worker_thread()以得到一个工作线程的句柄。你能够通过调用SessionManager:signaling_thread()获得一个信号线程的句柄。一个对象有几种方式去访问一个具体的线程::它可以请求并存储一个线程指针作为输入参数,它可以假定当前线程创建时(在其构造函数中访问ThreadManager:CurrentThread)是一个特定的线程并使用成员指针指向这个线程,它可以调用SessionManger:signal_thread()或SessionManager

22、:worker_thread()来检索线程。所有三个技术都可以在用于libjingle。Because an object can be called on any thread, an object may need to verify which thread a method is being called from. To do this, call Thread:Current (which retrieves the current thread) and compare that value against a known thread-this can be one of th

23、e threads exposed by SessionManager, or the object can store a pointer to its initial thread in the constructor. Here is a more extended example of calling a method in the same object on another thread. 因为一个对象能够在任何线程中被调用,一个对象需要验证被哪一个线程的哪个方法中调用,要做到这一点,调用Thread:Current(检索当前线程)并且根据一个已知的线程可能通过SessionMan

24、ager或者在构造函数中存储的线程初始化时的指针来进行值的比较。这是一个较为扩展的示例,在不同的线程上同一个对象中调用一个方法。/请注意,工作线程是没有初始化,直到有人 /调用PseudoTcpChannel:连接/ Also note that this method *is* thread-safe. bool PseudoTcpChannel:Connect(const std:string& channel_name) ASSERT(signal_thread_-IsCurrent(); CritScope lock(&cs_); if (channel_) return false;

25、 ASSERT(session_ != NULL); worker_thread_ = session_-session_manager()-worker_thread();.void PseudoTcpChannel:SomeFunction() . / Post a message to yourself over the worker thread. worker_thread_-Post(this, MSG_PING); / message_id = MSG_SORT) OnSort(); else if (pmsg-message_id = MSG_PING) / - And com

26、es out here! / Check that were in the worker thread before proceding. ASSERT(worker_thread_-IsCurrent(); OnPing(); else if (pmsg-message_id = MSG_ALLOCATE) OnAllocate(); else assert(false);五、 Naming Conventionslibjingle has some naming conventions that it is useful to be aware of:OnSomeMethodMethods

27、 beginning with On are often connected to a signal, either from this or another object. If called from the same object, it is probably called on a different thread. SomeMethod_wMethods ending with _w exist in the worker thread and are called from another threadSignalSomeNameThese are the signals tha

28、t send messages to callback methods. Libjingle有一些命名约定,知道这些是有用的:On SomeMethod方法以“On”开始,通常连接一个这个对象或另一个对象的信号,如果从同一个对象调用,它可能是调用一个不同的线程。SomeMethod_w 方法以“_w“结尾存在于工作线程中并且从另一个线程被调用。SignalSomeName,这些是发送消息到回调方法的信号。六、 SSL Supportlibjingle支持两种类型的SSL:OpenSSL(UNIX)SChannel(对于Windows)使用SSL,您必须执行以下步骤:#define FEATUR

29、E_ENABLE_SSL (在Visual Studio项目中,这个值是项目中定义的设置,而不是代码中).Ensure that either SSL_USE_OPENSSL or SSL_USE_SCHANNEL are #defined in ssladapter.cc. One of these should be defined by default, depending on the build settings for your operating system. 确保SSL_USE_OPENSSL或SSL_USE_SCHANNEL 被定义在 ssladapter.cc中定义。 默

30、认情况下其中一个应该被定义,根据构建设置您的操作系统。Call InitializeSSL to initialize required components. This function is defined in ssladapter.cc. When the application closes down, call CleanupSSL. You do not need to call InitializeSSLThread (it is used internally by InitializeSSL). InitializeSSL调用初始化所需的组件。 该函数定义在ssladapt

31、er.cc中。 当应用程序关闭时调用CleanupSSL。你不需要调用InitializeSSLThread(它被用于内部由InitializeSSL使用)。七、 ConnectionsA libjingle peer-to-peer connection actually consists of two channels: The session negotiation channel (also called the signaling channel) is the communication link used to negotiate the data connection. This channel is used to request a connection, exchange candidates, and negotiate the details of the session (such as

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1