This commit is contained in:
lenheart
2024-09-07 15:18:28 +08:00
parent bbaa7af861
commit 37fe87af67
149 changed files with 721 additions and 32707 deletions

View File

@@ -27,10 +27,13 @@ private:
std::string Ip = "192.168.200.27";
// std::string Ip = "127.0.0.1";
std::string Port = "65109";
std::mutex PackMtx;
std::mutex SizeMtx;
std::mutex ClearFlagMtx;
public:
bool ConnectState = false;
bool ClearFlag = false;
int LogiThreadSize = 0;
std::fstream file;
@@ -74,14 +77,16 @@ public:
socket.async_read_some(asio::buffer(buffer), [this](const asio::error_code &error, std::size_t bytes_transferred)
{
if (!error) {
PackMtx.lock();
if (ClearFlag)
ClearPack();
for (std::size_t i = 0; i < bytes_transferred; ++i)
{
PackData[writeDataSize + i] = buffer[i];
}
writeDataSize += bytes_transferred;
PackMtx.unlock();
read();
SizeMtx.lock();
writeDataSize += bytes_transferred;
SizeMtx.unlock();
read();
} else {
std::cerr << "Error reading data: " << error.message() << std::endl;
reconnect();
@@ -132,43 +137,69 @@ public:
} });
}
void ClearPack()
{
SizeMtx.lock();
// 复制已读大小开始到结束到 开始 作用删除前已读大小的数据
std::copy(PackData.begin() + receivedDataSize, PackData.end(), PackData.begin());
// 将最后元素设置为0
std::fill(PackData.end() - receivedDataSize, PackData.end(), 0);
// 写的大小重置
writeDataSize -= receivedDataSize;
// 读的大小重置
receivedDataSize = 0;
SizeMtx.unlock();
ClearFlagMtx.lock();
ClearFlag = false;
ClearFlagMtx.unlock();
}
void PackLogic()
{
int RealSize = 0;
int RealReadSize = -1;
if (SizeMtx.try_lock())
{
receivedDataSize += LogiThreadSize;
LogiThreadSize = 0;
RealSize = writeDataSize;
RealReadSize = receivedDataSize;
SizeMtx.unlock();
}
// 如果包长度不够直接返回
if (writeDataSize < 4)
if (RealSize < 4 || RealReadSize == -1)
return;
// 如果里面已经读了超过一半的大小了
if (writeDataSize >= 8192)
if (RealSize >= 40960)
{
PackMtx.lock();
// 复制已读大小开始到结束到 开始 作用删除前已读大小的数据
std::copy(PackData.begin() + receivedDataSize, PackData.end(), PackData.begin());
// 将最后元素设置为0
std::fill(PackData.end() - receivedDataSize, PackData.end(), 0);
// 写的大小重置
writeDataSize -= receivedDataSize;
// 读的大小重置
receivedDataSize = 0;
PackMtx.unlock();
if (!ClearFlag)
{
ClearFlagMtx.lock();
ClearFlag = true;
ClearFlagMtx.unlock();
}
}
unsigned char *Count = new unsigned char[4];
std::copy(PackData.begin() + receivedDataSize, PackData.begin() + 4 + receivedDataSize, Count);
std::copy(PackData.begin() + RealReadSize, PackData.begin() + 4 + RealReadSize, Count);
int PackSize = ByteLittleToInt(Count);
delete[] Count;
// 如果包长度不够或者缓冲区长度直接返回
if (PackSize <= 0 || ((writeDataSize - receivedDataSize - 4) < PackSize))
if (PackSize <= 0 || ((RealSize - RealReadSize - 4) < PackSize))
return;
char *StrBuffer = new char[PackSize];
std::copy(PackData.begin() + 4 + receivedDataSize, PackData.begin() + 4 + PackSize + receivedDataSize, StrBuffer);
std::copy(PackData.begin() + 4 + RealReadSize, PackData.begin() + 4 + PackSize + RealReadSize, StrBuffer);
std::string Str(StrBuffer, PackSize);
delete[] StrBuffer;
// 包数据大小读取的偏移 这次读了多少
receivedDataSize += (4 + PackSize);
LogiThreadSize += (4 + PackSize);
// std::cout << "包大小: " << PackSize << std::endl;
// std::cout << "包内容: " << Str << std::endl;