Net/include/server.h

432 lines
12 KiB
C
Raw Normal View History

2019-01-15 17:36:22 +00:00
//
// server.hpp
// Net
//
// Created by 胡一兵 on 2019/1/16.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#ifndef server_h
#define server_h
2019-01-17 14:55:52 +00:00
#include "clock.h"
2019-01-15 17:36:22 +00:00
#include "net.h"
2019-01-17 14:55:52 +00:00
#include "cpart.h"
#include "cthread.h"
2019-02-05 14:37:32 +00:00
#include "sqlite3.h"
2019-02-04 08:31:54 +00:00
#include "rsa.h"
2019-02-08 16:05:01 +00:00
#include "rng.hpp"
#include "aes.h"
#include "sha1.h"
2019-01-15 17:36:22 +00:00
class Server;
//外来数据包解析结构
struct compute_result{
string name;
vector<void *> *args_in;
vector<void *> *args_out;
2019-01-16 10:08:59 +00:00
vector<int> *fargs_in;
vector<int> *fargs_out;
};
2019-02-04 08:31:54 +00:00
//请求数据包
struct request {
2019-02-08 14:11:07 +00:00
// 匹配id
2019-02-08 16:05:01 +00:00
uint64_t r_id;
2019-02-08 14:11:07 +00:00
// 类型
2019-02-04 08:31:54 +00:00
string type;
2019-02-08 14:11:07 +00:00
// 数据
2019-02-04 08:31:54 +00:00
string data;
2019-02-08 14:11:07 +00:00
// 接收端口
2019-02-06 13:22:03 +00:00
uint32_t recv_port;
// json结构
Document req_doc;
StringBuffer doc_str;
2019-02-08 14:11:07 +00:00
// 标记是否为加密请求
bool if_encrypt;
2019-02-05 14:37:32 +00:00
Addr t_addr;
request();
void Json2Data(void);
void JsonParse(string data_from);
2019-02-05 14:37:32 +00:00
};
2019-02-08 14:11:07 +00:00
//加密端对端报文
struct encrypt_post{
// 注册客户端id
2019-02-08 16:05:01 +00:00
uint64_t client_id;
// 目标地址信息
Addr t_addr;
2019-02-08 14:11:07 +00:00
// 匹配id
uint64_t p_id;
2019-02-08 14:11:07 +00:00
// 类型
uint32_t type;
// 内容
Byte *buff = nullptr;
// 内容长度
uint32_t buff_size = 0;
2019-02-26 05:43:52 +00:00
Document edoc;
2019-02-26 14:25:59 +00:00
StringBuffer sb;
2019-02-26 05:43:52 +00:00
bool Parse(string json);
2019-02-26 14:25:59 +00:00
void SelfParse(void);
2019-02-26 05:43:52 +00:00
void GetJSON(string &json);
void SetBuff(Byte *buff, uint32_t size);
void FreeBuff(void);
~encrypt_post(void);
2019-02-26 05:43:52 +00:00
void InitNew(uint64_t client_id, Addr t_addr, const char *type);
2019-02-08 14:11:07 +00:00
};
//回复数据包
2019-02-05 14:37:32 +00:00
struct respond {
2019-02-08 16:05:01 +00:00
uint64_t r_id;
2019-02-05 14:37:32 +00:00
string type;
2019-02-06 13:22:03 +00:00
Byte *buff = nullptr;
uint32_t buff_size;
2019-02-05 14:37:32 +00:00
Addr t_addr;
2019-02-06 13:22:03 +00:00
void SetBuff(Byte *buff, uint32_t size);
~respond();
2019-02-04 08:31:54 +00:00
};
2019-02-03 16:26:45 +00:00
2019-01-17 17:25:01 +00:00
//通用数据包类
class packet{
public:
// 数据包类型
2019-01-15 17:36:22 +00:00
unsigned int type;
2019-02-05 14:37:32 +00:00
struct sockaddr_in address;
2019-01-17 17:25:01 +00:00
// 记录块的大小及内容所在的内存地址
2019-01-15 17:36:22 +00:00
vector<pair<unsigned int, void *>> buffs;
void AddBuff(const void *pbuff, uint32_t size);
2019-02-08 14:11:07 +00:00
bool if_encrypt = false;
2019-02-05 14:37:32 +00:00
~packet();
2019-01-15 17:36:22 +00:00
};
2019-01-17 17:25:01 +00:00
//带标签的二进制串管理结构
class raw_data{
public:
// 二进制串
unsigned char *data = NULL;
2019-01-16 10:08:59 +00:00
unsigned long size = 0;
uint64_t r_id;
2019-01-17 17:25:01 +00:00
// 标签
2019-01-16 10:08:59 +00:00
uint32_t head, tail;
uint32_t info;
2019-01-17 17:25:01 +00:00
// 信息串
2019-01-16 10:08:59 +00:00
char *msg = NULL;
2019-03-04 10:40:45 +00:00
unsigned long msg_size = 0;
2019-02-05 14:37:32 +00:00
// 来源ip地址
struct sockaddr_in address;
2019-01-17 17:25:01 +00:00
// 用简单字符串直接出适合
2019-01-16 11:32:15 +00:00
void setData(string str){
data = (unsigned char *)malloc(str.size());
2019-01-18 03:09:08 +00:00
size = str.size();
2019-01-16 11:32:15 +00:00
memcpy(data, str.data(),str.size());
}
raw_data();
2019-01-16 10:08:59 +00:00
};
2019-02-07 09:48:49 +00:00
//请求监听管理结构
struct request_listener{
2019-02-08 14:11:07 +00:00
void (*callback)(respond *,void *args);
2019-02-07 09:48:49 +00:00
request *p_req;
uint32_t timeout;
uint32_t clicks;
raw_data trwd;
bool active;
2019-02-08 14:11:07 +00:00
void *args;
2019-02-07 09:48:49 +00:00
~request_listener();
};
2019-02-03 16:26:45 +00:00
struct server_info{
string tag;
string name;
string msqes_ip;
int msqes_prot;
string key;
};
2019-02-08 16:05:01 +00:00
struct aes_key256{
uint64_t key[4];
uint64_t iv[4];
// 生成新的随机密钥
2019-02-08 16:05:01 +00:00
aes_key256();
void MakeIV(void);
// 获得初始化向量
const uint8_t *GetIV(void);
const uint8_t *GetKey(void);
2019-02-08 16:05:01 +00:00
};
//UDP分包
struct net_box{
uint16_t idx;
uint16_t cnt;
uint32_t head;
uint32_t tail;
uint64_t b_id;
void *data = nullptr;
uint16_t data_size = 0;
UByte *send_data = nullptr;
uint16_t sdt_size = 0;
void set(void *pbuff, uint16_t pbsize);
void build(void);
void FreeNetBox(void);
net_box();
~net_box();
};
//UDP分包监听结构
struct box_listener{
uint64_t b_id;
// 生命
int32_t clicks;
// 应该接收的分包数量
uint16_t cnt;
// 接收到的分包数量
uint16_t nbn;
2019-02-25 09:26:25 +00:00
//分包来源地址
sockaddr_in address;
// 储存接收到的分包的动态数组
net_box **boxs;
// 合并分包成RawData
void TogtRawData(raw_data &trdt);
// 释放动态数组所关联的所有内存
void free_boxs(void);
};
//注册客户端管理
struct client_register{
// 客户端id
uint64_t client_id;
// 通信密钥
aes_key256 key;
string name;
string tag;
// 服务器资源租用时间
uint32_t click;
// 认证口令
uint64_t passwd;
// 目标地址信息
Addr t_addr;
// 守护线程ID
pthread_t tid;
2019-03-04 10:40:45 +00:00
sqlite3 *psql;
};
struct client_listen{
bool if_get;
bool if_connected = true;
pthread_t pid;
SocketTCPClient *ptcps;
encrypt_post *pcryp;
client_register *pcltr;
};
2019-03-04 10:40:45 +00:00
struct connection_info {
bool if_listen = false;
bool if_beat = false;
bool if_send = false;
};
struct connection_listener{
int data_sfd;
Addr client_addr;
aes_key256 key;
2019-02-26 14:25:59 +00:00
pthread_t pid = 0;
void *father_buff = nullptr;
SocketTCPCServer *server_cnt = nullptr;
bool if_active = true;
bool *pif_atv = nullptr;
2019-03-04 10:40:45 +00:00
void *write_buff = nullptr;
struct connection_info *p_ci = nullptr;
pthread_t *beat_pid = nullptr, *listen_pid = nullptr, *send_pid = nullptr;
2019-03-05 05:03:19 +00:00
sqlite3 *psql;
};
2019-01-17 14:55:52 +00:00
//通用服务器类
2019-01-15 17:36:22 +00:00
class Server{
2019-01-17 17:25:01 +00:00
protected:
2019-01-18 03:09:08 +00:00
// 缓存通用数据包
2019-02-04 08:31:54 +00:00
list<packet *> packets_in;
2019-01-18 03:09:08 +00:00
// 缓存带标签的二进制串管理结构
2019-02-04 08:31:54 +00:00
list<raw_data *> rawdata_in;
map<uint64_t, client_register *> rids;
2019-02-06 13:22:03 +00:00
// 输出的数据包列表
list<packet *> packets_out;
map<uint64_t,box_listener *> boxls;
2019-02-03 16:26:45 +00:00
struct server_info tsi;
2019-02-04 08:31:54 +00:00
sqlite3 *psql;
2019-02-08 14:11:07 +00:00
// 服务器公私钥
public_key_class pkc;
private_key_class prc;
2019-01-17 17:25:01 +00:00
public:
2019-01-17 14:55:52 +00:00
// 服务器类的接收套接字对象与发送套接字对象
2019-01-17 17:25:01 +00:00
SocketUDPServer socket;
SocketUDPClient send_socket;
2019-02-05 14:37:32 +00:00
int packet_max = 1024;
2019-01-17 17:25:01 +00:00
Server(int port = 9048, string send_ip = "127.0.0.1",int send_port = 9049);
2019-01-16 11:32:15 +00:00
// 重新设置服务器的发送端口
2019-01-17 14:55:52 +00:00
void SetSendPort(int port);
2019-01-16 11:32:15 +00:00
// 重新设置服务器的发送IP地址
2019-01-17 14:55:52 +00:00
void SetSendIP(string ip_addr);
// 将结构数据包转换成二进制串
2019-02-04 08:31:54 +00:00
static void Packet2Rawdata(packet &tpkt, raw_data &rdt);
2019-01-17 14:55:52 +00:00
// 将通用二进制串转换为通用数据包
2019-02-05 14:37:32 +00:00
static void Rawdata2Packet(packet &tpkt, raw_data &trdt);
2019-01-17 14:55:52 +00:00
// 释放二进制串占用的空间
2019-02-05 14:37:32 +00:00
static void freeRawdataServer(struct raw_data &trdt);
2019-01-17 14:55:52 +00:00
// 释放通用数据包包占用
static void freePcaketServer(struct packet tpkt);
2019-02-04 08:31:54 +00:00
2019-01-17 14:55:52 +00:00
// 释放计算结果包占用的空间
static void freeCPURServer(struct compute_result tcpur);
// 给二进制串贴上识别标签
static void SignedRawdata(struct raw_data *trdt,string info);
// 发送已经贴上标签的二进制串
int SentRawdata(struct raw_data *trdt);
// 检查消息串是否为一个贴上标签的二进制串
static bool CheckRawMsg(char *p_rdt, ssize_t size);
// 处理一个已贴上标签的原始二进制串,获得其包含的信息
2019-02-04 08:31:54 +00:00
static void ProcessSignedRawMsg(char *p_rdt, ssize_t size, raw_data &rdt);
2019-02-08 14:11:07 +00:00
// 解码已加密的原始二进制串
static void DecryptRSARawMsg(raw_data &rdt, private_key_class &pkc);
2019-02-08 14:11:07 +00:00
// 编码原始二进制串
static void EncryptRSARawMsg(raw_data &rdt, public_key_class &pkc);
// 检查是否为UDP分包
static bool CheckNetBox(char *p_nb, ssize_t size);
// 将二进制信息转换成UDP分包
static void ProcessNetBox(net_box &tnb, Byte *p_data);
2019-01-18 03:09:08 +00:00
// 服务器守护线程
friend void *serverDeamon(void *psvr);
// 分包处理守护线程
friend void *boxProcessorDeamon(void *pvcti);
2019-02-04 08:31:54 +00:00
// 处理RawData
void ProcessRawData(void);
2019-02-06 13:22:03 +00:00
void ProcessSendPackets(void);
void CleaningBoxs(void);
2019-01-16 10:08:59 +00:00
2019-01-16 11:32:15 +00:00
2019-01-15 17:36:22 +00:00
};
2019-01-17 17:25:01 +00:00
//计算节点服务器类
class CNodeServer:public Server{
vector<compute_result> cpurs_in;
public:
// 将计算结果包转化为结构数据包
static packet CPURS2Packet(compute_result tcpur);
// 将结构数据包转化为计算结果包
static compute_result Packet2CPUR(packet *tpkt);
};
2019-02-04 08:31:54 +00:00
class SQEServer:public Server{
protected:
// 请求数据包
2019-02-05 14:37:32 +00:00
list<request *> req_list;
2019-02-08 14:11:07 +00:00
// 注册客户端管理
map<uint64_t,client_register *> client_lst;
2019-02-08 14:11:07 +00:00
// 加密端对端报文
list<encrypt_post *>post_lst;
2019-02-25 09:26:25 +00:00
//服务器名
string name;
2019-02-04 08:31:54 +00:00
public:
2019-02-05 14:37:32 +00:00
SQEServer(int port = 9048);
void ProcessPacket(void);
void ProcessRequset(void);
static void Packet2Request(packet &pkt, request &req);
static void Request2Packet(packet &pkt, request &req);
2019-02-06 13:22:03 +00:00
static void Respond2Packet(packet &pkt, respond &res);
static void Packet2Respond(packet &pkt, respond &res);
static void BuildBeatsRawData(raw_data &rwd);
static void BuildSmallRawData(raw_data &rwd, const char *info);
static void Post2SignedRawData(void *buff, uint32_t buff_size, const char *info, aes_key256 &key, raw_data &rw);
2019-02-26 05:43:52 +00:00
static void Post2SignedRawData(encrypt_post &ecyp, aes_key256 &key, raw_data &rw);
static void SignedRawData2Post(raw_data &rwd, encrypt_post &pst, aes_key256 &key);
static void Post2Packet(packet &pkt, encrypt_post &pst, aes_key256 &key);
static void Packet2Post(packet &pkt, encrypt_post &pst, aes_key256 &key);
static void GetPostInfo(packet &pkt, encrypt_post &pst);
2019-03-04 10:40:45 +00:00
static void SendConnectionInfo(SocketTCPClient *pcnt_sock, string type);
2019-02-06 13:22:03 +00:00
};
//通用客户端类
2019-02-06 13:22:03 +00:00
class Client{
2019-02-07 09:48:49 +00:00
// 请求监听列表
list<request_listener *> req_lst;
list<raw_data *> rwd_lst;
2019-02-21 11:02:31 +00:00
list<encrypt_post *> ecryp_lst;
2019-02-26 05:43:52 +00:00
//TCP模式下有效二进制段列表
list<raw_data *> rwd_tcp;
2019-02-07 09:48:49 +00:00
// 回复处理列表
list<respond *> res_lst;
// 请求监听端口
2019-02-25 09:26:25 +00:00
uint16_t listen_port;
2019-02-07 09:48:49 +00:00
SocketUDPServer socket;
SocketUDPClient send_socket;
// 与服务器建立的稳定链接
SocketTCPCServer *server_cnt;
2019-02-08 14:11:07 +00:00
// 广场服务器通信公钥
public_key_class sqe_pbc;
// 报文密钥
aes_key256 post_key;
// 客户端名与标签
string name,tag;
// 广场服务器服务密钥
string sqe_key;
// 数据库
sqlite3 *psql;
2019-02-07 09:48:49 +00:00
public:
// 构造函数(send_port指的是发送的目标端口)
Client(int port = 9050, string send_ip = "127.0.0.1",int send_port = 9049);
// 处理请求监听
void ProcessRequestListener(void);
// 新的请求
void NewRequest(request **ppreq,string send_ip,int send_port,string type, string data, bool if_encrypt = false);
2019-02-07 09:48:49 +00:00
// 新的请求监听
2019-02-08 14:11:07 +00:00
void NewRequestListener(request *preq, int timeout, void *args, void (*callback)(respond *, void *));
// 设置公钥
void SetPublicKey(public_key_class &t_pbc);
// 设置AES密钥
void SetAESKey(aes_key256 &key);
// 发送RawData
void SendRawData(raw_data *trdt);
2019-02-07 09:48:49 +00:00
// 友元回复接受守护进程
friend void *clientRespondDeamon(void *);
// 友元客户端控制器
friend int client(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets);
2019-02-04 08:31:54 +00:00
};
2019-02-06 13:22:03 +00:00
//设置服务器守护线程的时钟
2019-01-17 14:55:52 +00:00
void setServerClock(Server *psvr, int clicks);
2019-02-06 13:22:03 +00:00
//设置广场服务器守护线程的时钟
2019-02-05 14:37:32 +00:00
void setServerClockForSquare(SQEServer *psvr, int clicks);
//服务器接收数据包守护线程
2019-01-17 14:55:52 +00:00
void *serverDeamon(void *psvr);
2019-02-06 13:22:03 +00:00
//服务器处理原始数据守护线程
2019-02-05 14:37:32 +00:00
void *dataProcessorDeamon(void *pvcti);
//UDP分包监听守护进程
void *boxProcessorDeamon(void *pvcti);
//UDP分包监听清理守护进程
void *boxsCleaningProcessorDeamon(void *pvcti);
2019-02-06 13:22:03 +00:00
//广场服务器处理数据包守护线程
2019-02-05 14:37:32 +00:00
void *packetProcessorDeamonForSquare(void *pvcti);
2019-02-06 13:22:03 +00:00
//广场服务器处理请求守护线程
void *requestProcessorDeamonForSquare(void *pvcti);
//服务器发送数据包守护线程
void *sendPacketProcessorDeamonForSquare(void *pvcti);
2019-01-17 14:55:52 +00:00
2019-02-07 09:48:49 +00:00
//设置客户端请求监听守护时钟
void setClientClock(Client *pclient,int clicks);
//客户端请求监听守护线程
void *clientRequestDeamon(void *pvclt);
//客户端回复接收守护线程
void *clientRespondDeamon(void *pvclt);
2019-02-08 14:11:07 +00:00
//客户端待机守护线程
void *clientWaitDeamon(void *pvclt);
2019-02-07 09:48:49 +00:00
2019-01-15 17:36:22 +00:00
#endif /* server_h */