基础类库和系统内核尽可能的使用int替代uint,方便计算,避免溢出了也不知道。
This commit is contained in:
parent
91229367f1
commit
fc216e5f69
|
@ -0,0 +1,9 @@
|
||||||
|
################################################################################
|
||||||
|
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
/vs/Debug
|
||||||
|
*.lib
|
||||||
|
*.db
|
||||||
|
*.opendb
|
||||||
|
*.suo
|
|
@ -50,9 +50,9 @@ bool Alarm::Set(const Pair& args, Stream& result)
|
||||||
result.Write(id);
|
result.Write(id);
|
||||||
|
|
||||||
// 马上调度一次
|
// 马上调度一次
|
||||||
if(id) Sys.SetTask(_taskid, true, 0);
|
if (id) Sys.SetTask(_taskid, true, 0);
|
||||||
|
|
||||||
return id;
|
return id > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Alarm::Get(const Pair& args, Stream& result)
|
bool Alarm::Get(const Pair& args, Stream& result)
|
||||||
|
|
|
@ -59,7 +59,7 @@ public:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint _Capacity; // 最大个数。非字节数
|
int _Capacity; // 最大个数。非字节数
|
||||||
bool _needFree; // 是否需要释放
|
bool _needFree; // 是否需要释放
|
||||||
bool _canWrite; // 是否可写
|
bool _canWrite; // 是否可写
|
||||||
ushort _Size; // 单个元素大小。字节
|
ushort _Size; // 单个元素大小。字节
|
||||||
|
|
|
@ -91,7 +91,7 @@ void IList::Add(void* item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加多个元素
|
// 添加多个元素
|
||||||
void IList::Add(void** items, uint count)
|
void IList::Add(void** items, int count)
|
||||||
{
|
{
|
||||||
if(!items || !count) return;
|
if(!items || !count) return;
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ void IList::Add(void** items, uint count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除指定位置元素
|
// 删除指定位置元素
|
||||||
void IList::RemoveAt(uint index)
|
void IList::RemoveAt(int index)
|
||||||
{
|
{
|
||||||
int len = _Count;
|
int len = _Count;
|
||||||
if(len <= 0 || index >= len) return;
|
if(len <= 0 || index >= len) return;
|
||||||
|
|
|
@ -23,10 +23,10 @@ public:
|
||||||
void Add(void* item);
|
void Add(void* item);
|
||||||
|
|
||||||
// 添加多个元素
|
// 添加多个元素
|
||||||
void Add(void** items, uint count);
|
void Add(void** items, int count);
|
||||||
|
|
||||||
// 删除指定位置元素
|
// 删除指定位置元素
|
||||||
void RemoveAt(uint index);
|
void RemoveAt(int index);
|
||||||
|
|
||||||
// 删除指定元素
|
// 删除指定元素
|
||||||
int Remove(const void* item);
|
int Remove(const void* item);
|
||||||
|
@ -49,8 +49,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void** _Arr;
|
void** _Arr;
|
||||||
uint _Count;
|
int _Count;
|
||||||
uint _Capacity;
|
int _Capacity;
|
||||||
|
|
||||||
void* Arr[0x04];
|
void* Arr[0x04];
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ Queue::Queue() : _s((void*)nullptr, 0)
|
||||||
Clear();
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Queue::SetCapacity(uint len)
|
void Queue::SetCapacity(int len)
|
||||||
{
|
{
|
||||||
_s.SetLength(len);
|
_s.SetLength(len);
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ INROOT byte Queue::Dequeue()
|
||||||
return dat;
|
return dat;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint Queue::Write(const Buffer& bs)
|
int Queue::Write(const Buffer& bs)
|
||||||
{
|
{
|
||||||
int total = _s.Capacity();
|
int total = _s.Capacity();
|
||||||
if(!total) _s.SetLength(64);
|
if(!total) _s.SetLength(64);
|
||||||
|
@ -77,17 +77,17 @@ uint Queue::Write(const Buffer& bs)
|
||||||
4,如果队列过小,很有可能后来数据会覆盖前面数据
|
4,如果队列过小,很有可能后来数据会覆盖前面数据
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint len = bs.Length();
|
int len = bs.Length();
|
||||||
|
|
||||||
// 如果队列满了,不需要覆盖
|
// 如果队列满了,不需要覆盖
|
||||||
if(_size + len > total)
|
if(_size + len > total)
|
||||||
len = total - _size;
|
len = total - _size;
|
||||||
|
|
||||||
uint rs = 0;
|
int rs = 0;
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
// 计算这一个循环剩下的位置
|
// 计算这一个循环剩下的位置
|
||||||
uint remain = _s.Capacity() - _head;
|
int remain = _s.Capacity() - _head;
|
||||||
// 如果要写入的数据足够存放
|
// 如果要写入的数据足够存放
|
||||||
if(len <= remain)
|
if(len <= remain)
|
||||||
{
|
{
|
||||||
|
@ -113,7 +113,7 @@ uint Queue::Write(const Buffer& bs)
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint Queue::Read(Buffer& bs)
|
int Queue::Read(Buffer& bs)
|
||||||
{
|
{
|
||||||
if(_size == 0) return 0;
|
if(_size == 0) return 0;
|
||||||
|
|
||||||
|
@ -124,17 +124,17 @@ uint Queue::Read(Buffer& bs)
|
||||||
4,如果队列过小,很有可能后来数据会覆盖前面数据
|
4,如果队列过小,很有可能后来数据会覆盖前面数据
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint len = bs.Length();
|
int len = bs.Length();
|
||||||
if(!len) return 0;
|
if(!len) return 0;
|
||||||
|
|
||||||
if(len > _size) len = _size;
|
if(len > _size) len = _size;
|
||||||
|
|
||||||
uint rs = 0;
|
int rs = 0;
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
int total = _s.Capacity();
|
int total = _s.Capacity();
|
||||||
// 计算这一个循环剩下的位置
|
// 计算这一个循环剩下的位置
|
||||||
uint remain = total - _tail;
|
int remain = total - _tail;
|
||||||
// 如果要读取的数据都在这里
|
// 如果要读取的数据都在这里
|
||||||
if(len <= remain)
|
if(len <= remain)
|
||||||
{
|
{
|
||||||
|
|
16
Core/Queue.h
16
Core/Queue.h
|
@ -9,25 +9,25 @@ class Queue
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Array _s; // 数据流
|
Array _s; // 数据流
|
||||||
uint _head; // 头部位置
|
int _head; // 头部位置
|
||||||
uint _tail; // 尾部位置
|
int _tail; // 尾部位置
|
||||||
uint _size; // 长度
|
int _size; // 长度
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Queue();
|
Queue();
|
||||||
|
|
||||||
bool Empty() const { return _size == 0; } // 队列空
|
bool Empty() const { return _size == 0; } // 队列空
|
||||||
uint Capacity() const { return _s.Capacity(); } // 队列容量
|
int Capacity() const { return _s.Capacity(); } // 队列容量
|
||||||
uint Length() const { return _size; } // 队列大小
|
int Length() const { return _size; } // 队列大小
|
||||||
void SetCapacity(uint len);
|
void SetCapacity(int len);
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
void Enqueue(byte dat);
|
void Enqueue(byte dat);
|
||||||
byte Dequeue();
|
byte Dequeue();
|
||||||
|
|
||||||
uint Write(const Buffer& bs); // 批量写入
|
int Write(const Buffer& bs); // 批量写入
|
||||||
uint Read(Buffer& bs); // 批量读取
|
int Read(Buffer& bs); // 批量读取
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -11,19 +11,19 @@ Random::Random()
|
||||||
srand((uint)time(NULL));
|
srand((uint)time(NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
Random::Random(uint seed)
|
Random::Random(int seed)
|
||||||
{
|
{
|
||||||
srand(seed);
|
srand(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint Random::Next() const
|
int Random::Next() const
|
||||||
{
|
{
|
||||||
return rand();
|
return rand();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint Random::Next(uint max) const
|
int Random::Next(int max) const
|
||||||
{
|
{
|
||||||
uint value = rand();
|
int value = rand();
|
||||||
|
|
||||||
if(max == 0x100) return value & 0xFF;
|
if(max == 0x100) return value & 0xFF;
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,10 @@ class Random
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Random();
|
Random();
|
||||||
Random(uint seed);
|
Random(int seed);
|
||||||
|
|
||||||
uint Next() const;
|
int Next() const;
|
||||||
uint Next(uint max) const;
|
int Next(int max) const;
|
||||||
void Next(Buffer& bs) const;
|
void Next(Buffer& bs) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -158,14 +158,14 @@ private:
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void release();
|
void release();
|
||||||
bool Concat(cstring cstr, uint length);
|
bool Concat(cstring cstr, int length);
|
||||||
|
|
||||||
String& copy(cstring cstr, uint length);
|
String& copy(cstring cstr, int length);
|
||||||
void move(String& rhs);
|
void move(String& rhs);
|
||||||
bool CopyOrWrite();
|
bool CopyOrWrite();
|
||||||
|
|
||||||
using Array::CheckCapacity;
|
using Array::CheckCapacity;
|
||||||
bool CheckCapacity(uint size);
|
bool CheckCapacity(int size);
|
||||||
virtual void* Alloc(int len);
|
virtual void* Alloc(int len);
|
||||||
|
|
||||||
int Search(cstring str, int len, int startIndex, bool rev) const;
|
int Search(cstring str, int len, int startIndex, bool rev) const;
|
||||||
|
|
126
Core/Stream.cpp
126
Core/Stream.cpp
|
@ -11,12 +11,12 @@ extern ushort _REV16(ushort);
|
||||||
extern uint _REV(uint);
|
extern uint _REV(uint);
|
||||||
|
|
||||||
// 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度
|
// 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度
|
||||||
Stream::Stream(void* buf, uint len)
|
Stream::Stream(void* buf, int len)
|
||||||
{
|
{
|
||||||
Init(buf, len);
|
Init(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream::Stream(const void* buf, uint len)
|
Stream::Stream(const void* buf, int len)
|
||||||
{
|
{
|
||||||
Init((void*)buf, len);
|
Init((void*)buf, len);
|
||||||
|
|
||||||
|
@ -36,9 +36,9 @@ Stream::Stream(const Buffer& bs)
|
||||||
CanWrite = false;
|
CanWrite = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream::~Stream(){ }
|
Stream::~Stream() { }
|
||||||
|
|
||||||
void Stream::Init(void* buf, uint len)
|
void Stream::Init(void* buf, int len)
|
||||||
{
|
{
|
||||||
assert(buf, "buf");
|
assert(buf, "buf");
|
||||||
|
|
||||||
|
@ -51,13 +51,13 @@ void Stream::Init(void* buf, uint len)
|
||||||
Little = true;
|
Little = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Stream::CheckRemain(uint count)
|
bool Stream::CheckRemain(int count)
|
||||||
{
|
{
|
||||||
uint remain = _Capacity - _Position;
|
int remain = _Capacity - _Position;
|
||||||
// 容量不够,需要扩容
|
// 容量不够,需要扩容
|
||||||
if(count > remain)
|
if (count > remain)
|
||||||
{
|
{
|
||||||
if(CanResize)
|
if (CanResize)
|
||||||
debug_printf("数据流 0x%p 剩余容量 (%d - %d) = %d 不足 %d ,无法扩容!\r\n", this, _Capacity, _Position, remain, count);
|
debug_printf("数据流 0x%p 剩余容量 (%d - %d) = %d 不足 %d ,无法扩容!\r\n", this, _Capacity, _Position, remain, count);
|
||||||
else
|
else
|
||||||
assert(false, "无法扩容");
|
assert(false, "无法扩容");
|
||||||
|
@ -69,18 +69,18 @@ bool Stream::CheckRemain(uint count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 数据流容量
|
// 数据流容量
|
||||||
uint Stream::Capacity() const { return _Capacity; }
|
int Stream::Capacity() const { return _Capacity; }
|
||||||
void Stream::SetCapacity(uint len) { CheckRemain(len - _Position); }
|
void Stream::SetCapacity(int len) { CheckRemain(len - _Position); }
|
||||||
|
|
||||||
// 当前位置
|
// 当前位置
|
||||||
uint Stream::Position() const { return _Position; }
|
int Stream::Position() const { return _Position; }
|
||||||
|
|
||||||
// 设置位置
|
// 设置位置
|
||||||
bool Stream::SetPosition(int p)
|
bool Stream::SetPosition(int p)
|
||||||
{
|
{
|
||||||
// 允许移动到最后一个字节之后,也就是Length
|
// 允许移动到最后一个字节之后,也就是Length
|
||||||
//assert(p <= Length, "设置的位置超出长度");
|
//assert(p <= Length, "设置的位置超出长度");
|
||||||
if(p < 0 && p > Length)
|
if (p < 0 && p > Length)
|
||||||
{
|
{
|
||||||
debug_printf("设置的位置 %d 超出长度 %d\r\n", p, Length);
|
debug_printf("设置的位置 %d 超出长度 %d\r\n", p, Length);
|
||||||
return false;
|
return false;
|
||||||
|
@ -91,12 +91,12 @@ bool Stream::SetPosition(int p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 余下的有效数据流长度。0表示已经到达终点
|
// 余下的有效数据流长度。0表示已经到达终点
|
||||||
uint Stream::Remain() const { return Length - _Position; };
|
int Stream::Remain() const { return Length - _Position; };
|
||||||
|
|
||||||
// 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标
|
// 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标
|
||||||
bool Stream::Seek(int offset)
|
bool Stream::Seek(int offset)
|
||||||
{
|
{
|
||||||
if(offset == 0) return true;
|
if (offset == 0) return true;
|
||||||
|
|
||||||
return SetPosition(offset + _Position);
|
return SetPosition(offset + _Position);
|
||||||
}
|
}
|
||||||
|
@ -108,25 +108,25 @@ byte* Stream::GetBuffer() const { return _Buffer; }
|
||||||
byte* Stream::Current() const { return &_Buffer[_Position]; }
|
byte* Stream::Current() const { return &_Buffer[_Position]; }
|
||||||
|
|
||||||
// 读取7位压缩编码整数
|
// 读取7位压缩编码整数
|
||||||
uint Stream::ReadEncodeInt()
|
int Stream::ReadEncodeInt()
|
||||||
{
|
{
|
||||||
uint value = 0;
|
int value = 0;
|
||||||
// 同时计算最大4字节,避免无限读取错误数据
|
// 同时计算最大4字节,避免无限读取错误数据
|
||||||
for(int i = 0, k = 0; i < 4; i++, k += 7)
|
for (int i = 0, k = 0; i < 4; i++, k += 7)
|
||||||
{
|
{
|
||||||
int temp = ReadByte();
|
int temp = ReadByte();
|
||||||
if(temp < 0) break;
|
if (temp < 0) break;
|
||||||
|
|
||||||
value |= (temp & 0x7F) << k;
|
value |= (temp & 0x7F) << k;
|
||||||
if((temp & 0x80) == 0) break;
|
if ((temp & 0x80) == 0) break;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
|
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
|
||||||
uint Stream::Read(Buffer& bs)
|
int Stream::Read(Buffer& bs)
|
||||||
{
|
{
|
||||||
if(bs.Length() == 0) return 0;
|
if (bs.Length() == 0) return 0;
|
||||||
|
|
||||||
Buffer ss(_Buffer, Length);
|
Buffer ss(_Buffer, Length);
|
||||||
int count = bs.Copy(0, ss, _Position, bs.Length());
|
int count = bs.Copy(0, ss, _Position, bs.Length());
|
||||||
|
@ -140,13 +140,13 @@ uint Stream::Read(Buffer& bs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入7位压缩编码整数
|
// 写入7位压缩编码整数
|
||||||
uint Stream::WriteEncodeInt(uint value)
|
int Stream::WriteEncodeInt(int value)
|
||||||
{
|
{
|
||||||
if(!CanWrite) return 0;
|
if (!CanWrite) return 0;
|
||||||
|
|
||||||
byte buf[8];
|
byte buf[8];
|
||||||
int k = 0;
|
int k = 0;
|
||||||
for(int i = 0; i < 4 && value >= 0x80; i++)
|
for (int i = 0; i < 4 && value >= 0x80; i++)
|
||||||
{
|
{
|
||||||
buf[k++] = (byte)(value | 0x80);
|
buf[k++] = (byte)(value | 0x80);
|
||||||
|
|
||||||
|
@ -163,16 +163,16 @@ uint Stream::WriteEncodeInt(uint value)
|
||||||
bool Stream::Write(const Buffer& bs)
|
bool Stream::Write(const Buffer& bs)
|
||||||
{
|
{
|
||||||
int count = bs.Length();
|
int count = bs.Length();
|
||||||
if(count == 0) return true;
|
if (count == 0) return true;
|
||||||
if(!CanWrite) return false;
|
if (!CanWrite) return false;
|
||||||
if(!CheckRemain(count)) return false;
|
if (!CheckRemain(count)) return false;
|
||||||
|
|
||||||
Buffer ss(_Buffer, _Capacity);
|
Buffer ss(_Buffer, _Capacity);
|
||||||
count = ss.Copy((uint)_Position, bs, 0, count);
|
count = ss.Copy(_Position, bs, 0, count);
|
||||||
|
|
||||||
_Position += count;
|
_Position += count;
|
||||||
// 内容长度不是累加,而是根据位置而扩大
|
// 内容长度不是累加,而是根据位置而扩大
|
||||||
if(_Position > Length) Length = _Position;
|
if (_Position > Length) Length = _Position;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -181,10 +181,10 @@ bool Stream::Write(const Buffer& bs)
|
||||||
byte* Stream::ReadBytes(int count)
|
byte* Stream::ReadBytes(int count)
|
||||||
{
|
{
|
||||||
// 默认小于0时,读取全部数据
|
// 默认小于0时,读取全部数据
|
||||||
if(count < 0) count = Remain();
|
if (count < 0) count = Remain();
|
||||||
|
|
||||||
byte* p = Current();
|
byte* p = Current();
|
||||||
if(!Seek(count)) return nullptr;
|
if (!Seek(count)) return nullptr;
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -192,22 +192,22 @@ byte* Stream::ReadBytes(int count)
|
||||||
// 读取一个字节,不移动游标。如果没有可用数据,则返回-1
|
// 读取一个字节,不移动游标。如果没有可用数据,则返回-1
|
||||||
int Stream::Peek() const
|
int Stream::Peek() const
|
||||||
{
|
{
|
||||||
if(!Remain()) return -1;
|
if (!Remain()) return -1;
|
||||||
return *Current();
|
return *Current();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
|
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
|
||||||
uint Stream::ReadArray(Buffer& bs)
|
int Stream::ReadArray(Buffer& bs)
|
||||||
{
|
{
|
||||||
uint len = ReadEncodeInt();
|
int len = ReadEncodeInt();
|
||||||
if(!len)
|
if (!len)
|
||||||
{
|
{
|
||||||
bs.SetLength(0);
|
bs.SetLength(0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(len > bs.Length() && !bs.SetLength(len))
|
if (len > bs.Length() && !bs.SetLength(len))
|
||||||
{
|
{
|
||||||
// 在设计时,如果取得的长度超级大,可能是设计错误
|
// 在设计时,如果取得的长度超级大,可能是设计错误
|
||||||
//if(len > 0x40)
|
//if(len > 0x40)
|
||||||
|
@ -257,10 +257,10 @@ String Stream::ReadString()
|
||||||
|
|
||||||
int Stream::ReadByte()
|
int Stream::ReadByte()
|
||||||
{
|
{
|
||||||
if(Length == _Position) return -1;
|
if (Length == _Position) return -1;
|
||||||
|
|
||||||
byte* p = Current();
|
byte* p = Current();
|
||||||
if(!Seek(1)) return 0;
|
if (!Seek(1)) return 0;
|
||||||
|
|
||||||
return *p;
|
return *p;
|
||||||
}
|
}
|
||||||
|
@ -269,8 +269,8 @@ ushort Stream::ReadUInt16()
|
||||||
{
|
{
|
||||||
ushort v;
|
ushort v;
|
||||||
Buffer bs(&v, sizeof(v));
|
Buffer bs(&v, sizeof(v));
|
||||||
if(!Read(bs)) return 0;
|
if (!Read(bs)) return 0;
|
||||||
if(!Little) v = _REV16(v);
|
if (!Little) v = _REV16(v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,8 +278,8 @@ uint Stream::ReadUInt32()
|
||||||
{
|
{
|
||||||
uint v;
|
uint v;
|
||||||
Buffer bs(&v, sizeof(v));
|
Buffer bs(&v, sizeof(v));
|
||||||
if(!Read(bs)) return 0;
|
if (!Read(bs)) return 0;
|
||||||
if(!Little) v = _REV(v);
|
if (!Little) v = _REV(v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,8 +287,8 @@ UInt64 Stream::ReadUInt64()
|
||||||
{
|
{
|
||||||
UInt64 v;
|
UInt64 v;
|
||||||
Buffer bs(&v, sizeof(v));
|
Buffer bs(&v, sizeof(v));
|
||||||
if(!Read(bs)) return 0;
|
if (!Read(bs)) return 0;
|
||||||
if(!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32);
|
if (!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,32 +299,32 @@ bool Stream::Write(byte value)
|
||||||
|
|
||||||
bool Stream::Write(ushort value)
|
bool Stream::Write(ushort value)
|
||||||
{
|
{
|
||||||
if(!Little) value = _REV16(value);
|
if (!Little) value = _REV16(value);
|
||||||
|
|
||||||
return Write(Buffer(&value, sizeof(value)));
|
return Write(Buffer(&value, sizeof(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Stream::Write(uint value)
|
bool Stream::Write(uint value)
|
||||||
{
|
{
|
||||||
if(!Little) value = _REV(value);
|
if (!Little) value = _REV(value);
|
||||||
|
|
||||||
return Write(Buffer(&value, sizeof(value)));
|
return Write(Buffer(&value, sizeof(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Stream::Write(UInt64 value)
|
bool Stream::Write(UInt64 value)
|
||||||
{
|
{
|
||||||
if(!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32);
|
if (!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32);
|
||||||
|
|
||||||
return Write(Buffer(&value, sizeof(value)));
|
return Write(Buffer(&value, sizeof(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************** MemoryStream ********************************/
|
/******************************** MemoryStream ********************************/
|
||||||
|
|
||||||
MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr))
|
MemoryStream::MemoryStream(int len) : Stream(_Arr, ArrayLength(_Arr))
|
||||||
{
|
{
|
||||||
Length = 0;
|
Length = 0;
|
||||||
_needFree = false;
|
_needFree = false;
|
||||||
if(len > ArrayLength(_Arr))
|
if (len > ArrayLength(_Arr))
|
||||||
{
|
{
|
||||||
byte* buf = new byte[len];
|
byte* buf = new byte[len];
|
||||||
Init(buf, len);
|
Init(buf, len);
|
||||||
|
@ -332,7 +332,7 @@ MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryStream::MemoryStream(void* buf, uint len) : Stream(buf, len)
|
MemoryStream::MemoryStream(void* buf, int len) : Stream(buf, len)
|
||||||
{
|
{
|
||||||
_needFree = false;
|
_needFree = false;
|
||||||
}
|
}
|
||||||
|
@ -340,32 +340,32 @@ MemoryStream::MemoryStream(void* buf, uint len) : Stream(buf, len)
|
||||||
// 销毁数据流
|
// 销毁数据流
|
||||||
MemoryStream::~MemoryStream()
|
MemoryStream::~MemoryStream()
|
||||||
{
|
{
|
||||||
if(_needFree)
|
if (_needFree)
|
||||||
{
|
{
|
||||||
if(_Buffer != _Arr) delete[] _Buffer;
|
if (_Buffer != _Arr) delete[] _Buffer;
|
||||||
_Buffer = nullptr;
|
_Buffer = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MemoryStream::CheckRemain(uint count)
|
bool MemoryStream::CheckRemain(int count)
|
||||||
{
|
{
|
||||||
uint remain = _Capacity - _Position;
|
int remain = _Capacity - _Position;
|
||||||
// 容量不够,需要扩容
|
// 容量不够,需要扩容
|
||||||
if(count > remain)
|
if (count > remain)
|
||||||
{
|
{
|
||||||
if(!CanResize) return Stream::CheckRemain(count);
|
if (!CanResize) return Stream::CheckRemain(count);
|
||||||
|
|
||||||
// 原始容量成倍扩容
|
// 原始容量成倍扩容
|
||||||
uint total = _Position + count;
|
int total = _Position + count;
|
||||||
uint size = _Capacity;
|
int size = _Capacity;
|
||||||
if(size < 0x10) size = 0x10;
|
if (size < 0x10) size = 0x10;
|
||||||
while(size < total) size <<= 1;
|
while (size < total) size <<= 1;
|
||||||
|
|
||||||
// 申请新的空间,并复制数据
|
// 申请新的空间,并复制数据
|
||||||
byte* bufNew = new byte[size];
|
byte* bufNew = new byte[size];
|
||||||
if(Length > 0) Buffer(_Buffer, Length).CopyTo(0, bufNew, -1);
|
if (Length > 0) Buffer(_Buffer, Length).CopyTo(0, bufNew, -1);
|
||||||
|
|
||||||
if(_Buffer != _Arr && _needFree == true) delete[] _Buffer;
|
if (_Buffer != _Arr && _needFree == true) delete[] _Buffer;
|
||||||
|
|
||||||
_Buffer = bufNew;
|
_Buffer = bufNew;
|
||||||
_Capacity = size;
|
_Capacity = size;
|
||||||
|
@ -382,5 +382,5 @@ WEAK ushort _REV16(ushort value)
|
||||||
|
|
||||||
WEAK uint _REV(uint value)
|
WEAK uint _REV(uint value)
|
||||||
{
|
{
|
||||||
return (_REV16(value & 0xFFFF) << 16) | (_REV16(value >> 16) >> 16);
|
return (_REV16(value & 0xFFFF) << 16) | (_REV16(value >> 16));
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,34 +7,34 @@ class Stream
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
byte* _Buffer; // 数据缓冲区。扩容后会重新分配缓冲区
|
byte* _Buffer; // 数据缓冲区。扩容后会重新分配缓冲区
|
||||||
uint _Capacity; // 缓冲区容量
|
int _Capacity; // 缓冲区容量
|
||||||
uint _Position; // 游标位置
|
int _Position; // 游标位置
|
||||||
|
|
||||||
void Init(void* buf, uint len);
|
void Init(void* buf, int len);
|
||||||
virtual bool CheckRemain(uint count);
|
virtual bool CheckRemain(int count);
|
||||||
public:
|
public:
|
||||||
uint Length; // 数据长度
|
int Length; // 数据长度
|
||||||
bool Little; // 默认小字节序。仅影响数据读写操作
|
bool Little; // 默认小字节序。仅影响数据读写操作
|
||||||
bool CanWrite; // 是否可写
|
bool CanWrite; // 是否可写
|
||||||
bool CanResize; // 是否可以自动扩容
|
bool CanResize; // 是否可以自动扩容
|
||||||
|
|
||||||
// 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度
|
// 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度
|
||||||
Stream(void* buf, uint len);
|
Stream(void* buf, int len);
|
||||||
Stream(const void* buf, uint len);
|
Stream(const void* buf, int len);
|
||||||
// 使用字节数组初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度
|
// 使用字节数组初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度
|
||||||
explicit Stream(Buffer& bs);
|
explicit Stream(Buffer& bs);
|
||||||
explicit Stream(const Buffer& bs);
|
explicit Stream(const Buffer& bs);
|
||||||
virtual ~Stream();
|
virtual ~Stream();
|
||||||
|
|
||||||
// 数据流容量
|
// 数据流容量
|
||||||
uint Capacity() const;
|
int Capacity() const;
|
||||||
void SetCapacity(uint len);
|
void SetCapacity(int len);
|
||||||
// 当前位置
|
// 当前位置
|
||||||
uint Position() const;
|
int Position() const;
|
||||||
// 设置位置
|
// 设置位置
|
||||||
bool SetPosition(int p);
|
bool SetPosition(int p);
|
||||||
// 余下的有效数据流长度。0表示已经到达终点
|
// 余下的有效数据流长度。0表示已经到达终点
|
||||||
uint Remain() const;
|
int Remain() const;
|
||||||
// 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标
|
// 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标
|
||||||
bool Seek(int offset);
|
bool Seek(int offset);
|
||||||
|
|
||||||
|
@ -44,17 +44,17 @@ public:
|
||||||
byte* Current() const;
|
byte* Current() const;
|
||||||
|
|
||||||
// 读取7位压缩编码整数
|
// 读取7位压缩编码整数
|
||||||
uint ReadEncodeInt();
|
int ReadEncodeInt();
|
||||||
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
|
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
|
||||||
uint Read(Buffer& bs);
|
int Read(Buffer& bs);
|
||||||
|
|
||||||
// 写入7位压缩编码整数
|
// 写入7位压缩编码整数
|
||||||
uint WriteEncodeInt(uint value);
|
int WriteEncodeInt(int value);
|
||||||
// 把字节数组的数据写入到数据流。不包含长度前缀
|
// 把字节数组的数据写入到数据流。不包含长度前缀
|
||||||
bool Write(const Buffer& bs);
|
bool Write(const Buffer& bs);
|
||||||
|
|
||||||
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
|
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
|
||||||
uint ReadArray(Buffer& bs);
|
int ReadArray(Buffer& bs);
|
||||||
ByteArray ReadArray(int count);
|
ByteArray ReadArray(int count);
|
||||||
// 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度
|
// 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度
|
||||||
bool WriteArray(const Buffer& bs);
|
bool WriteArray(const Buffer& bs);
|
||||||
|
@ -105,13 +105,13 @@ private:
|
||||||
bool _needFree; // 是否自动释放
|
bool _needFree; // 是否自动释放
|
||||||
//bool _resize; // 是否可以自动扩容
|
//bool _resize; // 是否可以自动扩容
|
||||||
|
|
||||||
virtual bool CheckRemain(uint count);
|
virtual bool CheckRemain(int count);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// 分配指定大小的数据流
|
// 分配指定大小的数据流
|
||||||
MemoryStream(uint len = 0);
|
MemoryStream(int len = 0);
|
||||||
// 使用缓冲区初始化数据流,支持自动扩容
|
// 使用缓冲区初始化数据流,支持自动扩容
|
||||||
MemoryStream(void* buf, uint len);
|
MemoryStream(void* buf, int len);
|
||||||
// 销毁数据流
|
// 销毁数据流
|
||||||
virtual ~MemoryStream();
|
virtual ~MemoryStream();
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#include "SString.h"
|
#include "SString.h"
|
||||||
|
|
||||||
char* utohex(uint value, byte size, char* string, bool upper);
|
char* utohex(uint value, byte size, char* string, bool upper);
|
||||||
extern char* itoa(int value, char* string, int radix);
|
//extern char* itoa(int value, char* string, int radix);
|
||||||
extern char* ltoa(Int64 value, char* string, int radix);
|
extern char* ltoa(Int64 value, char* string, int radix);
|
||||||
extern char* utoa(uint value, char* string, int radix);
|
extern char* utoa(uint value, char* string, int radix);
|
||||||
extern char* ultoa(UInt64 value, char* string, int radix);
|
extern char* ultoa(UInt64 value, char* string, int radix);
|
||||||
|
@ -173,7 +173,7 @@ void String::release()
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool String::CheckCapacity(uint size)
|
bool String::CheckCapacity(int size)
|
||||||
{
|
{
|
||||||
int old = _Capacity;
|
int old = _Capacity;
|
||||||
CheckCapacity(size + 1, _Length);
|
CheckCapacity(size + 1, _Length);
|
||||||
|
@ -201,7 +201,7 @@ void* String::Alloc(int len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String& String::copy(cstring cstr, uint length)
|
String& String::copy(cstring cstr, int length)
|
||||||
{
|
{
|
||||||
if (!cstr || !length) return *this;
|
if (!cstr || !length) return *this;
|
||||||
|
|
||||||
|
@ -324,12 +324,12 @@ bool String::Concat(const String& s)
|
||||||
return Concat(s._Arr, s._Length);
|
return Concat(s._Arr, s._Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool String::Concat(cstring cstr, uint length)
|
bool String::Concat(cstring cstr, int length)
|
||||||
{
|
{
|
||||||
if (!cstr) return false;
|
if (!cstr) return false;
|
||||||
if (length == 0) return true;
|
if (length == 0) return true;
|
||||||
|
|
||||||
uint newlen = _Length + length;
|
int newlen = _Length + length;
|
||||||
if (!CheckCapacity(newlen)) return false;
|
if (!CheckCapacity(newlen)) return false;
|
||||||
|
|
||||||
//strcpy(_Arr + _Length, cstr);
|
//strcpy(_Arr + _Length, cstr);
|
||||||
|
|
|
@ -84,7 +84,7 @@ bool Port::Open()
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// 保护引脚
|
// 保护引脚
|
||||||
auto name = typeid(*this).name();
|
auto name = typeid(*this).name();
|
||||||
while(*name >= '0' && *name <= '9') name++;
|
while (*name >= '0' && *name <= '9') name++;
|
||||||
debug_printf("%s", name);
|
debug_printf("%s", name);
|
||||||
Port_Reserve(_Pin, true);
|
Port_Reserve(_Pin, true);
|
||||||
#endif
|
#endif
|
||||||
|
@ -106,7 +106,7 @@ void Port::Close()
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// 保护引脚
|
// 保护引脚
|
||||||
auto name = typeid(*this).name();
|
auto name = typeid(*this).name();
|
||||||
while(*name >= '0' && *name <= '9') name++;
|
while (*name >= '0' && *name <= '9') name++;
|
||||||
debug_printf("%s", name);
|
debug_printf("%s", name);
|
||||||
Port_Reserve(_Pin, false);
|
Port_Reserve(_Pin, false);
|
||||||
debug_printf("\r\n");
|
debug_printf("\r\n");
|
||||||
|
@ -238,7 +238,9 @@ WEAK bool OutputPort::ReadInput() const
|
||||||
{
|
{
|
||||||
if (Empty()) return false;
|
if (Empty()) return false;
|
||||||
|
|
||||||
return Port::Read() ^ Invert;
|
bool v = Port::Read();
|
||||||
|
if (Invert)return !v;
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputPort::Up(uint ms) const
|
void OutputPort::Up(uint ms) const
|
||||||
|
@ -341,7 +343,9 @@ InputPort& InputPort::Init(Pin pin, bool invert)
|
||||||
// 读取本组所有引脚,任意脚为true则返回true,主要为单一引脚服务
|
// 读取本组所有引脚,任意脚为true则返回true,主要为单一引脚服务
|
||||||
bool InputPort::Read() const
|
bool InputPort::Read() const
|
||||||
{
|
{
|
||||||
return Port::Read() ^ Invert;
|
bool v = Port::Read();
|
||||||
|
if (Invert)return !v;
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputPort::OnPress(bool down)
|
void InputPort::OnPress(bool down)
|
||||||
|
@ -367,7 +371,7 @@ void InputPort::OnPress(bool down)
|
||||||
if (down)
|
if (down)
|
||||||
_Start = now;
|
_Start = now;
|
||||||
else
|
else
|
||||||
PressTime = now - _Start;
|
PressTime = (ushort)(now - _Start);
|
||||||
|
|
||||||
/*if(down)
|
/*if(down)
|
||||||
{
|
{
|
||||||
|
|
|
@ -147,8 +147,8 @@ bool SerialPort::Flush(uint times)
|
||||||
// 从某个端口读取数据
|
// 从某个端口读取数据
|
||||||
uint SerialPort::OnRead(Buffer& bs)
|
uint SerialPort::OnRead(Buffer& bs)
|
||||||
{
|
{
|
||||||
uint count = 0;
|
int count = 0;
|
||||||
uint len = Rx.Length();
|
int len = Rx.Length();
|
||||||
// 如果没有数据,立刻返回,不要等待浪费时间
|
// 如果没有数据,立刻返回,不要等待浪费时间
|
||||||
if(!len)
|
if(!len)
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,7 +27,7 @@ Timer::~Timer()
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
|
|
||||||
if(OnTick.Method) SetHandler(false);
|
if (OnTick.Method) SetHandler(false);
|
||||||
|
|
||||||
Timers[_index] = nullptr;
|
Timers[_index] = nullptr;
|
||||||
}
|
}
|
||||||
|
@ -40,13 +40,13 @@ Timer* Timer::Create(byte index)
|
||||||
byte tcount = ArrayLength(Timers);
|
byte tcount = ArrayLength(Timers);
|
||||||
//byte tcount = 46;
|
//byte tcount = 46;
|
||||||
// 特殊处理随机分配
|
// 特殊处理随机分配
|
||||||
if(index == 0xFF)
|
if (index == 0xFF)
|
||||||
{
|
{
|
||||||
// 找到第一个可用的位置,没有被使用,并且该位置定时器存在
|
// 找到第一个可用的位置,没有被使用,并且该位置定时器存在
|
||||||
byte i = 0;
|
byte i = 0;
|
||||||
for(; i<tcount && (Timers[i] || !GetTimer(i)); i++);
|
for (; i < tcount && (Timers[i] || !GetTimer(i)); i++);
|
||||||
|
|
||||||
if(i >= tcount)
|
if (i >= tcount)
|
||||||
{
|
{
|
||||||
debug_printf("Timer::Create 失败!没有空闲定时器!\r\n");
|
debug_printf("Timer::Create 失败!没有空闲定时器!\r\n");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -57,7 +57,7 @@ Timer* Timer::Create(byte index)
|
||||||
|
|
||||||
assert(index < tcount, "index");
|
assert(index < tcount, "index");
|
||||||
|
|
||||||
if(Timers[index])
|
if (Timers[index])
|
||||||
return Timers[index];
|
return Timers[index];
|
||||||
else
|
else
|
||||||
return new Timer((TIMER)index);
|
return new Timer((TIMER)index);
|
||||||
|
@ -65,7 +65,7 @@ Timer* Timer::Create(byte index)
|
||||||
|
|
||||||
void Timer::Open()
|
void Timer::Open()
|
||||||
{
|
{
|
||||||
if(Opened) return;
|
if (Opened) return;
|
||||||
|
|
||||||
TS("Timer::Open");
|
TS("Timer::Open");
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ void Timer::Open()
|
||||||
|
|
||||||
void Timer::Close()
|
void Timer::Close()
|
||||||
{
|
{
|
||||||
if(!Opened) return;
|
if (!Opened) return;
|
||||||
|
|
||||||
TS("Timer::Close");
|
TS("Timer::Close");
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ void Timer::Register(const Delegate<Timer&>& dlg)
|
||||||
{
|
{
|
||||||
OnTick = dlg;
|
OnTick = dlg;
|
||||||
|
|
||||||
SetHandler(dlg.Method);
|
SetHandler(dlg.Method != nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::OnInterrupt()
|
void Timer::OnInterrupt()
|
||||||
|
|
|
@ -27,7 +27,7 @@ typedef struct MemoryBlock_
|
||||||
} MemoryBlock;
|
} MemoryBlock;
|
||||||
|
|
||||||
/******************************** Heap ********************************/
|
/******************************** Heap ********************************/
|
||||||
Heap::Heap(uint addr, uint size)
|
Heap::Heap(uint addr, int size)
|
||||||
{
|
{
|
||||||
Current = this;
|
Current = this;
|
||||||
|
|
||||||
|
@ -55,11 +55,11 @@ Heap::Heap(uint addr, uint size)
|
||||||
debug_printf("Heap::Init(%p, %d) Free=%d \r\n", Address, Size, FreeSize());
|
debug_printf("Heap::Init(%p, %d) Free=%d \r\n", Address, Size, FreeSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
uint Heap::Used() const { return _Used; }
|
int Heap::Used() const { return _Used; }
|
||||||
uint Heap::Count() const { return _Count; }
|
int Heap::Count() const { return _Count; }
|
||||||
uint Heap::FreeSize() const { return Size - _Used; }
|
int Heap::FreeSize() const { return Size - _Used; }
|
||||||
|
|
||||||
void* Heap::Alloc(uint size)
|
void* Heap::Alloc(int size)
|
||||||
{
|
{
|
||||||
// 要申请的内存大小需要对齐
|
// 要申请的内存大小需要对齐
|
||||||
size = (size+MEMORY_ALIGN-1) & (~(MEMORY_ALIGN-1));
|
size = (size+MEMORY_ALIGN-1) & (~(MEMORY_ALIGN-1));
|
||||||
|
@ -73,7 +73,7 @@ void* Heap::Alloc(uint size)
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// 检查头部完整性
|
// 检查头部完整性
|
||||||
auto head = (MemoryBlock*)Address;
|
auto head = (MemoryBlock*)Address;
|
||||||
assert(head->Used <= Size && (uint)head + head->Used <= (uint)head->Next, "堆头被破坏!");
|
assert(head->Used <= (uint)Size && (uint)head + head->Used <= (uint)head->Next, "堆头被破坏!");
|
||||||
assert(_Used <= Size, "Heap::Used异常!");
|
assert(_Used <= Size, "Heap::Used异常!");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -6,23 +6,23 @@ class Heap
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
uint Address;// 开始地址
|
uint Address;// 开始地址
|
||||||
uint Size; // 大小
|
int Size; // 大小
|
||||||
|
|
||||||
Heap(uint addr, uint size);
|
Heap(uint addr, int size);
|
||||||
|
|
||||||
uint Used() const; // 已使用内存数
|
int Used() const; // 已使用内存数
|
||||||
uint Count() const; // 已使用内存块数
|
int Count() const; // 已使用内存块数
|
||||||
uint FreeSize() const; // 可用内存数
|
int FreeSize() const; // 可用内存数
|
||||||
|
|
||||||
void* Alloc(uint size);
|
void* Alloc(int size);
|
||||||
void Free(void* ptr);
|
void Free(void* ptr);
|
||||||
|
|
||||||
// 当前堆
|
// 当前堆
|
||||||
static Heap* Current;
|
static Heap* Current;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint _Used;
|
int _Used;
|
||||||
uint _Count;
|
int _Count;
|
||||||
void* _First; // 第一个有空闲的内存块,加速搜索
|
void* _First; // 第一个有空闲的内存块,加速搜索
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -179,7 +179,7 @@ TaskScheduler::TaskScheduler(cstring name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用外部缓冲区初始化任务列表,避免频繁的堆分配
|
// 使用外部缓冲区初始化任务列表,避免频繁的堆分配
|
||||||
void TaskScheduler::Set(Task* tasks, uint count)
|
void TaskScheduler::Set(Task* tasks, int count)
|
||||||
{
|
{
|
||||||
for(int i=0; i<count; i++)
|
for(int i=0; i<count; i++)
|
||||||
{
|
{
|
||||||
|
@ -358,9 +358,9 @@ INROOT void TaskScheduler::Execute(uint msMax, bool& cancel)
|
||||||
Sleeping = true;
|
Sleeping = true;
|
||||||
// 通知外部,需要睡眠若干毫秒
|
// 通知外部,需要睡眠若干毫秒
|
||||||
if(EnterSleep)
|
if(EnterSleep)
|
||||||
EnterSleep(min);
|
EnterSleep((int)min);
|
||||||
else
|
else
|
||||||
Time.Sleep(min, &Sleeping);
|
Time.Sleep((int)min, &Sleeping);
|
||||||
Sleeping = false;
|
Sleeping = false;
|
||||||
|
|
||||||
// 累加睡眠时间
|
// 累加睡眠时间
|
||||||
|
@ -433,7 +433,7 @@ void TaskScheduler::ShowStatus()
|
||||||
debug_printf("Task::ShowStatus [%d]", ts);
|
debug_printf("Task::ShowStatus [%d]", ts);
|
||||||
debug_printf(" 负载 %d.%d%%", p/100, p%100);
|
debug_printf(" 负载 %d.%d%%", p/100, p%100);
|
||||||
debug_printf(" 平均 %dus 当前 ", ts ? ct/ts : 0);
|
debug_printf(" 平均 %dus 当前 ", ts ? ct/ts : 0);
|
||||||
DateTime(now/1000).Show();
|
DateTime((int)(now/1000)).Show();
|
||||||
debug_printf(" 启动 ");
|
debug_printf(" 启动 ");
|
||||||
TimeSpan(now).Show(false);
|
TimeSpan(now).Show(false);
|
||||||
|
|
||||||
|
|
|
@ -77,14 +77,14 @@ public:
|
||||||
UInt64 TotalSleep; // 所有任务的总睡眠时间ms
|
UInt64 TotalSleep; // 所有任务的总睡眠时间ms
|
||||||
UInt64 LastTrace; // 最后统计跟踪时间ms
|
UInt64 LastTrace; // 最后统计跟踪时间ms
|
||||||
|
|
||||||
typedef void (*SAction)(uint ms);
|
typedef void (*SAction)(int ms);
|
||||||
SAction EnterSleep; // 通知外部,需要睡眠若干毫秒
|
SAction EnterSleep; // 通知外部,需要睡眠若干毫秒
|
||||||
Func ExitSleep; // 通知外部,要求退出睡眠,恢复调度
|
Func ExitSleep; // 通知外部,要求退出睡眠,恢复调度
|
||||||
|
|
||||||
TaskScheduler(cstring name = nullptr);
|
TaskScheduler(cstring name = nullptr);
|
||||||
|
|
||||||
// 使用外部缓冲区初始化任务列表,避免频繁的堆分配
|
// 使用外部缓冲区初始化任务列表,避免频繁的堆分配
|
||||||
void Set(Task* tasks, uint count);
|
void Set(Task* tasks, int count);
|
||||||
// 查找任务 返回使用此函数的首个任务的ID
|
// 查找任务 返回使用此函数的首个任务的ID
|
||||||
uint FindID(Action func);
|
uint FindID(Action func);
|
||||||
// 查找任务 返回使用此函数的首个任务
|
// 查找任务 返回使用此函数的首个任务
|
||||||
|
|
|
@ -44,7 +44,7 @@ void TTime::SetTime(UInt64 sec)
|
||||||
{
|
{
|
||||||
if (sec >= BASE_YEAR_US) sec -= BASE_YEAR_US;
|
if (sec >= BASE_YEAR_US) sec -= BASE_YEAR_US;
|
||||||
|
|
||||||
BaseSeconds = (uint)(sec - Seconds);
|
BaseSeconds = (int)(sec - Seconds);
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
/*DateTime dt(sec);
|
/*DateTime dt(sec);
|
||||||
|
|
|
@ -36,7 +36,7 @@ Buffer BinaryPair::Get(cstring name) const
|
||||||
String sn = name;
|
String sn = name;
|
||||||
|
|
||||||
auto& ms = *_s;
|
auto& ms = *_s;
|
||||||
uint p = ms.Position();
|
int p = ms.Position();
|
||||||
for(int i=0; i<2; i++)
|
for(int i=0; i<2; i++)
|
||||||
{
|
{
|
||||||
while(ms.Remain() && (i ==0 || ms.Position() < p))
|
while(ms.Remain() && (i ==0 || ms.Position() < p))
|
||||||
|
|
|
@ -4,9 +4,9 @@
|
||||||
#define NET_DEBUG DEBUG
|
#define NET_DEBUG DEBUG
|
||||||
//#define NET_DEBUG 0
|
//#define NET_DEBUG 0
|
||||||
#if NET_DEBUG
|
#if NET_DEBUG
|
||||||
#define net_printf debug_printf
|
#define net_printf debug_printf
|
||||||
#else
|
#else
|
||||||
#define net_printf(format, ...)
|
#define net_printf(format, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
List<NetworkInterface*> NetworkInterface::All;
|
List<NetworkInterface*> NetworkInterface::All;
|
||||||
|
@ -52,25 +52,25 @@ NetworkInterface::~NetworkInterface()
|
||||||
debug_printf("Network::Remove 0x%p\r\n", this);
|
debug_printf("Network::Remove 0x%p\r\n", this);
|
||||||
All.Remove(this);
|
All.Remove(this);
|
||||||
|
|
||||||
if(!Opened) Close();
|
if (!Opened) Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkInterface::Open()
|
bool NetworkInterface::Open()
|
||||||
{
|
{
|
||||||
if(Opened) return true;
|
if (Opened) return true;
|
||||||
|
|
||||||
// 打开接口
|
// 打开接口
|
||||||
Opened = OnOpen();
|
Opened = OnOpen();
|
||||||
|
|
||||||
// 建立连接任务
|
// 建立连接任务
|
||||||
_taskLink = Sys.AddTask([](void* s){ ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测");
|
_taskLink = Sys.AddTask([](void* s) { ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测");
|
||||||
|
|
||||||
return Opened;
|
return Opened;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkInterface::Close()
|
void NetworkInterface::Close()
|
||||||
{
|
{
|
||||||
if(!Opened) return;
|
if (!Opened) return;
|
||||||
|
|
||||||
Sys.RemoveTask(_taskLink);
|
Sys.RemoveTask(_taskLink);
|
||||||
|
|
||||||
|
@ -89,10 +89,10 @@ void NetworkInterface::OnLoop()
|
||||||
{
|
{
|
||||||
bool old = Linked;
|
bool old = Linked;
|
||||||
// 未连接时执行连接
|
// 未连接时执行连接
|
||||||
if(!old)
|
if (!old)
|
||||||
{
|
{
|
||||||
bool link = OnLink(++_retry);
|
bool link = OnLink(++_retry);
|
||||||
if(link ^ old)
|
if (link ^ old)
|
||||||
{
|
{
|
||||||
debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开");
|
debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开");
|
||||||
Linked = link;
|
Linked = link;
|
||||||
|
@ -103,7 +103,7 @@ void NetworkInterface::OnLoop()
|
||||||
{
|
{
|
||||||
// 检测并通知状态改变
|
// 检测并通知状态改变
|
||||||
bool link = CheckLink();
|
bool link = CheckLink();
|
||||||
if(link ^ old)
|
if (link ^ old)
|
||||||
{
|
{
|
||||||
debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开");
|
debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开");
|
||||||
Linked = link;
|
Linked = link;
|
||||||
|
@ -115,10 +115,10 @@ void NetworkInterface::OnLoop()
|
||||||
|
|
||||||
Socket* NetworkInterface::CreateClient(const NetUri& uri)
|
Socket* NetworkInterface::CreateClient(const NetUri& uri)
|
||||||
{
|
{
|
||||||
if(!Active()) return nullptr;
|
if (!Active()) return nullptr;
|
||||||
|
|
||||||
auto socket = CreateSocket(uri.Type);
|
auto socket = CreateSocket(uri.Type);
|
||||||
if(socket)
|
if (socket)
|
||||||
{
|
{
|
||||||
socket->Local.Address = uri.Address;
|
socket->Local.Address = uri.Address;
|
||||||
socket->Local.Port = uri.Port;
|
socket->Local.Port = uri.Port;
|
||||||
|
@ -129,10 +129,10 @@ Socket* NetworkInterface::CreateClient(const NetUri& uri)
|
||||||
|
|
||||||
Socket* NetworkInterface::CreateRemote(const NetUri& uri)
|
Socket* NetworkInterface::CreateRemote(const NetUri& uri)
|
||||||
{
|
{
|
||||||
if(!Active()) return nullptr;
|
if (!Active()) return nullptr;
|
||||||
|
|
||||||
auto socket = CreateSocket(uri.Type);
|
auto socket = CreateSocket(uri.Type);
|
||||||
if(socket)
|
if (socket)
|
||||||
{
|
{
|
||||||
socket->Local.Address = IP;
|
socket->Local.Address = IP;
|
||||||
socket->Remote.Address = uri.Address;
|
socket->Remote.Address = uri.Address;
|
||||||
|
@ -150,7 +150,7 @@ void NetworkInterface::InitConfig()
|
||||||
// 随机IP,取ID最后一个字节
|
// 随机IP,取ID最后一个字节
|
||||||
IP = defip;
|
IP = defip;
|
||||||
byte first = Sys.ID[0];
|
byte first = Sys.ID[0];
|
||||||
if(first <= 1 || first >= 254) first = 2;
|
if (first <= 1 || first >= 254) first = 2;
|
||||||
IP[3] = first;
|
IP[3] = first;
|
||||||
|
|
||||||
Mask = IPAddress(255, 255, 255, 0);
|
Mask = IPAddress(255, 255, 255, 0);
|
||||||
|
@ -166,12 +166,12 @@ void NetworkInterface::InitConfig()
|
||||||
// 第一个字节最低位为1表示组播地址,所以第一个字节必须是偶数
|
// 第一个字节最低位为1表示组播地址,所以第一个字节必须是偶数
|
||||||
mac[0] = 'N';
|
mac[0] = 'N';
|
||||||
mac[1] = 'X';
|
mac[1] = 'X';
|
||||||
for(int i=0; i< 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
mac[2 + i] = Sys.ID[3 - i];
|
mac[2 + i] = Sys.ID[3 - i];
|
||||||
|
|
||||||
// 多网卡时MAC不能相同
|
// 多网卡时MAC不能相同
|
||||||
int k=0;
|
int k = 0;
|
||||||
for(;k<All.Count() && All[k]!=this;k++);
|
for (; k < All.Count() && All[k] != this; k++);
|
||||||
mac[5] += k;
|
mac[5] += k;
|
||||||
|
|
||||||
Mode = NetworkType::Wire;
|
Mode = NetworkType::Wire;
|
||||||
|
@ -182,10 +182,10 @@ void NetworkInterface::InitConfig()
|
||||||
bool NetworkInterface::LoadConfig()
|
bool NetworkInterface::LoadConfig()
|
||||||
{
|
{
|
||||||
auto cfg = Config::Current;
|
auto cfg = Config::Current;
|
||||||
if(!cfg) return false;
|
if (!cfg) return false;
|
||||||
|
|
||||||
auto nc = (NetConfig*)cfg->Get(Name);
|
auto nc = (NetConfig*)cfg->Get(Name);
|
||||||
if(!nc) return false;
|
if (!nc) return false;
|
||||||
|
|
||||||
debug_printf("%s::Load %d 字节\r\n", Name, sizeof(NetConfig));
|
debug_printf("%s::Load %d 字节\r\n", Name, sizeof(NetConfig));
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ bool NetworkInterface::LoadConfig()
|
||||||
bool NetworkInterface::SaveConfig() const
|
bool NetworkInterface::SaveConfig() const
|
||||||
{
|
{
|
||||||
auto cfg = Config::Current;
|
auto cfg = Config::Current;
|
||||||
if(!cfg) return false;
|
if (!cfg) return false;
|
||||||
|
|
||||||
NetConfig nc;
|
NetConfig nc;
|
||||||
nc.IP = IP.Value;
|
nc.IP = IP.Value;
|
||||||
|
@ -225,7 +225,7 @@ bool NetworkInterface::SaveConfig() const
|
||||||
debug_printf("%s::Save %d 字节\r\n", Name, sizeof(nc));
|
debug_printf("%s::Save %d 字节\r\n", Name, sizeof(nc));
|
||||||
|
|
||||||
Buffer bs(&nc, sizeof(nc));
|
Buffer bs(&nc, sizeof(nc));
|
||||||
return cfg->Set(Name, bs);
|
return cfg->Set(Name, bs) != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkInterface::ShowConfig() const
|
void NetworkInterface::ShowConfig() const
|
||||||
|
@ -243,14 +243,14 @@ void NetworkInterface::ShowConfig() const
|
||||||
//DHCPServer.Show();
|
//DHCPServer.Show();
|
||||||
net_printf("\r\n DNS:\t");
|
net_printf("\r\n DNS:\t");
|
||||||
DNSServer.Show();
|
DNSServer.Show();
|
||||||
if(!DNSServer2.IsAny())
|
if (!DNSServer2.IsAny())
|
||||||
{
|
{
|
||||||
net_printf("\r\n DNS2:\t");
|
net_printf("\r\n DNS2:\t");
|
||||||
DNSServer2.Show();
|
DNSServer2.Show();
|
||||||
}
|
}
|
||||||
net_printf("\r\n Speed:\t%dMps", Speed);
|
net_printf("\r\n Speed:\t%dMps", Speed);
|
||||||
net_printf("\r\n Mode:\t");
|
net_printf("\r\n Mode:\t");
|
||||||
switch(Mode)
|
switch (Mode)
|
||||||
{
|
{
|
||||||
case NetworkType::Wire:
|
case NetworkType::Wire:
|
||||||
net_printf("有线");
|
net_printf("有线");
|
||||||
|
@ -302,10 +302,10 @@ bool WiFiInterface::IsAP() const
|
||||||
Socket* WiFiInterface::CreateClient(const NetUri& uri)
|
Socket* WiFiInterface::CreateClient(const NetUri& uri)
|
||||||
{
|
{
|
||||||
// WiFi无需连接路由器就可以建立本地监听
|
// WiFi无需连接路由器就可以建立本地监听
|
||||||
if(!Opened) return nullptr;
|
if (!Opened) return nullptr;
|
||||||
|
|
||||||
auto socket = CreateSocket(uri.Type);
|
auto socket = CreateSocket(uri.Type);
|
||||||
if(socket)
|
if (socket)
|
||||||
{
|
{
|
||||||
socket->Local.Address = uri.Address;
|
socket->Local.Address = uri.Address;
|
||||||
socket->Local.Port = uri.Port;
|
socket->Local.Port = uri.Port;
|
||||||
|
@ -316,12 +316,12 @@ Socket* WiFiInterface::CreateClient(const NetUri& uri)
|
||||||
|
|
||||||
void WiFiInterface::OnInitConfig()
|
void WiFiInterface::OnInitConfig()
|
||||||
{
|
{
|
||||||
if(!SSID)
|
if (!SSID)
|
||||||
SSID = new String();
|
SSID = new String();
|
||||||
else
|
else
|
||||||
SSID->Clear();
|
SSID->Clear();
|
||||||
|
|
||||||
if(!Pass)
|
if (!Pass)
|
||||||
Pass = new String();
|
Pass = new String();
|
||||||
else
|
else
|
||||||
Pass->Clear();
|
Pass->Clear();
|
||||||
|
@ -330,19 +330,19 @@ void WiFiInterface::OnInitConfig()
|
||||||
void WiFiInterface::OnLoadConfig(const Buffer& buf)
|
void WiFiInterface::OnLoadConfig(const Buffer& buf)
|
||||||
{
|
{
|
||||||
Stream ms(buf);
|
Stream ms(buf);
|
||||||
if(SSID) *SSID = ms.ReadString();
|
if (SSID) *SSID = ms.ReadString();
|
||||||
if(Pass) *Pass = ms.ReadString();
|
if (Pass) *Pass = ms.ReadString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiInterface::OnSaveConfig(Buffer& buf) const
|
void WiFiInterface::OnSaveConfig(Buffer& buf) const
|
||||||
{
|
{
|
||||||
Stream ms(buf);
|
Stream ms(buf);
|
||||||
if(SSID) ms.WriteArray(*SSID);
|
if (SSID) ms.WriteArray(*SSID);
|
||||||
if(Pass) ms.WriteArray(*Pass);
|
if (Pass) ms.WriteArray(*Pass);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiInterface::OnShowConfig() const
|
void WiFiInterface::OnShowConfig() const
|
||||||
{
|
{
|
||||||
if(SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); }
|
if (SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); }
|
||||||
if(Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); }
|
if (Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Kernel\Sys.h"
|
#include "Kernel\Sys.h"
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
static void TestAssign()
|
static void TestAssign()
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Kernel\Sys.h"
|
#include "Kernel\Sys.h"
|
||||||
#include "Core\Dictionary.h"
|
#include "Core\Dictionary.h"
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
|
@ -7,7 +7,7 @@ void TestFlash()
|
||||||
debug_printf("TestFlash Start......\r\n");
|
debug_printf("TestFlash Start......\r\n");
|
||||||
|
|
||||||
byte buf[] = "STM32F10x SPI Firmware Library Example: communication with an AT45DB SPI FLASH";
|
byte buf[] = "STM32F10x SPI Firmware Library Example: communication with an AT45DB SPI FLASH";
|
||||||
uint size = ArrayLength(buf);
|
int size = ArrayLength(buf);
|
||||||
|
|
||||||
Flash flash;
|
Flash flash;
|
||||||
// 凑一个横跨两页的地址
|
// 凑一个横跨两页的地址
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Kernel\Sys.h"
|
#include "Kernel\Sys.h"
|
||||||
#include "Message\Json.h"
|
#include "Message\Json.h"
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Kernel\Sys.h"
|
#include "Kernel\Sys.h"
|
||||||
#include "Core\List.h"
|
#include "Core\List.h"
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
|
@ -28,7 +28,7 @@ TinyController::TinyController() : Controller()
|
||||||
while(Address < 2 || Address > 254)
|
while(Address < 2 || Address > 254)
|
||||||
{
|
{
|
||||||
Sys.Delay(30);
|
Sys.Delay(30);
|
||||||
Address = Sys.Ms();
|
Address = (byte)Sys.Ms();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0
|
// 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0
|
||||||
|
@ -350,7 +350,7 @@ void TinyController::AckRequest(const TinyMessage& msg)
|
||||||
auto& node = _Queue[i];
|
auto& node = _Queue[i];
|
||||||
if(node.Using && node.Seq == msg.Seq)
|
if(node.Using && node.Seq == msg.Seq)
|
||||||
{
|
{
|
||||||
int cost = Sys.Ms() - node.StartTime;
|
int cost = (int)(Sys.Ms() - node.StartTime);
|
||||||
if(cost < 0) cost = -cost;
|
if(cost < 0) cost = -cost;
|
||||||
|
|
||||||
Total.Cost += cost;
|
Total.Cost += cost;
|
||||||
|
|
|
@ -105,7 +105,7 @@ void TinyMessage::Write(Stream& ms) const
|
||||||
// 实际数据拷贝到占位符
|
// 实际数据拷贝到占位符
|
||||||
auto p = (TinyMessage*)this;
|
auto p = (TinyMessage*)this;
|
||||||
p->_Code = Code;
|
p->_Code = Code;
|
||||||
p->_Length = len;
|
p->_Length = (byte)len;
|
||||||
p->_Reply = Reply;
|
p->_Reply = Reply;
|
||||||
p->_Error = Error;
|
p->_Error = Error;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue