diff --git a/services/sink/transport/include/distributed_input_sink_transport.h b/services/sink/transport/include/distributed_input_sink_transport.h index df48cc2..cab916c 100644 --- a/services/sink/transport/include/distributed_input_sink_transport.h +++ b/services/sink/transport/include/distributed_input_sink_transport.h @@ -95,6 +95,8 @@ private: void RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value); void RecordEventLog(const std::vector &events); + + void DoSendMsgBatch(const int32_t sessionId, const std::vector &events); private: std::string mySessionName_; std::shared_ptr eventHandler_; diff --git a/services/sink/transport/src/distributed_input_sink_transport.cpp b/services/sink/transport/src/distributed_input_sink_transport.cpp index c5b74f2..843f506 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -37,6 +37,10 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +namespace { + // each time, we send msg batch with MAX 20 events. + constexpr int32_t MSG_BTACH_MAX_SIZE =20; +} DistributedInputSinkTransport::DistributedInputSinkTransport() : mySessionName_("") { std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); @@ -230,6 +234,22 @@ void DistributedInputSinkTransport::SendKeyStateNodeMsgBatch(const int32_t sessi } DHLOGI("SendKeyStateNodeMsgBatch sessionId: %d, event size: %d ", sessionId, events.size()); + std::vector eventBatch; + for (int32_t i = 0; i < events.size(); i++) { + eventBatch.push_back(events[i]); + if (i == MSG_BTACH_MAX_SIZE) { + DoSendMsgBatch(sessionId, eventBatch); + eventBatch.clear(); + } + } + + if (!eventBatch.empty()) { + DoSendMsgBatch(sessionId, eventBatch); + } +} + +void DistributedInputSinkTransport::DoSendMsgBatch(const int32_t sessionId, const std::vector &events) +{ int64_t currentTimeNs = GetCurrentTimeUs() * 1000LL; std::shared_ptr eventsJsonArr = std::make_shared(); for (const auto &ev : events) { diff --git a/services/transportbase/src/distributed_input_transport_base.cpp b/services/transportbase/src/distributed_input_transport_base.cpp index f3344d7..06a021f 100644 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -521,7 +521,7 @@ void DistributedInputTransportBase::HandleSession(int32_t sessionId, const std:: int32_t DistributedInputTransportBase::SendMsg(int32_t sessionId, std::string &message) { if (message.size() > MSG_MAX_SIZE) { - DHLOGE("SendMessage error: message.size() > MSG_MAX_SIZE"); + DHLOGE("SendMessage error: message.size() > MSG_MAX_SIZE, msg size: %d", message.size()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; } uint8_t *buf = reinterpret_cast(calloc((MSG_MAX_SIZE), sizeof(uint8_t)));