fix client issue of read fd after close

Signed-off-by: lixiangpeng5 <lixiangpeng5@huawei.com>
Change-Id: I3e28a904b04d57da6820776d859ea0087dc85de9
This commit is contained in:
lixiangpeng5 2024-09-28 09:51:52 +00:00
parent 7aa0c6b76c
commit 4c3c468cc1
3 changed files with 8 additions and 12 deletions

View File

@ -57,8 +57,7 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor)
SEN_HILOGE("Receive data buff_ is null");
return;
}
int32_t len =
recv(fileDescriptor, receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE, 0);
int32_t len = channel_->ReceiveData(receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE);
int32_t eventSize = static_cast<int32_t>(sizeof(SensorData));
while (len > 0) {
int32_t num = len / eventSize;
@ -74,7 +73,7 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor)
};
channel_->dataCB_(&event, 1, channel_->privateData_);
}
len = recv(fileDescriptor, receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE, 0);
len = channel_->ReceiveData(receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE);
}
}

View File

@ -70,7 +70,7 @@ HWTEST_F(SensorBasicDataChannelTest, SensorBasicDataChannelTest_001, TestSize.Le
ASSERT_EQ(ret, ERR_OK);
ret = sensorChannel.ReceiveData(static_cast<void *>(buff), sizeof(buff));
ASSERT_NE(ret, SENSOR_CHANNEL_RECEIVE_ADDR_ERR);
ASSERT_NE(ret, ERROR);
sensorChannel.DestroySensorBasicChannel();
}
@ -135,12 +135,12 @@ HWTEST_F(SensorBasicDataChannelTest, ReceiveData_001, TestSize.Level1)
SensorBasicDataChannel sensorChannel = SensorBasicDataChannel();
char buff[128] = {};
int32_t ret = sensorChannel.ReceiveData(static_cast<void *>(buff), sizeof(buff));
ASSERT_EQ(ret, SENSOR_CHANNEL_RECEIVE_ADDR_ERR);
ASSERT_EQ(ret, ERROR);
sensorChannel.CreateSensorBasicChannel();
char *buff1 = nullptr;
ret = sensorChannel.ReceiveData(static_cast<void *>(buff1), sizeof(buff1));
ASSERT_EQ(ret, SENSOR_CHANNEL_RECEIVE_ADDR_ERR);
ASSERT_EQ(ret, ERROR);
sensorChannel.DestroySensorBasicChannel();
}

View File

@ -172,13 +172,10 @@ int32_t SensorBasicDataChannel::ReceiveData(void *vaddr, size_t size)
std::unique_lock<std::mutex> lock(fdLock_);
if ((vaddr == nullptr) || (receiveFd_ < 0)) {
SEN_HILOGE("Failed, vaddr is null or receiveFd_ invalid");
return SENSOR_CHANNEL_RECEIVE_ADDR_ERR;
return ERROR;
}
ssize_t length;
do {
length = recv(receiveFd_, vaddr, size, MSG_DONTWAIT);
} while (errno == EINTR);
return length;
ssize_t length = recv(receiveFd_, vaddr, size, 0);
return static_cast<int32_t>(length);
}
int32_t SensorBasicDataChannel::GetSendDataFd()