Compare commits
10 Commits
cc994a091b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
4b57324ca2
|
|||
|
096a13e1fb
|
|||
|
7988de8eab
|
|||
|
43601edebf
|
|||
|
e33b0e7e05
|
|||
|
327edb8299
|
|||
|
6cbd7febf8
|
|||
|
00b12c8266
|
|||
|
aaf3af6f83
|
|||
|
c80777f064
|
@@ -1,8 +1,28 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
project(Transmission)
|
project(Transmission)
|
||||||
|
|
||||||
|
if(NOT DEFINED TARGET_SYSTEM_NAME)
|
||||||
|
set(TARGET_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
|
||||||
|
message("使用默认目标 : ${TARGET_SYSTEM_NAME}")
|
||||||
|
endif ()
|
||||||
|
if (${TARGET_SYSTEM_NAME} STREQUAL "Linux")
|
||||||
|
add_definitions(-DBUILD_LINUX=1)
|
||||||
|
message("以Linux为构建目标")
|
||||||
|
elseif (${TARGET_SYSTEM_NAME} STREQUAL "Windows")
|
||||||
|
add_definitions(-DBUILD_WINDOWS=1)
|
||||||
|
message("以Windows为构建目标")
|
||||||
|
elseif(${TARGET_SYSTEM_NAME} STREQUAL "Android")
|
||||||
|
add_definitions(-DBUILD_ANDROID=1)
|
||||||
|
message("以Android为构建目标")
|
||||||
|
else ()
|
||||||
|
message(FATAL_ERROR "未知的目标操作系统")
|
||||||
|
endif ()
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
if (IS_TEST)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
|
message("包含Gtest目标")
|
||||||
|
endif ()
|
||||||
|
|
||||||
add_executable(exec main.cpp
|
add_executable(exec main.cpp
|
||||||
)
|
)
|
||||||
|
|||||||
+15
-1
@@ -21,4 +21,18 @@ add_library(Transmission
|
|||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(Transmission PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
|
target_include_directories(Transmission PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
target_link_libraries(Transmission protobuf proto lzma)
|
if(NOT DEFINED PROTOBUF_LIB_PATH)
|
||||||
|
set(PROTOBUF_LIB_PATH "protobuf")
|
||||||
|
else ()
|
||||||
|
message("手动指定protobuf库为${PROTOBUF_LIB_PATH}")
|
||||||
|
endif ()
|
||||||
|
if(DEFINED PROTOBUF_INCLUDE_PATH)
|
||||||
|
message("手动指定protobuf头文件路径为${PROTOBUF_INCLUDE_PATH}")
|
||||||
|
target_include_directories(proto PUBLIC ${PROTOBUF_INCLUDE_PATH})
|
||||||
|
endif ()
|
||||||
|
if(NOT DEFINED LZMA_LIB_PATH)
|
||||||
|
set(LZMA_LIB_PATH "lzma")
|
||||||
|
else ()
|
||||||
|
message("手动指定LZMA库为${LZMA_LIB_PATH}")
|
||||||
|
endif ()
|
||||||
|
target_link_libraries(Transmission ${PROTOBUF_LIB_PATH} proto ${LZMA_LIB_PATH})
|
||||||
+11
-10
@@ -13,12 +13,13 @@
|
|||||||
namespace Transmission {
|
namespace Transmission {
|
||||||
|
|
||||||
const int32_t Transmit::DATA_STOP = 0x20030507;
|
const int32_t Transmit::DATA_STOP = 0x20030507;
|
||||||
|
int Transmit::compressSize = 64;
|
||||||
|
|
||||||
Transmit::Transmit(SOCKET fd, std::string ip) : ip(std::move(ip)), fd(fd) {
|
Transmit::Transmit(SOCKET fd, std::string ip) : ip(std::move(ip)), fd(fd) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transmit::dataArrives(unsigned char *data, size_t size) {
|
void Transmit::dataArrives(const unsigned char *data, size_t size) {
|
||||||
std::unique_lock<std::mutex> lock(dataMutex);
|
std::unique_lock<std::mutex> lock(dataMutex);
|
||||||
charBuff.put(data, 0, size);
|
charBuff.put(data, 0, size);
|
||||||
if (this->packSize < 0) {
|
if (this->packSize < 0) {
|
||||||
@@ -51,7 +52,7 @@ namespace Transmission {
|
|||||||
if (!dataPackets.ParseFromArray(buff.get(), this->packSize))
|
if (!dataPackets.ParseFromArray(buff.get(), this->packSize))
|
||||||
throw PrepareDataException("反序列化出错");
|
throw PrepareDataException("反序列化出错");
|
||||||
this->packSize = -1;
|
this->packSize = -1;
|
||||||
switch (dataPackets.type()) {
|
switch (dataPackets.algorithm()) {
|
||||||
//无压缩
|
//无压缩
|
||||||
case CompressAlgorithm::NOT: {
|
case CompressAlgorithm::NOT: {
|
||||||
auto pack = copyMem(dataPackets.data());
|
auto pack = copyMem(dataPackets.data());
|
||||||
@@ -118,13 +119,13 @@ namespace Transmission {
|
|||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transmit::sendData(unsigned char *data, size_t size, int type) {
|
void Transmit::sendData(const unsigned char *data, size_t size, int type) {
|
||||||
DataPackets dataPackets;
|
DataPackets dataPackets;
|
||||||
dataPackets.set_type(type);
|
dataPackets.set_type(type);
|
||||||
std::string byteData(reinterpret_cast<char *>(data), size);
|
std::string byteData(reinterpret_cast<const char *>(data), size);
|
||||||
dataPackets.set_data(byteData);
|
dataPackets.set_data(byteData);
|
||||||
if (size < 64) {
|
if (size < compressSize) {
|
||||||
//小于64字节的数据包不压缩
|
//小于compressSize的数据包不压缩
|
||||||
pushData(dataPackets);
|
pushData(dataPackets);
|
||||||
} else {
|
} else {
|
||||||
//大于64字节的数据使用lzma压缩后发送
|
//大于64字节的数据使用lzma压缩后发送
|
||||||
@@ -132,10 +133,10 @@ namespace Transmission {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transmit::pushData(unsigned char *data, int32_t size) const {
|
void Transmit::pushData(const unsigned char *data, int32_t size) const {
|
||||||
::send(this->fd, &size, sizeof(int32_t), 0);
|
::send(this->fd, (const char *) &size, sizeof(int32_t), 0);
|
||||||
::send(this->fd, data, size, 0);
|
::send(this->fd, (const char *) data, size, 0);
|
||||||
::send(this->fd, &DATA_STOP, sizeof(DATA_STOP), 0);
|
::send(this->fd, (const char *) &DATA_STOP, sizeof(DATA_STOP), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transmit::pushData(DataPackets &data) {
|
void Transmit::pushData(DataPackets &data) {
|
||||||
|
|||||||
+20
-3
@@ -8,7 +8,22 @@
|
|||||||
#define TRANSMISSION_TRANSMIT_H
|
#define TRANSMISSION_TRANSMIT_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifdef BUILD_LINUX
|
||||||
|
|
||||||
#include <pcap/socket.h>
|
#include <pcap/socket.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#ifdef BUILD_WINDOWS
|
||||||
|
#include <winsock.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#endif
|
||||||
|
#ifdef BUILD_ANDROID
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#define SOCKET int
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "CharBuff.h"
|
#include "CharBuff.h"
|
||||||
#include "transmission.pb.h"
|
#include "transmission.pb.h"
|
||||||
@@ -33,7 +48,7 @@ namespace Transmission {
|
|||||||
void pushLzmaData(DataPackets &data);
|
void pushLzmaData(DataPackets &data);
|
||||||
|
|
||||||
/// 实际发送
|
/// 实际发送
|
||||||
virtual void pushData(unsigned char *data, int32_t size) const;
|
virtual void pushData(const unsigned char *data, int32_t size) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
@@ -43,15 +58,17 @@ namespace Transmission {
|
|||||||
virtual std::shared_ptr<unsigned char> unLzma(const std::string &str, size_t &size);
|
virtual std::shared_ptr<unsigned char> unLzma(const std::string &str, size_t &size);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
//最小压缩大小
|
||||||
|
static int compressSize;
|
||||||
static const int32_t DATA_STOP;
|
static const int32_t DATA_STOP;
|
||||||
|
|
||||||
explicit Transmit(SOCKET fd, std::string ip);
|
explicit Transmit(SOCKET fd, std::string ip);
|
||||||
|
|
||||||
/// 收到来自网络的数据
|
/// 收到来自网络的数据
|
||||||
virtual void dataArrives(unsigned char *data, size_t size);
|
virtual void dataArrives(const unsigned char *data, size_t size);
|
||||||
|
|
||||||
/// 发送数据
|
/// 发送数据
|
||||||
virtual void sendData(unsigned char *data, size_t size, int type = 0);
|
virtual void sendData(const unsigned char *data, size_t size, int type);
|
||||||
|
|
||||||
/// 数据包就绪
|
/// 数据包就绪
|
||||||
/// @param type 数据包类型
|
/// @param type 数据包类型
|
||||||
|
|||||||
+17
-2
@@ -9,6 +9,15 @@
|
|||||||
static bool isExec = false;
|
static bool isExec = false;
|
||||||
static const char *shortStr = "Client Hello";
|
static const char *shortStr = "Client Hello";
|
||||||
static const char *LongStr = "Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello";
|
static const char *LongStr = "Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello";
|
||||||
|
static const char *LongLongStr = "Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello"
|
||||||
|
"Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello";
|
||||||
|
|
||||||
class Transmit : public Transmission::Transmit {
|
class Transmit : public Transmission::Transmit {
|
||||||
private:
|
private:
|
||||||
@@ -23,12 +32,14 @@ public:
|
|||||||
ASSERT_TRUE(strcmp((char *) data.get(), shortStr) == 0);
|
ASSERT_TRUE(strcmp((char *) data.get(), shortStr) == 0);
|
||||||
else if (type == 2)
|
else if (type == 2)
|
||||||
ASSERT_TRUE(strcmp((char *) data.get(), LongStr) == 0);
|
ASSERT_TRUE(strcmp((char *) data.get(), LongStr) == 0);
|
||||||
else
|
else if (type == 3) {
|
||||||
|
ASSERT_TRUE(strcmp((char *) data.get(), LongLongStr) == 0);
|
||||||
|
} else
|
||||||
ASSERT_TRUE(false);
|
ASSERT_TRUE(false);
|
||||||
isExec = true;
|
isExec = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushData(unsigned char *data, int32_t size) const override {
|
void pushData(const unsigned char *data, int32_t size) const override {
|
||||||
::write(this->fd, &size, sizeof(int32_t));
|
::write(this->fd, &size, sizeof(int32_t));
|
||||||
::write(this->fd, data, size);
|
::write(this->fd, data, size);
|
||||||
::write(this->fd, &DATA_STOP, sizeof(DATA_STOP));
|
::write(this->fd, &DATA_STOP, sizeof(DATA_STOP));
|
||||||
@@ -62,4 +73,8 @@ TEST(Transmit测试, 1) {
|
|||||||
transmitWrite.sendData((unsigned char *) LongStr, strlen(LongStr) + 1, 2);
|
transmitWrite.sendData((unsigned char *) LongStr, strlen(LongStr) + 1, 2);
|
||||||
transmitRead.read();
|
transmitRead.read();
|
||||||
ASSERT_TRUE(isExec);
|
ASSERT_TRUE(isExec);
|
||||||
|
isExec = false;
|
||||||
|
transmitWrite.sendData((unsigned char *) LongLongStr, strlen(LongLongStr) + 1, 3);
|
||||||
|
transmitRead.read();
|
||||||
|
ASSERT_TRUE(isExec);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user