mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
完成系统管理web端功能,实现系统管理服务端接口,实现登录功能
This commit is contained in:
117
src/protocol/TcpEntity.h
Normal file
117
src/protocol/TcpEntity.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#ifndef _TcpEntity_H_
|
||||
#define _TcpEntity_H_
|
||||
|
||||
#include <Winsock2.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
|
||||
#include "Communicator.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum class ETcpEvent
|
||||
{
|
||||
NUL = 0, //
|
||||
SRV_SUCCESS, // 服务端启动成功(listen成功)
|
||||
ACCEPT, // 服务端接收到客户端的连接
|
||||
CONN_OK, // 作为客户端时,连接成功
|
||||
CONN_ERR, // 作为客户端时,连接失败
|
||||
MSG_RECV, // 接收消息
|
||||
MSG_SEND, // 发送消息
|
||||
CLOSE, // 断开连接
|
||||
ERR // 异常错误
|
||||
};
|
||||
|
||||
class TcpHandler;
|
||||
class TcpParser;
|
||||
|
||||
class TcpEntity : public CommEntity, public std::enable_shared_from_this<TcpEntity>
|
||||
{
|
||||
public:
|
||||
struct Client
|
||||
{
|
||||
SOCKET sock;
|
||||
SOCKADDR_IN sock_addr;
|
||||
std::string host;
|
||||
std::shared_ptr<TcpParser> parser = nullptr;
|
||||
};
|
||||
|
||||
public:
|
||||
// 初始化服务端
|
||||
TcpEntity(TcpHandler* handler = nullptr);
|
||||
~TcpEntity();
|
||||
|
||||
int start() override;
|
||||
void close() override;
|
||||
|
||||
void runThreadTcp();
|
||||
|
||||
void setHost(string host, int port, bool is_client);
|
||||
std::string host() { return host_; }
|
||||
int port() { return port_; }
|
||||
std::string hostport() { return host_ + ":" + std::to_string(port_); }
|
||||
|
||||
void setReconnect(int ms);
|
||||
|
||||
bool isClient() { return isClient_; }
|
||||
|
||||
void setHandler(TcpHandler* handler);
|
||||
|
||||
bool sendData(std::string data, std::string clientId="");
|
||||
|
||||
bool isAlive();
|
||||
bool isConnected();
|
||||
|
||||
|
||||
std::shared_ptr<TcpParser> parser = nullptr;
|
||||
|
||||
private:
|
||||
void runServerLoop();
|
||||
void runServerRecvLoop(Client client, std::string client_name);
|
||||
|
||||
void runClientLoop();
|
||||
|
||||
private:
|
||||
// 本机的SOCKET对象
|
||||
SOCKET sock_ = INVALID_SOCKET;
|
||||
|
||||
// socket addr信息
|
||||
SOCKADDR_IN sockaddr_;
|
||||
|
||||
// TCP类型是否是客户端: true: 客户端, false: 服务端
|
||||
bool isClient_ = true;
|
||||
|
||||
// 通讯地址,作为客户端时有效
|
||||
std::string host_;
|
||||
|
||||
// 通讯端口
|
||||
int port_ = 0;
|
||||
|
||||
// 重连间隔时间,单位秒
|
||||
int tReconnect_ = 0;
|
||||
|
||||
// 作为服务端时连接的客户端SOCKET
|
||||
std::vector<Client> vecClient_;
|
||||
|
||||
// 回调处理对象
|
||||
TcpHandler* handler_ = nullptr;
|
||||
|
||||
bool isAlive_ = false;
|
||||
bool isCloseRequest_ = false;
|
||||
bool isConnected_ = false;
|
||||
|
||||
// 状态更新时间戳
|
||||
int64_t ts_;
|
||||
|
||||
int64_t tsHeartbeat_=0;
|
||||
};
|
||||
|
||||
class TcpHandler
|
||||
{
|
||||
public:
|
||||
virtual void onEvent(TcpEntity* entity, TcpEntity::Client* client, ETcpEvent evt, std::string msg) {};
|
||||
};
|
||||
|
||||
#endif // !_TcpEntity_H_
|
||||
Reference in New Issue
Block a user