Adjust namespace
parent
f5ca9c74df
commit
990d19886a
|
@ -10,6 +10,8 @@
|
|||
#include "peerconnect.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace pc;
|
||||
|
||||
void usage(const char* prg);
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include "peerconnect.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace pc;
|
||||
|
||||
void usage(const char* prg);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace pc;
|
||||
|
||||
bool parse_args(int argc, char* argv[], std::string& alias, std::string& connect_to, bool& server_mode);
|
||||
void usage(const char* prg);
|
||||
|
@ -64,7 +65,7 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
else {
|
||||
std::cerr << "Connecting to " << connec_to << std::endl;
|
||||
if (!server_mode) pc->Connect(connec_to);
|
||||
pc->Connect(connec_to);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ void Control::DeleteControl() {
|
|||
}
|
||||
|
||||
|
||||
void Control::SignIn(const std::string& user_id, const std::string& user_password, const std::string& open_id) {
|
||||
void Control::SignIn(const string& user_id, const string& user_password, const string& open_id) {
|
||||
// 1. Connect to signal server
|
||||
// 2. Send signin command to signal server
|
||||
// 3. Send createchannel command to signal server (channel name is id or alias)
|
||||
|
@ -119,7 +119,7 @@ void Control::SignOut() {
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void Control::Connect(const std::string id) {
|
||||
void Control::Connect(const string id) {
|
||||
// 1. Join channel on signal server
|
||||
// 2. Server(remote) peer createoffer
|
||||
// 3. Client(local) peer answeroffer
|
||||
|
@ -134,7 +134,7 @@ void Control::Connect(const std::string id) {
|
|||
JoinChannel(id);
|
||||
}
|
||||
|
||||
void Control::Close(const std::string id, bool force_queuing) {
|
||||
void Control::Close(const string id, bool force_queuing) {
|
||||
|
||||
//
|
||||
// Called by
|
||||
|
@ -167,12 +167,12 @@ void Control::Close(const std::string id, bool force_queuing) {
|
|||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
}
|
||||
|
||||
void Control::Close( const std::string id ) {
|
||||
void Control::Close( const string id ) {
|
||||
Close( id, QUEUEING_ON );
|
||||
}
|
||||
|
||||
void Control::Close() {
|
||||
std::vector<std::string> peer_ids;
|
||||
std::vector<string> peer_ids;
|
||||
|
||||
for (auto peer : peers_) {
|
||||
peer_ids.push_back(peer.second->remote_id());
|
||||
|
@ -190,9 +190,9 @@ void Control::Close() {
|
|||
// Send data to peer
|
||||
//
|
||||
|
||||
void Control::Send(const std::string to, const char* buffer, const size_t size) {
|
||||
void Control::Send(const string to, const char* buffer, const size_t size) {
|
||||
|
||||
typedef std::map<std::string, rtc::scoped_refptr<PeerControl>>::iterator it_type;
|
||||
typedef std::map<string, rtc::scoped_refptr<PeerControl>>::iterator it_type;
|
||||
|
||||
it_type it = peers_.find(to);
|
||||
if (it == peers_.end()) return;
|
||||
|
@ -201,9 +201,9 @@ void Control::Send(const std::string to, const char* buffer, const size_t size)
|
|||
return;
|
||||
}
|
||||
|
||||
bool Control::SyncSend(const std::string to, const char* buffer, const size_t size) {
|
||||
bool Control::SyncSend(const string to, const char* buffer, const size_t size) {
|
||||
|
||||
typedef std::map<std::string, rtc::scoped_refptr<PeerControl>>::iterator it_type;
|
||||
typedef std::map<string, rtc::scoped_refptr<PeerControl>>::iterator it_type;
|
||||
|
||||
it_type it = peers_.find(to);
|
||||
if (it == peers_.end()) return false;
|
||||
|
@ -216,12 +216,12 @@ bool Control::SyncSend(const std::string to, const char* buffer, const size_t si
|
|||
// Send command to other peer by signal server
|
||||
//
|
||||
|
||||
void Control::SendCommand(const std::string& id, const std::string& command, const Json::Value& data) {
|
||||
void Control::SendCommand(const string& id, const string& command, const Json::Value& data) {
|
||||
signal_->SendCommand(id, command, data);
|
||||
}
|
||||
|
||||
|
||||
void Control::OnConnected(const std::string id) {
|
||||
void Control::OnConnected(const string id) {
|
||||
if ( observer_ == nullptr ) {
|
||||
LOGP_F( WARNING ) << "observer_ is null, id is " << id;
|
||||
return;
|
||||
|
@ -238,7 +238,7 @@ void Control::OnConnected(const std::string id) {
|
|||
// Implements PeerObserver::OnDisconnected()
|
||||
//
|
||||
|
||||
void Control::OnClosed(const std::string id, const bool force_queuing) {
|
||||
void Control::OnClosed(const string id, const bool force_queuing) {
|
||||
|
||||
if (force_queuing || webrtc_thread_ != rtc::Thread::Current()) {
|
||||
ControlMessageData *data = new ControlMessageData(id, ref_);
|
||||
|
@ -266,7 +266,7 @@ void Control::OnClosed(const std::string id, const bool force_queuing) {
|
|||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
}
|
||||
|
||||
void Control::OnClosed(const std::string id) {
|
||||
void Control::OnClosed(const string id) {
|
||||
OnClosed( id, QUEUEING_ON );
|
||||
}
|
||||
|
||||
|
@ -275,7 +275,7 @@ void Control::OnClosed(const std::string id) {
|
|||
// Signal receiving data
|
||||
//
|
||||
|
||||
void Control::OnMessage(const std::string& id, const char* buffer, const size_t size) {
|
||||
void Control::OnMessage(const string& id, const char* buffer, const size_t size) {
|
||||
if ( observer_ == nullptr ) {
|
||||
LOGP_F( WARNING ) << "observer_ is null, id is " << id;
|
||||
return;
|
||||
|
@ -283,7 +283,7 @@ void Control::OnMessage(const std::string& id, const char* buffer, const size_t
|
|||
observer_->OnPeerMessage(id, buffer, size);
|
||||
}
|
||||
|
||||
void Control::OnWritable(const std::string& id) {
|
||||
void Control::OnWritable(const string& id) {
|
||||
if ( observer_ == nullptr ) {
|
||||
LOGP_F( WARNING ) << "observer_ is null, id is " << id;
|
||||
return;
|
||||
|
@ -291,7 +291,7 @@ void Control::OnWritable(const std::string& id) {
|
|||
observer_->OnPeerWritable(id);
|
||||
}
|
||||
|
||||
void Control::OnError( const std::string id, const std::string& reason ) {
|
||||
void Control::OnError( const string id, const string& reason ) {
|
||||
if ( observer_ == nullptr ) {
|
||||
LOGP_F( WARNING ) << "observer_ is null, id is " << id;
|
||||
return;
|
||||
|
@ -356,8 +356,8 @@ void Control::OnMessage(rtc::Message* msg) {
|
|||
void Control::OnCommandReceived(const Json::Value& message) {
|
||||
|
||||
Json::Value data;
|
||||
std::string command;
|
||||
std::string peer_id;
|
||||
string command;
|
||||
string peer_id;
|
||||
|
||||
if (!rtc::GetStringFromJsonObject(message, "command", &command) ||
|
||||
!rtc::GetValueFromJsonObject(message, "data", &data)) {
|
||||
|
@ -411,7 +411,7 @@ void Control::OnSignalConnectionClosed(websocketpp::close::status::value code) {
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void Control::OnSignedOut(const std::string& id) {
|
||||
void Control::OnSignedOut(const string& id) {
|
||||
LOGP_F( INFO ) << "Calling OnSignedOut() with " << id;
|
||||
|
||||
if ( signal_ == nullptr || signal_->opened() ) {
|
||||
|
@ -436,7 +436,7 @@ void Control::OnSignedOut(const std::string& id) {
|
|||
// Commands to signal server
|
||||
//
|
||||
|
||||
void Control::CreateChannel(const std::string name) {
|
||||
void Control::CreateChannel(const string name) {
|
||||
LOGP_F( INFO ) << "channel is " << name;
|
||||
|
||||
Json::Value data;
|
||||
|
@ -444,7 +444,7 @@ void Control::CreateChannel(const std::string name) {
|
|||
SendCommand(name, "createchannel", data);
|
||||
}
|
||||
|
||||
void Control::JoinChannel(const std::string name) {
|
||||
void Control::JoinChannel(const string name) {
|
||||
LOGP_F( INFO ) << "channel is " << name;
|
||||
|
||||
Json::Value data;
|
||||
|
@ -452,7 +452,7 @@ void Control::JoinChannel(const std::string name) {
|
|||
SendCommand(name, "joinchannel", data);
|
||||
}
|
||||
|
||||
void Control::LeaveChannel(const std::string name) {
|
||||
void Control::LeaveChannel(const string name) {
|
||||
LOGP_F( INFO ) << "channel is " << name;
|
||||
|
||||
Json::Value data;
|
||||
|
@ -492,11 +492,11 @@ bool Control::CreatePeerFactory(
|
|||
// Add ice candidate to local peer from remote peer
|
||||
//
|
||||
|
||||
void Control::AddIceCandidate(const std::string& peer_id, const Json::Value& data) {
|
||||
void Control::AddIceCandidate(const string& peer_id, const Json::Value& data) {
|
||||
|
||||
std::string sdp_mid;
|
||||
string sdp_mid;
|
||||
int sdp_mline_index;
|
||||
std::string candidate;
|
||||
string candidate;
|
||||
|
||||
if ( !rtc::GetStringFromJsonObject( data, "sdp_mid", &sdp_mid ) ) {
|
||||
LOGP_F( LERROR ) << "sdp_mid not found, " << data.toStyledString();
|
||||
|
@ -541,7 +541,7 @@ void Control::OnSignedIn(const Json::Value& data) {
|
|||
return;
|
||||
}
|
||||
|
||||
std::string session_id;
|
||||
string session_id;
|
||||
if (!rtc::GetStringFromJsonObject(data, "session_id", &session_id)) {
|
||||
LOGP_F(LERROR) << "Signin failed - no session_id";
|
||||
return;
|
||||
|
@ -565,7 +565,7 @@ void Control::OnChannelCreated(const Json::Value& data) {
|
|||
return;
|
||||
}
|
||||
|
||||
std::string channel;
|
||||
string channel;
|
||||
if (!rtc::GetStringFromJsonObject(data, "name", &channel)) {
|
||||
LOGP_F(LERROR) << "Create channel failed - no channel name";
|
||||
return;
|
||||
|
@ -573,7 +573,7 @@ void Control::OnChannelCreated(const Json::Value& data) {
|
|||
|
||||
if (!result) {
|
||||
LOGP_F(LERROR) << "Create channel failed";
|
||||
std::string reason;
|
||||
string reason;
|
||||
if (!rtc::GetStringFromJsonObject(data, "reason", &reason)) {
|
||||
reason = "Unknown reason";
|
||||
}
|
||||
|
@ -595,7 +595,7 @@ void Control::OnChannelJoined(const Json::Value& data) {
|
|||
return;
|
||||
}
|
||||
|
||||
std::string channel;
|
||||
string channel;
|
||||
if (!rtc::GetStringFromJsonObject(data, "name", &channel)) {
|
||||
LOGP_F(LERROR) << "Join channel failed - no channel name";
|
||||
return;
|
||||
|
@ -603,7 +603,7 @@ void Control::OnChannelJoined(const Json::Value& data) {
|
|||
|
||||
if (!result) {
|
||||
LOGP_F(LERROR) << "Join channel failed";
|
||||
std::string reason;
|
||||
string reason;
|
||||
if (!rtc::GetStringFromJsonObject(data, "reason", &reason)) {
|
||||
reason = "Unknown reason";
|
||||
}
|
||||
|
@ -637,7 +637,7 @@ void Control::CreateOffer(const Json::Value& data) {
|
|||
}
|
||||
|
||||
for (size_t i = 0; i < peers.size(); ++i) {
|
||||
std::string remote_id;
|
||||
string remote_id;
|
||||
if (!rtc::GetStringFromJsonArray(peers, i, &remote_id)) {
|
||||
LOGP_F(LERROR) << "Peer handshake failed - invalid peer id";
|
||||
return;
|
||||
|
@ -650,7 +650,7 @@ void Control::CreateOffer(const Json::Value& data) {
|
|||
return;
|
||||
}
|
||||
|
||||
peers_.insert(std::pair<std::string, Peer>(remote_id, peer));
|
||||
peers_.insert(std::pair<string, Peer>(remote_id, peer));
|
||||
peer->CreateOffer(NULL);
|
||||
}
|
||||
|
||||
|
@ -661,8 +661,8 @@ void Control::CreateOffer(const Json::Value& data) {
|
|||
// 'offersdp' command
|
||||
//
|
||||
|
||||
void Control::ReceiveOfferSdp(const std::string& peer_id, const Json::Value& data) {
|
||||
std::string sdp;
|
||||
void Control::ReceiveOfferSdp(const string& peer_id, const Json::Value& data) {
|
||||
string sdp;
|
||||
|
||||
if ( !rtc::GetStringFromJsonObject( data, "sdp", &sdp ) ) {
|
||||
LOGP_F( LERROR ) << "sdp not found, peer_id is " << peer_id << " and " <<
|
||||
|
@ -677,7 +677,7 @@ void Control::ReceiveOfferSdp(const std::string& peer_id, const Json::Value& dat
|
|||
return;
|
||||
}
|
||||
|
||||
peers_.insert(std::pair<std::string, Peer>(peer_id, peer));
|
||||
peers_.insert(std::pair<string, Peer>(peer_id, peer));
|
||||
peer->ReceiveOfferSdp(sdp);
|
||||
|
||||
LOGP_F( INFO ) << "Done";
|
||||
|
@ -688,8 +688,8 @@ void Control::ReceiveOfferSdp(const std::string& peer_id, const Json::Value& dat
|
|||
// 'answersdp' command
|
||||
//
|
||||
|
||||
void Control::ReceiveAnswerSdp(const std::string& peer_id, const Json::Value& data) {
|
||||
std::string sdp;
|
||||
void Control::ReceiveAnswerSdp(const string& peer_id, const Json::Value& data) {
|
||||
string sdp;
|
||||
|
||||
if ( !rtc::GetStringFromJsonObject( data, "sdp", &sdp ) ) {
|
||||
LOGP_F( LERROR ) << "sdp not found, peer_id is " << peer_id << " and " <<
|
||||
|
|
|
@ -24,8 +24,9 @@ class Control
|
|||
public sigslot::has_slots<>,
|
||||
public rtc::MessageHandler {
|
||||
public:
|
||||
typedef std::vector<std::unique_ptr<PeerDataChannelObserver> >
|
||||
DataChannelList;
|
||||
|
||||
using string = std::string;
|
||||
using DataChannelList = std::vector<std::unique_ptr<PeerDataChannelObserver> >;
|
||||
|
||||
explicit Control();
|
||||
explicit Control(std::shared_ptr<Signal> signal);
|
||||
|
@ -42,31 +43,31 @@ public:
|
|||
// Negotiation and send data
|
||||
//
|
||||
|
||||
void Send(const std::string to, const char* buffer, const size_t size);
|
||||
bool SyncSend(const std::string to, const char* buffer, const size_t size);
|
||||
void Send(const string to, const char* buffer, const size_t size);
|
||||
bool SyncSend(const string to, const char* buffer, const size_t size);
|
||||
|
||||
void SignIn(const std::string& user_id, const std::string& user_password, const std::string& open_id);
|
||||
void SignIn(const string& user_id, const string& user_password, const string& open_id);
|
||||
void SignOut();
|
||||
void Connect(const std::string id);
|
||||
void Connect(const string id);
|
||||
void Close();
|
||||
bool IsWritable(const std::string id);
|
||||
bool IsWritable(const string id);
|
||||
|
||||
void OnCommandReceived(const Json::Value& message);
|
||||
void OnSignalCommandReceived(const Json::Value& message);
|
||||
void OnSignalConnectionClosed(websocketpp::close::status::value code);
|
||||
void OnSignedOut(const std::string& id);
|
||||
void OnSignedOut(const string& id);
|
||||
|
||||
//
|
||||
// PeerObserver implementation
|
||||
//
|
||||
|
||||
virtual void SendCommand(const std::string& id, const std::string& command, const Json::Value& data);
|
||||
virtual void Close(const std::string id);
|
||||
virtual void OnConnected(const std::string id);
|
||||
virtual void OnClosed(const std::string id);
|
||||
virtual void OnMessage(const std::string& id, const char* buffer, const size_t size);
|
||||
virtual void OnWritable(const std::string& id);
|
||||
virtual void OnError( const std::string id, const std::string& reason );
|
||||
virtual void SendCommand(const string& id, const string& command, const Json::Value& data);
|
||||
virtual void Close(const string id);
|
||||
virtual void OnConnected(const string id);
|
||||
virtual void OnClosed(const string id);
|
||||
virtual void OnMessage(const string& id, const char* buffer, const size_t size);
|
||||
virtual void OnWritable(const string& id);
|
||||
virtual void OnError( const string id, const string& reason );
|
||||
|
||||
|
||||
// Register/Unregister observer
|
||||
|
@ -77,36 +78,36 @@ public:
|
|||
void OnMessage(rtc::Message* msg);
|
||||
|
||||
protected:
|
||||
void CreateChannel(const std::string name);
|
||||
void JoinChannel(const std::string name);
|
||||
void LeaveChannel(const std::string name);
|
||||
void CreateChannel(const string name);
|
||||
void JoinChannel(const string name);
|
||||
void LeaveChannel(const string name);
|
||||
bool CreatePeerFactory(const webrtc::MediaConstraintsInterface* constraints);
|
||||
void CreateOffer(const Json::Value& data);
|
||||
void AddIceCandidate(const std::string& peer_id, const Json::Value& data);
|
||||
void ReceiveOfferSdp(const std::string& peer_id, const Json::Value& data);
|
||||
void ReceiveAnswerSdp(const std::string& peer_id, const Json::Value& data);
|
||||
void AddIceCandidate(const string& peer_id, const Json::Value& data);
|
||||
void ReceiveOfferSdp(const string& peer_id, const Json::Value& data);
|
||||
void ReceiveAnswerSdp(const string& peer_id, const Json::Value& data);
|
||||
|
||||
void OnSignedIn(const Json::Value& data);
|
||||
void OnChannelCreated(const Json::Value& data);
|
||||
void OnChannelJoined(const Json::Value& data);
|
||||
void OnChannelLeaved(const Json::Value& data);
|
||||
|
||||
void Close( const std::string id, const bool force_queuing);
|
||||
void OnClosed(const std::string id, const bool force_queuing);
|
||||
void Close( const string id, const bool force_queuing);
|
||||
void OnClosed(const string id, const bool force_queuing);
|
||||
|
||||
|
||||
// open_id_: other peers can find this peer by open_id_ and it is user_id or alias
|
||||
// user_id_: A user id to sign in signal server (could be 'anonymous' for guest user)
|
||||
// session_id_: A unique id for signal server connection
|
||||
std::string open_id_;
|
||||
std::string user_id_;
|
||||
std::string session_id_;
|
||||
string open_id_;
|
||||
string user_id_;
|
||||
string session_id_;
|
||||
|
||||
std::shared_ptr<Signal> signal_;
|
||||
rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
|
||||
|
||||
typedef rtc::scoped_refptr<PeerControl> Peer;
|
||||
std::map<std::string, Peer> peers_;
|
||||
using Peer = rtc::scoped_refptr<PeerControl>;
|
||||
std::map<string, Peer> peers_;
|
||||
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
|
||||
peer_connection_factory_;
|
||||
|
@ -127,10 +128,10 @@ private:
|
|||
|
||||
struct ControlMessageData : public rtc::MessageData {
|
||||
explicit ControlMessageData(Json::Value data, std::shared_ptr<Control> ref) : data_json_(data), ref_(ref) {}
|
||||
explicit ControlMessageData(const std::string data, std::shared_ptr<Control> ref) : data_string_(data), ref_(ref) {}
|
||||
explicit ControlMessageData(const string data, std::shared_ptr<Control> ref) : data_string_(data), ref_(ref) {}
|
||||
explicit ControlMessageData(const uint32_t data, std::shared_ptr<Control> ref) : data_int32_(data), ref_(ref) {}
|
||||
Json::Value data_json_;
|
||||
std::string data_string_;
|
||||
string data_string_;
|
||||
uint32_t data_int32_;
|
||||
|
||||
private:
|
||||
|
|
30
src/peer.cc
30
src/peer.cc
|
@ -17,8 +17,8 @@ namespace pc {
|
|||
// class PeerControl
|
||||
//
|
||||
|
||||
PeerControl::PeerControl(const std::string local_id,
|
||||
const std::string remote_id,
|
||||
PeerControl::PeerControl(const string local_id,
|
||||
const string remote_id,
|
||||
PeerObserver* observer,
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
|
||||
peer_connection_factory)
|
||||
|
@ -46,7 +46,7 @@ bool PeerControl::Initialize() {
|
|||
}
|
||||
|
||||
webrtc::DataChannelInit init;
|
||||
const std::string data_channel_name = std::string("pc_data_") + remote_id_;
|
||||
const string data_channel_name = string("pc_data_") + remote_id_;
|
||||
if (!CreateDataChannel(data_channel_name, init)) {
|
||||
LOGP_F(LS_ERROR) << "CreateDataChannel failed";
|
||||
DeletePeerConnection();
|
||||
|
@ -129,7 +129,7 @@ void PeerControl::CreateAnswer(const webrtc::MediaConstraintsInterface* constrai
|
|||
}
|
||||
|
||||
|
||||
void PeerControl::ReceiveOfferSdp(const std::string& sdp) {
|
||||
void PeerControl::ReceiveOfferSdp(const string& sdp) {
|
||||
ASSERT( state_ == pClosed);
|
||||
SetRemoteDescription(webrtc::SessionDescriptionInterface::kOffer, sdp);
|
||||
CreateAnswer(NULL);
|
||||
|
@ -137,7 +137,7 @@ void PeerControl::ReceiveOfferSdp(const std::string& sdp) {
|
|||
}
|
||||
|
||||
|
||||
void PeerControl::ReceiveAnswerSdp(const std::string& sdp) {
|
||||
void PeerControl::ReceiveAnswerSdp(const string& sdp) {
|
||||
ASSERT( state_ == pConnecting );
|
||||
SetRemoteDescription(webrtc::SessionDescriptionInterface::kAnswer, sdp);
|
||||
LOGP_F( INFO ) << "Done";
|
||||
|
@ -197,7 +197,7 @@ void PeerControl::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConn
|
|||
|
||||
|
||||
void PeerControl::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
|
||||
std::string sdp;
|
||||
string sdp;
|
||||
if (!candidate->ToString(&sdp)) return;
|
||||
|
||||
Json::Value data;
|
||||
|
@ -214,7 +214,7 @@ void PeerControl::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
|
|||
|
||||
// This callback should take the ownership of |desc|.
|
||||
std::unique_ptr<webrtc::SessionDescriptionInterface> owned_desc(desc);
|
||||
std::string sdp;
|
||||
string sdp;
|
||||
|
||||
if (!desc->ToString(&sdp)) return;
|
||||
|
||||
|
@ -280,7 +280,7 @@ void PeerControl::OnPeerDisconnected() {
|
|||
|
||||
|
||||
void PeerControl::OnPeerMessage(const webrtc::DataBuffer& buffer) {
|
||||
std::string data;
|
||||
string data;
|
||||
control_->OnMessage(remote_id_, buffer.data.data<char>(), buffer.data.size());
|
||||
}
|
||||
|
||||
|
@ -294,7 +294,7 @@ void PeerControl::OnBufferedAmountChange(const uint64_t previous_amount) {
|
|||
|
||||
|
||||
bool PeerControl::CreateDataChannel(
|
||||
const std::string& label,
|
||||
const string& label,
|
||||
const webrtc::DataChannelInit& init) {
|
||||
|
||||
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel;
|
||||
|
@ -317,8 +317,8 @@ bool PeerControl::CreateDataChannel(
|
|||
return true;
|
||||
}
|
||||
|
||||
void PeerControl::AddIceCandidate(const std::string& sdp_mid, int sdp_mline_index,
|
||||
const std::string& candidate) {
|
||||
void PeerControl::AddIceCandidate(const string& sdp_mid, int sdp_mline_index,
|
||||
const string& candidate) {
|
||||
|
||||
std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
|
||||
webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
|
||||
|
@ -365,8 +365,8 @@ void PeerControl::DeletePeerConnection() {
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void PeerControl::SetLocalDescription(const std::string& type,
|
||||
const std::string& sdp) {
|
||||
void PeerControl::SetLocalDescription(const string& type,
|
||||
const string& sdp) {
|
||||
|
||||
if ( peer_connection_ == nullptr ) {
|
||||
LOGP_F( LERROR ) << "peer_connection_ is nullptr";
|
||||
|
@ -382,8 +382,8 @@ void PeerControl::SetLocalDescription(const std::string& type,
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void PeerControl::SetRemoteDescription(const std::string& type,
|
||||
const std::string& sdp) {
|
||||
void PeerControl::SetRemoteDescription(const string& type,
|
||||
const string& sdp) {
|
||||
|
||||
rtc::scoped_refptr<webrtc::MockSetSessionDescriptionObserver>
|
||||
observer(new rtc::RefCountedObject<
|
||||
|
|
30
src/peer.h
30
src/peer.h
|
@ -48,8 +48,10 @@ class PeerControl
|
|||
|
||||
public:
|
||||
|
||||
explicit PeerControl(const std::string local_session_id,
|
||||
const std::string remote_session_id,
|
||||
using string = std::string;
|
||||
|
||||
explicit PeerControl(const string local_session_id,
|
||||
const string remote_session_id,
|
||||
PeerObserver* observer,
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
|
||||
peer_connection_factory);
|
||||
|
@ -63,8 +65,8 @@ public:
|
|||
pClosed
|
||||
};
|
||||
|
||||
const std::string& local_id() const { return local_id_; }
|
||||
const std::string& remote_id() const { return remote_id_; }
|
||||
const string& local_id() const { return local_id_; }
|
||||
const string& remote_id() const { return remote_id_; }
|
||||
const PeerState state() const { return state_ ; }
|
||||
|
||||
//
|
||||
|
@ -83,10 +85,10 @@ public:
|
|||
|
||||
void CreateOffer(const webrtc::MediaConstraintsInterface* constraints);
|
||||
void CreateAnswer(const webrtc::MediaConstraintsInterface* constraints);
|
||||
void AddIceCandidate(const std::string& sdp_mid, int sdp_mline_index,
|
||||
const std::string& candidate);
|
||||
void ReceiveOfferSdp(const std::string& sdp);
|
||||
void ReceiveAnswerSdp(const std::string& sdp);
|
||||
void AddIceCandidate(const string& sdp_mid, int sdp_mline_index,
|
||||
const string& candidate);
|
||||
void ReceiveOfferSdp(const string& sdp);
|
||||
void ReceiveAnswerSdp(const string& sdp);
|
||||
|
||||
//
|
||||
// PeerConnectionObserver implementation.
|
||||
|
@ -107,7 +109,7 @@ public:
|
|||
//
|
||||
|
||||
void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
|
||||
void OnFailure(const std::string& error) override {}
|
||||
void OnFailure(const string& error) override {}
|
||||
|
||||
//
|
||||
// PeerDataChannelObserver
|
||||
|
@ -123,18 +125,18 @@ protected:
|
|||
|
||||
bool CreatePeerConnection();
|
||||
void DeletePeerConnection();
|
||||
bool CreateDataChannel(const std::string& label,
|
||||
bool CreateDataChannel(const string& label,
|
||||
const webrtc::DataChannelInit& init);
|
||||
void SetLocalDescription(const std::string& type, const std::string& sdp);
|
||||
void SetRemoteDescription(const std::string& type, const std::string& sdp);
|
||||
void SetLocalDescription(const string& type, const string& sdp);
|
||||
void SetRemoteDescription(const string& type, const string& sdp);
|
||||
void Attach(PeerDataChannelObserver* datachannel);
|
||||
void Detach(PeerDataChannelObserver* datachannel);
|
||||
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory_;
|
||||
|
||||
std::string local_id_;
|
||||
std::string remote_id_;
|
||||
string local_id_;
|
||||
string remote_id_;
|
||||
std::unique_ptr<PeerDataChannelObserver> local_data_channel_;
|
||||
std::unique_ptr<PeerDataChannelObserver> remote_data_channel_;
|
||||
|
||||
|
|
|
@ -11,30 +11,33 @@
|
|||
#include "control.h"
|
||||
#include "logging.h"
|
||||
|
||||
namespace pc {
|
||||
|
||||
|
||||
PeerConnect::PeerConnect()
|
||||
: PeerConnect(""){
|
||||
: PeerConnect( "" ) {
|
||||
}
|
||||
|
||||
PeerConnect::PeerConnect(const std::string setting) {
|
||||
PeerConnect::PeerConnect( const string options ) {
|
||||
// Log level
|
||||
#if DEBUG || _DEBUG
|
||||
rtc::LogMessage::LogToDebug(rtc::LS_NONE);
|
||||
pc::LogMessage::LogToDebug(pc::LS_VERBOSE);
|
||||
rtc::LogMessage::LogToDebug( rtc::LS_NONE );
|
||||
pc::LogMessage::LogToDebug( pc::LS_VERBOSE );
|
||||
#else
|
||||
rtc::LogMessage::LogToDebug(rtc::LS_NONE);
|
||||
pc::LogMessage::LogToDebug(pc::LS_NONE);
|
||||
rtc::LogMessage::LogToDebug( rtc::LS_NONE );
|
||||
pc::LogMessage::LogToDebug( pc::LS_NONE );
|
||||
#endif
|
||||
|
||||
// parse settings
|
||||
if (!setting.empty()) {
|
||||
ParseSetting(setting);
|
||||
if ( !options.empty() ) {
|
||||
ParseOptions( options );
|
||||
}
|
||||
|
||||
signout_ = false;
|
||||
|
||||
// create signal client
|
||||
if (signal_ == nullptr) {
|
||||
signal_ = std::make_shared<pc::Signal>(setting_.signal_uri_);
|
||||
if ( signal_ == nullptr ) {
|
||||
signal_ = std::make_shared<pc::Signal>( setting_.signal_uri_ );
|
||||
}
|
||||
|
||||
LOGP_F( INFO ) << "Done";
|
||||
|
@ -55,14 +58,14 @@ void PeerConnect::Stop() {
|
|||
}
|
||||
|
||||
|
||||
void PeerConnect::SignIn(const std::string alias, const std::string id, const std::string password) {
|
||||
void PeerConnect::SignIn( const string alias, const string id, const string password ) {
|
||||
|
||||
//
|
||||
// Check if already signed in
|
||||
//
|
||||
|
||||
if (control_.get() != nullptr) {
|
||||
LOGP_F(WARNING) << "Already signined in.";
|
||||
if ( control_.get() != nullptr ) {
|
||||
LOGP_F( WARNING ) << "Already signined in.";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -70,11 +73,11 @@ void PeerConnect::SignIn(const std::string alias, const std::string id, const st
|
|||
// Initialize control
|
||||
//
|
||||
|
||||
control_ = std::make_shared<pc::Control>(signal_);
|
||||
control_->RegisterObserver(this, control_);
|
||||
control_ = std::make_shared<pc::Control>( signal_ );
|
||||
control_->RegisterObserver( this, control_ );
|
||||
|
||||
if (control_.get() == NULL) {
|
||||
LOGP_F(LERROR) << "Failed to create class Control.";
|
||||
if ( control_.get() == NULL ) {
|
||||
LOGP_F( LERROR ) << "Failed to create class Control.";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -82,8 +85,8 @@ void PeerConnect::SignIn(const std::string alias, const std::string id, const st
|
|||
// Initialize peer connection
|
||||
//
|
||||
|
||||
if (!control_->InitializeControl()) {
|
||||
LOGP_F(LERROR) << "Failed to initialize Control.";
|
||||
if ( !control_->InitializeControl() ) {
|
||||
LOGP_F( LERROR ) << "Failed to initialize Control.";
|
||||
control_.reset();
|
||||
return;
|
||||
}
|
||||
|
@ -92,20 +95,20 @@ void PeerConnect::SignIn(const std::string alias, const std::string id, const st
|
|||
// Set user_id and open_id
|
||||
//
|
||||
|
||||
std::string user_id;
|
||||
std::string open_id;
|
||||
string user_id;
|
||||
string open_id;
|
||||
|
||||
user_id = tolower(id);
|
||||
if (user_id == "anonymous") user_id = "";
|
||||
user_id = tolower( id );
|
||||
if ( user_id == "anonymous" ) user_id = "";
|
||||
|
||||
open_id = tolower(alias);
|
||||
if (open_id.empty()) open_id = tolower(rtc::CreateRandomUuid());
|
||||
open_id = tolower( alias );
|
||||
if ( open_id.empty() ) open_id = tolower( rtc::CreateRandomUuid() );
|
||||
|
||||
//
|
||||
// Connect to signal server
|
||||
//
|
||||
|
||||
control_->SignIn(user_id, password, open_id);
|
||||
control_->SignIn( user_id, password, open_id );
|
||||
LOGP_F( INFO ) << "Done";
|
||||
return;
|
||||
}
|
||||
|
@ -116,14 +119,14 @@ void PeerConnect::SignOut() {
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void PeerConnect::Connect(const std::string id) {
|
||||
control_->Connect(id);
|
||||
void PeerConnect::Connect( const string id ) {
|
||||
control_->Connect( id );
|
||||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
return;
|
||||
}
|
||||
|
||||
void PeerConnect::Disconnect(const std::string id) {
|
||||
control_->Close(id);
|
||||
void PeerConnect::Disconnect( const string id ) {
|
||||
control_->Close( id );
|
||||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
return;
|
||||
}
|
||||
|
@ -133,28 +136,28 @@ void PeerConnect::Disconnect(const std::string id) {
|
|||
// Send message to destination peer session id
|
||||
//
|
||||
|
||||
void PeerConnect::Send(const std::string& id, const char* buffer, const size_t size) {
|
||||
control_->Send(id, buffer, size);
|
||||
void PeerConnect::Send( const string& id, const char* buffer, const size_t size ) {
|
||||
control_->Send( id, buffer, size );
|
||||
}
|
||||
|
||||
void PeerConnect::Send(const std::string& id, const char* message) {
|
||||
Send(id, message, strlen(message));
|
||||
void PeerConnect::Send( const string& id, const char* message ) {
|
||||
Send( id, message, strlen( message ) );
|
||||
}
|
||||
|
||||
void PeerConnect::Send(const std::string& id, const std::string& message) {
|
||||
Send(id, message.c_str(), message.size());
|
||||
void PeerConnect::Send( const string& id, const string& message ) {
|
||||
Send( id, message.c_str(), message.size() );
|
||||
}
|
||||
|
||||
bool PeerConnect::SyncSend(const std::string& id, const char* buffer, const size_t size) {
|
||||
return control_->SyncSend(id, buffer, size);
|
||||
bool PeerConnect::SyncSend( const string& id, const char* buffer, const size_t size ) {
|
||||
return control_->SyncSend( id, buffer, size );
|
||||
}
|
||||
|
||||
bool PeerConnect::SyncSend(const std::string& id, const char* message) {
|
||||
return SyncSend(id, message, strlen(message));
|
||||
bool PeerConnect::SyncSend( const string& id, const char* message ) {
|
||||
return SyncSend( id, message, strlen( message ) );
|
||||
}
|
||||
|
||||
bool PeerConnect::SyncSend(const std::string& id, const std::string& message) {
|
||||
return SyncSend(id, message.c_str(), message.size());
|
||||
bool PeerConnect::SyncSend( const string& id, const string& message ) {
|
||||
return SyncSend( id, message.c_str(), message.size() );
|
||||
}
|
||||
|
||||
|
||||
|
@ -168,12 +171,12 @@ std::string PeerConnect::CreateRandomUuid() {
|
|||
//
|
||||
// Register Event handler
|
||||
//
|
||||
PeerConnect& PeerConnect::On(std::string event_id, std::function<void(PeerConnect*, std::string)> handler) {
|
||||
PeerConnect& PeerConnect::On( string event_id, std::function<void( PeerConnect*, string )> handler ) {
|
||||
|
||||
if (event_id.empty()) return *this;
|
||||
if ( event_id.empty() ) return *this;
|
||||
|
||||
std::unique_ptr<EventHandler_2> f(new EventHandler_2(handler));
|
||||
event_handler_.insert(Events::value_type(event_id, std::move(f)));
|
||||
std::unique_ptr<EventHandler_2> f( new EventHandler_2( handler ) );
|
||||
event_handler_.insert( Events::value_type( event_id, std::move( f ) ) );
|
||||
|
||||
LOGP_F( INFO ) << "An event handler '" << event_id << "' has been inserted";
|
||||
return *this;
|
||||
|
@ -183,7 +186,7 @@ PeerConnect& PeerConnect::On(std::string event_id, std::function<void(PeerConnec
|
|||
// Register Message handler
|
||||
//
|
||||
|
||||
PeerConnect& PeerConnect::OnMessage(std::function<void(PeerConnect*, std::string, Buffer&)> handler) {
|
||||
PeerConnect& PeerConnect::OnMessage( std::function<void( PeerConnect*, string, Buffer& )> handler ) {
|
||||
message_handler_ = handler;
|
||||
LOGP_F( INFO ) << "A message handler has been inserted";
|
||||
return *this;
|
||||
|
@ -193,18 +196,18 @@ PeerConnect& PeerConnect::OnMessage(std::function<void(PeerConnect*, std::string
|
|||
// Signal event handler
|
||||
//
|
||||
|
||||
void PeerConnect::OnSignedIn(const std::string id) {
|
||||
void PeerConnect::OnSignedIn( const string id ) {
|
||||
signout_ = false;
|
||||
|
||||
if ( event_handler_.find( "signin" ) == event_handler_.end() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
CallEventHandler("signin", this, id);
|
||||
CallEventHandler( "signin", this, id );
|
||||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void PeerConnect::OnSignedOut(const std::string id) {
|
||||
void PeerConnect::OnSignedOut( const string id ) {
|
||||
if ( !signout_ ) {
|
||||
LOGP_F( WARNING ) << "signout_ is false, id is " << id;
|
||||
return;
|
||||
|
@ -214,97 +217,100 @@ void PeerConnect::OnSignedOut(const std::string id) {
|
|||
return;
|
||||
}
|
||||
|
||||
CallEventHandler("signout", this, id);
|
||||
CallEventHandler( "signout", this, id );
|
||||
|
||||
control_->UnregisterObserver();
|
||||
control_.reset();
|
||||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
}
|
||||
|
||||
void PeerConnect::OnPeerConnected(const std::string id) {
|
||||
void PeerConnect::OnPeerConnected( const string id ) {
|
||||
if ( event_handler_.find( "connect" ) == event_handler_.end() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
CallEventHandler("connect", this, id);
|
||||
CallEventHandler( "connect", this, id );
|
||||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
}
|
||||
|
||||
void PeerConnect::OnPeerDisconnected(const std::string id) {
|
||||
void PeerConnect::OnPeerDisconnected( const string id ) {
|
||||
if ( event_handler_.find( "disconnect" ) == event_handler_.end() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
CallEventHandler("disconnect", this, id);
|
||||
CallEventHandler( "disconnect", this, id );
|
||||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
}
|
||||
|
||||
void PeerConnect::OnPeerMessage(const std::string id, const char* buffer, const size_t size) {
|
||||
Buffer buf(buffer, size);
|
||||
message_handler_(this, id, buf);
|
||||
void PeerConnect::OnPeerMessage( const string id, const char* buffer, const size_t size ) {
|
||||
Buffer buf( buffer, size );
|
||||
message_handler_( this, id, buf );
|
||||
}
|
||||
|
||||
void PeerConnect::OnPeerWritable(const std::string id) {
|
||||
void PeerConnect::OnPeerWritable( const string id ) {
|
||||
if ( event_handler_.find( "writable" ) == event_handler_.end() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
CallEventHandler("writable", this, id);
|
||||
CallEventHandler( "writable", this, id );
|
||||
LOGP_F( INFO ) << "Done, id is " << id;
|
||||
}
|
||||
|
||||
void PeerConnect::OnError(const std::string id, const std::string& reason) {
|
||||
void PeerConnect::OnError( const string id, const string& reason ) {
|
||||
if ( event_handler_.find( "error" ) == event_handler_.end() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
error_reason_ = reason;
|
||||
CallEventHandler("error", this, id);
|
||||
CallEventHandler( "error", this, id );
|
||||
LOGP_F( INFO ) << "Done, id is " << id << " and reason is " << reason;
|
||||
}
|
||||
|
||||
template<typename ...A>
|
||||
void PeerConnect::CallEventHandler(std::string msg_id, A&& ... args)
|
||||
void PeerConnect::CallEventHandler( string msg_id, A&& ... args )
|
||||
{
|
||||
using eventhandler_t = EventHandler_t<A...>;
|
||||
using cb_t = std::function<void(A...)>;
|
||||
using cb_t = std::function<void( A... )>;
|
||||
const Handler_t& base = *event_handler_[msg_id];
|
||||
const cb_t& func = static_cast<const eventhandler_t&>(base).callback_;
|
||||
func(std::forward<A>(args)...);
|
||||
const cb_t& func = static_cast<const eventhandler_t&>( base ).callback_;
|
||||
func( std::forward<A>( args )... );
|
||||
}
|
||||
|
||||
|
||||
bool PeerConnect::ParseSetting(const std::string& setting) {
|
||||
bool PeerConnect::ParseOptions( const string& options ) {
|
||||
Json::Reader reader;
|
||||
Json::Value jsetting;
|
||||
Json::Value joptions;
|
||||
|
||||
std::string value;
|
||||
string value;
|
||||
|
||||
if (!reader.parse(setting, jsetting)) {
|
||||
LOGP_F(WARNING) << "Invalid setting: " << setting;
|
||||
if ( !reader.parse( options, joptions ) ) {
|
||||
LOGP_F( WARNING ) << "Invalid setting: " << options;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rtc::GetStringFromJsonObject(jsetting, "url", &value)) {
|
||||
if ( rtc::GetStringFromJsonObject( joptions, "url", &value ) ) {
|
||||
setting_.signal_uri_ = value;
|
||||
}
|
||||
|
||||
if (rtc::GetStringFromJsonObject(jsetting, "user_id", &value)) {
|
||||
if ( rtc::GetStringFromJsonObject( joptions, "user_id", &value ) ) {
|
||||
setting_.signal_id_ = value;
|
||||
}
|
||||
|
||||
if (rtc::GetStringFromJsonObject(jsetting, "user_password", &value)) {
|
||||
if ( rtc::GetStringFromJsonObject( joptions, "user_password", &value ) ) {
|
||||
setting_.signal_password_ = value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string PeerConnect::tolower(const std::string& str) {
|
||||
std::string PeerConnect::tolower( const string& str ) {
|
||||
std::locale loc;
|
||||
std::string lower_str;
|
||||
for (auto elem : str) {
|
||||
lower_str += std::tolower(elem, loc);
|
||||
string lower_str;
|
||||
for ( auto elem : str ) {
|
||||
lower_str += std::tolower( elem, loc );
|
||||
}
|
||||
return lower_str;
|
||||
}
|
||||
|
||||
|
||||
} // namespace pc
|
|
@ -17,32 +17,34 @@
|
|||
#define function_pc [&]
|
||||
|
||||
namespace pc {
|
||||
class Control;
|
||||
class Signal;
|
||||
}
|
||||
|
||||
class Control;
|
||||
class Signal;
|
||||
|
||||
|
||||
class PeerConnect
|
||||
: public pc::ControlObserver {
|
||||
: public ControlObserver {
|
||||
public:
|
||||
|
||||
using string = std::string;
|
||||
using Data = std::map<string, string>;
|
||||
using Control = Control;
|
||||
using Signal = Signal;
|
||||
|
||||
struct Setting {
|
||||
std::string signal_uri_;
|
||||
std::string signal_id_;
|
||||
std::string signal_password_;
|
||||
string signal_uri_;
|
||||
string signal_id_;
|
||||
string signal_password_;
|
||||
};
|
||||
|
||||
class Buffer {
|
||||
class Buffer {
|
||||
public:
|
||||
Buffer(): buf_(nullptr), size_(0) {};
|
||||
Buffer(const char* buf, const size_t size): buf_(buf), size_(size) {}
|
||||
Buffer() : buf_( nullptr ), size_( 0 ) {};
|
||||
Buffer( const char* buf, const size_t size ) : buf_( buf ), size_( size ) {}
|
||||
const char* buf_;
|
||||
const size_t size_;
|
||||
};
|
||||
|
||||
using Control = pc::Control;
|
||||
using Signal = pc::Signal;
|
||||
using Data = std::map<std::string, std::string>;
|
||||
|
||||
//
|
||||
// APIs
|
||||
//
|
||||
|
@ -50,22 +52,22 @@ public:
|
|||
static void Run();
|
||||
static void Stop();
|
||||
|
||||
void SignIn(const std::string alias = "", const std::string id = "", const std::string password = "");
|
||||
void SignIn( const string alias = "", const string id = "", const string password = "" );
|
||||
void SignOut();
|
||||
void Connect(const std::string id);
|
||||
void Disconnect(const std::string id);
|
||||
void Send(const std::string& id, const char* buffer, const size_t size);
|
||||
void Send(const std::string& id, const char* buffer);
|
||||
void Send(const std::string& id, const std::string& message);
|
||||
bool SyncSend(const std::string& id, const char* buffer, const size_t size);
|
||||
bool SyncSend(const std::string& id, const char* buffer);
|
||||
bool SyncSend(const std::string& id, const std::string& message);
|
||||
std::string GetErrorMessage() { return error_reason_; }
|
||||
void Connect( const string id );
|
||||
void Disconnect( const string id );
|
||||
void Send( const string& id, const char* buffer, const size_t size );
|
||||
void Send( const string& id, const char* buffer );
|
||||
void Send( const string& id, const string& message );
|
||||
bool SyncSend( const string& id, const char* buffer, const size_t size );
|
||||
bool SyncSend( const string& id, const char* buffer );
|
||||
bool SyncSend( const string& id, const string& message );
|
||||
string GetErrorMessage() { return error_reason_; }
|
||||
|
||||
static std::string CreateRandomUuid();
|
||||
|
||||
PeerConnect& On(std::string event_id, std::function<void(PeerConnect*, std::string)>);
|
||||
PeerConnect& OnMessage(std::function<void(PeerConnect*, std::string, Buffer&)>);
|
||||
PeerConnect& On( string event_id, std::function<void( PeerConnect*, string )> );
|
||||
PeerConnect& OnMessage( std::function<void( PeerConnect*, string, Buffer& )> );
|
||||
|
||||
|
||||
//
|
||||
|
@ -73,7 +75,7 @@ public:
|
|||
//
|
||||
|
||||
explicit PeerConnect();
|
||||
explicit PeerConnect(std::string setting);
|
||||
explicit PeerConnect( string options );
|
||||
~PeerConnect();
|
||||
|
||||
|
||||
|
@ -86,46 +88,48 @@ protected:
|
|||
// The derived type that represents a callback.
|
||||
template<typename ...A>
|
||||
struct EventHandler_t : public Handler_t {
|
||||
using cb = std::function<void(A...)>;
|
||||
using cb = std::function<void( A... )>;
|
||||
cb callback_;
|
||||
EventHandler_t(cb p_callback) : callback_(p_callback) {}
|
||||
EventHandler_t( cb p_callback ) : callback_( p_callback ) {}
|
||||
};
|
||||
|
||||
template<typename ...A>
|
||||
void CallEventHandler(std::string msg_id, A&& ... args);
|
||||
void CallEventHandler( string msg_id, A&& ... args );
|
||||
|
||||
using EventHandler_1 = EventHandler_t<PeerConnect*>;
|
||||
using EventHandler_2 = EventHandler_t<PeerConnect*, std::string>;
|
||||
using EventHandler_3 = EventHandler_t<PeerConnect*, std::string, Data&>;
|
||||
using Events = std::map<std::string, std::unique_ptr<Handler_t>>;
|
||||
using MessageHandler = std::function<void(PeerConnect*, std::string, Buffer&)>;
|
||||
using EventHandler_2 = EventHandler_t<PeerConnect*, string>;
|
||||
using EventHandler_3 = EventHandler_t<PeerConnect*, string, Data&>;
|
||||
using Events = std::map<string, std::unique_ptr<Handler_t>>;
|
||||
using MessageHandler = std::function<void( PeerConnect*, string, Buffer& )>;
|
||||
|
||||
//
|
||||
// ControlObserver implementation
|
||||
//
|
||||
|
||||
void OnSignedIn(const std::string id);
|
||||
void OnSignedOut(const std::string id);
|
||||
void OnPeerConnected(const std::string id);
|
||||
void OnPeerDisconnected(const std::string id);
|
||||
void OnPeerMessage(const std::string id, const char* buffer, const size_t size);
|
||||
void OnPeerWritable(const std::string id);
|
||||
void OnError(const std::string id, const std::string& reason);
|
||||
void OnSignedIn( const string id );
|
||||
void OnSignedOut( const string id );
|
||||
void OnPeerConnected( const string id );
|
||||
void OnPeerDisconnected( const string id );
|
||||
void OnPeerMessage( const string id, const char* buffer, const size_t size );
|
||||
void OnPeerWritable( const string id );
|
||||
void OnError( const string id, const string& reason );
|
||||
|
||||
|
||||
bool ParseSetting(const std::string& setting);
|
||||
std::string tolower(const std::string& str);
|
||||
bool ParseOptions( const string& options );
|
||||
std::string tolower( const string& str );
|
||||
|
||||
bool signout_;
|
||||
Setting setting_;
|
||||
Events event_handler_;
|
||||
MessageHandler message_handler_;
|
||||
|
||||
std::shared_ptr<pc::Control> control_;
|
||||
std::shared_ptr<pc::Signal> signal_;
|
||||
std::shared_ptr<Control> control_;
|
||||
std::shared_ptr<Signal> signal_;
|
||||
|
||||
std::string error_reason_;
|
||||
string error_reason_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace pc
|
||||
|
||||
#endif // __PEERCONNECT_PEERCONNECT_H__
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
|
||||
namespace pc {
|
||||
|
||||
Signal::Signal(const std::string url) :
|
||||
Signal::Signal(const string url) :
|
||||
con_state_(con_closed),
|
||||
network_thread_(),
|
||||
reconn_attempts_(0xFFFFFFFF),
|
||||
|
@ -92,7 +92,7 @@ Signal::~Signal() {
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void Signal::SignIn(const std::string& id, const std::string& password) {
|
||||
void Signal::SignIn(const string& id, const string& password) {
|
||||
user_id_ = id;
|
||||
user_password_ = password;
|
||||
Connect();
|
||||
|
@ -105,8 +105,8 @@ void Signal::SignOut() {
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void Signal::SendCommand(const std::string channel,
|
||||
const std::string commandname,
|
||||
void Signal::SendCommand(const string channel,
|
||||
const string commandname,
|
||||
const Json::Value& data) {
|
||||
|
||||
if (commandname.empty()) {
|
||||
|
@ -143,7 +143,7 @@ void Signal::SendCommand(const std::string channel,
|
|||
LOGP_F( INFO ) << "Done";
|
||||
}
|
||||
|
||||
void Signal::SendGlobalCommand(const std::string commandname,
|
||||
void Signal::SendGlobalCommand(const string commandname,
|
||||
const Json::Value& data) {
|
||||
SendCommand("", commandname, data);
|
||||
}
|
||||
|
@ -263,7 +263,7 @@ void Signal::ConnectInternal()
|
|||
|
||||
|
||||
|
||||
void Signal::CloseInternal(websocketpp::close::status::value const& code, std::string const& reason)
|
||||
void Signal::CloseInternal(websocketpp::close::status::value const& code, string const& reason)
|
||||
{
|
||||
LOGP_F(WARNING) << "Close by reason:" << reason;
|
||||
|
||||
|
|
|
@ -83,6 +83,8 @@ public:
|
|||
con_closed
|
||||
};
|
||||
|
||||
using string = std::string;
|
||||
|
||||
#if _DEBUG || DEBUG
|
||||
typedef websocketpp::config::debug_asio_tls client_config;
|
||||
#else
|
||||
|
@ -90,17 +92,17 @@ public:
|
|||
#endif //DEBUG
|
||||
typedef websocketpp::client<client_config> client_type;
|
||||
|
||||
Signal(const std::string url);
|
||||
Signal(const string url);
|
||||
~Signal();
|
||||
|
||||
void SignIn(const std::string& id, const std::string& password);
|
||||
void SignIn(const string& id, const string& password);
|
||||
void SignOut();
|
||||
|
||||
|
||||
void SendCommand(const std::string channel,
|
||||
const std::string commandname,
|
||||
void SendCommand(const string channel,
|
||||
const string commandname,
|
||||
const Json::Value& data);
|
||||
void SendGlobalCommand(const std::string commandname,
|
||||
void SendGlobalCommand(const string commandname,
|
||||
const Json::Value& data);
|
||||
|
||||
void Teardown();
|
||||
|
@ -123,7 +125,7 @@ private:
|
|||
|
||||
void RunLoop();
|
||||
void ConnectInternal();
|
||||
void CloseInternal(websocketpp::close::status::value const& code, std::string const& reason);
|
||||
void CloseInternal(websocketpp::close::status::value const& code, string const& reason);
|
||||
void TimeoutReconnect(websocketpp::lib::asio::error_code const& ec);
|
||||
unsigned NextDelay() const;
|
||||
|
||||
|
@ -152,9 +154,9 @@ private:
|
|||
unsigned reconn_made_;
|
||||
|
||||
// Signin
|
||||
std::string url_;
|
||||
std::string user_id_;
|
||||
std::string user_password_;
|
||||
string url_;
|
||||
string user_id_;
|
||||
string user_password_;
|
||||
}; // class Signal
|
||||
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "peerconnect.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace pc;
|
||||
|
||||
void test_normal();
|
||||
void test_writable();
|
||||
|
|
Loading…
Reference in New Issue