init
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
#ifndef _ERROR_H_
|
||||
#define _ERROR_H_
|
||||
|
||||
#define SYD_OK 0
|
||||
#define SYD_FAIL 0x01000000
|
||||
|
||||
#define SYD_PARAM_ERR 0x01000001 //参数错误
|
||||
#define SYD_POINT_ERR 0x01000002 //指针错误
|
||||
#define SYD_MEMORY_ERR 0x01000003 //内存错误
|
||||
#define SYD_DEVICE_NUM_ERR 0x01000004 //设备连接数错误
|
||||
#define SYD_DEVICE_PORT_ERR 0x01000005 //设备端口错误
|
||||
#define SYD_DEVICE_TIMEOUT_ERR 0x01000006 //设备超时错误
|
||||
#define SYD_INVALIDHANDLE_ERR 0x01000007 //无效的句柄
|
||||
#define SYD_OUTDATA_LEN_ERR 0x01000008 //输出缓冲区大小不足
|
||||
#define SYD_QUEUE_HANDLE_ERR 0x01000009 //队列句柄未初始化
|
||||
#define SYD_QUEUE_ISFULL 0x0100000A //队列已满
|
||||
#define SYD_QUEUE_ISEMPTY 0x0100000B //队列为空
|
||||
#define SYD_THREAD_CREATE_ERR 0x0100000C //线程创建失败
|
||||
#define SYD_THREADPOOL_CLOSE 0x0100000D //线程池已关闭
|
||||
#define SYD_THREADPOOL_HANDLE_ERROR 0x0100000E //线程句柄无效
|
||||
#define SYD_THREAPOOL_DESTORY_OVERTIME 0x0100000F //线程销毁超时
|
||||
#define SYD_MAC_CHECK_ERROR 0x01000010 //MAC校验错误
|
||||
#define SYD_THREADPOOL_COUNT_ERROR 0x01000011 //线程数错误
|
||||
#define SYD_HANDLE_TYPE_ERROR 0x01000012 //句柄类型错误
|
||||
#define SYD_LONGDATA_HANDLE_UNINIT 0x01000013 //长数据的过程句柄没有执行初始化操作(init, update, final 必须先执行init操作)
|
||||
#define SYD_DECRYPT_DATALEN_ERROR 0x01000014 //解密传入的数据长度错误(不能为0且必须满足16的倍数)
|
||||
#define SYD_BATCH_DATA_SIZE_ERROR 0x01000015 //批量数据数组大小错误(短连接最多只能支持4个,长连接不限制)
|
||||
|
||||
#define SYD_NET_CONNECT_ERR 0xFFFFFFFF //网络连接失败
|
||||
#define SYD_NET_SEND_ERR 0xFFFFFFFE //网络发送失败
|
||||
#define SYD_NET_RECV_ERR 0xFFFFFFFD //网络接收失败
|
||||
#define SYD_NET_PARAM_ERR 0xFFFFFFFC //网络参数错误
|
||||
#define SYD_NET_PKGLEN_ERR 0xFFFFFFFB //发送或接收的数据大小超过最大缓冲区大小
|
||||
#define SYD_NET_CMD_ERR 0xFFFFFFFA //网络返回包命令码错误
|
||||
#define SYD_NET_SN_ERR 0xFFFFFFF9 //网络返回包序列号错误
|
||||
#define SYD_NET_OVERTIME 0xFFFFFFF8 //数据处理超时(可以将处理超时调大)
|
||||
#define SYD_NET_NOBLOCK_EINTR 0xFFFFFFF7 //被信号中断,需要重试(非阻塞模式)
|
||||
#define SYD_NET_NOBLOCK_EWOUDBLOCK_EAGAIN 0xFFFFFFF6 //send:TCP 窗口太小,数据暂时发不出去 recv:当前内核缓冲区中无可读数据 (非阻塞模式)
|
||||
#define SYD_NET_NOBLOCK_CLOSE 0xFFFFFFF5 //对端关闭连接(非阻塞模式)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef _LOG_H_
|
||||
#define _LOG_H_
|
||||
|
||||
#define LOG_MSG(A) Log_Two(__FILE__, __func__, __LINE__, A)
|
||||
#define LOG_TWO(A, ...) Log_Two(__FILE__, __func__, __LINE__, A, __VA_ARGS__)
|
||||
#define LOG(A, ...) Log_One(__FILE__, __func__, __LINE__, A, __VA_ARGS__)
|
||||
#define LOG_NET(A, ...) Log_Net(__FILE__, __func__, __LINE__, A, __VA_ARGS__)
|
||||
|
||||
#define LOG_MAX_LINE 1024
|
||||
#define LOG_MIN_LIN 256
|
||||
|
||||
#define LOG_CLOSE 0
|
||||
#define LOG_OPEN 1
|
||||
#define LOG_LEVEL_ONE 1
|
||||
#define LOG_LEVEL_TWO 2
|
||||
#define LOG_LEVEL_NET 3
|
||||
|
||||
typedef struct {
|
||||
int iLogFlag;
|
||||
int iLogLevel;
|
||||
}STC_LOGINFO;
|
||||
|
||||
extern STC_LOGINFO g_stcLogInfo;
|
||||
|
||||
void Log_SetAttribute(int iLogFlag, int iLogLevel);
|
||||
void Log_MutexInit();
|
||||
void Log_MutexDestory();
|
||||
void Log_Init(char *pcWorkPath);
|
||||
void Log_One(const char* pcFile, const char* pcFunc, int iLine, char* pcLogBuf, ...);
|
||||
void Log_Two(const char* pcFile, const char* pcFunc, int iLine, char* pcLogBuf, ...);
|
||||
void Log_Net(const char* pcFile, const char* pcFunc, int iLine, char* pcLogBuf, ...);
|
||||
void Log_Msg(char* pcLogBuf, ...);
|
||||
void LOG_PrintHex(char* pcTitle, unsigned char* ucData, unsigned int uiDataLen);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef _NETAPI_H_
|
||||
#define _NETAPI_H_
|
||||
|
||||
#define IPMODE_IPV4 0
|
||||
#define IPMODE_IPV6 1
|
||||
|
||||
int socket_connect(
|
||||
const char *ip,
|
||||
const int port,
|
||||
const int timeout,
|
||||
const int ipmode,
|
||||
int *pSockfd);
|
||||
|
||||
int socket_send(
|
||||
int fd,
|
||||
char *buf,
|
||||
int len);
|
||||
|
||||
int socket_recv(
|
||||
int fd,
|
||||
char *buf,
|
||||
int len);
|
||||
|
||||
void socket_close(
|
||||
int fd);
|
||||
|
||||
int socket_connect_noblock(
|
||||
const char* ip,
|
||||
const int port,
|
||||
const int timeout,
|
||||
const int ipmode,
|
||||
int* pSockfd);
|
||||
|
||||
int socket_send_noblock(
|
||||
int fd,
|
||||
char *buf,
|
||||
int len,
|
||||
struct timeval start_time,
|
||||
int deal_time);
|
||||
|
||||
int socket_recv_noblock(
|
||||
int fd,
|
||||
char *buf,
|
||||
int len,
|
||||
struct timeval start_time,
|
||||
int deal_time);
|
||||
|
||||
void socket_close_noblock(
|
||||
int fd);
|
||||
|
||||
int ms_difftime(
|
||||
struct timeval start_time,
|
||||
struct timeval end_time);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef _PUBLIC_H_
|
||||
#define _PUBLIC_H_
|
||||
|
||||
#define MIN_LIN 256
|
||||
#define MAX_LIN 1024
|
||||
|
||||
/*动态库加载状态*/
|
||||
#define P_DLL_INIT_OK 1
|
||||
#define P_DLL_INIT_FAIL 0
|
||||
|
||||
/*连接池*/
|
||||
#define P_THREADPOOL_MAX_COUNT 16
|
||||
#define P_THREADPOOL_QUERY_MAX_COUNT 1000
|
||||
#define P_THREADAPOOL_PKG_FLAG_OK 0x10001
|
||||
#define P_THREADAPOOL_PKG_FLAG_FAIL 0x00000
|
||||
|
||||
/*句柄类型*/
|
||||
#define P_HANDLE_DEV 0
|
||||
#define P_HANDLE_SM4_ENCDEC 1
|
||||
#define P_HANDLE_SM4_MAC 2
|
||||
#define P_HANDLE_SM3_HASH 3
|
||||
#define P_HANDLE_SM4_ENCDECMAC 4
|
||||
#define P_HANDLE_NOTIFY 5
|
||||
|
||||
/*句柄有效性*/
|
||||
#define P_HANDLE_OK 1
|
||||
#define P_HANDLE_FAIL 0
|
||||
|
||||
/*错误码字节长度*/
|
||||
#define P_ERR_LEN 2
|
||||
|
||||
/*主备机最大设备数*/
|
||||
#define P_DEVICE_MAX_COUNT 16
|
||||
|
||||
/*批量调用流程*/
|
||||
#define P_BATCH_ONLY_ONE 0
|
||||
#define P_BATCH_ALL_PKG 1
|
||||
|
||||
/*批量分组个数*/
|
||||
#define P_BATCH_GROUP 4
|
||||
|
||||
/*长数据分组初始化标志*/
|
||||
#define P_DATA_INIT 1
|
||||
#define P_DATA_FINAL 0
|
||||
|
||||
#define P_USEFLAG_THREADPOOL 1
|
||||
#define P_USEFLAG_ORDINARY 0
|
||||
|
||||
void P_BinToHex(char* pcInBuf, int iInBufLen,char* pcOutBuf);
|
||||
void P_HexToBin(char* pcInBuf, int iInBufLen,char* pcOutBuf);
|
||||
int P_GetWorkPath(char *pcWorkPath);
|
||||
|
||||
#endif
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
#ifndef _QUEUE_H_
|
||||
#define _QUEUE_H_
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int* (*function)(void*, void*);
|
||||
void *arg;
|
||||
void *device;
|
||||
}task_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
task_t* task; //任务
|
||||
int head; //队列头
|
||||
int tail; //队列尾
|
||||
int length; //队列长度
|
||||
int capacity; //队列总容量
|
||||
int status; //句柄状态
|
||||
}queue_t;
|
||||
|
||||
/*初始化队列*/
|
||||
queue_t* queue_init(int capacity);
|
||||
|
||||
/*销毁队列*/
|
||||
void queue_destory(queue_t* queue);
|
||||
|
||||
/*清空队列*/
|
||||
void queue_clear(queue_t* queue);
|
||||
|
||||
/*获取队列长度*/
|
||||
int queue_getlength(queue_t* queue);
|
||||
|
||||
/*入队*/
|
||||
int queue_enqueue(queue_t* queue, int* (*function)(void* arg, void *device), void *arg, void *device);
|
||||
|
||||
/*出队*/
|
||||
int queue_dequeue(queue_t* queue, int* (*function)(void* arg, void *device), void *arg, void *device);
|
||||
|
||||
#endif
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
#ifndef _SUNYARD_H_
|
||||
#define _SUNYARD_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
|
||||
#if (defined(_WIN32) || defined(_WIN64)) && !defined(__WIN__)
|
||||
#define __WIN__
|
||||
#endif
|
||||
|
||||
#define DLL_EXPORT
|
||||
|
||||
#ifdef __WIN__
|
||||
#ifdef DLL_EXPORT
|
||||
#define DLL_DECLARE __declspec(dllexport)
|
||||
#else
|
||||
#define DLL_DECLARE __declspec(dllimport)
|
||||
#endif
|
||||
#define DEVAPI __stdcall
|
||||
#else
|
||||
#define DEVAPI
|
||||
#define DLL_DECLARE __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
/*log level*/
|
||||
#define LOG_LEVEL_0 0
|
||||
#define LOG_LEVEL_1 1
|
||||
#define LOG_LEVEL_2 2
|
||||
#define LOG_LEVEL_3 3
|
||||
|
||||
/*sm4 operate*/
|
||||
#define SM4_ENCRYPT 1
|
||||
#define SM4_DECRYPT 0
|
||||
|
||||
/*batch data package serial number*/
|
||||
#define PKG_ONLY_ONE 0
|
||||
#define PKG_FIRST 1
|
||||
#define PKG_MIDDLE 2
|
||||
#define PKG_LAST 3
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SetLogConfig(
|
||||
const char *pcLogPath,
|
||||
int iLogPathLen,
|
||||
int iLogLevel);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_Connect_Ex(
|
||||
const char *pcIpList[],
|
||||
int iPortList[],
|
||||
int iSize,
|
||||
int iConnectTimeout,
|
||||
int iDealTimeOut,
|
||||
int iThreadCount,
|
||||
void** phHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_DisConnect_Ex(
|
||||
void* phHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_GetVersionInfo(
|
||||
void* phHandle,
|
||||
char* pcVersion,
|
||||
int* piVersionLen);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_ShortData(
|
||||
void* phHandle,
|
||||
int iKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
int iFlag,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_BatchData(
|
||||
void* phHandle,
|
||||
int iKeyIndex[],
|
||||
char* pcData[],
|
||||
int iDataLen[],
|
||||
int iSize,
|
||||
int iFlag,
|
||||
char* pcOutData[],
|
||||
int iOutDataLen[]);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_LongDataInit(
|
||||
int iKeyIndex,
|
||||
int iFlag,
|
||||
void** phProcHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_LongDataUpdate(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen,
|
||||
void* phProcHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_LongDataFinal(
|
||||
void* phHandle,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen,
|
||||
void* phProcHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM3_Hash_ShortData(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcHash);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM3_Hash_Init(
|
||||
void** phHash);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM3_Hash_Update(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
void* phHash);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM3_Hash_Final(
|
||||
void* phHandle,
|
||||
char* pcHash,
|
||||
void* phHash);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4Mac_ShortData(
|
||||
void* phHandle,
|
||||
int iKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcMac,
|
||||
int* piCheckRet);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4Mac_BatchData(
|
||||
void* phHandle,
|
||||
int iKeyIndex[],
|
||||
char* pcData[],
|
||||
int iDataLen[],
|
||||
int iSize,
|
||||
char* pcMac[],
|
||||
int piCheckRet[]);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4Mac_LongDataInit(
|
||||
int iKeyIndex,
|
||||
void** phMac);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4Mac_LongDataUpdate(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
void* phMac);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4Mac_LongDataFinal(
|
||||
void* phHandle,
|
||||
char* pcMac,
|
||||
int* piCheckRet,
|
||||
void* phMac);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_CloseHandle(
|
||||
void *hHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_Encrypt_Mac_ShortData(
|
||||
void* phHandle,
|
||||
int iEncKeyIndex,
|
||||
int iCheckKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcEncData,
|
||||
int* piEncDataLen,
|
||||
char* pcMac);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_Decrypt_Mac_ShortData(
|
||||
void* phHandle,
|
||||
int iEncKeyIndex,
|
||||
int iCheckKeyIndex,
|
||||
char* pcEncData,
|
||||
int iEncDataLen,
|
||||
char* pcMac,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_Encrypt_Mac_BatchData(
|
||||
void* phHandle,
|
||||
int iEncKeyIndex[],
|
||||
int iCheckKeyIndex[],
|
||||
char* pcData[],
|
||||
int iDataLen[],
|
||||
int iSize,
|
||||
char* pcEncData[],
|
||||
int iEncDataLen[],
|
||||
char* pcMac[]);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4_Decrypt_Mac_BatchData(
|
||||
void* phHandle,
|
||||
int iEncKeyIndex[],
|
||||
int iCheckKeyIndex[],
|
||||
char* pcEncData[],
|
||||
int iEncDataLen[],
|
||||
char* pcMac[],
|
||||
int iSize,
|
||||
char* pcOutData[],
|
||||
int iOutDataLen[],
|
||||
int iCheckRet[]);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4EncDecMac_LongDataInit(
|
||||
int iEncKeyIndex,
|
||||
int iCheckKeyIndex,
|
||||
int iFlag,
|
||||
void** phProcHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4EncDecMac_LongDataUpdate(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen,
|
||||
void* phProcHandle);
|
||||
|
||||
DLL_DECLARE int DEVAPI SYD_SM4EncDecMac_LongDataFinal(
|
||||
void* phHandle,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen,
|
||||
char* pcMac,
|
||||
void* phProcHandle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
#ifndef _SUNYARD_D_H_
|
||||
#define _SUNYARD_D_H_
|
||||
|
||||
#ifndef _SUNYARD_H_
|
||||
#include "sunyard.h"
|
||||
#endif
|
||||
|
||||
#ifndef _PUBLIC_H_
|
||||
#include "public.h"
|
||||
#endif
|
||||
|
||||
#ifndef _THREAD_POOL_H_
|
||||
#include "threadpool.h"
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int socket_fd;
|
||||
char ip[40];
|
||||
int port;
|
||||
int con_timeout;
|
||||
int deal_timeout;
|
||||
int status;
|
||||
} STC_CONNECT_INFO;
|
||||
|
||||
typedef struct {
|
||||
threadpool_t *threadpool; /*连接池*/
|
||||
pthread_mutex_t threadpool_mutex; /*连接池锁*/
|
||||
int threadpool_deal_time; /*连接池处理时间*/
|
||||
STC_CONNECT_INFO connect_info[P_DEVICE_MAX_COUNT]; /*普通连接信息*/
|
||||
int connect_idex; /*当前使用机器序号*/
|
||||
int connect_count; /*机器连接个数*/
|
||||
int deal_time; /*普通连接处理时间*/
|
||||
int connect_timeout; /*设备连接时间*/
|
||||
int use_flag; /*使用标志:0:使用普通连接 1:使用线程池*/
|
||||
int status; /*句柄有效性*/
|
||||
}STC_DEVICE_INFO;
|
||||
|
||||
typedef struct {
|
||||
int keyidex; /*密钥索引*/
|
||||
int flag; /*加解密标识*/
|
||||
char procdata[64]; /*加密过渡数据*/
|
||||
int procdatalen; /*加密过渡数据长度*/
|
||||
int initflag; /*初始化标志*/
|
||||
int status; /*句柄有效性*/
|
||||
}STC_SM4_ENCDEC_INFO;
|
||||
|
||||
typedef struct {
|
||||
int keyidex; /*密钥索引*/
|
||||
char iv[64]; /*过程IV*/
|
||||
int ivlen; /*过程IV长度*/
|
||||
char procdata[64]; /*过渡数据*/
|
||||
int procdatalen; /*过渡数据长度*/
|
||||
int initflag; /*初始化标志*/
|
||||
int status; /*句柄有效性*/
|
||||
}STC_SM4_MAC_INFO;
|
||||
|
||||
typedef struct {
|
||||
char procdata[MAX_LIN]; /*过渡数据*/
|
||||
int procdatalen; /*过渡数据长度*/
|
||||
int firstflag; /*第一包标志*/
|
||||
int initflag; /*初始化标志*/
|
||||
int status; /*句柄有效性*/
|
||||
}STC_SM3_HASH_INFO;
|
||||
|
||||
typedef struct {
|
||||
STC_SM4_ENCDEC_INFO sm4_encdec_info;
|
||||
STC_SM4_MAC_INFO sm4_mac_info;
|
||||
int initflag; /*初始化标志*/
|
||||
int status; /*句柄有效性*/
|
||||
}STC_SM4_ENCDECMAC_INFO;
|
||||
|
||||
typedef struct {
|
||||
STC_DEVICE_INFO *device_info;
|
||||
STC_SM4_ENCDEC_INFO *sm4_encdec_info;
|
||||
STC_SM4_MAC_INFO *sm4_mac_info;
|
||||
STC_SM3_HASH_INFO *sm3_hash_info;
|
||||
STC_SM4_ENCDECMAC_INFO *sm4_encdecmac_info;
|
||||
int iHandleType;
|
||||
int iStatus;
|
||||
}STC_D_HANDLE;
|
||||
|
||||
int SYD_D_AnalysisDevHandle(
|
||||
void* handle,
|
||||
int handletype,
|
||||
void** outhandle);
|
||||
|
||||
int SYD_D_Connect_Ex(
|
||||
const char *pcIpList[],
|
||||
int iPortList[],
|
||||
int iSize,
|
||||
int iConnectTimeout,
|
||||
int iDealTimeOut,
|
||||
int iThreadCount,
|
||||
void** phHandle);
|
||||
|
||||
int SYD_D_DisConnect_Ex(
|
||||
void* phHandle);
|
||||
|
||||
int SYD_D_GetServerVersion(
|
||||
void* phHandle,
|
||||
char* pcServerVersion,
|
||||
int* piServerVersionLen);
|
||||
|
||||
int SYD_D_SM4_ShortData(
|
||||
void* phHandle,
|
||||
int iKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
int iFlag,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen);
|
||||
|
||||
int SYD_D_SM4_LongData(
|
||||
void* phHandle,
|
||||
int iKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
int iPkgNum,
|
||||
int iFlag,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen);
|
||||
|
||||
int SYD_D_SM4_Data(
|
||||
void* phHandle,
|
||||
int iKeyIndex[],
|
||||
char *pcData[],
|
||||
int iDataLen[],
|
||||
int iSize,
|
||||
int iFlag,
|
||||
char *pcOutData[],
|
||||
int iOutDataLen[]);
|
||||
|
||||
int SYD_D_SM4_BatchData(
|
||||
void* phHandle,
|
||||
int iKeyIndex[],
|
||||
char *pcData[],
|
||||
int iDataLen[],
|
||||
int iSize,
|
||||
int iFlag,
|
||||
char *pcOutData[],
|
||||
int iOutDataLen[],
|
||||
int iCheckRet[]);
|
||||
|
||||
int SYD_D_SM3_Hash_ShortData(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcHash);
|
||||
|
||||
int SYD_D_SM3_Hash_LongData(
|
||||
void* phHandle,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
int iPkgNum,
|
||||
char* pcProcData,
|
||||
int* piProcDataLen,
|
||||
char* pcHash);
|
||||
|
||||
int SYD_D_SM4Mac_ShortData(
|
||||
void* phHandle,
|
||||
int iKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcMac,
|
||||
int* piCheckRet);
|
||||
|
||||
int SYD_D_SM4Mac_BatchData(
|
||||
void* phHandle,
|
||||
int iKeyIndex[],
|
||||
char* pcData[],
|
||||
int iDataLen[],
|
||||
int iSize,
|
||||
char* pcMac[],
|
||||
int piCheckRet[]);
|
||||
|
||||
int SYD_D_SM4Mac_LongData(
|
||||
void* phHandle,
|
||||
int iKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
int iPkgNum,
|
||||
char* pcProcData,
|
||||
int* piProcDataLen,
|
||||
char* pcMac,
|
||||
int* piCheckRet);
|
||||
|
||||
int SYD_D_CloseHandle(
|
||||
void *handle);
|
||||
|
||||
int SYD_D_SM4_ShortData_CheckValue(
|
||||
void* phHandle,
|
||||
int iEncKeyIndex,
|
||||
int iCheckKeyIndex,
|
||||
char* pcData,
|
||||
int iDataLen,
|
||||
char* pcMac,
|
||||
int iFlag,
|
||||
char* pcOutData,
|
||||
int* piOutDataLen,
|
||||
char* pcOutMac);
|
||||
|
||||
int SYD_D_SM4_BatchData_CheckValue(
|
||||
void* phHandle,
|
||||
int iEncKeyIndex[],
|
||||
int iCheckKeyIndex[],
|
||||
char* pcData[],
|
||||
int iDataLen[],
|
||||
char* pcMac[],
|
||||
int iSize,
|
||||
int iFlag,
|
||||
char* pcOutData[],
|
||||
int iOutDataLen[],
|
||||
char* pcOutMac[]);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,103 @@
|
||||
#ifndef _THREAD_POOL_H_
|
||||
#define _THREAD_POOL_H_
|
||||
|
||||
#ifndef _TYPEDEF_H_
|
||||
#include "typedef.h"
|
||||
#endif
|
||||
|
||||
#ifndef _PUBLIC_H_
|
||||
#include "public.h"
|
||||
#endif
|
||||
|
||||
#ifndef _QUEUE_H_
|
||||
#include "queue.h"
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
int socket_fd;
|
||||
char ip[40];
|
||||
int port;
|
||||
int con_timeout;
|
||||
int deal_timeout;
|
||||
int status;
|
||||
} threadpool_connect_info_t;
|
||||
|
||||
typedef struct {
|
||||
pthread_t thread_id;
|
||||
queue_t* work_queue;
|
||||
threadpool_connect_info_t connect_info[P_DEVICE_MAX_COUNT];
|
||||
int connect_count;
|
||||
int connect_idex;
|
||||
} threadpool_task_t;
|
||||
|
||||
typedef struct {
|
||||
threadpool_task_t* work_threads; /* 工作线程 */
|
||||
|
||||
/*管理线程*/
|
||||
threadpool_task_t* admin_threads; /* 管理线程 */
|
||||
pthread_t admin_thread_id; /* 管理线程ID */
|
||||
|
||||
/*线程池信息*/
|
||||
int total_thr_num; /* 线程总数 */
|
||||
int live_thr_num; /* 线程池中存活的线程数 */
|
||||
int busy_thr_num; /* 忙线程,正在工作的线程 */
|
||||
unsigned long long task_num; /* 已执行的所有任务量统计值(不包含正在执行中的任务) */
|
||||
|
||||
/*任务队列信息*/
|
||||
queue_t* queue[64];
|
||||
int queue_max_size; /* 队列能容纳的最大任务数 */
|
||||
pthread_mutex_t queue_mutex; /* 多个工作线程创建队列时加锁 */
|
||||
int free_queue;
|
||||
|
||||
/*线程池状态*/
|
||||
int shutdown; /* 线程池开关: FALSE: 开启 TRUE: 关闭 */
|
||||
} threadpool_t;
|
||||
|
||||
|
||||
/*
|
||||
功能:创建线程池-多队列*
|
||||
注意:这里的queue_max_size是一个工作线程的队列容量
|
||||
*/
|
||||
threadpool_t* threadpool_create(int thread_num, int queue_max_size);
|
||||
|
||||
/*
|
||||
功能:添加任务-多队列
|
||||
注意:该功能使用时必须加锁(必须与销毁线程(threadpool_destroy)使用相同的锁)
|
||||
*/
|
||||
int threadpool_add_task(threadpool_t *threadpool, int* (*function)(void* arg, void* device), void* arg, void* device);
|
||||
|
||||
/*
|
||||
功能:销毁线程
|
||||
注意:该功能使用时必须加锁(必须与添加任务(threadpool_add_task)使用相同的锁)
|
||||
*/
|
||||
int threadpool_destroy(threadpool_t *threadpool);
|
||||
|
||||
/*创建线程池中设备连接*/
|
||||
int threadpool_create_connect(
|
||||
threadpool_t *threadpool,
|
||||
const char *pcIpList[],
|
||||
int iPortList[],
|
||||
int dev_num,
|
||||
int iConnectTimeout);
|
||||
|
||||
/*断开线程池中的所有设备连接*/
|
||||
int threadpool_disconnect(threadpool_t *threadpool);
|
||||
|
||||
/*设置线程池中的连接信息*/
|
||||
int threadpool_set_connect_info(
|
||||
threadpool_t *threadpool,
|
||||
const char *pcIpList[],
|
||||
int iPortList[],
|
||||
int dev_num,
|
||||
int iConnectTimeout,
|
||||
int iDealTimeOut);
|
||||
|
||||
/*清除线程池中的连接信息*/
|
||||
int threadpool_clear_connect_info(threadpool_t *threadpool);
|
||||
|
||||
/*主备机监控恢复线程*/
|
||||
int threadpool_device_admin(threadpool_t *threadpool);
|
||||
|
||||
|
||||
#endif
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
#ifndef _TRANS_H_
|
||||
#define _TRANS_H_
|
||||
|
||||
#ifndef _THREADPOOL_H_
|
||||
#include "threadpool.h"
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char LENGTH[2];
|
||||
unsigned char HEADER[8];
|
||||
unsigned char COMMAND[2];
|
||||
unsigned char data[8192];
|
||||
} TRD_PKG_ST;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TRD_PKG_ST send_pkg_st;
|
||||
TRD_PKG_ST recv_pkg_st;
|
||||
char cmd[3];
|
||||
int deal_time;
|
||||
int task_ret;
|
||||
int task_flag;
|
||||
int connect_idex;
|
||||
} TRD_PKG_THREADPOOL_ST;
|
||||
|
||||
int Get_retcode(TRD_PKG_ST *pkg);
|
||||
unsigned int Get_sn(TRD_PKG_ST *pkg);
|
||||
char *Get_cmd(TRD_PKG_ST *pkg);
|
||||
int Get_length(TRD_PKG_ST *pkg);
|
||||
void Set_sn(TRD_PKG_ST *pkg, unsigned int sn);
|
||||
void Set_cmd(TRD_PKG_ST *pkg, char *cmd);
|
||||
void Set_length(TRD_PKG_ST *pkg, int length);
|
||||
int Packet_Recv(int fd, TRD_PKG_ST *pkg, int pkgsize);
|
||||
int Packet_Send(int fd, TRD_PKG_ST *pkg, int pkgsize);
|
||||
void SetDataLength(int iInData, unsigned char* pcOutData);
|
||||
int GetDataLength(unsigned char* pcInData);
|
||||
|
||||
|
||||
/*非阻塞-线程回调-主备模式*/
|
||||
int TP_Process_NoBlock_Standby(char *cmd, TRD_PKG_ST *send_pkg, TRD_PKG_ST *recv_pkg, void *device, int deal_time);
|
||||
|
||||
/*非阻塞-非线程回调-主备模式*/
|
||||
int TP_Process_NoBlock_Ordinary(char *cmd, TRD_PKG_ST *send_pkg, TRD_PKG_ST *recv_pkg, STC_CONNECT_INFO *connect_info, int *connect_idex, int device_count, int deal_time);
|
||||
|
||||
int TP_Process_NoBlock(threadpool_t *threadpool, pthread_mutex_t *threadpool_mutex, TRD_PKG_THREADPOOL_ST *pkg_threadpool_st, int pkg_number);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef _TYPEDEF_H_
|
||||
#define _TYPEDEF_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/time.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
#ifndef _ERROR_H_
|
||||
#include "error.h"
|
||||
#endif
|
||||
|
||||
/* 内存访问栅 */
|
||||
#define barrier() (__sync_synchronize())
|
||||
|
||||
/*原子设置,如果原值和新值不一样则设置*/
|
||||
#define AO_SET(ptr, value) ((void)__sync_lock_test_and_set((ptr), (value)))
|
||||
|
||||
/*原子获取*/
|
||||
#define AO_GET(ptr) ({ __typeof__(*(ptr)) volatile *_val = (ptr); barrier(); (*_val); })
|
||||
|
||||
/*原子加*/
|
||||
#define AO_F_ADD(ptr, value) ((__typeof__(*(ptr)))__sync_fetch_and_add((ptr), (value)))
|
||||
|
||||
/*原子减*/
|
||||
#define AO_F_SUB(ptr, value) ((__typeof__(*(ptr)))__sync_fetch_and_sub((ptr), (value)))
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user