微网队列间隔5ms,超时50ms
This commit is contained in:
parent
a3e1cf9ccf
commit
cfd8f2022b
|
@ -1,7 +1,7 @@
|
||||||
#include "Controller.h"
|
#include "Controller.h"
|
||||||
|
|
||||||
#define MSG_DEBUG DEBUG
|
//#define MSG_DEBUG DEBUG
|
||||||
//#define MSG_DEBUG 0
|
#define MSG_DEBUG 0
|
||||||
#if MSG_DEBUG
|
#if MSG_DEBUG
|
||||||
#define msg_printf debug_printf
|
#define msg_printf debug_printf
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -18,8 +18,8 @@ TinyClient::TinyClient(TinyController* control)
|
||||||
{
|
{
|
||||||
assert_ptr(control);
|
assert_ptr(control);
|
||||||
|
|
||||||
Control = control;
|
Control = control;
|
||||||
Control->CallblackKey = GetDeviceKey;
|
Control->GetKey = GetDeviceKey;
|
||||||
|
|
||||||
|
|
||||||
Opened = false;
|
Opened = false;
|
||||||
|
|
|
@ -11,9 +11,6 @@
|
||||||
#define msg_printf(format, ...)
|
#define msg_printf(format, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void SendTask(void* param);
|
|
||||||
void StatTask(void* param);
|
|
||||||
|
|
||||||
/*================================ 微网消息 ================================*/
|
/*================================ 微网消息 ================================*/
|
||||||
typedef struct{
|
typedef struct{
|
||||||
byte Retry:2; // 重发次数。
|
byte Retry:2; // 重发次数。
|
||||||
|
@ -130,9 +127,9 @@ bool TinyMessage::Valid() const
|
||||||
{
|
{
|
||||||
if(Checksum == Crc) return true;
|
if(Checksum == Crc) return true;
|
||||||
|
|
||||||
debug_printf("Message::Valid Crc Error %04X != Checksum: %04X \r\n", Crc, Checksum);
|
msg_printf("Message::Valid Crc Error %04X != Checksum: %04X \r\n", Crc, Checksum);
|
||||||
#if MSG_DEBUG
|
#if MSG_DEBUG
|
||||||
debug_printf("指令校验错误 ");
|
msg_printf("指令校验错误 ");
|
||||||
Show();
|
Show();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -191,14 +188,13 @@ TinyMessage TinyMessage::CreateReply() const
|
||||||
}
|
}
|
||||||
|
|
||||||
/*================================ 微网控制器 ================================*/
|
/*================================ 微网控制器 ================================*/
|
||||||
|
void SendTask(void* param);
|
||||||
|
void StatTask(void* param);
|
||||||
|
|
||||||
// 构造控制器
|
// 构造控制器
|
||||||
TinyController::TinyController() : Controller()
|
TinyController::TinyController() : Controller()
|
||||||
{
|
{
|
||||||
_taskID = 0;
|
MinSize = TinyMessage::MinSize;
|
||||||
Interval = 2;
|
|
||||||
Timeout = 200;
|
|
||||||
|
|
||||||
MinSize = TinyMessage::MinSize;
|
|
||||||
|
|
||||||
// 初始化一个随机地址
|
// 初始化一个随机地址
|
||||||
Address = Sys.ID[0];
|
Address = Sys.ID[0];
|
||||||
|
@ -212,8 +208,10 @@ TinyController::TinyController() : Controller()
|
||||||
|
|
||||||
// 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0
|
// 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0
|
||||||
Mode = 0;
|
Mode = 0;
|
||||||
NoAck = false;
|
Interval = 10;
|
||||||
|
Timeout = 50;
|
||||||
|
|
||||||
|
_taskID = 0;
|
||||||
ArrayZero(_Queue);
|
ArrayZero(_Queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,7 +334,7 @@ bool TinyController::Dispatch(Stream& ms, Message* pmsg, void* param)
|
||||||
bool TinyController::Valid(const Message& msg)
|
bool TinyController::Valid(const Message& msg)
|
||||||
{
|
{
|
||||||
auto& tmsg = (TinyMessage&)msg;
|
auto& tmsg = (TinyMessage&)msg;
|
||||||
//debug_printf("TinyController::Valid\n");
|
|
||||||
// 代码为0是非法的
|
// 代码为0是非法的
|
||||||
if(!msg.Code) return false;
|
if(!msg.Code) return false;
|
||||||
// 没有源地址是很不负责任的
|
// 没有源地址是很不负责任的
|
||||||
|
@ -398,7 +396,7 @@ bool TinyController::Valid(const Message& msg)
|
||||||
if(tmsg.Dest == Address)
|
if(tmsg.Dest == Address)
|
||||||
{
|
{
|
||||||
ByteArray key(0);
|
ByteArray key(0);
|
||||||
CallblackKey(tmsg.Src, key, Param);
|
GetKey(tmsg.Src, key, Param);
|
||||||
if(key.Length() > 0) Encrypt(tmsg, key);
|
if(key.Length() > 0) Encrypt(tmsg, key);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -457,7 +455,6 @@ void TinyController::AckRequest(const TinyMessage& msg)
|
||||||
// 向对方发出Ack包
|
// 向对方发出Ack包
|
||||||
void TinyController::AckResponse(const TinyMessage& msg)
|
void TinyController::AckResponse(const TinyMessage& msg)
|
||||||
{
|
{
|
||||||
if(NoAck) return;
|
|
||||||
// 广播消息不要给确认
|
// 广播消息不要给确认
|
||||||
if(msg.Dest == 0) return;
|
if(msg.Dest == 0) return;
|
||||||
|
|
||||||
|
@ -496,7 +493,7 @@ bool TinyController::Send(Message& msg)
|
||||||
if(!tmsg.Reply) tmsg.Seq = ++_Sequence;
|
if(!tmsg.Reply) tmsg.Seq = ++_Sequence;
|
||||||
|
|
||||||
ByteArray key;
|
ByteArray key;
|
||||||
CallblackKey(tmsg.Dest, key, Param);
|
GetKey(tmsg.Dest, key, Param);
|
||||||
if(key.Length() > 0) Encrypt(tmsg, key);
|
if(key.Length() > 0) Encrypt(tmsg, key);
|
||||||
|
|
||||||
#if MSG_DEBUG
|
#if MSG_DEBUG
|
||||||
|
@ -609,7 +606,7 @@ void TinyController::Loop()
|
||||||
// 已过期则删除
|
// 已过期则删除
|
||||||
if(node.Expired < now)
|
if(node.Expired < now)
|
||||||
{
|
{
|
||||||
debug_printf("消息过期 Dest=0x%02X Seq=0x%02X Times=%d\r\n", node.Data[0], node.Seq, node.Times);
|
msg_printf("消息过期 Dest=0x%02X Seq=0x%02X Times=%d\r\n", node.Data[0], node.Seq, node.Times);
|
||||||
node.Using = 0;
|
node.Using = 0;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
@ -642,8 +639,9 @@ void TinyController::Loop()
|
||||||
node.LastSend = now;
|
node.LastSend = now;
|
||||||
|
|
||||||
// 随机延迟。随机数1~5。每次延迟递增
|
// 随机延迟。随机数1~5。每次延迟递增
|
||||||
byte rnd = (uint)now % 3;
|
//byte rnd = (uint)now % 3;
|
||||||
node.Next = now + (rnd + 1) * Interval;
|
//node.Next = now + (rnd + 1) * Interval;
|
||||||
|
node.Next = now + Interval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -738,7 +736,7 @@ bool RingQueue::Check(ushort item)
|
||||||
// 首先检查是否过期。如果已过期,说明很长时间都没有收到消息
|
// 首先检查是否过期。如果已过期,说明很长时间都没有收到消息
|
||||||
if(Expired < Sys.Ms())
|
if(Expired < Sys.Ms())
|
||||||
{
|
{
|
||||||
//debug_printf("环形队列过期,清空 \r\n");
|
//msg_printf("环形队列过期,清空 \r\n");
|
||||||
// 清空队列,重新开始
|
// 清空队列,重新开始
|
||||||
Index = 0;
|
Index = 0;
|
||||||
ArrayZero(Arr);
|
ArrayZero(Arr);
|
||||||
|
|
|
@ -126,12 +126,9 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
byte Address; // 本地地址
|
byte Address; // 本地地址
|
||||||
bool NoAck; // 是否不使用Ack,若为true,即使收到消息要求Ack也不发送Ack。默认false
|
|
||||||
byte Mode; // 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0
|
byte Mode; // 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0
|
||||||
uint Interval; // 消息队列发送间隔ms
|
ushort Interval; // 队列发送间隔,默认10ms
|
||||||
int Timeout; // 消息队列发送消息的默认超时时间ms。如果不需要超时重发,那么直接设置为-1
|
short Timeout; // 队列发送超时,默认50ms。如果不需要超时重发,那么直接设置为-1
|
||||||
|
|
||||||
void (*CallblackKey)(byte id, Array& key, void* param);
|
|
||||||
|
|
||||||
TinyController();
|
TinyController();
|
||||||
virtual ~TinyController();
|
virtual ~TinyController();
|
||||||
|
@ -151,6 +148,9 @@ public:
|
||||||
// 循环处理待发送的消息队列
|
// 循环处理待发送的消息队列
|
||||||
void Loop();
|
void Loop();
|
||||||
|
|
||||||
|
// 获取密钥的回调
|
||||||
|
void (*GetKey)(byte id, Array& key, void* param);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// 统计。平均值=(LastCost + TotalCost)/(LastSend + TotalSend)。每一组完成以后,TotalXXX整体复制给LastXXX
|
// 统计。平均值=(LastCost + TotalCost)/(LastSend + TotalSend)。每一组完成以后,TotalXXX整体复制给LastXXX
|
||||||
TinyStat Total; // 总统计
|
TinyStat Total; // 总统计
|
||||||
|
|
|
@ -25,8 +25,8 @@ TinyServer::TinyServer(TinyController* control)
|
||||||
Cfg = NULL;
|
Cfg = NULL;
|
||||||
DeviceType = Sys.Code;
|
DeviceType = Sys.Code;
|
||||||
|
|
||||||
Control->Received = OnServerReceived;
|
Control->Received = OnServerReceived;
|
||||||
Control->CallblackKey = GetDeviceKey;
|
Control->GetKey = GetDeviceKey;
|
||||||
Control->Param = this;
|
Control->Param = this;
|
||||||
|
|
||||||
Control->Mode = 2; // 服务端接收所有消息
|
Control->Mode = 2; // 服务端接收所有消息
|
||||||
|
|
Loading…
Reference in New Issue