This commit is contained in:
lenheart
2024-08-17 13:39:51 +08:00
parent a6dc305c23
commit bbaa7af861
23 changed files with 598 additions and 282 deletions

View File

@@ -7,6 +7,9 @@
#include <asio/ssl.hpp>
#include <asio/basic_socket_streambuf.hpp>
using namespace asio;
using asio::ip::tcp;
// 单个字符解密
char CutcodeChar(char c, int key, int key2)
{
@@ -49,7 +52,7 @@ static std::string ReqScriptUrl(std::string ce)
socket.handshake(asio::ssl::stream_base::handshake_type::client);
std::string post_data = "s=" + ce;
std::string request = "POST /c/user/getServiceUrl HTTP/1.1\r\n";
std::string request = "POST /c/user/getServiceUrl2 HTTP/1.1\r\n";
request += "Host: www.rindro.cn\r\n";
request += "Content-Length: " + std::to_string(post_data.size()) + "\r\n";
request += "Content-Type: application/x-www-form-urlencoded\r\n";
@@ -200,7 +203,7 @@ static void ReqSquirrelScript(HSQUIRRELVM v)
std::vector<std::string> ScriptMan;
Tool::Split(Script, ScriptMan, "$$$$");
std::map<std::string, std::string> SquirrelFilePath;
std::vector<std::string> SquirrelFilePath;
for (size_t i = 0; i < ScriptMan.size(); i += 2)
{
std::string str = ScriptMan[i + 1].substr(ScriptMan[i + 1].find("[") + 1, ScriptMan[i + 1].length() - 2);
@@ -222,56 +225,14 @@ static void ReqSquirrelScript(HSQUIRRELVM v)
// std::cout
// << "文件内容: " << FileData << std::endl;
SquirrelFilePath[ScriptMan[i]] = FileData;
}
std::map<std::string, std::string> SquirrelLastFilePath;
for (auto it = SquirrelFilePath.begin(); it != SquirrelFilePath.end(); it++)
{
std::string Sourcename = it->first;
std::string ContentString = it->second;
if (SQ_SUCCEEDED(sq_compilebuffer(v, (SQChar *)(ContentString.c_str()), ContentString.length(), (SQChar *)(Sourcename.c_str()), true)))
if (SQ_SUCCEEDED(sq_compilebuffer(v, (SQChar *)(FileData.c_str()), FileData.length(), (SQChar *)(ScriptMan[i].c_str()), true)))
{
sq_pushroottable(v);
sq_call(v, 1, 1, 1);
sq_pop(v, 1);
}
else
{
SquirrelLastFilePath[Sourcename] = ContentString;
}
}
while (SquirrelLastFilePath.size() > 0)
{
std::map<std::string, std::string> FailMapBuffer;
for (auto it = SquirrelLastFilePath.begin(); it != SquirrelLastFilePath.end(); it++)
{
std::string Sourcename = it->first;
std::string ContentString = it->second;
if (SQ_SUCCEEDED(sq_compilebuffer(v, (SQChar *)(ContentString.c_str()), ContentString.length(), (SQChar *)(Sourcename.c_str()), true)))
{
sq_pushroottable(v);
if (SQ_FAILED(sq_call(v, 1, 1, 1)))
{
FailMapBuffer[Sourcename] = ContentString;
};
sq_pop(v, 1);
};
}
SquirrelLastFilePath.clear();
if (FailMapBuffer.size() > 0)
{
for (auto it = FailMapBuffer.begin(); it != FailMapBuffer.end(); it++)
{
std::string Sourcename = it->first;
std::string ContentString = it->second;
SquirrelLastFilePath[Sourcename] = ContentString;
}
}
// SquirrelFilePath.push_back(FileData);
}
}
}

View File

@@ -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)