modify msg batch too long then CAN NOT send

Signed-off-by: hwzhangchuang <zhangchuang.zhang@huawei.com>
This commit is contained in:
hwzhangchuang
2023-12-29 15:18:55 +08:00
parent c2177e7983
commit 9c61ccb6e3
3 changed files with 23 additions and 1 deletions
@@ -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<struct RawEvent> &events);
void DoSendMsgBatch(const int32_t sessionId, const std::vector<struct RawEvent> &events);
private:
std::string mySessionName_;
std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> eventHandler_;
@@ -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<AppExecFwk::EventRunner> 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<struct RawEvent> 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<struct RawEvent> &events)
{
int64_t currentTimeNs = GetCurrentTimeUs() * 1000LL;
std::shared_ptr<nlohmann::json> eventsJsonArr = std::make_shared<nlohmann::json>();
for (const auto &ev : events) {
@@ -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<uint8_t *>(calloc((MSG_MAX_SIZE), sizeof(uint8_t)));