Add error event handler
parent
5cc1dbbc36
commit
5eed82a0b2
|
@ -79,6 +79,11 @@ int main(int argc, char *argv[]) {
|
|||
Throughnet::Stop();
|
||||
});
|
||||
|
||||
tn.On("error", function_tn(Throughnet* tn, string id){
|
||||
std::cerr << tn->GetErrorMessage() << std::endl;
|
||||
Throughnet::Stop();
|
||||
});
|
||||
|
||||
tn.OnMessage(function_tn(Throughnet* tn, string id, Throughnet::Buffer& data) {
|
||||
if (!write_stdout(data.buf_, data.size_)) {
|
||||
tn->Disconnect(id);
|
||||
|
@ -176,7 +181,6 @@ bool parse_args(int argc, char* argv[], std::string& alias, std::string& connect
|
|||
}
|
||||
|
||||
void usage(const char* prg) {
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "P2P netcat version 0.1 (http://github.com/throughnet/throughnet)" << std::endl << std::endl;
|
||||
std::cerr << "Usage: " << prg << " [-l] name" << std::endl << std::endl;
|
||||
std::cerr << " Options:" << std::endl;
|
||||
|
|
|
@ -498,22 +498,47 @@ void Control::OnChannelCreated(const Json::Value& data) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
LOG(LS_WARNING) << "Create channel failed";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string channel;
|
||||
if (!rtc::GetStringFromJsonObject(data, "name", &channel)) {
|
||||
LOG(LS_WARNING) << "Create channel failed - no channel name";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
LOG(LS_WARNING) << "Create channel failed";
|
||||
std::string reason;
|
||||
if (!rtc::GetStringFromJsonObject(data, "reason", &reason)) {
|
||||
reason = "Unknown reason";
|
||||
}
|
||||
observer_->OnError(channel, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
observer_->OnSignedIn(channel);
|
||||
}
|
||||
|
||||
void Control::OnChannelJoined(const Json::Value& data) {
|
||||
// Nothing
|
||||
bool result;
|
||||
if (!rtc::GetBoolFromJsonObject(data, "result", &result)) {
|
||||
LOG(LS_WARNING) << "Unknown channel join response";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string channel;
|
||||
if (!rtc::GetStringFromJsonObject(data, "name", &channel)) {
|
||||
LOG(LS_WARNING) << "Join channel failed - no channel name";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
LOG(LS_WARNING) << "Join channel failed";
|
||||
std::string reason;
|
||||
if (!rtc::GetStringFromJsonObject(data, "reason", &reason)) {
|
||||
reason = "Unknown reason";
|
||||
}
|
||||
observer_->OnError(channel, reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ public:
|
|||
virtual void OnPeerDisconnected(const std::string id) = 0;
|
||||
virtual void OnPeerMessage(const std::string id, const char* buffer, const size_t size) = 0;
|
||||
virtual void OnPeerWritable(const std::string id) = 0;
|
||||
virtual void OnError(const std::string id, const std::string& reason) = 0;
|
||||
};
|
||||
|
||||
} // namespace tn
|
||||
|
|
|
@ -220,6 +220,12 @@ void Throughnet::OnPeerWritable(const std::string id) {
|
|||
CallEventHandler("writable", this, id);
|
||||
}
|
||||
|
||||
void Throughnet::OnError(const std::string id, const std::string& reason) {
|
||||
if (event_handler_.find("error") == event_handler_.end()) return;
|
||||
|
||||
error_reason_ = reason;
|
||||
CallEventHandler("error", this, id);
|
||||
}
|
||||
|
||||
template<typename ...A>
|
||||
void Throughnet::CallEventHandler(std::string msg_id, A&& ... args)
|
||||
|
|
|
@ -59,6 +59,7 @@ public:
|
|||
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_; }
|
||||
|
||||
static std::string CreateRandomUuid();
|
||||
|
||||
|
@ -108,6 +109,7 @@ protected:
|
|||
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);
|
||||
|
||||
|
||||
bool ParseSetting(const std::string& setting);
|
||||
|
@ -120,6 +122,8 @@ protected:
|
|||
|
||||
std::shared_ptr<tn::Control> control_;
|
||||
std::shared_ptr<tn::Signal> signal_;
|
||||
|
||||
std::string error_reason_;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue