Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cmu
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李维杰
cmu
Commits
f4cbc4db
Commit
f4cbc4db
authored
Feb 16, 2022
by
李维杰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
为了测试turn 81.70.124.49 及 纯P2P通讯(不走中转)而修改代码
parent
f5787584
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
567 additions
and
566 deletions
+567
-566
cmd_processer_server.cc
cmu/src/cmd_processer_server.cc
+159
-158
cmd_processer_server.h
cmu/src/cmd_processer_server.h
+40
-39
mqtt_request.cc
thirdparty/mqtt/mqtt_request.cc
+211
-213
mqtt_wrapper.cc
thirdparty/mqtt/mqtt_wrapper.cc
+157
-156
No files found.
cmu/src/cmd_processer_server.cc
View file @
f4cbc4db
#include "cmd_processer_server.h"
#include "mqtt_request.h"
namespace
offcn
{
static
const
std
::
string
kGlobalTopic
=
"kcp_server_mqtt"
;
static
const
std
::
string
k
LocalID
=
"kcp_server_mqtt"
;
CmdProcesserServer
::
CmdProcesserServer
()
:
mqtt_wrapper_
(
NULL
)
{
mqtt_wrapper_
=
new
offcn
::
MqttWrapper
(
this
);
}
CmdProcesserServer
::~
CmdProcesserServer
()
{
if
(
mqtt_wrapper_
)
{
delete
mqtt_wrapper_
;
mqtt_wrapper_
=
NULL
;
}
}
bool
CmdProcesserServer
::
Connect
()
{
return
mqtt_wrapper_
->
ConnectMqttServer
(
kLocalID
);
}
bool
CmdProcesserServer
::
SubscribeTopic
()
{
return
mqtt_wrapper_
->
SubscribeTopic
(
kGlobalTopic
);
}
/**
* CmdObserver
*
/
void
CmdProcesserServer
::
OnServerConnectSuccess
()
{
printf
(
"connect mqtt server success!
\n
"
);
SubscribeTopic
();
}
void
CmdProcesserServer
::
OnServerConnectFailure
()
{
printf
(
"connect mqtt server failure!
\n
"
);
}
void
CmdProcesserServer
::
OnServerSubscribeSuccess
()
{
printf
(
"subscribe topic success!
\n
"
);
}
void
CmdProcesserServer
::
OnServerSubscribeFailure
()
{
printf
(
"subscribe topic failure!
\n
"
);
}
void
CmdProcesserServer
::
OnServerMessageArrived
(
std
::
string
topic
,
std
::
string
message
)
{
printf
(
"
\n
receive message : topic = %s, context : %s
\n
"
,
topic
.
c_str
(),
message
.
c_str
());
std
::
string
method
;
std
::
string
error
;
ReqValue
values
;
if
(
!
MqttRequest
::
ParseRequestType
(
message
,
method
,
error
))
{
printf
(
"Error : Parse request error
\n
"
);
return
;
}
if
(
method
.
compare
(
"join"
)
==
0
)
{
if
(
MqttRequest
::
ParseJoinRequest
(
message
,
values
,
error
))
{
OnParseJoinSuccess
(
values
[
"message_id"
],
values
[
"from_id"
]);
}
else
{
OnParseFailure
(
values
[
"message_id"
],
values
[
"from_id"
],
error
);
}
}
else
if
(
method
.
compare
(
"update"
)
==
0
)
{
if
(
MqttRequest
::
ParseUpdateRequest
(
message
,
values
,
error
))
{
OnParseUpdateSuccess
(
values
[
"message_id"
],
values
[
"from_id"
],
values
[
"pull_type"
]);
}
else
{
OnParseFailure
(
values
[
"message_id"
],
values
[
"from_id"
],
error
);
}
}
}
void
CmdProcesserServer
::
OnParseJoinSuccess
(
std
::
string
message_id
,
std
::
string
from_id
)
{
std
::
string
response
;
if
(
client_lists_
.
find
(
from_id
)
!=
client_lists_
.
end
())
{
response
=
MqttRequest
::
BuildResponse
(
message_id
,
"id exist"
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
return
;
}
else
{
if
(
client_lists_
.
size
()
==
0
)
{
client_lists_
.
insert
(
std
::
pair
<
std
::
string
,
std
::
string
>
(
from_id
,
"rtmp"
));
}
else
{
client_lists_
.
insert
(
std
::
pair
<
std
::
string
,
std
::
string
>
(
from_id
,
"null"
));
}
//response
response
=
MqttRequest
::
BuildResponse
(
message_id
,
"ok"
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
printf
(
"
\n
------------------------------------
\n
"
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
itor
;
for
(
itor
=
client_lists_
.
begin
();
itor
!=
client_lists_
.
end
();
itor
++
)
{
if
(
itor
->
first
.
compare
(
from_id
)
!=
0
&&
itor
->
second
.
compare
(
"rtmp"
)
==
0
)
{
response
=
MqttRequest
::
PublishersRequest
(
"sdf"
,
from_id
,
itor
->
first
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
}
printf
(
"id = %s, pull type = %s
\n
"
,
itor
->
first
.
c_str
(),
itor
->
second
.
c_str
());
}
printf
(
"------------------------------------
\n
"
);
}
}
void
CmdProcesserServer
::
OnParseFailure
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
error
)
{
std
::
string
response
=
MqttRequest
::
BuildResponse
(
message_id
,
error
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
}
void
CmdProcesserServer
::
OnParseUpdateSuccess
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
pull_type
)
{
std
::
string
response
;
if
(
client_lists_
.
find
(
from_id
)
==
client_lists_
.
end
())
{
response
=
MqttRequest
::
BuildResponse
(
message_id
,
"id is not exist"
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
return
;
}
else
{
client_lists_
.
insert
(
std
::
pair
<
std
::
string
,
std
::
string
>
(
from_id
,
pull_type
));
response
=
MqttRequest
::
BuildResponse
(
message_id
,
"ok"
);
printf
(
"
\n
------------------------------------
\n
"
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
itor
;
for
(
itor
=
client_lists_
.
begin
();
itor
!=
client_lists_
.
end
();
itor
++
)
{
printf
(
"id = %s, pull type = %s
\n
"
,
itor
->
first
.
c_str
(),
itor
->
second
.
c_str
());
}
printf
(
"------------------------------------
\n
"
);
}
}
#include "cmd_processer_server.h"
#include "mqtt_request.h"
#include "json/json.h"
namespace
offcn
{
static
const
std
::
string
k
GlobalTopic
=
"topic_for_turn_test"
;
static
const
std
::
string
kLocalID
=
"mqtt_server_for_turn_test"
;
CmdProcesserServer
::
CmdProcesserServer
()
:
mqtt_wrapper_
(
NULL
)
{
mqtt_wrapper_
=
new
offcn
::
MqttWrapper
(
this
);
}
CmdProcesserServer
::~
CmdProcesserServer
()
{
if
(
mqtt_wrapper_
)
{
delete
mqtt_wrapper_
;
mqtt_wrapper_
=
NULL
;
}
}
bool
CmdProcesserServer
::
Connect
()
{
return
mqtt_wrapper_
->
ConnectMqttServer
(
kLocalID
);
}
bool
CmdProcesserServer
::
SubscribeTopic
()
{
return
mqtt_wrapper_
->
SubscribeTopic
(
kGlobalTopic
);
}
/**
*
CmdObserver
*/
void
CmdProcesserServer
::
OnServerConnectSuccess
()
{
printf
(
"connect mqtt server success!
\n
"
);
SubscribeTopic
();
}
void
CmdProcesserServer
::
OnServerConnectFailure
()
{
printf
(
"connect mqtt server failure!
\n
"
);
}
void
CmdProcesserServer
::
OnServerSubscribeSuccess
()
{
printf
(
"subscribe topic success!
\n
"
);
}
void
CmdProcesserServer
::
OnServerSubscribeFailure
()
{
printf
(
"subscribe topic failure!
\n
"
);
}
void
CmdProcesserServer
::
OnServerMessageArrived
(
std
::
string
topic
,
std
::
string
message
)
{
printf
(
"
\n
receive message : topic = %s, context : %s
\n
"
,
topic
.
c_str
(),
message
.
c_str
());
std
::
string
method
;
std
::
string
error
;
ReqValue
values
;
if
(
!
MqttRequest
::
ParseRequestType
(
message
,
method
,
error
))
{
printf
(
"Error : Parse request error
\n
"
);
return
;
}
if
(
method
.
compare
(
"login"
)
==
0
)
{
if
(
MqttRequest
::
ParseJoinRequest
(
message
,
values
,
error
))
{
OnParseJoinSuccess
(
values
[
"id"
]);
}
else
{
OnParseFailure
(
values
[
"id"
],
values
[
"id"
],
error
);
}
}
}
void
CmdProcesserServer
::
OnParseJoinSuccess
(
std
::
string
from_id
)
{
std
::
string
response
;
if
(
client_lists_
.
find
(
from_id
)
!=
client_lists_
.
end
())
{
response
=
MqttRequest
::
BuildResponse
(
from_id
,
"id exist"
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
return
;
}
else
{
client_lists_
.
insert
(
std
::
pair
<
std
::
string
,
std
::
string
>
(
from_id
,
from_id
));
Json
::
Value
root
;
Json
::
Value
root1
;
Json
::
Value
ids
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"method"
]
=
"publishers"
;
root1
[
"method"
]
=
"online"
;
root1
[
"id"
]
=
from_id
;
printf
(
"
\n
----------------online list--------------------
\n
"
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
itor
;
for
(
itor
=
client_lists_
.
begin
();
itor
!=
client_lists_
.
end
();
itor
++
)
{
if
(
itor
->
first
.
compare
(
from_id
)
!=
0
)
{
//response = MqttRequest::PublishersRequest("sdf", from_id, itor->first);
//mqtt_wrapper_->SendRequest(itor->first, response);
ids
.
append
(
itor
->
first
);
}
printf
(
"id = %s, pull type = %s
\n
"
,
itor
->
first
.
c_str
(),
itor
->
second
.
c_str
());
}
printf
(
"
\n
-----------------------------------------------
\n
"
);
root
[
"ids"
]
=
ids
;
std
::
string
req
=
Json
::
writeString
(
wBuilder
,
root
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
req
);
//req = Json::writeString(wBuilder, root1);
//std::string topic = kGlobalTopic;
//mqtt_wrapper_->SendRequest(topic, req);
}
}
void
CmdProcesserServer
::
OnParseFailure
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
error
)
{
std
::
string
response
=
MqttRequest
::
BuildResponse
(
message_id
,
error
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
}
void
CmdProcesserServer
::
OnParseUpdateSuccess
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
pull_type
)
{
std
::
string
response
;
if
(
client_lists_
.
find
(
from_id
)
==
client_lists_
.
end
())
{
response
=
MqttRequest
::
BuildResponse
(
message_id
,
"id is not exist"
);
mqtt_wrapper_
->
SendRequest
(
from_id
,
response
);
return
;
}
else
{
client_lists_
.
insert
(
std
::
pair
<
std
::
string
,
std
::
string
>
(
from_id
,
pull_type
));
response
=
MqttRequest
::
BuildResponse
(
message_id
,
"ok"
);
printf
(
"
\n
------------------------------------
\n
"
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
itor
;
for
(
itor
=
client_lists_
.
begin
();
itor
!=
client_lists_
.
end
();
itor
++
)
{
printf
(
"id = %s, pull type = %s
\n
"
,
itor
->
first
.
c_str
(),
itor
->
second
.
c_str
());
}
printf
(
"------------------------------------
\n
"
);
}
}
}
\ No newline at end of file
cmu/src/cmd_processer_server.h
View file @
f4cbc4db
#pragma once
#include "mqtt_wrapper.h"
#include <map>
namespace
offcn
{
class
CmdProcesserServer
:
public
CmdObserver
{
public
:
CmdProcesserServer
();
~
CmdProcesserServer
();
public
:
bool
Connect
();
bool
SubscribeTopic
();
public
:
/**
* CmdObserver
*/
virtual
void
OnServerConnectSuccess
();
virtual
void
OnServerConnectFailure
();
virtual
void
OnServerSubscribeSuccess
();
virtual
void
OnServerSubscribeFailure
();
virtual
void
OnServerMessageArrived
(
std
::
string
topic
,
std
::
string
message
);
private
:
void
OnParseJoinSuccess
(
std
::
string
message_id
,
std
::
string
from_id
);
void
OnParseFailure
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
error
);
void
OnParseUpdateSuccess
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
pull_type
);
private
:
MqttWrapper
*
mqtt_wrapper_
;
private
:
std
::
map
<
std
::
string
,
std
::
string
>
client_lists_
;
};
#pragma once
#include "mqtt_wrapper.h"
#include <map>
namespace
offcn
{
class
CmdProcesserServer
:
public
CmdObserver
{
public
:
CmdProcesserServer
();
~
CmdProcesserServer
();
public
:
bool
Connect
();
bool
SubscribeTopic
();
public
:
/**
* CmdObserver
*/
virtual
void
OnServerConnectSuccess
();
virtual
void
OnServerConnectFailure
();
virtual
void
OnServerSubscribeSuccess
();
virtual
void
OnServerSubscribeFailure
();
virtual
void
OnServerMessageArrived
(
std
::
string
topic
,
std
::
string
message
);
private
:
void
OnParseJoinSuccess
(
std
::
string
from_id
);
void
OnParseFailure
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
error
);
void
OnParseUpdateSuccess
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
pull_type
);
private
:
MqttWrapper
*
mqtt_wrapper_
;
private
:
std
::
map
<
std
::
string
,
std
::
string
>
client_lists_
;
};
}
\ No newline at end of file
thirdparty/mqtt/mqtt_request.cc
View file @
f4cbc4db
#include "mqtt_request.h"
#include "json/json.h"
namespace
offcn
{
static
bool
GetJsonValue
(
std
::
string
&
response
,
Json
::
Value
&
root
)
{
Json
::
CharReaderBuilder
readerBuilder
;
std
::
unique_ptr
<
Json
::
CharReader
>
const
reader
(
readerBuilder
.
newCharReader
());
const
char
*
start_pos
=
response
.
c_str
();
std
::
string
err
;
if
(
!
reader
->
parse
(
start_pos
,
start_pos
+
response
.
length
(),
&
root
,
&
err
))
{
return
false
;
}
return
true
;
}
std
::
string
MqttRequest
::
JoinRequest
(
std
::
string
message_id
,
std
::
string
from_id
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"join"
;
root
[
"from_id"
]
=
from_id
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
UpdateRequest
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
pull_type
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"update"
;
root
[
"from_id"
]
=
from_id
;
root
[
"pull_type"
]
=
pull_type
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
OfferRequest
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
to_id
,
std
::
string
candidate
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"offer"
;
root
[
"from_id"
]
=
from_id
;
root
[
"to_id"
]
=
to_id
;
root
[
"candidate"
]
=
candidate
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
AnswerRequest
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
to_id
,
std
::
string
candidate
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"answer"
;
root
[
"from_id"
]
=
from_id
;
root
[
"to_id"
]
=
to_id
;
root
[
"candidate"
]
=
candidate
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
PublishersRequest
(
std
::
string
message_id
,
std
::
string
to_id
,
std
::
string
publisher_id
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"publishers"
;
root
[
"publishers_id"
]
=
publisher_id
;
root
[
"to_id"
]
=
to_id
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
bool
MqttRequest
::
ParseRequestType
(
std
::
string
request
,
std
::
string
&
type
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"method"
]
&&
!
root
[
"result_code"
])
{
error
=
"param error"
;
return
false
;
}
type
=
root
[
"method"
].
asString
();
return
true
;
}
bool
MqttRequest
::
ParseJoinRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
goto
Error
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"from_id"
])
{
error
=
"param error"
;
goto
Error
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"from_id"
]
=
root
[
"from_id"
].
asString
();
return
true
;
Error
:
values
[
"message_id"
]
=
""
;
values
[
"method"
]
=
""
;
values
[
"from_id"
]
=
""
;
return
false
;
}
bool
MqttRequest
::
ParseUpdateRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"from_id"
]
||
!
root
[
"pull_type"
])
{
error
=
"param error"
;
return
false
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"from_id"
]
=
root
[
"from_id"
].
asString
();
values
[
"pull_type"
]
=
root
[
"pull_type"
].
asString
();
return
true
;
}
bool
MqttRequest
::
ParseOfferRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"from_id"
]
||
!
root
[
"to_id"
]
||
!
root
[
"candidate"
])
{
error
=
"param error"
;
return
false
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"from_id"
]
=
root
[
"from_id"
].
asString
();
values
[
"to_id"
]
=
root
[
"from_id"
].
asString
();
values
[
"candidate"
]
=
root
[
"candidate"
].
asString
();
return
true
;
}
bool
MqttRequest
::
ParseAnswerRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
return
ParseOfferRequest
(
request
,
values
,
error
);
}
bool
MqttRequest
::
ParsePublishersRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"publishers"
]
||
!
root
[
"to_id"
])
{
error
=
"param error"
;
return
false
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"publishers_id"
]
=
root
[
"publishers_id"
].
asString
();
values
[
"to_id"
]
=
root
[
"to_id"
].
asString
();
return
true
;
}
std
::
string
MqttRequest
::
BuildResponse
(
std
::
string
message_id
,
std
::
string
result_code
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"method"
]
=
"response"
;
root
[
"message_id"
]
=
message_id
;
root
[
"result_code"
]
=
result_code
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
#include "mqtt_request.h"
#include "json/json.h"
namespace
offcn
{
static
bool
GetJsonValue
(
std
::
string
&
response
,
Json
::
Value
&
root
)
{
Json
::
CharReaderBuilder
readerBuilder
;
std
::
unique_ptr
<
Json
::
CharReader
>
const
reader
(
readerBuilder
.
newCharReader
());
const
char
*
start_pos
=
response
.
c_str
();
std
::
string
err
;
if
(
!
reader
->
parse
(
start_pos
,
start_pos
+
response
.
length
(),
&
root
,
&
err
))
{
return
false
;
}
return
true
;
}
std
::
string
MqttRequest
::
JoinRequest
(
std
::
string
message_id
,
std
::
string
from_id
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"join"
;
root
[
"from_id"
]
=
from_id
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
UpdateRequest
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
pull_type
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"update"
;
root
[
"from_id"
]
=
from_id
;
root
[
"pull_type"
]
=
pull_type
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
OfferRequest
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
to_id
,
std
::
string
candidate
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"offer"
;
root
[
"from_id"
]
=
from_id
;
root
[
"to_id"
]
=
to_id
;
root
[
"candidate"
]
=
candidate
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
AnswerRequest
(
std
::
string
message_id
,
std
::
string
from_id
,
std
::
string
to_id
,
std
::
string
candidate
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"message_id"
]
=
message_id
;
root
[
"method"
]
=
"answer"
;
root
[
"from_id"
]
=
from_id
;
root
[
"to_id"
]
=
to_id
;
root
[
"candidate"
]
=
candidate
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
std
::
string
MqttRequest
::
PublishersRequest
(
std
::
string
message_id
,
std
::
string
to_id
,
std
::
string
publisher_id
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"method"
]
=
"publishers"
;
root
[
"id"
]
=
message_id
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
bool
MqttRequest
::
ParseRequestType
(
std
::
string
request
,
std
::
string
&
type
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"method"
])
{
error
=
"param error"
;
return
false
;
}
type
=
root
[
"method"
].
asString
();
return
true
;
}
bool
MqttRequest
::
ParseJoinRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
goto
Error
;
}
if
(
!
root
[
"method"
]
||
!
root
[
"id"
])
{
error
=
"param error"
;
goto
Error
;
}
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"id"
]
=
root
[
"id"
].
asString
();
return
true
;
Error
:
values
[
"method"
]
=
""
;
values
[
"id"
]
=
""
;
return
false
;
}
bool
MqttRequest
::
ParseUpdateRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"from_id"
]
||
!
root
[
"pull_type"
])
{
error
=
"param error"
;
return
false
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"from_id"
]
=
root
[
"from_id"
].
asString
();
values
[
"pull_type"
]
=
root
[
"pull_type"
].
asString
();
return
true
;
}
bool
MqttRequest
::
ParseOfferRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"from_id"
]
||
!
root
[
"to_id"
]
||
!
root
[
"candidate"
])
{
error
=
"param error"
;
return
false
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"from_id"
]
=
root
[
"from_id"
].
asString
();
values
[
"to_id"
]
=
root
[
"from_id"
].
asString
();
values
[
"candidate"
]
=
root
[
"candidate"
].
asString
();
return
true
;
}
bool
MqttRequest
::
ParseAnswerRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
return
ParseOfferRequest
(
request
,
values
,
error
);
}
bool
MqttRequest
::
ParsePublishersRequest
(
std
::
string
request
,
ReqValue
&
values
,
std
::
string
&
error
)
{
Json
::
Value
root
;
if
(
!
GetJsonValue
(
request
,
root
))
{
error
=
"json format error"
;
return
false
;
}
if
(
!
root
[
"message_id"
]
||
!
root
[
"method"
]
||
!
root
[
"publishers"
]
||
!
root
[
"to_id"
])
{
error
=
"param error"
;
return
false
;
}
values
[
"message_id"
]
=
root
[
"message_id"
].
asString
();
values
[
"method"
]
=
root
[
"method"
].
asString
();
values
[
"publishers_id"
]
=
root
[
"publishers_id"
].
asString
();
values
[
"to_id"
]
=
root
[
"to_id"
].
asString
();
return
true
;
}
std
::
string
MqttRequest
::
BuildResponse
(
std
::
string
message_id
,
std
::
string
result_code
)
{
Json
::
Value
root
;
Json
::
StreamWriterBuilder
wBuilder
;
root
[
"method"
]
=
"login"
;
root
[
"id"
]
=
message_id
;
root
[
"result_code"
]
=
result_code
;
return
Json
::
writeString
(
wBuilder
,
root
);
}
}
\ No newline at end of file
thirdparty/mqtt/mqtt_wrapper.cc
View file @
f4cbc4db
#include "mqtt_wrapper.h"
#include <stdlib.h>
namespace
offcn
{
static
const
std
::
string
kMqttUrl
=
"
101.200.63.143"
;
//"192.168.43.12";
static
const
std
::
string
kUserName
=
"admin"
;
static
const
std
::
string
kPassWord
=
"public"
;
static
struct
Options
{
char
*
connection
;
int
verbose
;
int
test_no
;
int
size
;
int
MQTTVersion
;
int
iterations
;
}
options
;
MqttWrapper
::
MqttWrapper
(
CmdObserver
*
observer
)
:
mqtt_client_
(
NULL
),
observer_
(
observer
)
{
}
bool
MqttWrapper
::
ConnectMqttServer
(
std
::
string
localID
)
{
options
.
connection
=
(
char
*
)
malloc
(
128
);
memset
(
options
.
connection
,
0
,
128
);
memcpy
(
options
.
connection
,
kMqttUrl
.
c_str
(),
kMqttUrl
.
size
());
options
.
verbose
=
0
;
options
.
test_no
=
-
1
;
options
.
size
=
10000
;
options
.
MQTTVersion
=
MQTTVERSION_3_1
;
options
.
iterations
=
1
;
MQTTAsync_connectOptions
opts
=
MQTTAsync_connectOptions_initializer
;
MQTTAsync_willOptions
wopts
=
MQTTAsync_willOptions_initializer
;
std
::
string
clientid
=
localID
;
int
nRet
=
MQTTAsync_create
(
&
mqtt_client_
,
options
.
connection
,
clientid
.
c_str
(),
MQTTCLIENT_PERSISTENCE_NONE
,
NULL
);
if
(
nRet
!=
MQTTASYNC_SUCCESS
)
{
return
false
;
}
nRet
=
MQTTAsync_setCallbacks
(
mqtt_client_
,
this
,
NULL
,
mqttMessageCallback
,
NULL
);
std
::
string
new_token
=
kPassWord
;
opts
.
keepAliveInterval
=
20
;
opts
.
cleansession
=
1
;
opts
.
username
=
kUserName
.
c_str
();
opts
.
password
=
kPassWord
.
c_str
();
opts
.
MQTTVersion
=
options
.
MQTTVersion
;
opts
.
cleansession
=
true
;
opts
.
will
=
NULL
;
opts
.
onSuccess
=
mqttConnectSuccess
;
opts
.
onFailure
=
mqttConnectFailure
;
opts
.
context
=
this
;
opts
.
connectTimeout
=
5
;
nRet
=
MQTTAsync_connect
(
mqtt_client_
,
&
opts
);
if
(
nRet
!=
MQTTASYNC_SUCCESS
)
{
MQTTAsync_destroy
(
&
mqtt_client_
);
mqtt_client_
=
NULL
;
return
false
;
}
return
true
;
}
bool
MqttWrapper
::
SubscribeTopic
(
std
::
string
topic
)
{
dst_topic_
=
topic
;
MQTTAsync_responseOptions
opts
=
MQTTAsync_responseOptions_initializer
;
opts
.
onSuccess
=
mqttSubscribeSuccess
;
opts
.
onFailure
=
mqttSubscribeFailure
;
opts
.
context
=
this
;
int
nRet
=
MQTTAsync_subscribe
(
mqtt_client_
,
topic
.
c_str
(),
2
,
&
opts
);
if
(
nRet
!=
MQTTASYNC_SUCCESS
)
{
return
false
;
}
return
true
;
}
int
MqttWrapper
::
SendRequest
(
std
::
string
&
topic
,
std
::
string
&
request
)
{
MQTTAsync_message
msg
=
MQTTAsync_message_initializer
;
msg
.
payload
=
(
void
*
)
request
.
c_str
();
msg
.
payloadlen
=
(
int
)
request
.
length
();
msg
.
qos
=
2
;
msg
.
retained
=
0
;
int
nRet
=
MQTTAsync_send
(
mqtt_client_
,
topic
.
c_str
(),
msg
.
payloadlen
,
msg
.
payload
,
msg
.
qos
,
msg
.
retained
,
NULL
);
if
(
nRet
==
MQTTASYNC_SUCCESS
)
{
return
0
;
}
return
-
1
;
}
void
MqttWrapper
::
OnMqttConnectSuccess
(
MQTTAsync_successData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerConnectSuccess
();
}
}
void
MqttWrapper
::
OnMqttConnectFailure
(
MQTTAsync_failureData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerConnectFailure
();
}
}
void
MqttWrapper
::
OnMqttSubscribeSuccess
(
MQTTAsync_successData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerSubscribeSuccess
();
}
}
void
MqttWrapper
::
OnMqttSubscribeFailure
(
MQTTAsync_failureData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerSubscribeFailure
();
}
}
int
MqttWrapper
::
mqttRecvMessage
(
char
*
topicName
,
int
topicNameLen
,
MQTTAsync_message
*
message
)
{
if
(
dst_topic_
.
compare
(
topicName
)
!=
0
)
return
1
;
if
(
message
->
payloadlen
<=
0
)
return
1
;
std
::
string
topic
;
topic
.
append
(
topicName
,
topicNameLen
);
std
::
string
msg
;
msg
.
append
((
char
*
)
message
->
payload
,
message
->
payloadlen
);
if
(
observer_
)
{
observer_
->
OnServerMessageArrived
(
topic
,
msg
);
}
MQTTAsync_freeMessage
(
&
message
);
MQTTAsync_free
(
topicName
);
return
1
;
}
#include "mqtt_wrapper.h"
#include <stdlib.h>
namespace
offcn
{
static
const
std
::
string
kMqttUrl
=
"
test-mqq.offcncloud.com"
;
//"192.168.43.12";
static
const
std
::
string
kUserName
=
"admin"
;
static
const
std
::
string
kPassWord
=
"public"
;
static
struct
Options
{
char
*
connection
;
int
verbose
;
int
test_no
;
int
size
;
int
MQTTVersion
;
int
iterations
;
}
options
;
MqttWrapper
::
MqttWrapper
(
CmdObserver
*
observer
)
:
mqtt_client_
(
NULL
),
observer_
(
observer
)
{
}
bool
MqttWrapper
::
ConnectMqttServer
(
std
::
string
localID
)
{
options
.
connection
=
(
char
*
)
malloc
(
128
);
memset
(
options
.
connection
,
0
,
128
);
memcpy
(
options
.
connection
,
kMqttUrl
.
c_str
(),
kMqttUrl
.
size
());
options
.
verbose
=
0
;
options
.
test_no
=
-
1
;
options
.
size
=
10000
;
options
.
MQTTVersion
=
MQTTVERSION_3_1
;
options
.
iterations
=
1
;
MQTTAsync_connectOptions
opts
=
MQTTAsync_connectOptions_initializer
;
MQTTAsync_willOptions
wopts
=
MQTTAsync_willOptions_initializer
;
std
::
string
clientid
=
localID
;
int
nRet
=
MQTTAsync_create
(
&
mqtt_client_
,
options
.
connection
,
clientid
.
c_str
(),
MQTTCLIENT_PERSISTENCE_NONE
,
NULL
);
if
(
nRet
!=
MQTTASYNC_SUCCESS
)
{
return
false
;
}
nRet
=
MQTTAsync_setCallbacks
(
mqtt_client_
,
this
,
NULL
,
mqttMessageCallback
,
NULL
);
std
::
string
new_token
=
kPassWord
;
opts
.
keepAliveInterval
=
20
;
opts
.
cleansession
=
1
;
opts
.
username
=
kUserName
.
c_str
();
opts
.
password
=
kPassWord
.
c_str
();
opts
.
MQTTVersion
=
options
.
MQTTVersion
;
opts
.
cleansession
=
true
;
opts
.
will
=
NULL
;
opts
.
onSuccess
=
mqttConnectSuccess
;
opts
.
onFailure
=
mqttConnectFailure
;
opts
.
context
=
this
;
opts
.
connectTimeout
=
5
;
nRet
=
MQTTAsync_connect
(
mqtt_client_
,
&
opts
);
if
(
nRet
!=
MQTTASYNC_SUCCESS
)
{
MQTTAsync_destroy
(
&
mqtt_client_
);
mqtt_client_
=
NULL
;
return
false
;
}
return
true
;
}
bool
MqttWrapper
::
SubscribeTopic
(
std
::
string
topic
)
{
dst_topic_
=
topic
;
MQTTAsync_responseOptions
opts
=
MQTTAsync_responseOptions_initializer
;
opts
.
onSuccess
=
mqttSubscribeSuccess
;
opts
.
onFailure
=
mqttSubscribeFailure
;
opts
.
context
=
this
;
int
nRet
=
MQTTAsync_subscribe
(
mqtt_client_
,
topic
.
c_str
(),
2
,
&
opts
);
if
(
nRet
!=
MQTTASYNC_SUCCESS
)
{
return
false
;
}
return
true
;
}
int
MqttWrapper
::
SendRequest
(
std
::
string
&
topic
,
std
::
string
&
request
)
{
MQTTAsync_message
msg
=
MQTTAsync_message_initializer
;
msg
.
payload
=
(
void
*
)
request
.
c_str
();
msg
.
payloadlen
=
(
int
)
request
.
length
();
msg
.
qos
=
2
;
msg
.
retained
=
0
;
int
nRet
=
MQTTAsync_send
(
mqtt_client_
,
topic
.
c_str
(),
msg
.
payloadlen
,
msg
.
payload
,
msg
.
qos
,
msg
.
retained
,
NULL
);
if
(
nRet
==
MQTTASYNC_SUCCESS
)
{
return
0
;
}
return
-
1
;
}
void
MqttWrapper
::
OnMqttConnectSuccess
(
MQTTAsync_successData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerConnectSuccess
();
}
}
void
MqttWrapper
::
OnMqttConnectFailure
(
MQTTAsync_failureData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerConnectFailure
();
}
}
void
MqttWrapper
::
OnMqttSubscribeSuccess
(
MQTTAsync_successData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerSubscribeSuccess
();
}
}
void
MqttWrapper
::
OnMqttSubscribeFailure
(
MQTTAsync_failureData
*
response
)
{
if
(
observer_
)
{
observer_
->
OnServerSubscribeFailure
();
}
}
int
MqttWrapper
::
mqttRecvMessage
(
char
*
topicName
,
int
topicNameLen
,
MQTTAsync_message
*
message
)
{
if
(
dst_topic_
.
compare
(
topicName
)
!=
0
)
return
1
;
if
(
message
->
payloadlen
<=
0
)
return
1
;
std
::
string
topic
;
topic
.
append
(
topicName
,
topicNameLen
);
std
::
string
msg
;
msg
.
append
((
char
*
)
message
->
payload
,
message
->
payloadlen
);
if
(
observer_
)
{
observer_
->
OnServerMessageArrived
(
topic
,
msg
);
}
MQTTAsync_freeMessage
(
&
message
);
MQTTAsync_free
(
topicName
);
return
1
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment