Update API

master
Ryan Lee 2016-09-20 20:04:20 -04:00
parent ca249094c1
commit 5ff6132d3b
11 changed files with 170 additions and 181 deletions

View File

@ -26,22 +26,22 @@ int main(int argc, char *argv[]) {
PeerConnect pc;
pc.On("open", function_pc(string channel) {
pc.On("open", function_pc(string peer) {
pc.Connect(server);
});
pc.On("connect", function_pc(string channel) {
pc.Send(channel, "Hello world");
std::cout << "Sent 'Hello world' message to " << channel << "." << std::endl;
pc.On("connect", function_pc(string peer) {
pc.Send(peer, "Hello world");
std::cout << "Sent 'Hello world' message to " << peer << "." << std::endl;
});
pc.On("close", function_pc(string channel, CloseCode code, string desc) {
std::cout << "Peer " << channel << " has been closed" << std::endl;
pc.On("close", function_pc(string peer, CloseCode code, string desc) {
std::cout << "Peer " << peer << " has been closed" << std::endl;
PeerConnect::Stop();
});
pc.On("message", function_pc(string channel, PeerConnect::Buffer& data) {
std::cout << "Message '" << std::string(data.buf_, data.size_) <<
pc.On("message", function_pc(string peer, char* data, size_t size) {
std::cout << "Message '" << std::string(data, size) <<
"' has been received." << std::endl;
pc.Close();
});
@ -56,5 +56,5 @@ void usage(const char* prg) {
std::cerr << std::endl;
std::cerr << "Usage: " << prg << " name" << std::endl << std::endl;
std::cerr << "Example: " << std::endl << std::endl;
std::cerr << " > " << prg << " myrandom" << std::endl;
std::cerr << " > " << prg << " peername" << std::endl;
}

View File

@ -31,10 +31,10 @@ int main(int argc, char *argv[]) {
std::cout << "Peer " << peer << " has been closed." << std::endl;
});
pc.On("message", function_pc(string peer, PeerConnect::Buffer& data) {
std::cout << "Message " << std::string(data.buf_, data.size_) <<
pc.On("message", function_pc(string peer, char* data, size_t size) {
std::cout << "Message " << std::string(data, size) <<
" has been received." << std::endl;
pc.Send(peer, data.buf_, data.size_);
pc.Send(peer,data, size);
});
pc.Open();
@ -47,5 +47,5 @@ void usage(const char* prg) {
std::cerr << std::endl;
std::cerr << "Usage: " << prg << " name" << std::endl << std::endl;
std::cerr << "Example: " << std::endl << std::endl;
std::cerr << " > " << prg << " myrandom" << std::endl;
std::cerr << " > " << prg << " peername" << std::endl;
}

View File

@ -27,9 +27,9 @@
using namespace std;
using namespace pc;
bool parse_args(int argc, char* argv[], string& local_channel, string& remote_channel, bool& server_mode);
bool parse_args(int argc, char* argv[], string& local_peer, string& remote_peer, bool& server_mode);
void usage(const char* prg);
void read_stdin(PeerConnect* pc, std::string channel);
void read_stdin(PeerConnect* pc, std::string peer);
bool write_stdout(const char* buf, int len);
void set_mode(PeerConnect* pc);
void ctrlc_handler(int s);
@ -39,15 +39,15 @@ static PeerConnect *pc_;
int main(int argc, char *argv[]) {
string local_channel;
string remote_channel;
string local_peer;
string remote_peer;
bool server_mode;
//
// Parse arguments
//
if (!parse_args(argc, argv, local_channel, remote_channel, server_mode)) {
if (!parse_args(argc, argv, local_peer, remote_peer, server_mode)) {
usage(argv[0]);
return 1;
}
@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
// Set event handlers
//
PeerConnect pc(local_channel);
PeerConnect pc(local_peer);
set_mode(&pc);
@ -65,8 +65,8 @@ int main(int argc, char *argv[]) {
std::cerr << "Listening " << peer << std::endl;
}
else {
std::cerr << "Connecting to " << remote_channel << std::endl;
pc.Connect(remote_channel);
std::cerr << "Connecting to " << remote_peer << std::endl;
pc.Connect(remote_peer);
}
});
@ -77,7 +77,7 @@ int main(int argc, char *argv[]) {
pc.On("close", function_pc( string peer, CloseCode code, string desc ) {
if ( peer == local_channel ) {
if ( peer == local_peer ) {
PeerConnect::Stop();
}
else {
@ -89,8 +89,8 @@ int main(int argc, char *argv[]) {
}
});
pc.On("message", function_pc(string peer, PeerConnect::Buffer& data) {
if (!write_stdout(data.buf_, data.size_)) {
pc.On("message", function_pc(string peer, char *data, size_t size) {
if (!write_stdout(data, size)) {
pc.Close(peer);
}
});
@ -119,7 +119,7 @@ int main(int argc, char *argv[]) {
#define STDERR_FILENO 2
#endif
void read_stdin(PeerConnect* pc, std::string channel)
void read_stdin(PeerConnect* pc, std::string peer)
{
int nbytes;
char buf[32*1024];
@ -127,11 +127,11 @@ void read_stdin(PeerConnect* pc, std::string channel)
for (;;) {
nbytes = read(STDIN_FILENO, buf, sizeof(buf));
if (nbytes <= 0) {
pc->Close( channel );
pc->Close( peer );
return;
}
if (!pc->Send(channel, buf, nbytes, WAITING_ON)) {
if (!pc->Send(peer, buf, nbytes, SYNC_ON)) {
return;
}
}
@ -171,15 +171,15 @@ void set_mode(PeerConnect* pc)
#endif
}
bool parse_args(int argc, char* argv[], string& local_channel, string& remote_channel, bool& server_mode) {
bool parse_args(int argc, char* argv[], string& local_peer, string& remote_peer, bool& server_mode) {
if (argc == 2) {
remote_channel = argv[1];
local_channel = PeerConnect::CreateRandomUuid();
remote_peer = argv[1];
local_peer = PeerConnect::CreateRandomUuid();
server_mode = false;
return true;
}
else if (argc == 3 && std::string(argv[1]) == "-l") {
local_channel = argv[2];
local_peer = argv[2];
server_mode = true;
return true;
}
@ -193,7 +193,7 @@ void usage(const char* prg) {
std::cerr << " -l Listen mode, for inbound connections" << std::endl << std::endl;
std::cerr << "Example: " << std::endl;
std::cerr << " > " << prg << " -l my_channel : Listen my_channel" << std::endl;
std::cerr << " > " << prg << " my_channel : Connect to my_channel" << std::endl;
std::cerr << " > " << prg << " -l peer_name : Listen peer_name" << std::endl;
std::cerr << " > " << prg << " peer_name : Connect to peer_name" << std::endl;
}

View File

@ -24,8 +24,8 @@ enum CloseCode {
};
const bool WAITING_OFF = false;
const bool WAITING_ON = true;
const bool SYNC_OFF = false;
const bool SYNC_ON = true;
const bool FORCE_QUEUING_OFF = false;
const bool FORCE_QUEUING_ON = true;

View File

@ -85,13 +85,13 @@ void Control::DeleteControl() {
}
void Control::Open(const string& user_id, const string& user_password, const string& channel) {
void Control::Open(const string& user_id, const string& user_password, const string& peer) {
// 1. Connect to signal server
// 2. Send open command to signal server
// 3. Send createchannel command to signal server.
// A channel name is user-supplied string to listen or random string.
// Other peers connect to this peer by channel name.
// A peer name is user-supplied string to listen or random string.
// Other peers connect to this peer by peer name.
// 4. Generate 'open' event to PeerConnect
if (signal_.get() == NULL) {
@ -99,7 +99,7 @@ void Control::Open(const string& user_id, const string& user_password, const str
return;
}
channel_ = channel;
peer_name_ = peer;
user_id_ = user_id;
// Connect to signal server
@ -109,7 +109,7 @@ void Control::Open(const string& user_id, const string& user_password, const str
return;
}
void Control::Connect(const string channel) {
void Control::Connect(const string peer) {
// 1. Join channel on signal server
// 2. Server(remote) peer createoffer
@ -121,8 +121,8 @@ void Control::Connect(const string channel) {
return;
}
LOGP_F( INFO ) << "Joining channel " << channel;
JoinChannel(channel);
LOGP_F( INFO ) << "Joining channel " << peer;
JoinChannel(peer);
}
void Control::Close(const CloseCode code, bool force_queuing) {
@ -162,13 +162,13 @@ void Control::Close(const CloseCode code, bool force_queuing) {
//
if ( pc_ ) {
pc_->OnClose( channel_ ,code );
pc_->OnClose( peer_name_ ,code );
}
LOGP_F( INFO ) << "Done";
}
void Control::ClosePeer( const string channel, const CloseCode code, bool force_queuing ) {
void Control::ClosePeer( const string peer, const CloseCode code, bool force_queuing ) {
//
// Called by
@ -177,7 +177,7 @@ void Control::ClosePeer( const string channel, const CloseCode code, bool force_
//
if (force_queuing || webrtc_thread_ != rtc::Thread::Current()) {
ControlMessageData *data = new ControlMessageData(channel, ref_);
ControlMessageData *data = new ControlMessageData(peer, ref_);
data->data_int32_ = code;
webrtc_thread_->Post(RTC_FROM_HERE, this, MSG_CLOSE_PEER, data);
return;
@ -186,45 +186,45 @@ void Control::ClosePeer( const string channel, const CloseCode code, bool force_
// 1. Erase peer
// 2. Close peer
auto peer_found = peers_.find(channel);
auto peer_found = peers_.find(peer);
if ( peer_found == peers_.end() ) {
LOGP_F( WARNING ) << "peer not found, " << channel;
LOGP_F( WARNING ) << "peer not found, " << peer;
return;
}
Peer peer = peer_found->second;
Peer item = peer_found->second;
peers_.erase( peer_found );
peer->Close(code);
item->Close(code);
// 3. Leave channel on signal server
LeaveChannel(channel);
LeaveChannel(peer);
LOGP_F( INFO ) << "Done, channel is " << channel;
LOGP_F( INFO ) << "Done, peer is " << peer;
}
//
// Send data to peer
//
void Control::Send(const string to, const char* buffer, const size_t size) {
void Control::Send(const string to, const char* data, const size_t size) {
typedef std::map<string, rtc::scoped_refptr<PeerControl>>::iterator it_type;
it_type it = peers_.find(to);
if (it == peers_.end()) return;
it->second->Send(buffer, size);
it->second->Send(data, size);
return;
}
bool Control::SyncSend(const string to, const char* buffer, const size_t size) {
bool Control::SyncSend(const string to, const char* data, const size_t size) {
typedef std::map<string, rtc::scoped_refptr<PeerControl>>::iterator it_type;
it_type it = peers_.find(to);
if (it == peers_.end()) return false;
return it->second->SyncSend(buffer, size);
return it->second->SyncSend(data, size);
}
@ -232,62 +232,62 @@ bool Control::SyncSend(const string to, const char* buffer, const size_t size) {
// Send command to other peer by signal server
//
void Control::SendCommand(const string& channel, const string& command, const Json::Value& data) {
signal_->SendCommand(channel, command, data);
void Control::SendCommand(const string& peer, const string& command, const Json::Value& data) {
signal_->SendCommand(peer, command, data);
}
void Control::OnPeerConnect(const string channel) {
void Control::OnPeerConnect(const string peer) {
if ( pc_ == nullptr ) {
LOGP_F( WARNING ) << "pc_ is null, channel is " << channel;
LOGP_F( WARNING ) << "pc_ is null, peer is " << peer;
return;
}
pc_->OnConnect(channel);
LOGP_F( INFO ) << "Done, channel is " << channel;
pc_->OnConnect(peer);
LOGP_F( INFO ) << "Done, peer is " << peer;
}
void Control::OnPeerClose(const string channel, CloseCode code) {
void Control::OnPeerClose(const string peer, CloseCode code) {
if (webrtc_thread_ != rtc::Thread::Current()) {
ControlMessageData *data = new ControlMessageData(channel, ref_);
ControlMessageData *data = new ControlMessageData(peer, ref_);
// Call Control::OnPeerDisconnected()
webrtc_thread_->Post(RTC_FROM_HERE, this, MSG_ON_PEER_CLOSE, data);
LOGP_F( INFO ) << "Queued, channel is " << channel;
LOGP_F( INFO ) << "Queued, peer is " << peer;
return;
}
LOGP_F( INFO ) << "Enter, channel is " << channel;
LOGP_F( INFO ) << "Enter, peer is " << peer;
if ( pc_ == nullptr ) {
LOGP_F( WARNING ) << "pc_ is null, channel is " << channel;
LOGP_F( WARNING ) << "pc_ is null, peer is " << peer;
return;
}
pc_->OnClose( channel, code );
pc_->OnClose( peer, code );
LOGP_F( INFO ) << "Done, channel is " << channel;
LOGP_F( INFO ) << "Done, peer is " << peer;
}
//
// Signal receiving data
//
void Control::OnPeerMessage(const string& channel, const char* buffer, const size_t size) {
void Control::OnPeerMessage(const string& peer, const char* data, const size_t size) {
if ( pc_ == nullptr ) {
LOGP_F( WARNING ) << "pc_ is null, channel is " << channel;
LOGP_F( WARNING ) << "pc_ is null, peer is " << peer;
return;
}
pc_->OnMessage(channel, buffer, size);
pc_->OnMessage(peer, data, size);
}
void Control::OnPeerWritable(const string& channel) {
void Control::OnPeerWritable(const string& peer) {
if ( pc_ == nullptr ) {
LOGP_F( WARNING ) << "pc_ is null, channel is " << channel;
LOGP_F( WARNING ) << "pc_ is null, peer is " << peer;
return;
}
pc_->OnWritable(channel);
pc_->OnWritable(peer);
}
void Control::RegisterObserver(ControlObserver* observer, std::shared_ptr<Control> ref) {
@ -526,7 +526,7 @@ void Control::OnOpen(const Json::Value& data) {
// Create channel
//
CreateChannel(channel_);
CreateChannel(peer_name_);
LOGP_F( INFO ) << "Done";
}
@ -535,13 +535,13 @@ void Control::OnChannelCreate(const Json::Value& data) {
bool result;
if (!rtc::GetBoolFromJsonObject(data, "result", &result)) {
LOGP_F(WARNING) << "Unknown open response";
pc_->OnClose(channel_, CLOSE_SIGNAL_ERROR);
pc_->OnClose(peer_name_, CLOSE_SIGNAL_ERROR);
return;
}
string channel;
if (!rtc::GetStringFromJsonObject(data, "name", &channel)) {
pc_->OnClose(channel_, CLOSE_SIGNAL_ERROR);
string peer;
if (!rtc::GetStringFromJsonObject(data, "name", &peer)) {
pc_->OnClose(peer_name_, CLOSE_SIGNAL_ERROR);
LOGP_F(LERROR) << "Create channel failed - no channel name";
return;
}
@ -553,11 +553,11 @@ void Control::OnChannelCreate(const Json::Value& data) {
desc = "Unknown reason";
}
pc_->OnClose(channel, CLOSE_SIGNAL_ERROR, desc);
pc_->OnClose(peer, CLOSE_SIGNAL_ERROR, desc);
return;
}
pc_->OnOpen(channel);
pc_->OnOpen(peer);
LOGP_F( INFO ) << "Done";
}
@ -572,8 +572,8 @@ void Control::OnChannelJoin(const Json::Value& data) {
return;
}
string channel;
if (!rtc::GetStringFromJsonObject(data, "name", &channel)) {
string peer;
if (!rtc::GetStringFromJsonObject(data, "name", &peer)) {
pc_->OnClose( "", CLOSE_SIGNAL_ERROR );
LOGP_F(LERROR) << "Join channel failed - no channel name";
return;
@ -586,7 +586,7 @@ void Control::OnChannelJoin(const Json::Value& data) {
desc = "Unknown reason";
}
pc_->OnClose( channel, CLOSE_SIGNAL_ERROR, desc );
pc_->OnClose( peer, CLOSE_SIGNAL_ERROR, desc );
return;
}
@ -626,7 +626,7 @@ void Control::CreateOffer(const Json::Value& data) {
return;
}
Peer peer = new rtc::RefCountedObject<PeerControl>(channel_, remote_id, this, peer_connection_factory_);
Peer peer = new rtc::RefCountedObject<PeerControl>(peer_name_, remote_id, this, peer_connection_factory_);
if ( !peer->Initialize() ) {
LOGP_F( LERROR ) << "Peer initialization failed";
OnPeerClose( remote_id, CLOSE_ABNORMAL );
@ -653,7 +653,7 @@ void Control::ReceiveOfferSdp(const string& peer_id, const Json::Value& data) {
return;
}
Peer peer = new rtc::RefCountedObject<PeerControl>(channel_, peer_id, this, peer_connection_factory_);
Peer peer = new rtc::RefCountedObject<PeerControl>(peer_name_, peer_id, this, peer_connection_factory_);
if ( !peer->Initialize() ) {
LOGP_F( LERROR ) << "Peer initialization failed";
OnPeerClose( peer_id, CLOSE_ABNORMAL );

View File

@ -43,13 +43,13 @@ public:
// Negotiation and send data
//
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 Send(const string to, const char* data, const size_t size);
bool SyncSend(const string to, const char* data, const size_t size);
void Open(const string& user_id, const string& user_password, const string& channel);
void Open(const string& user_id, const string& user_password, const string& peer);
void Close(const CloseCode code, bool force_queueing = FORCE_QUEUING_OFF);
void Connect(const string channel);
bool IsWritable(const string channel);
void Connect(const string peer);
bool IsWritable(const string peer);
void OnCommandReceived(const Json::Value& message);
void OnSignalCommandReceived(const Json::Value& message);
@ -59,12 +59,12 @@ public:
// PeerObserver implementation
//
virtual void SendCommand(const string& channel, const string& command, const Json::Value& data);
virtual void ClosePeer( const string channel, const CloseCode code, bool force_queueing = FORCE_QUEUING_OFF );
virtual void OnPeerConnect(const string channel);
virtual void OnPeerClose(const string channel, const CloseCode code);
virtual void OnPeerMessage(const string& channel, const char* buffer, const size_t size);
virtual void OnPeerWritable(const string& channel);
virtual void SendCommand(const string& peer, const string& command, const Json::Value& data);
virtual void ClosePeer( const string peer, const CloseCode code, bool force_queueing = FORCE_QUEUING_OFF );
virtual void OnPeerConnect(const string peer);
virtual void OnPeerClose(const string peer, const CloseCode code);
virtual void OnPeerMessage(const string& peer, const char* data, const size_t size);
virtual void OnPeerWritable(const string& peer);
// Register/Unregister observer
@ -91,10 +91,10 @@ protected:
void OnRemotePeerClose(const string& peer_id, const Json::Value& data);
// channel_: A name of local channel. Other peers can find this peer by channel_
// peer_name_: A name of local peer. Other peers can find this peer by peer_
// 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
string channel_;
string peer_name_;
string user_id_;
string session_id_;

View File

@ -13,11 +13,11 @@ namespace pc {
class ControlObserver {
public:
virtual void OnOpen(const std::string channel) = 0;
virtual void OnClose(const std::string channel, const pc::CloseCode code, const std::string desc = "") = 0;
virtual void OnConnect(const std::string channel) = 0;
virtual void OnMessage(const std::string channel, const char* buffer, const size_t size) = 0;
virtual void OnWritable(const std::string channel) = 0;
virtual void OnOpen(const std::string peer) = 0;
virtual void OnClose(const std::string peer, const pc::CloseCode code, const std::string desc = "") = 0;
virtual void OnConnect(const std::string peer) = 0;
virtual void OnMessage(const std::string peer, const char* data, const size_t size) = 0;
virtual void OnWritable(const std::string peer) = 0;
};
} // namespace pc

View File

@ -25,12 +25,12 @@ namespace pc {
class PeerObserver {
public:
virtual void SendCommand(const std::string& channel, const std::string& command, const Json::Value& data) = 0;
virtual void ClosePeer(const std::string channel, const pc::CloseCode code, bool force_queuing = FORCE_QUEUING_OFF ) = 0;
virtual void OnPeerConnect(const std::string channel) = 0;
virtual void OnPeerClose(const std::string channel, const pc::CloseCode code) = 0;
virtual void OnPeerMessage(const std::string& channel, const char* buffer, const size_t size) = 0;
virtual void OnPeerWritable(const std::string& channel) = 0;
virtual void SendCommand(const std::string& peer, const std::string& command, const Json::Value& data) = 0;
virtual void ClosePeer(const std::string peer, const pc::CloseCode code, bool force_queuing = FORCE_QUEUING_OFF ) = 0;
virtual void OnPeerConnect(const std::string peer) = 0;
virtual void OnPeerClose(const std::string peer, const pc::CloseCode code) = 0;
virtual void OnPeerMessage(const std::string& peer, const char* buffer, const size_t size) = 0;
virtual void OnPeerWritable(const std::string& peer) = 0;
};
class PeerDataChannelObserver;

View File

@ -13,7 +13,7 @@
namespace pc {
PeerConnect::PeerConnect( const string channel ) {
PeerConnect::PeerConnect( const string peer ) {
// Log level
#if DEBUG || _DEBUG
rtc::LogMessage::LogToDebug( rtc::LS_NONE );
@ -23,16 +23,16 @@ PeerConnect::PeerConnect( const string channel ) {
pc::LogMessage::LogToDebug( pc::LS_NONE );
#endif
string local_channel;
string local_peer;
if ( channel.empty() ) {
local_channel = rtc::CreateRandomUuid();
if ( peer.empty() ) {
local_peer = rtc::CreateRandomUuid();
}
else {
local_channel = channel;
local_peer = peer;
}
channel_ = local_channel;
peer_ = local_peer;
close_once_ = false;
LOGP_F( INFO ) << "Done";
@ -94,26 +94,26 @@ void PeerConnect::Open() {
// Connect to signal server
//
control_->Open( setting_.signal_id_, setting_.signal_password_, channel_ );
control_->Open( setting_.signal_id_, setting_.signal_password_, peer_ );
LOGP_F( INFO ) << "Done";
return;
}
void PeerConnect::Close( const string channel ) {
void PeerConnect::Close( const string peer ) {
if ( channel.empty() || channel == channel_ ) {
if ( peer.empty() || peer == peer_ ) {
control_->Close( CLOSE_NORMAL, FORCE_QUEUING_ON );
signal_->SyncClose();
}
else {
control_->ClosePeer( channel, CLOSE_NORMAL, FORCE_QUEUING_ON );
control_->ClosePeer( peer, CLOSE_NORMAL, FORCE_QUEUING_ON );
}
LOGP_F( INFO ) << "Done";
}
void PeerConnect::Connect( const string channel ) {
control_->Connect( channel );
LOGP_F( INFO ) << "Done, channel is " << channel;
void PeerConnect::Connect( const string peer ) {
control_->Connect( peer );
LOGP_F( INFO ) << "Done, peer is " << peer;
return;
}
@ -121,7 +121,7 @@ void PeerConnect::Connect( const string channel ) {
// Send message to destination peer session id
//
bool PeerConnect::Send( const string& channel, const char* buffer, const size_t size, const bool wait ) {
bool PeerConnect::Send( const string& peer, const char* data, const size_t size, const bool wait ) {
if ( wait ) {
//
@ -129,10 +129,10 @@ bool PeerConnect::Send( const string& channel, const char* buffer, const size_t
// and a timeout is 60*1000 ms by default.
//
return control_->SyncSend( channel, buffer, size );
return control_->SyncSend( peer, data, size );
}
else {
control_->Send( channel, buffer, size );
control_->Send( peer, data, size );
//
// Asyncronous send always returns true and
@ -143,12 +143,12 @@ bool PeerConnect::Send( const string& channel, const char* buffer, const size_t
}
}
bool PeerConnect::Send( const string& channel, const char* message, const bool wait ) {
return Send( channel, message, strlen( message ), wait );
bool PeerConnect::Send( const string& peer, const char* message, const bool wait ) {
return Send( peer, message, strlen( message ), wait );
}
bool PeerConnect::Send( const string& channel, const string& message, const bool wait ) {
return Send( channel, message.c_str(), message.size(), wait );
bool PeerConnect::Send( const string& peer, const string& message, const bool wait ) {
return Send( peer, message.c_str(), message.size(), wait );
}
bool PeerConnect::SetOptions( const string options ) {
@ -207,7 +207,7 @@ PeerConnect& PeerConnect::On( string event_id, std::function<void( string, pc::C
return *this;
}
PeerConnect& PeerConnect::On( string event_id, std::function<void( string, Buffer& )> handler ) {
PeerConnect& PeerConnect::On( string event_id, std::function<void( string, char*, std::size_t )> handler ) {
if ( event_id.empty() ) return *this;
if ( event_id == "message" ) {
@ -227,29 +227,29 @@ PeerConnect& PeerConnect::On( string event_id, std::function<void( string, Buffe
// Signal event handler
//
void PeerConnect::OnOpen( const string channel ) {
void PeerConnect::OnOpen( const string peer ) {
close_once_ = false;
if ( event_handler_.find( "open" ) != event_handler_.end() ) {
CallEventHandler( "open", channel );
CallEventHandler( "open", peer );
}
LOGP_F( INFO ) << "Done";
}
void PeerConnect::OnClose( const string channel, const CloseCode code, const string desc ) {
void PeerConnect::OnClose( const string peer, const CloseCode code, const string desc ) {
// This instance of PeerConnect and local channel is going to be closed
if ( channel == channel_ ) {
// This instance of PeerConnect and local peer is going to be closed
if ( peer == peer_ ) {
if ( close_once_ ) {
LOGP_F( WARNING ) << "close_ is false, channel is " << channel;
LOGP_F( WARNING ) << "close_ is false, peer is " << peer;
return;
}
close_once_ = true;
if ( event_handler_.find( "close" ) != event_handler_.end() ) {
CallEventHandler( "close", channel, code, desc );
CallEventHandler( "close", peer, code, desc );
}
control_->UnregisterObserver();
@ -258,35 +258,33 @@ void PeerConnect::OnClose( const string channel, const CloseCode code, const str
// Remote peer has been closed
else {
if ( event_handler_.find( "close" ) != event_handler_.end() ) {
CallEventHandler( "close", channel, code, desc );
CallEventHandler( "close", peer, code, desc );
}
}
LOGP_F( INFO ) << "Done, channel is " << channel;
LOGP_F( INFO ) << "Done, peer is " << peer;
}
void PeerConnect::OnConnect( const string channel ) {
void PeerConnect::OnConnect( const string peer ) {
if ( event_handler_.find( "connect" ) != event_handler_.end() ) {
CallEventHandler( "connect", channel );
CallEventHandler( "connect", peer );
}
LOGP_F( INFO ) << "Done, channel is " << channel;
LOGP_F( INFO ) << "Done, peer is " << peer;
}
void PeerConnect::OnMessage( const string channel, const char* buffer, const size_t size ) {
Buffer buf( buffer, size );
void PeerConnect::OnMessage( const string peer, const char* data, const size_t size ) {
if ( event_handler_.find( "message" ) != event_handler_.end() ) {
CallEventHandler( "message", channel, buf );
CallEventHandler( "message", peer, data, size );
}
}
void PeerConnect::OnWritable( const string channel ) {
void PeerConnect::OnWritable( const string peer ) {
if ( event_handler_.find( "writable" ) != event_handler_.end() ) {
CallEventHandler( "writable", channel );
CallEventHandler( "writable", peer );
}
LOGP_F( INFO ) << "Done, channel is " << channel;
LOGP_F( INFO ) << "Done, peer is " << peer;
}

View File

@ -34,14 +34,6 @@ public:
string signal_password_;
};
class Buffer {
public:
Buffer() : buf_( nullptr ), size_( 0 ) {};
Buffer( const char* buf, const size_t size ) : buf_( buf ), size_( size ) {}
const char* buf_;
const size_t size_;
};
//
// APIs
//
@ -50,23 +42,23 @@ public:
static void Stop();
void Open();
void Close( const string channel = "" );
void Connect( const string channel );
bool Send( const string& channel, const char* buffer, const size_t size, const bool wait = WAITING_OFF );
bool Send( const string& channel, const char* buffer, const bool wait = WAITING_OFF );
bool Send( const string& channel, const string& message, const bool wait = WAITING_OFF );
void Close( const string peer = "" );
void Connect( const string peer );
bool Send( const string& peer, const char* data, const std::size_t size, const bool wait = SYNC_OFF );
bool Send( const string& peer, const char* data, const bool wait = SYNC_OFF );
bool Send( const string& peer, const string& message, const bool wait = SYNC_OFF );
bool SetOptions( const string options );
PeerConnect& On( string event_id, std::function<void( string )> );
PeerConnect& On( string event_id, std::function<void( string, string )> );
PeerConnect& On( string event_id, std::function<void( string, pc::CloseCode, string )> );
PeerConnect& On( string event_id, std::function<void( string, Buffer& )> );
PeerConnect& On( string event_id, std::function<void( string, char*, std::size_t )> );
//
// Member functions
//
explicit PeerConnect( const string channel = "" );
explicit PeerConnect( const string peer = "" );
~PeerConnect();
static std::string CreateRandomUuid();
@ -93,19 +85,18 @@ protected:
using EventHandler_2 = EventHandler_t<string>;
using EventHandler_3 = EventHandler_t<string, Data&>;
using EventHandler_Close = EventHandler_t<string, pc::CloseCode, string>;
using EventHandler_Message = EventHandler_t<string, Buffer&>;
using EventHandler_Message = EventHandler_t<string, char*, std::size_t>;
using Events = std::map<string, std::unique_ptr<Handler_t>>;
using MessageHandler = std::function<void( PeerConnect*, string, Buffer& )>;
//
// ControlObserver implementation
//
void OnOpen( const string channel );
void OnClose( const string channel, const pc::CloseCode code, const string desc = "" );
void OnConnect( const string channel );
void OnMessage( const string channel, const char* buffer, const size_t size );
void OnWritable( const string channel );
void OnOpen( const string peer );
void OnClose( const string peer, const pc::CloseCode code, const string desc = "" );
void OnConnect( const string peer );
void OnMessage( const string peer, const char* data, const size_t size );
void OnWritable( const string peer );
bool ParseOptions( const string& options );
@ -116,7 +107,7 @@ protected:
std::shared_ptr<Control> control_;
std::shared_ptr<Signal> signal_;
string channel_;
string peer_;
};

View File

@ -58,8 +58,8 @@ void test_normal() {
}
});
pc1.On("message", function_pc( string peer, PeerConnect::Buffer& data ) {
assert(std::string(data.buf_, data.size_) == "Ping");
pc1.On("message", function_pc( string peer, char* data, size_t size ) {
assert(std::string(data, size) == "Ping");
assert(peer == client);
std::cout << "pc1: a message has been received" << std::endl;
pc1.Send(client, "Pong");
@ -90,8 +90,8 @@ void test_normal() {
}
});
pc2.On("message", function_pc( string peer, PeerConnect::Buffer& data ) {
assert(std::string(data.buf_, data.size_) == "Pong");
pc2.On("message", function_pc( string peer, char* data, size_t size ) {
assert(std::string(data, size) == "Pong");
assert(peer == server);
std::cout << "pc2 has received message" << std::endl;
pc2.Close(server);
@ -137,8 +137,8 @@ void test_writable() {
std::cout << "pc1: writable" << std::endl;
});
pc1.On("message", function_pc( string peer, PeerConnect::Buffer& data ) {
assert(std::string(data.buf_, data.size_) == "Ping");
pc1.On("message", function_pc( string peer, char* data, size_t size ) {
assert(std::string(data, size) == "Ping");
assert(peer == client);
std::cout << "pc1: a message has been received" << std::endl;
pc1.Send(client, "Pong");
@ -174,8 +174,8 @@ void test_writable() {
pc2.Send(server, "Ping");
});
pc2.On("message", function_pc( string peer, PeerConnect::Buffer& data ) {
assert(std::string(data.buf_, data.size_) == "Pong");
pc2.On("message", function_pc( string peer, char* data, size_t size ) {
assert(std::string(data, size) == "Pong");
assert(peer == server);
std::cout << "pc2 has received message" << std::endl;
pc2.Close(server);