Pre Merge pull request !1 from chenhaijian/master

This commit is contained in:
chenhaijian 2024-08-13 07:48:36 +00:00 committed by Gitee
commit 32ca3f3301
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
35 changed files with 1451 additions and 935 deletions

84
Environmental.md Normal file
View File

@ -0,0 +1,84 @@
# 前期需要下载的软件
* VMware Workstration
* git bash
* vscode
* Beyond Compare
* MoBaXterm
# 前期快速上手
* git的基本操作我们主要使用git进行代码管理
* Linux的基本操作主要是命令行操作了解常用的命令了解怎么使用命令行管理文件
//csdn相关文章写得很详尽搭配上ai工具边用边学可以很快地熟悉常用的指令
# 前期需要准备的基本环境
1. 在VMware Workstation中安装Ubuntu安装好gcc和g++编译器,并配置好网络(能正常上网)
2. 学会vscode用ssh远程连接Ubuntu下载CMake、CMake Tools、Baidu Comateai工具对后续学习有比较大的帮助等基本插件
3. 创建一个gitee账号
# 编译过程
1. 将gitee上的源码文件保存到虚拟机中
2. 依照顺序执行以下命令
* 安装libssl-dev软件包
```
sudo apt-get update
sudo apt-get install openssl
sudo apt-get install libssl-dev
```
* 安装LDAP软件包
```
sudo apt-get update
sudo apt-get install libldap2-dev
```
* 安装cmakellvm使用cmake编译cmake版本要求 3.20以上此处安装cmake-3.27.4
cmake源码压缩包所在的路径embedded-framework/tools/cmake/cmake-3.27.4.tar.gz
在embedded-framework/tools/cmake目录下执行以下指令来安装cmake-3.27.4
```
tar zxvf cmake-3.27.4.tar.gz
cd cmake-3.27.4
sudo apt-get install openssl // 如果执行./bootstrap提示缺少ssl相关资源执行此安装命令
./bootstrap
make
sudo make install
cmake --version //查看版本,确认安装完成
```
* 安装llvm
```
// 下载源码
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm
make -j8 //这个过程会有点久
find ./ -name clang-tidy //查看是否有clang-tidy的文件
clang-tidy --version //查看版本,确认编译完成
```
3. 进行编译在embedded-framework目录下执行以下命令
```
make clean //CmakeLists.txt是生成Makefile文件的构建脚本如果CmakeLists.txt被修改需要重新执行make clean清除之前的构建文件
make cmake //构建源码生成Makefile文件
cd cmake-shell //在中间文件目录进行编译,把所有中间文件创建在此
make //编译全部输出构建文件
```
# 问题汇总
1. gcc编译器版本过低后续编译会出bug亲测gcc 7.5.0版本不能用可以使用gcc --version命令查看自己gcc的版本版本过低需要更新版本可以自行搜索怎么更新也可以参考https://blog.csdn.net/weixin_43354152/article/details/129247408
2. 下载llvm源码时git clone https://github.com/llvm/llvm-project.git 这个过程可能会经常报网络错误多试几次可以用网易uu加速器的学术资源加速可能会好下一点
3. 编译llvm过程可能会多次卡顿退出可能的原因有磁盘空间不足、编译器版本过低、缺少依赖等。可以尝试多线程编译make -j8

3
bug.md Normal file
View File

@ -0,0 +1,3 @@
# 仓库中隐藏的bug
1. “小”指针强制转换成“大”指针,可能造成内存的非法操作
* embedded.framework/utils/confaBase /src/ConfigBaselmpl.cpp

View File

@ -16,6 +16,7 @@
#define I_FILES_MANAGER_H #define I_FILES_MANAGER_H
#include "StatusCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
#include <string>
typedef struct save_file_info typedef struct save_file_info
{ {
save_file_info(const std::string &fileName); save_file_info(const std::string &fileName);

View File

@ -17,6 +17,7 @@
#include "StatusCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <string>
bool CreateMediaManagerModule(void); bool CreateMediaManagerModule(void);
bool DestroyMediaManagerModule(void); bool DestroyMediaManagerModule(void);
enum class MediaChannel enum class MediaChannel

View File

@ -9,8 +9,10 @@ include_directories(
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
) )
link_directories( link_directories(
${LIBS_OUTPUT_PATH} ${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH} ${EXTERNAL_LIBS_OUTPUT_PATH}

View File

@ -1,3 +1,4 @@
#include "ILog.h" #include "ILog.h"
// #include <gmock/gmock.h> // #include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -6,6 +7,7 @@ namespace ILogTest
// ../output_files/test/bin/LogTest --gtest_filter=ILogTest.Demo // ../output_files/test/bin/LogTest --gtest_filter=ILogTest.Demo
TEST(ILogTest, Demo) TEST(ILogTest, Demo)
{ {
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
LogInfo("hello world."); LogInfo("hello world.");

View File

@ -16,9 +16,11 @@
#define CONFIG_BASE_H #define CONFIG_BASE_H
#include "StatusCode.h" #include "StatusCode.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" //The declaration is to allow the C++compiler to process code surrounded by extern "C" in the same way as the C language. This is necessary
{
#endif #endif
enum CONFIG_CODE //#The three preprocessing instructions ifndef VNet BASE.H, # define VNet BASE.H, and # endif work together to ensure that the header file VNet BASE.H is only included once in any compilation unit. This is achieved by defining a unique macro (VNet BASE_H), skipping the file content if the macro has already been defined.
enum CONFIG_CODE//Defined an enumeration type VNet CODE for possible configuration error codes. It starts with VNet CODE-IND (assuming VNet CODE-IND is a status code defined in Status Code. h, indicating the end of a set of status codes), and defines a VNet CODE-IND as the end flag for enumeration.
{ {
CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END,
CONFIG_CODE_END CONFIG_CODE_END
@ -46,7 +48,9 @@ StatusCode ConfigGetDouble(void *object, const char *name, double *value);
StatusCode ConfigSetDouble(void *object, const char *name, const double value); StatusCode ConfigSetDouble(void *object, const char *name, const double value);
StatusCode ConfigGetString(void *object, const char *name, const char **value); StatusCode ConfigGetString(void *object, const char *name, const char **value);
StatusCode ConfigSetString(void *object, const char *name, const char *value); StatusCode ConfigSetString(void *object, const char *name, const char *value);
//Provides a series of functions for opening, saving, closing configuration files, as well as obtaining and setting values for different data types (such as integer, short integer, long integer, long integer, character, boolean, floating-point, double precision floating-point, and string) in the configuration file. Most of these functions accept a void * object as the first parameter, which is typically a pointer to the configuration file context or object used to access and manipulate the configuration file. Other parameters include the name of the configuration item (const char * name) and a pointer to the variable used to store the retrieved value (such as int * value).
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif
//Overall, this header file defines a configuration management interface that allows developers to read and write configuration file values in a type safe manner, while considering interoperability between C and C++.

View File

@ -0,0 +1,34 @@
# CONFIGBASE
#涉及的知识
----------------------------------------------------------------
1.CMake
2.C/C++
3.配置文件
4.状态码
5.libconfig库
### 各文件的作用
*1.conffig_base.cmake:定义了一个名为config_owner的函数该函数旨在为一个特定的CMake目标比如一个可执行文件或库的所有源文件添加一个新的编译定义COMPILE_DEFINITIONS。在这个上下文中编译定义通常用于在编译时向源代码中注入宏定义这些宏定义可以在源代码中被用作条件编译的开关。*
*2.ConfigBase.h:是一个C/C++兼容的头文件CONFIG_BASE_H它定义了一个配置管理的基础接口。这个接口提供了打开、保存、关闭配置文件以及获取和设置配置文件中不同类型数据的功能。*
*3.ConfigBase.cpp:实现了一个配置文件管理器的接口封装,它提供了一系列用于打开、关闭、保存配置文件以及获取和设置配置文件中的不同类型配置项的功能。*
*4.ConfigBaseCode.c:涉及到一个用于处理配置代码ConfigCode的系统这些配置代码似乎是从一个更大的状态码StatusCode系统中派生出来的。代码依赖于几个自定义的类型和函数包括StatusCode结构体、ILog接口通过LogInfo和LogError宏或函数实现、以及ConfigBase和ConfigBaseCode。*
*5.ConfigBaseCode.h:用于定义与配置代码相关的接口。它采用了C和C++的混合编程方式,并通过预处理器指令来控制代码的包含和编译。*
*6.ConfigBaselmpl.cpp:定义了一个名为ConfigBaseImpl的类它用于处理基于文本的配置文件如INI或类似格式的文件使用libconfig库来实现这一功能。libconfig是一个用于处理结构化配置文件的C库它支持读取和写入配置文件。ConfigBaseImpl类封装了libconfig的基本操作如打开、关闭配置文件以及读取和写入配置项的值。*
*7.ConfigBaselmpl.h:定义了一个名为 ConfigBaseImpl 的类,它实现了 IConfigBase 接口,用于处理配置文件。它使用了 libconfig 库来解析和生成配置文件。*
*8.lConfigBase.cpp:定义了一个名为 IConfigBase 的接口类,用于配置文件的操作,包括打开、关闭、读取和设置不同类型的配置值(如整数、短整数、长整数、长长整数、字符、布尔值、浮点数、双精度浮点数和字符串)。然而,这些函数的具体实现在 IConfigBase 类中都被设置为返回特定的状态码 STATUS_CODE_VIRTUAL_FUNCTION这通常表示该函数是虚函数需要在派生类中具体实现。*
*9.lConfigBase.h:定义了一个C++接口IConfigBase和一些相关的类型及函数声明主要用于配置文件的操作。*
*10.CMakeLists.txt:是一个CMake脚本用于配置和编译一个静态库项目名为ConfigBase并包含了一些额外的自定义目标和依赖管理。*

View File

@ -12,18 +12,24 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
//Copyright Statement and License: The beginning of the file contains a copyright statement and license information, indicating that this code is copyrighted by Fancy Code and follows the terms of Apache License, Version 2.0. This means that using this code must comply with the Apache 2.0 license regulations
#ifndef CONFIG_BASE_CODE_H #ifndef CONFIG_BASE_CODE_H
#define CONFIG_BASE_CODE_H #define CONFIG_BASE_CODE_H
//#The three pieces of code, ifndef CONFIG_SBASE-CEDE.H, # define CONFIG_SBASE-CEDE.H, and # endif, are standard header file protectors (also known as inclusion guards) used to prevent header files from being duplicated.
#include "ConfigBase.h" #include "ConfigBase.h"
#include "StatusCode.h" #include "StatusCode.h"
//These two lines of code contain two other header files that may define the Config Base class and Status Code enumeration or class, which will be used in the current header file.
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
//It is to ensure that the code written in C language can be linked and used correctly when used in the C++environment. Extern "C" tells the C++compiler that this part of the code should be compiled and linked according to the rules of the C language to avoid the problem of name mangling in C++.
#ifndef CONFIG_OWNER #ifndef CONFIG_OWNER
#error This is internal file, never include it. #error This is internal file, never include it.
// These two lines of code are a compile time check. If the VNet OWNER macro is not defined, the compiler will report an error and display the message 'This is internal file, never include it.'. This is usually used to prevent this header file from being directly included externally, possibly because it contains some definitions or declarations that should only be used internally within the library.
#endif #endif
const StatusCode CreateConfigCode(const long int code); const StatusCode CreateConfigCode(const long int code);// This line of code declares a function called CreateConfig Code, which takes a parameter code of type long int and returns a value of type Status Code. This function may be used to create a configuration object or perform a configuration operation based on a given code (possibly some configuration identifier), and return a status code to indicate the success or failure of the operation.
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif
//In summary, this header file defines a function declaration related to the configuration code, and ensures the correctness and security of the code through a series of conditional compilations and header file protectors.

View File

@ -14,16 +14,18 @@
*/ */
#ifndef CONFIG_BASE_IMPL_H #ifndef CONFIG_BASE_IMPL_H
#define CONFIG_BASE_IMPL_H #define CONFIG_BASE_IMPL_H
#include "IConfigBase.h" //Standard header file protection is used to prevent duplicate header files from being included.
#include <libconfig.h> #include "IConfigBase.h"//This is an interface definition file, and the Config Base Impl class implements all the pure virtual functions declared in this interface.
class ConfigBaseImpl : public IConfigBase #include <libconfig.h>//This is the header file of the libconfig library, which provides an API for handling configuration files.
#include <string>//String processing class in the C++standard library.
class ConfigBaseImpl : public IConfigBase//The Config Base Impl class inherits from the FHIR figBase interface, which means it needs to implement all the pure virtual functions declared in the interface.
{ {
public: public:
ConfigBaseImpl(const std::string &fileName); ConfigBaseImpl(const std::string &fileName);
virtual ~ConfigBaseImpl() = default; virtual ~ConfigBaseImpl() = default;
bool OpenConfigFile(void) override; bool OpenConfigFile(void) override;//Open configuration file
void CloseConfigFile(void) override; void CloseConfigFile(void) override;//Close configuration file
StatusCode ConfigSaveFile(void) override; StatusCode ConfigSaveFile(void) override;//Used to save configuration changes back to a file, but the return type is Status Code
StatusCode ConfigGetInt(const char *name, int *value) override; StatusCode ConfigGetInt(const char *name, int *value) override;
StatusCode ConfigSetInt(const char *name, const int value) override; StatusCode ConfigSetInt(const char *name, const int value) override;
StatusCode ConfigGetShort(const char *name, short *value) override; StatusCode ConfigGetShort(const char *name, short *value) override;
@ -42,9 +44,10 @@ public:
StatusCode ConfigSetDouble(const char *name, const double value) override; StatusCode ConfigSetDouble(const char *name, const double value) override;
StatusCode ConfigGetString(const char *name, const char **value) override; StatusCode ConfigGetString(const char *name, const char **value) override;
StatusCode ConfigSetString(const char *name, const char *value) override; StatusCode ConfigSetString(const char *name, const char *value) override;
//These functions provide interfaces for obtaining and setting various data types (such as integers, floating-point numbers, strings, etc.) from configuration files. They accept the name of a configuration item and a pointer to the value (for Get functions) or the value itself (for Set functions), and return a Status Code representing the operation result.
private: private:
const std::string mFileName; const std::string mFileName;// The path to store configuration files.
config_t mCfg; config_t mCfg;//The confic_t type in the libconfig library is used to represent the internal structure of configuration files.
}; };
#endif #endif
//In summary, this code defines a class for handling configuration files that provides rich interfaces to read and modify configuration items in the configuration file, achieved through the use of the libconfig library.

View File

@ -14,9 +14,11 @@
*/ */
#ifndef I_CONFIG_BASE_H #ifndef I_CONFIG_BASE_H
#define I_CONFIG_BASE_H #define I_CONFIG_BASE_H
//Header file protection (preventing header files from being duplicated).
#include "StatusCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
class IConfigBase //Status Code. h (which may be a file that defines a status code enumeration or class to indicate the status of successful or failed operations) and<memory>(a part of the C++standard library that provides memory management features such as smart pointers).
class IConfigBase// FHIR gBase interface
{ {
public: public:
IConfigBase() = default; IConfigBase() = default;
@ -43,15 +45,18 @@ public:
virtual StatusCode ConfigGetString(const char *name, const char **value); virtual StatusCode ConfigGetString(const char *name, const char **value);
virtual StatusCode ConfigSetString(const char *name, const char *value); virtual StatusCode ConfigSetString(const char *name, const char *value);
}; };
typedef struct i_config_base_header //IKON Base is an abstract base class that defines interfaces related to configuration management. It includes default implementations of constructors and destructors (i.e. do not perform any operations), as well as a series of virtual functions for opening and closing configuration files, saving configurations, and obtaining and setting configuration values for various data types. The specific implementation of these virtual functions will be provided by derived classes.
typedef struct i_config_base_header//Defined a struct i_config.base_header containing a pointer mCheckName to a character, which may be used for some form of validation or recognition.
{ {
const char *mCheckName; const char *mCheckName;
} IConfigBaseHeader; } IConfigBaseHeader;
typedef struct config_base typedef struct config_base//Defined a struct config.base that contains a member mHeader of type iKON figBaseHeader and a smart pointer mIConfig Base pointing to iKON figBase. This structure may be used to store and manage configuration instances in applications.
{ {
IConfigBaseHeader mHeader; IConfigBaseHeader mHeader;
std::shared_ptr<IConfigBase> mIConfigBase; std::shared_ptr<IConfigBase> mIConfigBase;
} ConfigBase; } ConfigBase;
const char *GetConfigBaseModuleName(void); const char *GetConfigBaseModuleName(void);
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName); std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName);
//Declared two functions: FHIR nfigBaseModulus Name (which may be used to obtain the name of a configuration module) and NewConfig Base (which takes a file name as a parameter and returns a pointer to the FHIR figBase smart pointer for creating and returning a new configuration instance).
#endif #endif
//This code defines a C++interface and related data structures for configuration management, as well as function declarations related to it. It utilizes the abstract base classes, virtual functions, smart pointers, and other features of C++to achieve flexible configuration management functionality.

View File

@ -54,4 +54,24 @@ function(log_disable target)
PROPERTY COMPILE_DEFINITIONS ${defs}) PROPERTY COMPILE_DEFINITIONS ${defs})
endforeach() endforeach()
endfunction() endfunction()
...
``` ```
## 1.4. 各文件的作用
* **easyloggingpp**在Log目录下发挥了日志记录、存储、管理的重要作用为C++项目的开发和维护提供了强有力的支持。
* **lLogMakePtr.h**:提供了一个日志实例创建和管理的机制,可能是作为应用程序中日志系统的一部分。通过使用单例模式和虚函数,它允许在应用程序的不同部分之间共享和交换日志实现,同时保持对日志系统底层实现的抽象。
* **lLogMakePtr.cpp**: 提供了一个日志模块的创建和销毁机制,以及基于不同实现的日志对象的创建方法。
* **Log.h**: 提供了一个C语言兼容的日志接口允许C或C++代码以统一的方式使用日志功能。
* **Log.cpp**: 提供了一个C兼容的日志接口实现该实现基于C++的 ILogCpp 类来处理实际的日志记录。它允许C或C++代码使用统一的日志接口,而无需关心底层的日志实现细节。
* **LogEasylogging.h**: 为基于 Easylogging++ 的日志系统提供了一个C++接口实现。通过继承 ILogCpp 接口并实现其中的函数,它使得日志功能的使用者能够以一种统一和标准化的方式记录和管理日志,而无需关心底层的日志实现细节。
* **LogEasylogging.cpp**: 通过实现 LogEasylogging 类,为应用程序提供了一个基于 Easylogging++ 库的日志系统,支持不同级别的日志记录,并允许通过配置文件来定制日志的行为。
* **Loglmpl.h**: 提供了一个用于扩展和实现具体日志功能的基类,同时保持了与 ILogCpp 接口的兼容性。
* **Loglmpl.cpp**: 为空。

View File

@ -16,6 +16,11 @@
#define ILOGCPP_H #define ILOGCPP_H
#include "ILog.h" #include "ILog.h"
#include <memory> #include <memory>
/**
* @brief Abstract interface class
* If you want to add additional functions, you can derive a class from it to
* meet the product requirements.
*/
class ILogCpp class ILogCpp
{ {
public: public:

View File

@ -12,6 +12,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/**
* @brief Create a log instance
*
*/
#ifndef ILOG_MAKE_PTR_H #ifndef ILOG_MAKE_PTR_H
#define ILOG_MAKE_PTR_H #define ILOG_MAKE_PTR_H
#include "ILogCpp.h" #include "ILogCpp.h"

View File

@ -19,6 +19,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <easylogging++.h>
/**
* @brief jieshao
*
* @param object
*/
static void LogFree(ILog *object) static void LogFree(ILog *object)
{ {
printf("log instance free.\n"); printf("log instance free.\n");
@ -29,6 +35,23 @@ static void LogFree(ILog *object)
static int LogPrintf(ILog *object, const char *function, const int line, const enum LogType type, const char *format, static int LogPrintf(ILog *object, const char *function, const int line, const enum LogType type, const char *format,
...) ...)
{ {
el::Configurations defaultConf;
defaultConf.setToDefault();
defaultConf.set(el::Level::Info,
el::ConfigurationType::Format,
"\x1b[32m%datetime %level %msg\x1b[0m");
defaultConf.set(el::Level::Debug,
el::ConfigurationType::Format,
"\x1b[33m%datetime %level %msg\x1b[0m");
defaultConf.set(el::Level::Error,
el::ConfigurationType::Format,
"\x1b[31m%datetime %level %msg\x1b[0m");
el::Loggers::reconfigureLogger("default", defaultConf);
// TODO: // TODO:
// LogTypeToString(type); // LogTypeToString(type);
constexpr int SEND_TRACE_BUFF_SIZE = 2048; constexpr int SEND_TRACE_BUFF_SIZE = 2048;

View File

@ -19,6 +19,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <string>
void *ICreateMediaBase(const MediaHandleType type) void *ICreateMediaBase(const MediaHandleType type)
{ {
return NewIMediaBase(type); return NewIMediaBase(type);

View File

@ -81,7 +81,7 @@ add_custom_target(
curl curl
DEPENDS openssl DEPENDS openssl
COMMAND echo "Build curl. openssl path = ${EXTERNAL_SOURCE_PATH}" COMMAND echo "Build curl. openssl path = ${EXTERNAL_SOURCE_PATH}"
COMMAND ./configure --without-zlib --prefix=${EXTERNAL_SOURCE_PATH}/curl --with-ssl=${EXTERNAL_SOURCE_PATH}/openssl/build ${CURL_HOST} CC=${CMAKE_C_COMPILER} COMMAND ./configure --disable-shared --without-zlib --prefix=${EXTERNAL_SOURCE_PATH}/curl --with-ssl=${EXTERNAL_SOURCE_PATH}/openssl/build ${CURL_HOST} CC=${CMAKE_C_COMPILER}
COMMAND make COMMAND make
COMMAND cp ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/lib*.a ${LIBS_OUTPUT_PATH} COMMAND cp ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/lib*.a ${LIBS_OUTPUT_PATH}
COMMAND cp ${EXTERNAL_SOURCE_PATH}/openssl/build/lib/lib*.a ${LIBS_OUTPUT_PATH} COMMAND cp ${EXTERNAL_SOURCE_PATH}/openssl/build/lib/lib*.a ${LIBS_OUTPUT_PATH}

View File

@ -16,7 +16,7 @@
#define TCP_MODULE_H #define TCP_MODULE_H
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#define TCP_MODULE_WRITE_ERROR -1; #define TCP_MODULE_WRITE_ERROR -1
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -25,15 +25,15 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *);
typedef void (*SocketClosedFunction)(const void *); typedef void (*SocketClosedFunction)(const void *);
typedef struct client_accept_parm typedef struct client_accept_parm
{ {
const TcpReadFunction mReadFunc; const TcpReadFunction mReadFunc; ///< This function is defined in the test file
const SocketClosedFunction mClosedFunc; const SocketClosedFunction mClosedFunc; ///< This function is defined in the test file.
} ClientAcceptParam; } ClientAcceptParam;
typedef struct tcp_server_parm typedef struct tcp_server_parm
{ {
const char *mIp; const char *mIp; ///< Server ip
const int mPort; const int mPort; ///< Server port
const TcpAcceptClientFunction mAcceptClientFunc; const TcpAcceptClientFunction mAcceptClientFunc; ///< This function is defined in the test file.
const ClientAcceptParam mClientAcceptParam; const ClientAcceptParam mClientAcceptParam; ///< This function is defined in the test file.
} TcpServerParam; } TcpServerParam;
typedef struct tcp_parm typedef struct tcp_parm
{ {

64
utils/TcpModule/readme.md Normal file
View File

@ -0,0 +1,64 @@
# TCP模块
这个项目实现了一个用于网络通信的TCP模块提供了服务器和客户端功能。
在网络编程中套接字socket是一个用于网络通信的端点。在客户端和服务器模型中客户端会创建一个套接字来与服务器建立连接而服务器则监听来自客户端的连接请求。一旦连接建立客户端和服务器就可以通过各自的套接字进行数据的发送和接收。
# 涉及的知识
* 网络通信
* 多线程处理
* 回调函数
* C++/C
* I/O多路复用
* 套接字编程
* TCP协议
* 事件循环
# 各文件的作用
* **TcpModule.h**定义了TCP模块的公共接口和数据结构包括服务器和客户端参数结构体、回调函数类型等。
* **TcpModule.cpp**实现了TcpModule.h中定义的接口包括创建和释放TCP服务器和客户端的函数。
* **ITcpClient.h/ITcpClient.cpp**定义和实现了TCP客户端接口包括初始化、读取数据、写入数据和关闭连接等虚函数。
* **ITcpServer.h/ITcpServer.cpp**定义和实现了TCP服务器接口包括初始化、关闭服务器等虚函数。
* **TcpClientImpl.h/TcpClientImpl.cpp**实现了ITcpClient接口是TCP客户端的具体实现。
* **TcpServerImpl.h/TcpServerImpl.cpp**实现了ITcpServer接口是TCP服务器的具体实现。
* **TcpModuleMakePtr.h/TcpModuleMakePtr.cpp**提供了创建TCP服务器和客户端实例的工厂方法。
* **TcpServerHandle.h/TcpServerHandle.cpp**管理TCP服务器的实例提供了添加和获取服务器实例的方法。
* **TcpClientAcceptImpl.h/TcpClientAcceptImpl.cpp**实现了ITcpClientAccept接口处理客户端连接的接受和数据交换。
# 一些易混淆的点
1. 客户端创建的套接字包含指定`目标的IP地址和端口号`TcpClientImpl类中的init函数服务器创建的套接字只包含`本机IP地址和监听端口`。
2. 在网络编程中,`服务器`通常会在一个监听端口上等待来自客户端的连接请求。当服务器接受到一个连接请求时它会创建一个新的套接字或文件描述符来表示这个新建立的连接TcpClientAccept类实例并且通常会为这个新的连接设置一系列的事件回调函数。
3. 客户端实例TcpClientImpl类对象进行init操作时会创建一个套接字并对目标服务器发出连接请求并启动事件循环Loop函数然后客户端实例会一直进行事件循环监听是否有读写操作直至连接关闭Close函数
4. 服务器`监听连接的套接字`和`连接后新建立的套接字`要分别设置回调函数,这是两个东西,不要混淆。
# 该模块的实现过程
## 服务器端:
1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。
2. 设置回调函数:为服务器端套接字设置回调函数,包括:
on_accept当有新的客户端尝试连接时调用。
on_close当服务器端套接字关闭时调用。
3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。
4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。
5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。
on_close当客户端连接关闭时调用。
on_recv当客户端发送数据时调用。
6. 管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。
## 客户端:
1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。
2. 设置回调函数:为客户端套接字设置回调函数,包括:
on_connect当连接成功建立时调用。
on_close当连接关闭时调用。
on_message当接收到服务器发送的数据时调用。
3. 连接到服务器:使用 hio_connect 函数向服务器发起连接请求。
4. 启动事件循环:使用 hloop_run 启动客户端的事件循环,等待连接结果和数据传输。
5. 处理连接结果:在 on_connect 回调函数中处理连接结果,如果连接成功,可以开始发送和接收数据。

View File

@ -16,6 +16,7 @@
#define I_TCP_CLIENT_H #define I_TCP_CLIENT_H
#include "StatusCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
#include <sys/types.h>
class ITcpClient class ITcpClient
{ {
public: public:
@ -27,6 +28,9 @@ public:
virtual ssize_t Write(const void *buf, const size_t bufLenght); virtual ssize_t Write(const void *buf, const size_t bufLenght);
virtual void Closed(void); virtual void Closed(void);
}; };
/**
* @brief Verify that the header of the client object is legal.
*/
typedef struct i_tcp_client_header typedef struct i_tcp_client_header
{ {
const char *mCheckName; const char *mCheckName;

View File

@ -17,6 +17,9 @@
#include "StatusCode.h" #include "StatusCode.h"
#include "TcpModule.h" #include "TcpModule.h"
#include <memory> #include <memory>
/**
* @brief Handle data interaction and connection state changes on established TCP connections.
*/
class ITcpClientAccept class ITcpClientAccept
{ {
public: public:
@ -35,6 +38,9 @@ public:
virtual void Init(void); virtual void Init(void);
virtual void UnInit(void); virtual void UnInit(void);
}; };
/**
* @brief Verify that the header of the server object is legal.
*/
typedef struct i_tcp_server_header typedef struct i_tcp_server_header
{ {
const char *mCheckName; const char *mCheckName;

View File

@ -23,18 +23,34 @@
#include <mutex> #include <mutex>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
/**
* @brief Called when data is received on a TCP connection.
*
* @param io Client's socket
* @param buf The transmitted data content
* @param len Byte length of transmitted data
*/
static void on_message(hio_t *io, void *buf, int len) static void on_message(hio_t *io, void *buf, int len)
{ {
LogInfo("onmessage: %.*s\n", len, (char *)buf); LogInfo("onmessage: %.*s\n", len, (char *)buf);
TcpClientImpl *tcpClient = (TcpClientImpl *)hevent_userdata(io); TcpClientImpl *tcpClient = (TcpClientImpl *)hevent_userdata(io);
tcpClient->Readed(buf, len); tcpClient->Readed(buf, len);
} }
/**
* @brief Called when TCP connection is established.
* @param io Client's socket
*/
static void on_connect(hio_t *io) static void on_connect(hio_t *io)
{ {
LogInfo("onconnect: connfd=%d\n", hio_fd(io)); LogInfo("onconnect: connfd=%d\n", hio_fd(io));
hio_setcb_read(io, on_message); hio_setcb_read(io, on_message);
/// Start a read operation. Read data if it arrives.
hio_read(io); hio_read(io);
} }
/**
* @brief Called when tcp connection is disconnected.
* @param io Client't socket
*/
static void on_close(hio_t *io) static void on_close(hio_t *io)
{ {
LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io)); LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io));
@ -44,23 +60,43 @@ static void on_close(hio_t *io)
TcpClientImpl::TcpClientImpl(const TcpClientParam &param, const void *object) : mParam(param), mObjectThis(object) TcpClientImpl::TcpClientImpl(const TcpClientParam &param, const void *object) : mParam(param), mObjectThis(object)
{ {
} }
/**
* @brief Create a socket and connect it, and start an event loop to listen to io object operations (read and write,
* etc.)
*/
void TcpClientImpl::Init(void) void TcpClientImpl::Init(void)
{ {
constexpr int NO_FALGS = 0; constexpr int NO_FALGS = 0;
/// Initialize event loop
mLoop = hloop_new(NO_FALGS); mLoop = hloop_new(NO_FALGS);
if (nullptr == mLoop) { if (nullptr == mLoop) {
LogError("TcpClientImpl::Init hloop_new failed.\n"); LogError("TcpClientImpl::Init hloop_new failed.\n");
return; return;
} }
/**
* @brief The client's socket, which is used to actively send connection requests to the server and handle related
* I/O operations, has no listening function.
* @param mLoop Event loop.
* When I/O events (connection closed, connection successful, data readable, etc.) occur, the event loop will call
* the corresponding callback function to handle them.
*
* @param mParam.mIp Server IP address
* @param mParam.mPort Server port
* @param HIO_TYPE_TCP TCP connection
* @param HIO_CLIENT_SIDE Indicates that the socket will be used as a client.
*
*/
hio_t *io = hio_create_socket(mLoop, mParam.mIp, mParam.mPort, HIO_TYPE_TCP, HIO_CLIENT_SIDE); hio_t *io = hio_create_socket(mLoop, mParam.mIp, mParam.mPort, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
if (nullptr == io) { if (nullptr == io) {
LogError("TcpClientImpl::Init hio_create_socket failed.\n"); LogError("TcpClientImpl::Init hio_create_socket failed.\n");
return; return;
} }
hevent_set_userdata(io, this); hevent_set_userdata(io, this);
/// Set the callback function of successful connection, and there is no connection operation.
hio_setcb_connect(io, on_connect); hio_setcb_connect(io, on_connect);
/// Set the callback function to close the connection, and there is no connection operation.
hio_setcb_close(io, on_close); hio_setcb_close(io, on_close);
hio_connect(io); hio_connect(io); ///< Connection operation
mIo = io; mIo = io;
std::shared_ptr<TcpClientImpl> impl = std::dynamic_pointer_cast<TcpClientImpl>(shared_from_this()); std::shared_ptr<TcpClientImpl> impl = std::dynamic_pointer_cast<TcpClientImpl>(shared_from_this());
auto recvThread = [](std::shared_ptr<TcpClientImpl> tcpClient) { auto recvThread = [](std::shared_ptr<TcpClientImpl> tcpClient) {
@ -68,6 +104,9 @@ void TcpClientImpl::Init(void)
}; };
mTcpClientThread = std::thread(recvThread, impl); mTcpClientThread = std::thread(recvThread, impl);
} }
/**
* @brief De-initialize the TCP client, close the client't socket and wait for the event cycle to end.
*/
void TcpClientImpl::UnInit(void) void TcpClientImpl::UnInit(void)
{ {
if (nullptr != mIo) { if (nullptr != mIo) {
@ -79,6 +118,12 @@ void TcpClientImpl::UnInit(void)
mTcpClientThread.join(); mTcpClientThread.join();
} }
} }
/**
* @brief Read the data sent by the server in tcp connection.
*
* @param data Data content sent by the server
* @param length Byte length of data content sent by the server.
*/
void TcpClientImpl::Readed(const void *data, size_t length) void TcpClientImpl::Readed(const void *data, size_t length)
{ {
if (nullptr != mParam.mReadFunc) { if (nullptr != mParam.mReadFunc) {
@ -87,6 +132,13 @@ void TcpClientImpl::Readed(const void *data, size_t length)
} }
LogError("mParam.mReadFunc is nullptr.\n"); LogError("mParam.mReadFunc is nullptr.\n");
} }
/**
* @brief The client writes data to the server, and the writing operation can only be performed in the connected state.
*
* @param buf Content of written data
* @param bufLenght Byte length of written data
* @return ssize_t
*/
ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght)
{ {
std::lock_guard<std::mutex> locker(mMutex); std::lock_guard<std::mutex> locker(mMutex);
@ -94,8 +146,13 @@ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght)
LogError("mIo is nullptr.\n"); LogError("mIo is nullptr.\n");
return TCP_MODULE_WRITE_ERROR; return TCP_MODULE_WRITE_ERROR;
} }
/// Returns the byte length of a packet.If it fails, an error code is returned.
return hio_write(mIo, buf, bufLenght); return hio_write(mIo, buf, bufLenght);
} }
/**
* @brief Close the tcp connection and stop the event loop (reading and writing I/O objects).
*
*/
void TcpClientImpl::Closed(void) void TcpClientImpl::Closed(void)
{ {
std::lock_guard<std::mutex> locker(mMutex); std::lock_guard<std::mutex> locker(mMutex);
@ -108,6 +165,10 @@ void TcpClientImpl::Closed(void)
hloop_stop(mLoop); hloop_stop(mLoop);
} }
} }
/**
* @brief Run an event loop and release resources after completion or error.
*
*/
void TcpClientImpl::Loop(void) void TcpClientImpl::Loop(void)
{ {
if (nullptr == mLoop) { if (nullptr == mLoop) {
@ -120,15 +181,17 @@ void TcpClientImpl::Loop(void)
} }
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam &param) std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam &param)
{ {
LogInfo("Create tcp server object.\n"); LogInfo("Create tcp client object.\n");
TcpClient *impl = (TcpClient *)malloc(sizeof(TcpClient)); TcpClient *impl = (TcpClient *)malloc(sizeof(TcpClient));
if (nullptr == impl) { if (nullptr == impl) {
LogError("NewTcpServer::malloc failed.\n"); LogError("NewTcpServer::malloc failed.\n");
return nullptr; return nullptr;
} }
TcpClient tmp; TcpClient tmp;
/// Initialize impl with tmp
memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient)); memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient));
impl->mHeader.mCheckName = GetTcpClientModuleName(); impl->mHeader.mCheckName = GetTcpClientModuleName();
/// ObjectThis is actually a pointer to mTcpClient in the TcpClient structure.
std::shared_ptr<ITcpClient> *objectThis = std::shared_ptr<ITcpClient> *objectThis =
(std::shared_ptr<ITcpClient> *)(((char *)impl) + sizeof(ITcpClientHeader)); (std::shared_ptr<ITcpClient> *)(((char *)impl) + sizeof(ITcpClientHeader));
impl->mTcpClient = std::make_shared<TcpClientImpl>(param, objectThis); impl->mTcpClient = std::make_shared<TcpClientImpl>(param, objectThis);

View File

@ -27,20 +27,21 @@ class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this<Tcp
public: public:
TcpClientImpl(const TcpClientParam &param, const void *object); TcpClientImpl(const TcpClientParam &param, const void *object);
virtual ~TcpClientImpl() = default; virtual ~TcpClientImpl() = default;
void Init(void) override; void Init(void) override; ///< Create a socket and connect it, and start an event loop to listen to io object
void UnInit(void) override; ///< operations (read and write, etc.)
void Readed(const void *data, size_t length) override; void UnInit(void) override; ///< close the client't socket and wait for the event cycle to end.
ssize_t Write(const void *buf, const size_t bufLenght) override; void Readed(const void *data, size_t length) override; ///< Read the data sent by the server in tcp connection.
void Closed(void) override; ssize_t Write(const void *buf, const size_t bufLenght) override; ///< client writes data to the server
void Closed(void) override; ///< Close the tcp connection and stop the event loop (reading and writing I/O objects).
void Loop(void); void Loop(void);
private: private:
std::mutex mMutex; std::mutex mMutex; ///< A mutex lock used to synchronize access to shared resources.
hloop_t *mLoop; hloop_t *mLoop; ///< Event loop, Listen for read/write or connection closing operations.
hio_t *mIo; hio_t *mIo; ///< Client's socket
const TcpClientParam mParam; const TcpClientParam mParam; ///< Basic information of the client, including port, ip, reading and closing.
std::thread mTcpClientThread; std::thread mTcpClientThread; ///< A separate thread that runs an event loop to receive and process network data
const void *mObjectThis; const void *mObjectThis; ///< ObjectThis is used to verify whether the client is legal.
}; };
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam &param); std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam &param);
#endif #endif

View File

@ -21,6 +21,15 @@
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <sys/types.h> #include <sys/types.h>
/**
* @brief Verify that object is a legitimate (existing) server.
*
* @param object Save the address of the character pointer variable.
* Should be substituted into the private member objectThis of the client instance.
* If the value of the character pointer pointed by *object is "tcp_server", return turn
* @return true Indicates that the server exists.
* @return false Indicates that the server does not exist
*/
static bool TcpServerObjectCheck(void *object) static bool TcpServerObjectCheck(void *object)
{ {
if (nullptr == object) { if (nullptr == object) {
@ -33,6 +42,14 @@ static bool TcpServerObjectCheck(void *object)
} }
return true; return true;
} }
/**
* @brief Verify that object is a legitimate (existing) client.
*
* @param object Save the address of the character pointer variable.
*
* @return true Indicates that the client exists.
* @return false Indicates that the client does not exist
*/
static bool TcpClientObjectCheck(void *object) static bool TcpClientObjectCheck(void *object)
{ {
if (nullptr == object) { if (nullptr == object) {

View File

@ -19,6 +19,10 @@
#include "StatusCode.h" #include "StatusCode.h"
#include "TcpModule.h" #include "TcpModule.h"
#include <memory> #include <memory>
/**
* @brief A factory class that indirectly creates server-side and client-side instances through this class
*
*/
class TcpModuleMakePtr class TcpModuleMakePtr
{ {
public: public:

View File

@ -16,6 +16,12 @@
#include "ILog.h" #include "ILog.h"
#include "ITcpServer.h" #include "ITcpServer.h"
#include <memory> #include <memory>
/**
* @brief Get the singleton instance of TcpServerHandle
* @param impl Optional implementation pointer to replace the current instance
* @return The singleton instance of TcpServerHandle
*/
std::shared_ptr<TcpServerHandle> &TcpServerHandle::GetInstance(std::shared_ptr<TcpServerHandle> *impl) std::shared_ptr<TcpServerHandle> &TcpServerHandle::GetInstance(std::shared_ptr<TcpServerHandle> *impl)
{ {
static auto instance = std::make_shared<TcpServerHandle>(); static auto instance = std::make_shared<TcpServerHandle>();
@ -30,11 +36,24 @@ std::shared_ptr<TcpServerHandle> &TcpServerHandle::GetInstance(std::shared_ptr<T
} }
return instance; return instance;
} }
/**
* @brief Add a server
* @param fd File descriptor of the server
* @param server Shared pointer to the ITcpServer instance
*/
void TcpServerHandle::AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server) void TcpServerHandle::AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server)
{ {
LogInfo("AddServer fd = %d\n", fd); LogInfo("AddServer fd = %d\n", fd);
mFd[fd] = server; mFd[fd] = server;
} }
/**
* @brief Get a server by file descriptor
* @param fd File descriptor of the server
* @param server Reference to a shared pointer to store the ITcpServer instance
* @return True if the server is found, false otherwise
*/
bool TcpServerHandle::GetServer(const int &fd, std::shared_ptr<ITcpServer> &server) bool TcpServerHandle::GetServer(const int &fd, std::shared_ptr<ITcpServer> &server)
{ {
auto it = mFd.find(fd); auto it = mFd.find(fd);

View File

@ -20,13 +20,36 @@
class TcpServerHandle class TcpServerHandle
{ {
public: public:
/**
* @brief Default constructor
*/
TcpServerHandle() = default; TcpServerHandle() = default;
/**
* @brief Default destructor
*/
~TcpServerHandle() = default; ~TcpServerHandle() = default;
/**
* @brief Get the singleton instance of TcpServerHandle
* @param impl Optional implementation pointer to replace the current instance
* @return The singleton instance of TcpServerHandle
*/
static std::shared_ptr<TcpServerHandle> &GetInstance(std::shared_ptr<TcpServerHandle> *impl = nullptr); static std::shared_ptr<TcpServerHandle> &GetInstance(std::shared_ptr<TcpServerHandle> *impl = nullptr);
/**
* @brief Add a server
* @param fd File descriptor of the server
* @param server Shared pointer to the ITcpServer instance
*/
void AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server); void AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server);
/**
* @brief Get a server by file descriptor
* @param fd File descriptor of the server
* @param server Reference to a shared pointer to store the ITcpServer instance
* @return True if the server is found, false otherwise
*/
bool GetServer(const int &fd, std::shared_ptr<ITcpServer> &server); bool GetServer(const int &fd, std::shared_ptr<ITcpServer> &server);
private: private:
/// Store multiple servers
std::map<int, std::shared_ptr<ITcpServer>> mFd; std::map<int, std::shared_ptr<ITcpServer>> mFd;
}; };
#endif #endif

View File

@ -24,18 +24,35 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
/**
* @brief Handle the close connection event.
*
* @param io Server's socket
*/
static void on_close(hio_t *io) static void on_close(hio_t *io)
{ {
LogInfo("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io)); LogInfo("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io); TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
server->RemoveClient(io); server->RemoveClient(io);
} }
/**
* @brief Close the port on which the server listens for client connections.
*
* @param io The port on which the server listens for client connections.
*/
static void server_on_close(hio_t *io) static void server_on_close(hio_t *io)
{ {
LogInfo("server_on_close fd=%d error=%d\n", hio_fd(io), hio_error(io)); LogInfo("server_on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io); TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
server->Closed(); server->Closed();
} }
/**
* @brief The server processes data
*
* @param io Socket
* @param buf data
* @param readbytes bytes
*/
static void on_recv(hio_t *io, void *buf, int readbytes) static void on_recv(hio_t *io, void *buf, int readbytes)
{ {
LogInfo("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes); LogInfo("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
@ -48,6 +65,11 @@ static void on_recv(hio_t *io, void *buf, int readbytes)
std::shared_ptr<ITcpClientAccept> *client = server->GetClient(io); std::shared_ptr<ITcpClientAccept> *client = server->GetClient(io);
(*client)->Readed((const char *)buf, readbytes); (*client)->Readed((const char *)buf, readbytes);
} }
/**
* @brief Handles a new accept client connection event
*
* @param io Socket for newly accepted client connection
*/
static void on_accept(hio_t *io) static void on_accept(hio_t *io)
{ {
LogInfo("on_accept connfd=%d\n", hio_fd(io)); LogInfo("on_accept connfd=%d\n", hio_fd(io));
@ -63,12 +85,17 @@ static void on_accept(hio_t *io)
// std::shared_ptr<ITcpClientAccept> *client = NewTcpClientAccept(io); // std::shared_ptr<ITcpClientAccept> *client = NewTcpClientAccept(io);
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io); TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
server->AddClient(io); server->AddClient(io);
/// Set up a read event
hio_read_start(io); hio_read_start(io);
} }
TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam &param) TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam &param)
: mIo(io), mObjectThis(object), mParam(param) : mIo(io), mObjectThis(object), mParam(param)
{ {
} }
/**
* @brief Disconnect the client
*
*/
void TcpClientAcceptImpl::Close(void) void TcpClientAcceptImpl::Close(void)
{ {
if (nullptr != mIo) { if (nullptr != mIo) {
@ -76,14 +103,28 @@ void TcpClientAcceptImpl::Close(void)
hio_close(io); hio_close(io);
} }
} }
/**
* @brief The server reads the data received by tcp connection.
*
* @param data Read data content
* @param length Read data byte length
*/
void TcpClientAcceptImpl::Readed(const void *data, size_t length) void TcpClientAcceptImpl::Readed(const void *data, size_t length)
{ {
if (nullptr != mParam.mReadFunc) { if (nullptr != mParam.mReadFunc) {
mParam.mReadFunc(data, length, mObjectThis); mParam.mReadFunc(data, length, mObjectThis);
return; return;
} }
LogWarning("mParam.mClosedFunc is null\n"); LogWarning("mParam.mReadedFunc is null\n");
} }
/**
* @brief The server writes data to the tcp connection.
*
* @param data Written data
* @param length Byte length of written data
* @return ssize_t
* Returns the number of bytes written if the writing is successful, and returns the error code -1 if it fails.
*/
ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length) ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length)
{ {
if (mIo) { if (mIo) {
@ -93,6 +134,11 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length)
LogError("mIo is null\n"); LogError("mIo is null\n");
return TCP_MODULE_WRITE_ERROR; return TCP_MODULE_WRITE_ERROR;
} }
/**
* @brief Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is
* closed
*
*/
void TcpClientAcceptImpl::Closed(void) void TcpClientAcceptImpl::Closed(void)
{ {
if (nullptr != mParam.mClosedFunc) { if (nullptr != mParam.mClosedFunc) {
@ -106,14 +152,21 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param)
mLoop = nullptr; mLoop = nullptr;
mIo = nullptr; mIo = nullptr;
} }
/**
* @brief Create an event loop to listen for connection requests from the server socket, set up relevant callback
* functions, and start a separate thread to run the event loop
*
*/
void TcpServerImpl::Init(void) void TcpServerImpl::Init(void)
{ {
constexpr int NO_FALGS = 0; constexpr int NO_FALGS = 0;
/// mLoop is used to manage all I/O objects and the events that occur on them.
mLoop = hloop_new(NO_FALGS); mLoop = hloop_new(NO_FALGS);
if (nullptr == mLoop) { if (nullptr == mLoop) {
LogError("hloop_new failed\n"); LogError("hloop_new failed\n");
return; return;
} }
/// Create a socket to listen whether a new client sends a connection request.
hio_t *listenio = hloop_create_tcp_server(mLoop, mParam.mIp, mParam.mPort, on_accept); hio_t *listenio = hloop_create_tcp_server(mLoop, mParam.mIp, mParam.mPort, on_accept);
if (nullptr == listenio) { if (nullptr == listenio) {
LogError("hloop_create_tcp_server failed\n"); LogError("hloop_create_tcp_server failed\n");
@ -132,11 +185,17 @@ void TcpServerImpl::Init(void)
}; };
mTcpServerThread = std::thread(recvThread, impl); mTcpServerThread = std::thread(recvThread, impl);
} }
/**
* @brief De-initialize the TCP server, close the I/O object and wait for the receiving thread to end.
*
*/
void TcpServerImpl::UnInit(void) void TcpServerImpl::UnInit(void)
{ {
LogInfo("UnInit TcpServerImpl\n"); LogInfo("UnInit TcpServerImpl\n");
/// Close all client connections
FreeClients(); FreeClients();
if (nullptr != mIo) { if (nullptr != mIo) {
/// Turn off connection monitoring
hio_close(mIo); hio_close(mIo);
mIo = nullptr; mIo = nullptr;
} }
@ -144,6 +203,10 @@ void TcpServerImpl::UnInit(void)
mTcpServerThread.join(); mTcpServerThread.join();
} }
} }
/**
* @brief Run an event loop and release resources after completion or error.
*
*/
void TcpServerImpl::Loop(void) void TcpServerImpl::Loop(void)
{ {
if (nullptr == mLoop) { if (nullptr == mLoop) {
@ -154,6 +217,11 @@ void TcpServerImpl::Loop(void)
hloop_free(&mLoop); hloop_free(&mLoop);
mLoop = nullptr; mLoop = nullptr;
} }
/**
* @brief Add a newly connected client to the server connection management list.
*
* @param io Socket associated with server and client connections
*/
void TcpServerImpl::AddClient(hio_t *io) void TcpServerImpl::AddClient(hio_t *io)
{ {
mMutex.lock(); mMutex.lock();
@ -165,6 +233,7 @@ void TcpServerImpl::AddClient(hio_t *io)
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
std::shared_ptr<ITcpClientAccept> *addClient = NewTcpClientAccept(io, mParam.mClientAcceptParam); std::shared_ptr<ITcpClientAccept> *addClient = NewTcpClientAccept(io, mParam.mClientAcceptParam);
mClients[hio_fd(io)] = addClient; mClients[hio_fd(io)] = addClient;
/// Check whether the server side accepts the connection of the client side.
if (mParam.mAcceptClientFunc) { if (mParam.mAcceptClientFunc) {
if (mParam.mAcceptClientFunc(addClient, peeraddrstr) == true) { if (mParam.mAcceptClientFunc(addClient, peeraddrstr) == true) {
mMutex.unlock(); mMutex.unlock();
@ -176,6 +245,12 @@ void TcpServerImpl::AddClient(hio_t *io)
hio_close(io); hio_close(io);
LogWarning("AddClient failed.\n"); LogWarning("AddClient failed.\n");
} }
/**
* @brief Retrieve the corresponding client acceptance object based on the socket handle.
*
* @param io Handle pointing to socket
* @return std::shared_ptr<ITcpClientAccept>*
*/
std::shared_ptr<ITcpClientAccept> *TcpServerImpl::GetClient(hio_t *io) std::shared_ptr<ITcpClientAccept> *TcpServerImpl::GetClient(hio_t *io)
{ {
std::lock_guard<std::mutex> locker(mMutex); std::lock_guard<std::mutex> locker(mMutex);
@ -186,6 +261,11 @@ std::shared_ptr<ITcpClientAccept> *TcpServerImpl::GetClient(hio_t *io)
LogError("GetClient failed, client not exit.\n"); LogError("GetClient failed, client not exit.\n");
return nullptr; return nullptr;
} }
/**
* @brief Removes a specific connection from the list of client connections maintained by the server.
*
* @param io A server-side socket associated with an established client connection.
*/
void TcpServerImpl::RemoveClient(hio_t *io) void TcpServerImpl::RemoveClient(hio_t *io)
{ {
std::lock_guard<std::mutex> locker(mMutex); std::lock_guard<std::mutex> locker(mMutex);
@ -203,6 +283,10 @@ void TcpServerImpl::RemoveClient(hio_t *io)
LogError("RemoveClient failed, client not exit.\n"); LogError("RemoveClient failed, client not exit.\n");
} }
} }
/**
* @brief Clear all connected clients.
*
*/
void TcpServerImpl::FreeClients(void) void TcpServerImpl::FreeClients(void)
{ {
std::lock_guard<std::mutex> locker(mMutex); std::lock_guard<std::mutex> locker(mMutex);
@ -218,6 +302,10 @@ void TcpServerImpl::FreeClients(void)
} }
mClients.clear(); mClients.clear();
} }
/**
* @brief Stop listening to the event loop
*
*/
void TcpServerImpl::Closed(void) void TcpServerImpl::Closed(void)
{ {
if (nullptr != mLoop) { if (nullptr != mLoop) {
@ -234,6 +322,7 @@ std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam &param)
return nullptr; return nullptr;
} }
TcpServer tmp; TcpServer tmp;
/// Initialize impl with tmp
memcpy((void *)impl, (void *)&tmp, sizeof(TcpServer)); memcpy((void *)impl, (void *)&tmp, sizeof(TcpServer));
impl->mHeader.mCheckName = GetTcpServerModuleName(); impl->mHeader.mCheckName = GetTcpServerModuleName();
impl->mTcpServer = std::make_shared<TcpServerImpl>(param); impl->mTcpServer = std::make_shared<TcpServerImpl>(param);
@ -241,7 +330,7 @@ std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam &param)
} }
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam &param) std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam &param)
{ {
LogInfo("Create tcp server object.\n"); LogInfo("Create tcp client accept object.\n");
TcpClientAccept *impl = (TcpClientAccept *)malloc(sizeof(TcpClientAccept)); TcpClientAccept *impl = (TcpClientAccept *)malloc(sizeof(TcpClientAccept));
if (nullptr == impl) { if (nullptr == impl) {
LogError("NewTcpServer::malloc failed.\n"); LogError("NewTcpServer::malloc failed.\n");
@ -250,6 +339,7 @@ std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const Cli
TcpClientAccept tmp; TcpClientAccept tmp;
memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept)); memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept));
impl->mHeader.mCheckName = GetTcpClientAcceptName(); impl->mHeader.mCheckName = GetTcpClientAcceptName();
/// ObjectThis actually refers to mTcpClientAccept
std::shared_ptr<ITcpClientAccept> *objectThis = std::shared_ptr<ITcpClientAccept> *objectThis =
(std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader)); (std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader));
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis, param); impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis, param);

View File

@ -22,21 +22,32 @@
#include <map> #include <map>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
/**
* @brief The TcpClient Accept class instance is used to associate with the socket after connecting to the server and
* client, responsible for a series of operations (read/write, etc.) after the connection
*
*/
class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this<TcpClientAcceptImpl> class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this<TcpClientAcceptImpl>
{ {
public: public:
TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam &param); TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam &param);
virtual ~TcpClientAcceptImpl() = default; virtual ~TcpClientAcceptImpl() = default;
void Close(void) override; void Close(void) override; ///< Disconnect the client
void Readed(const void *data, size_t length) override; void Readed(const void *data, size_t length) override; ///< The server reads the data received by tcp connection.
ssize_t Write(const void *data, size_t length) override; ssize_t Write(const void *data, size_t length) override; ///< The server writes data to the tcp connection.
void Closed(void) override; void Closed(void) override; ///< Used to perform some cleaning work or notify upper layer logic when a TCP client
///< accepts a connection that is closed
private: private:
const hio_t *mIo; const hio_t *mIo; ///< Connected client socket
const void *mObjectThis; const void *mObjectThis; ///< Used to verify whether this type of instance is legal
const ClientAcceptParam mParam; const ClientAcceptParam mParam;
}; };
/**
* @brief The TcpServerImpl class is responsible for listening to connection requests from clients and creating an
* instance of TcpClientAcceptImpl for each accepted connection.
*
*/
class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this<TcpServerImpl> class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this<TcpServerImpl>
{ {
public: public:
@ -44,20 +55,21 @@ public:
virtual ~TcpServerImpl() = default; virtual ~TcpServerImpl() = default;
void Init(void) override; void Init(void) override;
void UnInit(void) override; void UnInit(void) override;
void Loop(void); void Loop(void); ///< Run an event loop and release resources after completion or error.
void AddClient(hio_t *io); void AddClient(hio_t *io); ///< Add a newly connected client to the server connection management list.
std::shared_ptr<ITcpClientAccept> *GetClient(hio_t *io); std::shared_ptr<ITcpClientAccept> *
void RemoveClient(hio_t *io); GetClient(hio_t *io); ///< Retrieve the corresponding client acceptance object based on the socket handle.
void FreeClients(void); void RemoveClient(hio_t *io); ///< Remove the data element in the map, that is, the connected client.
void Closed(void); void FreeClients(void); ///< Clear all connected clients.
void Closed(void); ///< Stop listening to the event loop(mLoop)
private: private:
std::mutex mMutex; std::mutex mMutex; ///< A mutex lock used to synchronize access to shared resources.
hloop_t *mLoop; hloop_t *mLoop; ///< Event loop, listening for all io objects
hio_t *mIo; hio_t *mIo; ///< A server socket to listen whether a new client sends a connection request.
const TcpServerParam mParam; const TcpServerParam mParam;
std::thread mTcpServerThread; std::thread mTcpServerThread; ///< A separate thread used to run event loops
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients; std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients; ///< Container for storing clients connected to servers
}; };
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam &param); std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam &param);
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam &param); std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam &param);

View File

@ -0,0 +1,14 @@
# UartDevice模块
该模块是UART设备接口模块提供了UART 设备操作接口通过uart通信操作相关的硬件设备。
# 涉及的知识
* uart通信协议
* 多线程
* C++类和对象
* 函数指针
# 各文件的作用
* **UartDevice.h**为上层应用提供了封装好的函数接口用于与uart硬件设备进行交互。
* **UartDevice.cpp**UartDevice.h中函数接口的具体定义和封装。
* **UartDeviceImpl.cpp/UartDeviceImpl.h**这里面定义和声明的函数是对uart硬件设备更底层更具体的一些操作它们会被提供给UartDevice.cpp做进一步的封装。