110 lines
2.4 KiB
Plaintext
110 lines
2.4 KiB
Plaintext
|
|
{# TCP接收器模板 #}
|
|||
|
|
{% if part == 'header' %}
|
|||
|
|
#pragma once
|
|||
|
|
|
|||
|
|
#include <vector>
|
|||
|
|
#include <cstdint>
|
|||
|
|
#include <string>
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* TCP数据接收器
|
|||
|
|
* 用于接收{{ source_message.full_name }}消息
|
|||
|
|
*/
|
|||
|
|
class DataReceiver {
|
|||
|
|
public:
|
|||
|
|
DataReceiver();
|
|||
|
|
~DataReceiver();
|
|||
|
|
|
|||
|
|
// 初始化TCP连接
|
|||
|
|
bool initialize(const std::string& host, int port);
|
|||
|
|
|
|||
|
|
// 接收数据
|
|||
|
|
std::vector<uint8_t> receive();
|
|||
|
|
|
|||
|
|
// 关闭连接
|
|||
|
|
void close();
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
int socket_fd_;
|
|||
|
|
bool connected_;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
{% else %}
|
|||
|
|
#include "receiver.h"
|
|||
|
|
#include <sys/socket.h>
|
|||
|
|
#include <netinet/in.h>
|
|||
|
|
#include <arpa/inet.h>
|
|||
|
|
#include <unistd.h>
|
|||
|
|
#include <cstring>
|
|||
|
|
#include <stdexcept>
|
|||
|
|
|
|||
|
|
DataReceiver::DataReceiver()
|
|||
|
|
: socket_fd_(-1), connected_(false) {
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DataReceiver::~DataReceiver() {
|
|||
|
|
close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool DataReceiver::initialize(const std::string& host, int port) {
|
|||
|
|
// 创建socket
|
|||
|
|
socket_fd_ = socket(AF_INET, SOCK_STREAM, 0);
|
|||
|
|
if (socket_fd_ < 0) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 设置服务器地址
|
|||
|
|
struct sockaddr_in server_addr;
|
|||
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|||
|
|
server_addr.sin_family = AF_INET;
|
|||
|
|
server_addr.sin_port = htons(port);
|
|||
|
|
inet_pton(AF_INET, host.c_str(), &server_addr.sin_addr);
|
|||
|
|
|
|||
|
|
// 连接服务器
|
|||
|
|
if (connect(socket_fd_, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
|
|||
|
|
::close(socket_fd_);
|
|||
|
|
socket_fd_ = -1;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
connected_ = true;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::vector<uint8_t> DataReceiver::receive() {
|
|||
|
|
if (!connected_) {
|
|||
|
|
throw std::runtime_error("Not connected");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 先接收数据长度(4字节)
|
|||
|
|
uint32_t data_length = 0;
|
|||
|
|
ssize_t n = recv(socket_fd_, &data_length, sizeof(data_length), 0);
|
|||
|
|
if (n != sizeof(data_length)) {
|
|||
|
|
throw std::runtime_error("Failed to receive data length");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 接收数据
|
|||
|
|
std::vector<uint8_t> buffer(data_length);
|
|||
|
|
size_t total_received = 0;
|
|||
|
|
|
|||
|
|
while (total_received < data_length) {
|
|||
|
|
n = recv(socket_fd_, buffer.data() + total_received,
|
|||
|
|
data_length - total_received, 0);
|
|||
|
|
if (n <= 0) {
|
|||
|
|
throw std::runtime_error("Failed to receive data");
|
|||
|
|
}
|
|||
|
|
total_received += n;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return buffer;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void DataReceiver::close() {
|
|||
|
|
if (socket_fd_ >= 0) {
|
|||
|
|
::close(socket_fd_);
|
|||
|
|
socket_fd_ = -1;
|
|||
|
|
}
|
|||
|
|
connected_ = false;
|
|||
|
|
}
|
|||
|
|
{% endif %}
|