拆分数据操作

This commit is contained in:
nnhy 2015-12-18 15:54:51 +00:00
parent 7834ca5884
commit bf804bdc78
4 changed files with 78 additions and 66 deletions

53
TinyNet/DataMessage.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "DataMessage.h"
// 读取数据
bool ReadData(Stream& ms, const Array& bs, uint offset, uint len)
{
TS("DataMessage::ReadData");
int remain = bs.Length() - offset;
if(remain < 0)
{
ms.Write((byte)2);
ms.WriteEncodeInt(offset);
ms.WriteEncodeInt(len);
return false;
}
else
{
ms.WriteEncodeInt(offset);
if(len > remain) len = remain;
if(len > 0) ms.Write(bs.GetBuffer(), offset, len);
return true;
}
}
// 写入数据
bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds)
{
TS("DataMessage::WriteData");
// 剩余可写字节数
uint len = ds.Remain();
int remain = bs.Length() - offset;
if(remain < 0)
{
ms.Write((byte)2);
ms.WriteEncodeInt(offset);
ms.WriteEncodeInt(len);
return false;
}
else
{
ms.WriteEncodeInt(offset);
if(len > remain) len = remain;
bs.Copy(ds.Current(), len);
ms.WriteEncodeInt(len);
return true;
}
}

14
TinyNet/DataMessage.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __DataMessage_H__
#define __DataMessage_H__
#include "Stream.h"
// 数据消息
class DataMessage
{
public:
static bool ReadData(Stream& ms, const Array& bs, uint offset, uint len);
static bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds);
};
#endif

View File

@ -5,6 +5,7 @@
#include "JoinMessage.h"
#include "PingMessage.h"
#include "DataMessage.h"
TinyClient* TinyClient::Current = NULL;
@ -12,9 +13,6 @@ static void TinyClientTask(void* param);
//static void TinyClientReset();
static void GetDeviceKey(byte id, Array& key, void* param);
static bool ReadData(Stream& ms, const Array& bs, uint offset, uint len);
static bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds);
/******************************** 初始化和开关 ********************************/
TinyClient::TinyClient(TinyController* control)
@ -168,9 +166,9 @@ void TinyClient::OnRead(const TinyMessage& msg)
bool rt = true;
if(offset < 64)
rt = ReadData(ms2, Store.Data, offset, len);
rt = DataMessage::ReadData(ms2, Store.Data, offset, len);
else if(offset < 128)
rt = ReadData(ms2, Array(Cfg, Cfg->Length), offset - 64, len);
rt = DataMessage::ReadData(ms2, Array(Cfg, Cfg->Length), offset - 64, len);
rs.Error = !rt;
rs.Length = ms2.Position();
@ -198,11 +196,11 @@ void TinyClient::OnWrite(const TinyMessage& msg)
bool rt = true;
if(offset < 64)
rt = WriteData(ms2, Store.Data, offset, ms);
rt = DataMessage::WriteData(ms2, Store.Data, offset, ms);
else if(offset < 128)
{
Array cs(Cfg, Cfg->Length);
rt = WriteData(ms2, cs, offset - 64, ms);
rt = DataMessage::WriteData(ms2, cs, offset - 64, ms);
}
rs.Error = !rt;
@ -540,55 +538,3 @@ bool TinyClient::OnPing(const TinyMessage& msg)
return true;
}
// 读取数据
bool ReadData(Stream& ms, const Array& bs, uint offset, uint len)
{
TS("DataMessage::ReadData");
int remain = bs.Length() - offset;
if(remain < 0)
{
ms.Write((byte)2);
ms.WriteEncodeInt(offset);
ms.WriteEncodeInt(len);
return false;
}
else
{
ms.WriteEncodeInt(offset);
if(len > remain) len = remain;
if(len > 0) ms.Write(bs.GetBuffer(), offset, len);
return true;
}
}
// 写入数据
bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds)
{
TS("DataMessage::WriteData");
// 剩余可写字节数
uint len = ds.Remain();
int remain = bs.Length() - offset;
if(remain < 0)
{
ms.Write((byte)2);
ms.WriteEncodeInt(offset);
ms.WriteEncodeInt(len);
return false;
}
else
{
ms.WriteEncodeInt(offset);
if(len > remain) len = remain;
bs.Copy(ds.Current(), len);
ms.WriteEncodeInt(len);
return true;
}
}

View File

@ -4,6 +4,7 @@
#include "JoinMessage.h"
#include "PingMessage.h"
#include "DataMessage.h"
#include "Config.h"
#include "Drivers\ShunCom.h"
@ -489,7 +490,7 @@ bool TinyServer::OnRead(TinyMessage& msg, Device& dv)
return true;
}
// 读取响应,服务端趁机缓存一份。定时上报也是采用该指令。
// 读取响应,服务端趁机缓存一份。
bool TinyServer::OnReadReply(const TinyMessage& msg, Device& dv)
{
if(!msg.Reply || msg.Error) return false;
@ -497,14 +498,11 @@ bool TinyServer::OnReadReply(const TinyMessage& msg, Device& dv)
TS("TinyServer::OnReadReply");
//debug_printf("响应读取写入数据 \r\n") ;
// 起始地址为7位压缩编码整数
Stream ms = msg.ToStream();
auto ms = msg.ToStream();
uint offset = ms.ReadEncodeInt();
auto bs = dv.GetStore();
int remain = bs.Capacity() - offset;
if(remain < 0) return false;
uint len = ms.Remain();
@ -574,6 +572,7 @@ bool TinyServer::OnWrite(TinyMessage& msg, Device& dv)
return true;
}
//设置zigbee的通道2401无效
void TinyServer::SetChannel(byte channel)
{