From: SukHyung, Kang Date: Wed, 28 Apr 2021 08:11:03 +0000 (+0900) Subject: Add timeout sequence for senddata sync calling X-Git-Tag: submit/tizen/20210806.015209~60 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b8bd84f2f1637291117642e2de1cdc45809c8e88;p=platform%2Fcore%2Fappfw%2Fcion.git Add timeout sequence for senddata sync calling Signed-off-by: SukHyung, Kang --- diff --git a/cion/channel/client_channel.cc b/cion/channel/client_channel.cc index 6a8a257..498be11 100644 --- a/cion/channel/client_channel.cc +++ b/cion/channel/client_channel.cc @@ -163,15 +163,27 @@ void ClientChannel::Impl::OnReceived( unsigned int cmd; pl.ReadUInt32(&cmd); if (cmd == cmd::DataSyncReply) { - LOG(INFO) << "PayloadReceived from server"; uint32_t size; pl.ReadUInt32(&size); - unsigned char* payload_data = - (unsigned char*)calloc(size, sizeof(unsigned char)); - pl.Read(payload_data, size); - reply_queue_.Push( - std::vector(payload_data, payload_data + size)); - free(payload_data); + unsigned int sequence_id; + pl.ReadUInt32(&sequence_id); + LOG(INFO) << "OnReceived datasync reply seq id : " << sequence_id; + + if (is_timeover_) { + LOG(INFO) << "OnReceived data sync reply timeover seq id : " << sequence_id; + return; + } + + if (sequence_id == sequence_id_) { + unsigned char* payload_data = + (unsigned char*)calloc(size, sizeof(unsigned char)); + pl.Read(payload_data, size); + reply_queue_.Push( + std::vector(payload_data, payload_data + size)); + free(payload_data); + } else { + LOG(INFO) << "OnReceived data sync sequence id not matched"; + } return; } @@ -375,11 +387,23 @@ void ClientChannel::Disconnect() { } std::vector ClientChannel::SendData(std::vector data, int timeout) { - LOG(WARNING) << "send data"; + std::lock_guard lock(impl_->mutex_); + std::vector ret; - auto *d = reinterpret_cast*>(&data); + impl_->sequence_id_ = IDGenerator::GetInst().GenSequenceId(); + impl_->is_timeover_ = false; + LOG(WARNING) << "send data seq id : " << impl_->sequence_id_; + + auto *d = reinterpret_cast*>(&data); impl_->Send(cmd::DataSync, *d); - impl_->reply_queue_.WaitAndPop(ret); + impl_->reply_queue_.WaitAndPopFor(ret, timeout); + + if (ret.empty()) { + impl_->sequence_id_ = -1; + impl_->is_timeover_ = true; + THROW(error::CION_ERROR_SEND_DATA_TIME_OUT); + } + return ret; } @@ -424,10 +448,16 @@ void ClientChannel::Impl::Send(cmd::CionCmd cmd, std::vector raw) { tizen_base::Parcel parcel; parcel.WriteUInt32(cmd); parcel.WriteUInt32(raw.size()); + + if (cmd == cmd::DataSync) { + parcel.WriteUInt32(sequence_id_); + LOG(INFO) << "send uint& seq id : " << sequence_id_; + } + parcel.Write(raw.data(), raw.size()); client_dp->SendDataAsync(parcel.GetRaw()); - LOG(INFO) << "send " << parcel.GetRaw().size(); + LOG(INFO) << "send uint& " << parcel.GetRaw().size(); } std::shared_ptr ClientChannel::GetPeerInfo() { diff --git a/cion/channel/client_channel_implementation.hh b/cion/channel/client_channel_implementation.hh index b23b184..fba89b0 100644 --- a/cion/channel/client_channel_implementation.hh +++ b/cion/channel/client_channel_implementation.hh @@ -70,6 +70,10 @@ class ClientChannel::Impl : public IVineBrowser, IVineDpOpenedEventHandler, SharedQueue> reply_queue_; SharedQueue terminated_dp_queue_; std::shared_ptr discoverer_ = nullptr; + + bool is_timeover_ = false; + unsigned int sequence_id_ = 0; + mutable std::mutex mutex_; }; } // namespace channel diff --git a/cion/channel/server_channel.cc b/cion/channel/server_channel.cc index 14f3a87..56af4ff 100644 --- a/cion/channel/server_channel.cc +++ b/cion/channel/server_channel.cc @@ -182,7 +182,7 @@ void ServerChannel::Impl::ReceiveFile(std::shared_ptr payload, void ServerChannel::Impl::OnReceived( VineDpPtr dp, std::vector data) { - LOG(INFO) << "OnReceived " << dp->GetRawDp(); + LOG(INFO) << "server OnReceived " << dp->GetRawDp(); ChannelJob job(parent_, std::make_shared( data.data(), data.size()), dp); queue_.Push(job); @@ -247,8 +247,11 @@ void ServerChannel::Impl::OnReceived( case cmd::CionCmd::DataSync: { - LOG(INFO) << "DataSync from client"; parcel->ReadUInt32(&size); + unsigned int sequence_id; + parcel->ReadUInt32(&sequence_id); + LOG(INFO) << "server DataSync from client seq id : " << sequence_id; + unsigned char* sync_data = (unsigned char*)calloc(size, sizeof(unsigned char)); parcel->Read(sync_data, size); @@ -260,6 +263,7 @@ void ServerChannel::Impl::OnReceived( std::vector result = channel->OnDataReceived( std::vector(sync_data, sync_data + size), peer); auto *r = reinterpret_cast*>(&result); + speer->GetClientDp()->SetSequenceId(sequence_id); channel->impl_->Send(cmd::CionCmd::DataSyncReply, speer->GetClientDp(), *r); break; @@ -332,10 +336,18 @@ void ServerChannel::Impl::Send(cmd::CionCmd cmd, VineDpPtr client_dp, tizen_base::Parcel parcel; parcel.WriteUInt32(cmd); parcel.WriteUInt32(raw.size()); + + if (cmd == cmd::CionCmd::DataSyncReply) { + unsigned int seq_id = client_dp->GetSequenceId(); + LOG(WARNING) << "server send dp seq : " << seq_id; + + parcel.WriteUInt32(seq_id); + } + parcel.Write(raw.data(), raw.size()); client_dp->SendDataAsync(parcel.GetRaw()); - LOG(WARNING) << "send " << parcel.GetRaw().size(); + LOG(WARNING) << "server send " << parcel.GetRaw().size(); } void ServerChannel::Listen() { diff --git a/cion/channel/shared_queue.hh b/cion/channel/shared_queue.hh index 347740e..82b5230 100644 --- a/cion/channel/shared_queue.hh +++ b/cion/channel/shared_queue.hh @@ -21,6 +21,9 @@ #include #include #include +#include + +#include "cion/common/util/logging.hh" namespace cion { @@ -56,6 +59,23 @@ class SharedQueue { queue_.pop(); } + void WaitAndPopFor(T& item, int timeout) { + std::unique_lock lock(mutex_); + + std::cv_status ret; + + std::chrono::milliseconds duration(timeout); + ret = cond_var_.wait_for(lock, duration); + + if (ret == std::cv_status::timeout) { + LOG(ERROR) << "WaitAndPopFor timeout"; + return; + } + + item = queue_.front(); + queue_.pop(); + } + bool Empty() { std::lock_guard lock(mutex_); return queue_.empty(); diff --git a/cion/common/cion_error.hh b/cion/common/cion_error.hh index 54dbe1b..73fcf0e 100644 --- a/cion/common/cion_error.hh +++ b/cion/common/cion_error.hh @@ -93,6 +93,13 @@ namespace error { * Wi-Fi Interface is down */ CION_ERROR_INTERFACE_DOWN = -ENETDOWN, + /** + * Interface down(100) + */ + CION_ERROR_SEND_DATA_TIME_OUT = -ETIME, + /** + * Send data time out(62) + */ }; } diff --git a/cion/common/exception.hh b/cion/common/exception.hh index fecd9fb..3d89d46 100644 --- a/cion/common/exception.hh +++ b/cion/common/exception.hh @@ -81,6 +81,8 @@ class Exception : public std::exception { return ": CION_ERROR_REJECTED_BY_PEER"; case cion::error::CION_ERROR_INTERFACE_DOWN: return ": CION_ERROR_INTERFACE_DOWN"; + case cion::error::CION_ERROR_SEND_DATA_TIME_OUT: + return ": CION_ERROR_SEND_DATA_TIME_OUT"; default: return ""; } diff --git a/cion/tizen-api/cion_client.cc b/cion/tizen-api/cion_client.cc index 51b2a30..8501738 100644 --- a/cion/tizen-api/cion_client.cc +++ b/cion/tizen-api/cion_client.cc @@ -23,6 +23,7 @@ #include "cion/tizen-api/cion_error.h" #include "cion/channel/client_channel.hh" +#include "cion/common/exception.hh" #ifndef C_EXPORT #define C_EXPORT extern "C" __attribute__((visibility("default"))) @@ -223,7 +224,13 @@ C_EXPORT int cion_client_send_data(cion_client_h client, unsigned char* data, // std::vector ?? std::vector v(data, data + data_size); - std::vector r = (*c)->SendData(v, timeout); + std::vector r; + try { + r = (*c)->SendData(v, timeout); + } catch (cion::Exception& e) { + return e.GetErrorCode(); + } + if (r.empty()) return CION_ERROR_IO_ERROR; diff --git a/cion/vine/vine_dp.cc b/cion/vine/vine_dp.cc index d779d48..3f39fba 100644 --- a/cion/vine/vine_dp.cc +++ b/cion/vine/vine_dp.cc @@ -193,4 +193,12 @@ bool VineDp::SetVineDpReceivedCb() { return true; } +void VineDp::SetSequenceId(unsigned int id) { + seq_id_ = id; +} + +unsigned int VineDp::GetSequenceId() { + return seq_id_; +} + } // namespace cion diff --git a/cion/vine/vine_dp.hh b/cion/vine/vine_dp.hh index 79b7101..d03c21a 100644 --- a/cion/vine/vine_dp.hh +++ b/cion/vine/vine_dp.hh @@ -50,6 +50,9 @@ class VineDp { bool SetVineDpTerminatedCb(); bool SetVineDpReceivedCb(); + void SetSequenceId(unsigned int id); + unsigned int GetSequenceId(); + protected: explicit VineDp(std::shared_ptr session); @@ -61,6 +64,8 @@ class VineDp { IVineDpOpenedEventHandler* opened_handler_ = nullptr; IVineDpReceivedEventHandler* received_handler_ = nullptr; bool is_owned_; + + unsigned int seq_id_; }; } // namespace cion