ExistQt
ServicePublisher.cpp
浏览该文件的文档.
1 #include "EqPreCompile.h" //QApplication
2 
3 #include "ServicePublisher.h"
4 
5 
10 {
11  connectSignals(); //连接信号槽。
12 
13  listen(); //监听。加入组播组。
14 } //ServicePublisher::ServicePublisher()
15 
20 {
21  networkThread.quit(); //工作线程退出。
22 } //void Communicator::quit()
23 
30 void ServicePublisher::publishService(QString LanServiceName,quint16 LanServicePort,exist::ServicePublishMessage::ServiceProtocolType LanServiceProtocolType)
31 {
32  Q_UNUSED(LanServiceName); //不使用这个参数。
33  Q_UNUSED(LanServicePort); //不使用这个参数。
34  Q_UNUSED(LanServiceProtocolType); //不使用这个参数。
35 
36  exist::ExistMessage existMessage; //消息。
37 
38  existMessage.set_messagetype (exist::ExistMessage::SERVICEPUBLISH); //设置消息类型。
39 
40  exist::ServicePublishMessage * servicePublishMessage=existMessage.mutable_servicepublishmessage (); //获取服务发布消息对象。
41 
42  servicePublishMessage->set_name (LanServiceName.toStdString ()); //设置服务名字。
43  servicePublishMessage->set_port (LanServicePort); //设置端口号。
44  servicePublishMessage->set_protocoltype (LanServiceProtocolType); //设置协议类型。
45 
46  string packageString=existMessage.SerializeAsString (); //序列化为字符串。
47 
48  emit shoudCastData(packageString); //发射信号,应当发送数据。
49 } //void ServicePublisher::publishService(QString LanServiceName,quint16 LanServicePort,ServiceProtocolType LanServiceProtocolType)
50 
54 void ServicePublisher::listen()
55 {
56  startNetworkThread(); //启动网络线程。
57 } //void UdpServer::listen()
58 
63 {
64 } //ServicePublisher::~ServicePublisher()
65 
69 void ServicePublisher::connectSignals()
70 {
71  connect (&networkThread,&EqNetworkThread::readedSocketData,this,&ServicePublisher::processDatagram); //读取到了数据,则处理。
72  connect (this,&ServicePublisher::shoudCastData,&networkThread,&EqNetworkThread::castData); //应当发送数据,则发送。
73 } //void ServicePublisher::connectSignals()
74 
79 void ServicePublisher::processDatagram(char * datagram, int datagramLength, QHostAddress senderAddress)
80 {
81  ExistMessage existMessage;
82 
83  if (datagramLength>0) //长度有效。
84  {
85  string datagramtoStdString(datagram,datagramLength); //数据包。
86 
87  existMessage.ParseFromString (datagramtoStdString); //解析数据包。
88 
89  switch (existMessage.messagetype ()) //根据消息类型来处理。
90  {
91  case ExistMessage::SERVICEPUBLISH: //服务发布。
92  reportServiceDiscovered(existMessage,senderAddress.toString().toStdString()); //报告,发现了服务。
93 
94  break; //跳出。
95 
96  default: //默认情况。
97  break; //跳出。
98  } //switch (existMessage.messagetype ()) //根据消息类型来处理。
99 
100  } //if (datagramLength>0) //长度有效。
101 } //void UdpServer::processDatagram(QByteArray datagram)
102 
107 void ServicePublisher::reportServiceDiscovered(ExistMessage existMessage,string address)
108 {
109  const ServicePublishMessage servicePublishMessage=existMessage.servicepublishmessage (); //获取消息发布消息对象。
110 
111  QString serviceName=QString::fromStdString (servicePublishMessage.name ()); //获取名字。
112  quint16 servicePort=servicePublishMessage.port (); //获取端口号。
113  ServicePublishMessage::ServiceProtocolType serviceProtocolType=servicePublishMessage.protocoltype (); //获取协议类型。
114 
115  emit serviceDiscovered(serviceName,servicePort,serviceProtocolType,address); //发射信号,发现了服务。
116 } //void ServicePublisher::reportServiceDiscovered(ExistMessage existMessage)
117 
121 void ServicePublisher::startNetworkThread()
122 {
123  networkThread.start(); //启动线程。
124 } //void ServicePublisher::startNetworkThread()
ExistCpp的主要接口,用于向局域网发布服务。
::com::stupidbeauty::exist::ServicePublishMessage_ServiceProtocolType protocoltype() const
void set_protocoltype(::com::stupidbeauty::exist::ServicePublishMessage_ServiceProtocolType value)
::com::stupidbeauty::exist::ExistMessage_MessageType messagetype() const
void set_port(::google::protobuf::int32 value)
void serviceDiscovered(QString serviceName, quint16 servicePort, ServicePublishMessage::ServiceProtocolType serviceProtocolType, string address)
serviceDiscovered 信号,发现了服务。
~ServicePublisher()
析构函数。
::google::protobuf::int32 port() const
::com::stupidbeauty::exist::ServicePublishMessage * mutable_servicepublishmessage()
const ::com::stupidbeauty::exist::ServicePublishMessage & servicepublishmessage() const
ServicePublisher()
默认构造函数。
void shoudCastData(string datagram)
shoudCastData 信号,应当发送数据。
void set_name(const ::std::string &value)
void set_messagetype(::com::stupidbeauty::exist::ExistMessage_MessageType value)
void publishService(QString LanServiceName, quint16 LanServicePort, exist::ServicePublishMessage::ServiceProtocolType LanServiceProtocolType)
发布服务。
void quit()
退出。