1212
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
#include "l_squirrel.h"
|
||||
#include <iostream>
|
||||
#include <asio.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
|
||||
extern HSQUIRRELVM v;
|
||||
extern std::recursive_mutex SqMtx;
|
||||
@@ -21,7 +24,6 @@ private:
|
||||
std::array<char, 8192000> PackData;
|
||||
std::size_t writeDataSize = 0;
|
||||
std::size_t receivedDataSize = 0;
|
||||
std::size_t TestCode = 0;
|
||||
std::string Ip = "192.168.200.27";
|
||||
// std::string Ip = "127.0.0.1";
|
||||
std::string Port = "65109";
|
||||
@@ -30,6 +32,8 @@ private:
|
||||
public:
|
||||
bool ConnectState = false;
|
||||
|
||||
std::fstream file;
|
||||
|
||||
public:
|
||||
Client(asio::io_context &io_context, std::string Ip, std::string Port)
|
||||
: resolver(io_context),
|
||||
@@ -61,6 +65,7 @@ public:
|
||||
io_context.poll(); // 处理一次事件循环,避免主线程阻塞
|
||||
} else {
|
||||
std::cerr << "Error connecting to server: " << error.message() << std::endl;
|
||||
start();
|
||||
} });
|
||||
}
|
||||
|
||||
@@ -69,14 +74,14 @@ public:
|
||||
socket.async_read_some(asio::buffer(buffer), [this](const asio::error_code &error, std::size_t bytes_transferred)
|
||||
{
|
||||
if (!error) {
|
||||
PackMtx.lock();
|
||||
for (std::size_t i = 0; i < bytes_transferred; ++i)
|
||||
{
|
||||
PackData[writeDataSize + i] = buffer[i];
|
||||
}
|
||||
writeDataSize += bytes_transferred;
|
||||
PackMtx.unlock();
|
||||
read();
|
||||
PackMtx.lock();
|
||||
for (std::size_t i = 0; i < bytes_transferred; ++i)
|
||||
{
|
||||
PackData[writeDataSize + i] = buffer[i];
|
||||
}
|
||||
writeDataSize += bytes_transferred;
|
||||
PackMtx.unlock();
|
||||
read();
|
||||
} else {
|
||||
std::cerr << "Error reading data: " << error.message() << std::endl;
|
||||
reconnect();
|
||||
@@ -130,17 +135,15 @@ public:
|
||||
void PackLogic()
|
||||
{
|
||||
// 如果包长度不够直接返回
|
||||
if (PackData.size() < 4)
|
||||
if (writeDataSize < 4)
|
||||
return;
|
||||
// 如果里面已经读了超过一半的大小了
|
||||
if (writeDataSize >= 40960)
|
||||
if (writeDataSize >= 8192)
|
||||
{
|
||||
std::cout << "清空一次二级缓存" << std::endl;
|
||||
std::cout << "清空一次二级缓存" << std::endl;
|
||||
PackMtx.lock();
|
||||
// 删除前XX个元素
|
||||
// 复制已读大小开始到结束到 开始 作用删除前已读大小的数据
|
||||
std::copy(PackData.begin() + receivedDataSize, PackData.end(), PackData.begin());
|
||||
// 将最后16个元素设置为0
|
||||
// 将最后元素设置为0
|
||||
std::fill(PackData.end() - receivedDataSize, PackData.end(), 0);
|
||||
// 写的大小重置
|
||||
writeDataSize -= receivedDataSize;
|
||||
@@ -150,23 +153,27 @@ public:
|
||||
}
|
||||
|
||||
unsigned char *Count = new unsigned char[4];
|
||||
|
||||
std::copy(PackData.begin() + receivedDataSize, PackData.begin() + 4 + receivedDataSize, Count);
|
||||
|
||||
int PackSize = ByteLittleToInt(Count);
|
||||
delete[] Count;
|
||||
// 如果长度不够直接返回
|
||||
if (PackSize <= 0)
|
||||
// 如果包长度不够或者缓冲区长度直接返回
|
||||
if (PackSize <= 0 || ((writeDataSize - receivedDataSize - 4) < PackSize))
|
||||
return;
|
||||
char *StrBuffer = new char[PackSize + 1];
|
||||
char *StrBuffer = new char[PackSize];
|
||||
|
||||
std::copy(PackData.begin() + 4 + receivedDataSize, PackData.begin() + 4 + PackSize + receivedDataSize, StrBuffer);
|
||||
StrBuffer[PackSize] = '\0';
|
||||
// 这次读了多少
|
||||
std::string Str(StrBuffer, PackSize);
|
||||
delete[] StrBuffer;
|
||||
|
||||
// 包数据大小读取的偏移 这次读了多少
|
||||
receivedDataSize += (4 + PackSize);
|
||||
|
||||
std::string Str = StrBuffer;
|
||||
delete[] StrBuffer;
|
||||
// std::cout << "包大小: " << PackSize << std::endl;
|
||||
// std::cout << "包内容: " << Str << std::endl;
|
||||
// std::cout << "收到了第: " << TestCode << "个包" << std::endl;
|
||||
// spdlog::info(Str);
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(SqMtx);
|
||||
SQInteger top = sq_gettop(v); // saves the stack size before the call
|
||||
@@ -175,12 +182,11 @@ public:
|
||||
if (SQ_SUCCEEDED(sq_get(v, -2)))
|
||||
{ // gets the field 'foo' from the global table
|
||||
sq_pushroottable(v); // push the 'this' (in this case is the global table)
|
||||
sq_pushstring(v, Str.c_str(), -1);
|
||||
sq_pushstring(v, Str.c_str(), PackSize);
|
||||
sq_call(v, 2, SQFalse, SQTrue); // calls the function
|
||||
}
|
||||
sq_settop(v, top); // restores the original stack size
|
||||
|
||||
TestCode++;
|
||||
// TestCode++;
|
||||
}
|
||||
|
||||
static int ByteLittleToInt(unsigned char *Count)
|
||||
|
||||
Reference in New Issue
Block a user