112 lines
3.1 KiB
C++
112 lines
3.1 KiB
C++
#ifndef __LinkClient_H__
|
||
#define __LinkClient_H__
|
||
|
||
#include "Kernel\Sys.h"
|
||
|
||
#include "Message\DataStore.h"
|
||
#include "Message\Json.h"
|
||
|
||
// 物联客户端
|
||
class LinkClient
|
||
{
|
||
public:
|
||
bool Opened;
|
||
int Status; // 状态。0准备、1握手完成、2登录后
|
||
|
||
UInt64 LoginTime; // 登录时间ms
|
||
UInt64 LastSend; // 最后发送时间ms
|
||
UInt64 LastActive; // 最后活跃时间ms
|
||
int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间
|
||
int MaxNotActive; // 最大不活跃时间ms,超过该时间时重启系统。默认0
|
||
|
||
DataStore Store; // 数据存储区
|
||
Dictionary<cstring, IDelegate*> Routes; // 路由集合
|
||
|
||
LinkClient();
|
||
|
||
void Open();
|
||
void Close();
|
||
|
||
// 发送消息
|
||
bool Send(TokenMessage& msg, TokenController* ctrl = nullptr);
|
||
bool Reply(TokenMessage& msg, TokenController* ctrl = nullptr);
|
||
void OnReceive(TokenMessage& msg, TokenController& ctrl);
|
||
void OnReceiveLocal(TokenMessage& msg, TokenController& ctrl);
|
||
void LocalSend(int start, const Buffer& bs);
|
||
|
||
// 收到功能消息时触发
|
||
MessageHandler Received;
|
||
void* Param;
|
||
|
||
// 常用系统级消息
|
||
// 握手广播
|
||
void SayHello(bool broadcast);
|
||
|
||
// 注册
|
||
void Register();
|
||
|
||
// 登录
|
||
void Login();
|
||
// 重置并上报
|
||
void Reset(const String& reason);
|
||
void Reboot(const String& reason);
|
||
|
||
// Ping指令用于保持与对方的活动状态
|
||
void Ping();
|
||
|
||
void Read(int start, int size);
|
||
void Write(int start, const Buffer& bs);
|
||
void Write(int start, byte dat);
|
||
|
||
// 异步上传并等待响应,返回实际上传字节数
|
||
int WriteAsync(int start, const Buffer& bs, int msTimeout);
|
||
|
||
// 必须满足 start > 0 才可以。
|
||
void ReportAsync(int start, uint length = 1);
|
||
|
||
// 远程调用
|
||
void Invoke(const String& action, const Buffer& bs);
|
||
|
||
// 远程调用委托。传入参数名值对以及结果缓冲区引用,业务失败时返回false并把错误信息放在结果缓冲区
|
||
typedef bool(*InvokeHandler)(void* param, const Pair& args, Stream& result);
|
||
// 注册远程调用处理器
|
||
void Register(cstring action, InvokeHandler handler, void* param = nullptr);
|
||
// 模版支持成员函数
|
||
template<typename T>
|
||
void Register(cstring action, bool(T::*func)(const Pair&, Stream&), T* target)
|
||
{
|
||
Register(action, *(InvokeHandler*)&func, target);
|
||
}
|
||
|
||
static LinkClient* Current;
|
||
|
||
private:
|
||
// 跳转
|
||
bool OnRedirect(HelloMessage& msg);
|
||
|
||
bool OnLogin(TokenMessage& msg, TokenController* ctrl);
|
||
|
||
bool OnPing(TokenMessage& msg, TokenController* ctrl);
|
||
|
||
bool ChangeIPEndPoint(const NetUri& uri);
|
||
|
||
void OnRead(const TokenMessage& msg, TokenController* ctrl);
|
||
void OnWrite(const TokenMessage& msg, TokenController* ctrl);
|
||
|
||
void OnInvoke(const TokenMessage& msg, TokenController* ctrl);
|
||
bool OnInvoke(const String& action, const Pair& args, Stream& result);
|
||
|
||
private:
|
||
uint _task;
|
||
void* _Expect; // 等待内容
|
||
|
||
int NextReport = -1; // 下次上报偏移,-1不动
|
||
byte ReportLength; // 下次上报数据长度
|
||
|
||
void LoopTask();
|
||
bool CheckReport();
|
||
void CheckNet();
|
||
};
|
||
|
||
#endif
|