This commit is contained in:
2025-10-11 23:37:00 +08:00
parent d32f0e9f90
commit 5f768f2cdc
2 changed files with 202 additions and 5 deletions

View File

@@ -210,6 +210,123 @@ namespace QCL
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TcpClient::TcpClient(const std::string &serverIP, int serverPort)
: serverIP_(serverIP), serverPort_(serverPort), clientSock_(-1),
connected_(false), running_(false)
{
}
TcpClient::~TcpClient()
{
disconnect();
}
bool TcpClient::connectToServer()
{
std::lock_guard<std::mutex> lock(socketMutex_);
clientSock_ = socket(AF_INET, SOCK_STREAM, 0);
if (clientSock_ < 0)
{
perror("Socket creation failed");
return false;
}
sockaddr_in serverAddr{};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(serverPort_);
if (inet_pton(AF_INET, serverIP_.c_str(), &serverAddr.sin_addr) <= 0)
{
perror("Invalid address");
close(clientSock_);
return false;
}
if (connect(clientSock_, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
perror("Connection failed");
close(clientSock_);
return false;
}
connected_ = true;
running_ = true;
std::cout << "Connected to server " << serverIP_ << ":" << serverPort_ << std::endl;
return true;
}
void TcpClient::disconnect()
{
std::lock_guard<std::mutex> lock(socketMutex_);
running_ = false;
if (connected_)
{
close(clientSock_);
connected_ = false;
std::cout << "Disconnected from server" << std::endl;
}
}
void TcpClient::sendToServer(const std::string &message)
{
std::lock_guard<std::mutex> lock(socketMutex_);
if (!connected_)
return;
ssize_t bytesSent = send(clientSock_, message.c_str(), message.size(), 0);
if (bytesSent <= 0)
{
perror("Send failed");
connected_ = false;
}
}
std::string TcpClient::receiveFromServer(bool flag)
{
std::lock_guard<std::mutex> lock(socketMutex_);
if (!connected_)
return "";
char buffer[1024] = {0};
int flags = flag ? 0 : MSG_DONTWAIT;
ssize_t bytesRead = recv(clientSock_, buffer, sizeof(buffer) - 1, flags);
if (bytesRead <= 0)
{
if (bytesRead < 0 && errno == EAGAIN)
return ""; // 非阻塞下无数据
connected_ = false;
perror("Receive failed");
return "";
}
return std::string(buffer, bytesRead);
}
std::string TcpClient::getServerIPAndPort() const
{
return serverIP_ + ":" + std::to_string(serverPort_);
}
bool TcpClient::isConnected() const
{
return connected_;
}
void TcpClient::reconnectLoop(int retryIntervalMs)
{
while (running_)
{
if (!connected_)
{
std::cout << "Attempting to reconnect..." << std::endl;
if (connectToServer())
{
std::cout << "Reconnected successfully!" << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(retryIntervalMs));
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WriteFile::WriteFile(const std::string &filePath)
: filePath_(filePath) {}
@@ -314,7 +431,7 @@ namespace QCL
return 0;
}
bool WriteFile::writeAfterPatternOrAppend(const std::string &pattern, const std::string &content)
bool WriteFile::writeAfterPatternOrAppend(const std::string &pattern, const std::string &content)
{
std::lock_guard<std::mutex> lock(writeMutex_);
@@ -507,16 +624,19 @@ namespace QCL
// return lines;
std::lock_guard<std::mutex> lock(mtx_);
if (!file_.is_open()) {
if (!file_.is_open())
{
file_.open(filename_, std::ios::in | std::ios::binary);
if (!file_.is_open()) return {};
if (!file_.is_open())
return {};
}
file_.clear();
file_.seekg(0, std::ios::beg);
std::vector<std::string> lines;
std::string line;
while (std::getline(file_, line)) lines.push_back(line);
while (std::getline(file_, line))
lines.push_back(line);
return lines;
}