GSM07专用任务处理收到的指令,避免阻塞

This commit is contained in:
大石头 2017-05-08 15:25:42 +08:00
parent 6f3f0b8d31
commit c17c2735f9
2 changed files with 51 additions and 7 deletions

View File

@ -43,6 +43,8 @@ GSM07::GSM07()
//At.DataKey = "+CIPRCV:";
_task = 0;
InitConfig();
LoadConfig();
}
@ -136,6 +138,8 @@ bool GSM07::OnOpen()
// 接收数据时是否增加IP头提示
At.SendCmd("AT+CIPHEAD=1");
if (!_task) _task = Sys.AddTask(&GSM07::Process, this, -1, -1, "GSM07");
At.Received.Bind<GSM07>([](GSM07& gsm, Buffer& bs) { gsm.OnReceive(bs); }, this);
return true;
@ -306,22 +310,57 @@ Socket* GSM07::CreateSocket(NetType type)
}
}
// 数据到达
void GSM07::Process()
{
if (_Buffer.Length() <= 1) return;
byte idx = _Buffer[0];
if (idx >= ArrayLength(Sockets)) return;
// 分发给各个Socket
auto es = (GSMSocket**)Sockets;
auto sk = es[idx];
if (sk)
{
auto data = _Buffer.Sub(1, -1);
_Buffer.SetLength(0);
sk->OnProcess(data, _Remote);
}
// 清零长度,其它数据包才可能进来
_Buffer.SetLength(0);
}
// 数据到达
void GSM07::OnReceive(Buffer& bs)
{
OnProcess(0, bs, _Remote);
}
void GSM07::OnProcess(int index, Buffer& data, const IPEndPoint& remotre)
void GSM07::OnProcess(int index, Buffer& data, const IPEndPoint& remote)
{
Received(data);
// 分发到各个Socket
auto es = (GSMSocket**)Sockets;
auto sk = es[index];
if (sk)
// 如果有数据包正在处理,则丢弃
if (_Buffer.Length() > 0)
{
sk->OnProcess(data, remotre);
#if NET_DEBUG
net_printf("已有数据包 %d 字节正在处理,丢弃当前数据包 %d 字节 \r\n处理:", _Buffer.Length(), data.Length());
_Buffer.Show(true);
net_printf("当前:");
data.Show(true);
#endif
}
else
{
_Remote = remote;
// 第一字节放Socket索引数据包放在后面
_Buffer.SetAt(0, index);
_Buffer.Copy(1, data, 0, -1);
Sys.SetTask(_task, true, 0);
}
}

View File

@ -97,6 +97,8 @@ public:
bool IPTransparent(bool enable);
protected:
uint _task; // 调度任务
ByteArray _Buffer; // 待处理数据包
IPEndPoint _Remote; // 当前数据包远程地址
// 打开与关闭
@ -109,7 +111,10 @@ protected:
// 数据到达
virtual void OnReceive(Buffer& bs);
void OnProcess(int index, Buffer& data, const IPEndPoint& remotre);
void OnProcess(int index, Buffer& data, const IPEndPoint& remote);
// 处理收到的数据包
void Process();
};
class GSMSocket : public ITransport, public Socket