修正编译警告,HttpClient移到Net目录
This commit is contained in:
parent
3f2747222c
commit
91229367f1
|
@ -101,7 +101,7 @@ void AP0103::InitLeds()
|
|||
}
|
||||
}
|
||||
|
||||
void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
static void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
// if (port->PressTime > 1000)
|
||||
AP0103::OnPress(port, down);
|
||||
|
@ -198,7 +198,7 @@ void AP0103::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void OnInitNet(void* param)
|
||||
static void OnInitNet(void* param)
|
||||
{
|
||||
auto& bsp = *(AP0103*)param;
|
||||
|
||||
|
|
|
@ -101,7 +101,7 @@ void AP0104::InitLeds()
|
|||
}
|
||||
}
|
||||
|
||||
void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
static void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
// if (port->PressTime > 1000)
|
||||
AP0104::OnPress(port, down);
|
||||
|
@ -192,7 +192,7 @@ void AP0104::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void OnInitNet(void* param)
|
||||
static void OnInitNet(void* param)
|
||||
{
|
||||
auto& bsp = *(AP0104*)param;
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ void AP0801::InitLeds()
|
|||
}
|
||||
}
|
||||
|
||||
void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
static void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
if (port->PressTime > 1000)
|
||||
AP0801::Current->OnLongPress(port, down);
|
||||
|
@ -205,7 +205,7 @@ void AP0801::Register(uint offset, uint size, Handler hook)
|
|||
ds.Register(offset, size, hook);
|
||||
}
|
||||
|
||||
void OnInitNet(void* param)
|
||||
static void OnInitNet(void* param)
|
||||
{
|
||||
auto& bsp = *(AP0801*)param;
|
||||
|
||||
|
|
|
@ -228,7 +228,7 @@ void AP0802::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void OnInitNet(void* param)
|
||||
static void OnInitNet(void* param)
|
||||
{
|
||||
auto& bsp = *(AP0802*)param;
|
||||
|
||||
|
@ -246,7 +246,7 @@ void AP0802::InitNet()
|
|||
/******************************** 2401 ********************************/
|
||||
|
||||
|
||||
int Fix2401(const Buffer& bs)
|
||||
static int Fix2401(const Buffer& bs)
|
||||
{
|
||||
//auto& bs = *(Buffer*)param;
|
||||
// 微网指令特殊处理长度
|
||||
|
|
|
@ -95,7 +95,7 @@ void IOK0612::InitLeds()
|
|||
}
|
||||
}
|
||||
|
||||
void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
static void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
if (port->PressTime > 1000)
|
||||
IOK0612::OnLongPress(port, down);
|
||||
|
|
|
@ -95,7 +95,7 @@ void NH3_0317::InitLeds()
|
|||
}
|
||||
}
|
||||
|
||||
void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
static void ButtonOnpress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
if (port->PressTime > 1000)
|
||||
NH3_0317::OnLongPress(port, down);
|
||||
|
|
378
Core/String.cpp
378
Core/String.cpp
|
@ -21,7 +21,8 @@ extern char* itoa(int 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* ultoa(UInt64 value, char* string, int radix);
|
||||
char* dtostrf(double val, byte prec, char* sout);
|
||||
char* dtostrf(double val, byte prec, char* sout, int len);
|
||||
|
||||
|
||||
/******************************** String ********************************/
|
||||
|
||||
|
@ -29,13 +30,13 @@ String::String(cstring cstr) : Array(Arr, ArrayLength(Arr))
|
|||
{
|
||||
init();
|
||||
|
||||
_Length = strlen(cstr);
|
||||
if(_Length)
|
||||
_Length = strlen(cstr);
|
||||
if (_Length)
|
||||
{
|
||||
_Arr = (char*)cstr;
|
||||
_Arr = (char*)cstr;
|
||||
// 此时保证外部一定是0结尾
|
||||
_Capacity = _Length + 1;
|
||||
_canWrite = false;
|
||||
_Capacity = _Length + 1;
|
||||
_canWrite = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,7 +65,7 @@ String::String(char c) : Array(Arr, ArrayLength(Arr))
|
|||
|
||||
_Arr[0] = c;
|
||||
_Arr[1] = 0;
|
||||
_Length = 1;
|
||||
_Length = 1;
|
||||
}
|
||||
|
||||
String::String(byte value, int radix) : Array(Arr, ArrayLength(Arr))
|
||||
|
@ -133,36 +134,36 @@ String::String(double value, int decimalPlaces) : Array(Arr, ArrayLength(Arr))
|
|||
// 外部传入缓冲区供内部使用,内部计算字符串长度,注意长度减去零结束符
|
||||
String::String(char* str, int length) : Array(str, length)
|
||||
{
|
||||
_Arr = str;
|
||||
_Capacity = length - 1;
|
||||
_Arr = str;
|
||||
_Capacity = length - 1;
|
||||
|
||||
// 计算外部字符串长度
|
||||
int len = strlen(str);
|
||||
if(len >= length) len = length - 1;
|
||||
_Length = len;
|
||||
_Arr[_Length] = '\0';
|
||||
int len = strlen(str);
|
||||
if (len >= length) len = length - 1;
|
||||
_Length = len;
|
||||
_Arr[_Length] = '\0';
|
||||
}
|
||||
|
||||
// 外部传入缓冲区供内部使用,内部计算字符串长度,注意长度减去零结束符
|
||||
String::String(char* str, int length, bool expand) : String(str, length)
|
||||
{
|
||||
Expand = expand;
|
||||
Expand = expand;
|
||||
}
|
||||
|
||||
// 包装静态字符串,直接使用,修改时扩容
|
||||
String::String(cstring str, int length) : Array((char*)str, length)
|
||||
{
|
||||
// 此时不能保证外部一定是0结尾
|
||||
_Capacity = length + 1;
|
||||
_canWrite = false;
|
||||
_Capacity = length + 1;
|
||||
_canWrite = false;
|
||||
}
|
||||
|
||||
inline void String::init()
|
||||
{
|
||||
_Arr = Arr;
|
||||
_Capacity = sizeof(Arr) - 1;
|
||||
_Length = 0;
|
||||
_Arr[0] = '\0';
|
||||
_Arr = Arr;
|
||||
_Capacity = sizeof(Arr) - 1;
|
||||
_Length = 0;
|
||||
_Arr[0] = '\0';
|
||||
}
|
||||
|
||||
void String::release()
|
||||
|
@ -174,12 +175,12 @@ void String::release()
|
|||
|
||||
bool String::CheckCapacity(uint size)
|
||||
{
|
||||
int old = _Capacity;
|
||||
int old = _Capacity;
|
||||
CheckCapacity(size + 1, _Length);
|
||||
if(old == _Capacity) return true;
|
||||
if (old == _Capacity) return true;
|
||||
|
||||
// 强制最后一个字符为0
|
||||
_Arr[_Length] = '\0';
|
||||
_Arr[_Length] = '\0';
|
||||
|
||||
_Capacity--;
|
||||
|
||||
|
@ -188,21 +189,21 @@ bool String::CheckCapacity(uint size)
|
|||
|
||||
void* String::Alloc(int len)
|
||||
{
|
||||
if(len <= sizeof(Arr))
|
||||
if (len <= sizeof(Arr))
|
||||
{
|
||||
_needFree = false;
|
||||
_needFree = false;
|
||||
return Arr;
|
||||
}
|
||||
else
|
||||
{
|
||||
_needFree = true;
|
||||
_needFree = true;
|
||||
return new byte[len];
|
||||
}
|
||||
}
|
||||
|
||||
String& String::copy(cstring cstr, uint length)
|
||||
{
|
||||
if(!cstr || !length) return *this;
|
||||
if (!cstr || !length) return *this;
|
||||
|
||||
if (!CheckCapacity(length))
|
||||
release();
|
||||
|
@ -211,8 +212,8 @@ String& String::copy(cstring cstr, uint length)
|
|||
_Length = length;
|
||||
//strcpy(_Arr, cstr);
|
||||
//!!! 特别注意要拷贝的长度
|
||||
if(length) Buffer(_Arr, _Capacity).Copy(0, cstr, length);
|
||||
_Arr[length] = '\0';
|
||||
if (length) Buffer(_Arr, _Capacity).Copy(0, cstr, length);
|
||||
_Arr[length] = '\0';
|
||||
}
|
||||
|
||||
return *this;
|
||||
|
@ -227,7 +228,7 @@ void String::move(String& rhs)
|
|||
3,如果右值是外部指针,而不需要释放,则拷贝数据,因为那指针可能是借用外部的栈内存
|
||||
*/
|
||||
|
||||
if(rhs._Arr != rhs.Arr && rhs._needFree)
|
||||
if (rhs._Arr != rhs.Arr && rhs._needFree)
|
||||
{
|
||||
Array::move(rhs);
|
||||
|
||||
|
@ -242,7 +243,7 @@ void String::move(String& rhs)
|
|||
bool String::CopyOrWrite()
|
||||
{
|
||||
// 如果不可写
|
||||
if(!_canWrite) return CheckCapacity(_Length);
|
||||
if (!_canWrite) return CheckCapacity(_Length);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -251,18 +252,18 @@ bool String::SetLength(int len, bool bak)
|
|||
{
|
||||
//if(!Array::SetLength(length, bak)) return false;
|
||||
// 字符串的最大长度为容量减一,因为需要保留一个零结束字符
|
||||
if(len < _Capacity)
|
||||
if (len < _Capacity)
|
||||
{
|
||||
_Length = len;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!CheckCapacity(len + 1, bak ? _Length : 0)) return false;
|
||||
if (!CheckCapacity(len + 1, bak ? _Length : 0)) return false;
|
||||
// 扩大长度
|
||||
if(len > _Length) _Length = len;
|
||||
if (len > _Length) _Length = len;
|
||||
}
|
||||
|
||||
_Arr[_Length] = '\0';
|
||||
_Arr[_Length] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -270,10 +271,10 @@ bool String::SetLength(int len, bool bak)
|
|||
// 拷贝数据,默认-1长度表示当前长度
|
||||
int String::Copy(int destIndex, const void* src, int len)
|
||||
{
|
||||
int rs = Buffer::Copy(destIndex, src, len);
|
||||
if(!rs) return 0;
|
||||
int rs = Buffer::Copy(destIndex, src, len);
|
||||
if (!rs) return 0;
|
||||
|
||||
_Arr[_Length] = '\0';
|
||||
_Arr[_Length] = '\0';
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
@ -281,10 +282,10 @@ int String::Copy(int destIndex, const void* src, int len)
|
|||
// 把数据复制到目标缓冲区,默认-1长度表示当前长度
|
||||
int String::CopyTo(int srcIndex, void* dest, int len) const
|
||||
{
|
||||
int rs = Buffer::CopyTo(srcIndex, dest, len);
|
||||
if(!rs) return 0;
|
||||
int rs = Buffer::CopyTo(srcIndex, dest, len);
|
||||
if (!rs) return 0;
|
||||
|
||||
((char*)dest)[rs] = '\0';
|
||||
((char*)dest)[rs] = '\0';
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
@ -334,7 +335,7 @@ bool String::Concat(cstring cstr, uint length)
|
|||
//strcpy(_Arr + _Length, cstr);
|
||||
Buffer(_Arr, _Capacity).Copy(_Length, cstr, length);
|
||||
_Length = newlen;
|
||||
_Arr[_Length] = '\0';
|
||||
_Arr[_Length] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -354,7 +355,7 @@ bool String::Concat(char c)
|
|||
{
|
||||
if (!CheckCapacity(_Length + 1)) return false;
|
||||
|
||||
_Arr[_Length++] = c;
|
||||
_Arr[_Length++] = c;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -362,18 +363,22 @@ bool String::Concat(char c)
|
|||
bool String::Concat(byte num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16)
|
||||
if (radix == 16 || radix == -16)
|
||||
{
|
||||
if (!CheckCapacity(_Length + (sizeof(num) << 1))) return false;
|
||||
|
||||
utohex(num, sizeof(num), _Arr + _Length, radix < 0);
|
||||
_Length += (sizeof(num) << 1);
|
||||
_Length += (sizeof(num) << 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char buf[1 + 3 * sizeof(byte)];
|
||||
#if defined(_MSC_VER)
|
||||
_itoa_s(num, buf, sizeof(buf), radix);
|
||||
#else
|
||||
itoa(num, buf, radix);
|
||||
#endif
|
||||
|
||||
return Concat(buf, strlen(buf));
|
||||
}
|
||||
|
@ -381,22 +386,26 @@ bool String::Concat(byte num, int radix)
|
|||
bool String::Concat(short num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16) return Concat((ushort)num, radix);
|
||||
if (radix == 16 || radix == -16) return Concat((ushort)num, radix);
|
||||
|
||||
char buf[2 + 3 * sizeof(int)];
|
||||
#if defined(_MSC_VER)
|
||||
_itoa_s(num, buf, sizeof(buf), radix);
|
||||
#else
|
||||
itoa(num, buf, radix);
|
||||
#endif
|
||||
return Concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
bool String::Concat(ushort num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16)
|
||||
if (radix == 16 || radix == -16)
|
||||
{
|
||||
if (!CheckCapacity(_Length + (sizeof(num) << 1))) return false;
|
||||
|
||||
utohex(num, sizeof(num), _Arr + _Length, radix < 0);
|
||||
_Length += (sizeof(num) << 1);
|
||||
_Length += (sizeof(num) << 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -409,22 +418,26 @@ bool String::Concat(ushort num, int radix)
|
|||
bool String::Concat(int num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16) return Concat((uint)num, radix);
|
||||
if (radix == 16 || radix == -16) return Concat((uint)num, radix);
|
||||
|
||||
char buf[2 + 3 * sizeof(int)];
|
||||
#if defined(_MSC_VER)
|
||||
_itoa_s(num, buf, sizeof(buf), radix);
|
||||
#else
|
||||
itoa(num, buf, radix);
|
||||
#endif
|
||||
return Concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
bool String::Concat(uint num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16)
|
||||
if (radix == 16 || radix == -16)
|
||||
{
|
||||
if (!CheckCapacity(_Length + (sizeof(num) << 1))) return false;
|
||||
|
||||
utohex(num, sizeof(num), _Arr + _Length, radix < 0);
|
||||
_Length += (sizeof(num) << 1);
|
||||
_Length += (sizeof(num) << 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -437,7 +450,7 @@ bool String::Concat(uint num, int radix)
|
|||
bool String::Concat(Int64 num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16) return Concat((UInt64)num, radix);
|
||||
if (radix == 16 || radix == -16) return Concat((UInt64)num, radix);
|
||||
|
||||
char buf[2 + 3 * sizeof(Int64)];
|
||||
ltoa(num, buf, radix);
|
||||
|
@ -447,14 +460,14 @@ bool String::Concat(Int64 num, int radix)
|
|||
bool String::Concat(UInt64 num, int radix)
|
||||
{
|
||||
// 十六进制固定长度
|
||||
if(radix == 16 || radix == -16)
|
||||
if (radix == 16 || radix == -16)
|
||||
{
|
||||
if (!CheckCapacity(_Length + (sizeof(num) << 1))) return false;
|
||||
|
||||
utohex((int)(num >> 32), sizeof(num) >> 1, _Arr + _Length, radix < 0);
|
||||
_Length += sizeof(num);
|
||||
_Length += sizeof(num);
|
||||
utohex((int)(num & 0xFFFFFFFF), sizeof(num) >> 1, _Arr + _Length, radix < 0);
|
||||
_Length += sizeof(num);
|
||||
_Length += sizeof(num);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -464,13 +477,17 @@ bool String::Concat(UInt64 num, int radix)
|
|||
return Concat(buf, strlen(buf));
|
||||
}
|
||||
|
||||
char* ftoa(char* str, double num)
|
||||
static char* ftoa(char* str, int len, double num)
|
||||
{
|
||||
int len = sprintf(str, "%.8f", num);
|
||||
#if defined(_MSC_VER)
|
||||
len = sprintf_s(str, len, "%.8f", num);
|
||||
#else
|
||||
len = sprintf(str, "%.8f", num);
|
||||
#endif
|
||||
// 干掉后面多余的0
|
||||
for(int i=len; i>=0; i--)
|
||||
for (int i = len; i >= 0; i--)
|
||||
{
|
||||
if(str[i] == '0') str[i] = '\0';
|
||||
if (str[i] == '0') str[i] = '\0';
|
||||
}
|
||||
|
||||
return str;
|
||||
|
@ -479,7 +496,7 @@ char* ftoa(char* str, double num)
|
|||
bool String::Concat(float num, int decimalPlaces)
|
||||
{
|
||||
char buf[20];
|
||||
dtostrf(num, decimalPlaces, buf);
|
||||
dtostrf(num, decimalPlaces, buf, sizeof(buf));
|
||||
//sprintf(buf, "%f", num);
|
||||
//ftoa(buf, num);
|
||||
return Concat(buf, strlen(buf));
|
||||
|
@ -488,7 +505,7 @@ bool String::Concat(float num, int decimalPlaces)
|
|||
bool String::Concat(double num, int decimalPlaces)
|
||||
{
|
||||
char buf[20];
|
||||
dtostrf(num, decimalPlaces, buf);
|
||||
dtostrf(num, decimalPlaces, buf, sizeof(buf));
|
||||
//sprintf(buf, "%f", num);
|
||||
//ftoa(buf, num);
|
||||
return Concat(buf, strlen(buf));
|
||||
|
@ -504,16 +521,16 @@ int String::CompareTo(const String& s) const
|
|||
#endif
|
||||
int String::CompareTo(cstring cstr, int len, bool ignoreCase) const
|
||||
{
|
||||
if(len < 0) len = strlen(cstr);
|
||||
if (len < 0) len = strlen(cstr);
|
||||
if (!_Arr)
|
||||
{
|
||||
if (cstr && len > 0) return -1;
|
||||
return 0;
|
||||
}
|
||||
if(!cstr && _Arr && _Length > 0) return 1;
|
||||
if (!cstr && _Arr && _Length > 0) return 1;
|
||||
|
||||
// 逐个比较字符,直到指定长度或者源字符串结束
|
||||
if(ignoreCase)
|
||||
if (ignoreCase)
|
||||
return strncasecmp(_Arr, cstr, _Length);
|
||||
else
|
||||
return strncmp(_Arr, cstr, _Length);
|
||||
|
@ -526,8 +543,8 @@ bool String::Equals(const String& str) const
|
|||
|
||||
bool String::Equals(cstring cstr) const
|
||||
{
|
||||
int len = strlen(cstr);
|
||||
if(len != _Length) return false;
|
||||
int len = strlen(cstr);
|
||||
if (len != _Length) return false;
|
||||
|
||||
return CompareTo(cstr, len, false) == 0;
|
||||
}
|
||||
|
@ -539,8 +556,8 @@ bool String::EqualsIgnoreCase(const String &str) const
|
|||
|
||||
bool String::EqualsIgnoreCase(cstring cstr) const
|
||||
{
|
||||
int len = strlen(cstr);
|
||||
if(len != _Length) return false;
|
||||
int len = strlen(cstr);
|
||||
if (len != _Length) return false;
|
||||
|
||||
return CompareTo(cstr, len, true) == 0;
|
||||
}
|
||||
|
@ -620,19 +637,19 @@ ByteArray String::ToHex() const
|
|||
bs.SetLength(_Length / 2);
|
||||
|
||||
char cs[3];
|
||||
cs[2] = 0;
|
||||
byte* b = bs.GetBuffer();
|
||||
auto p = _Arr;
|
||||
int n = 0;
|
||||
for(int i=0; i<_Length; i+=2)
|
||||
cs[2] = 0;
|
||||
byte* b = bs.GetBuffer();
|
||||
auto p = _Arr;
|
||||
int n = 0;
|
||||
for (int i = 0; i < _Length; i += 2)
|
||||
{
|
||||
cs[0] = *p++;
|
||||
cs[1] = *p++;
|
||||
cs[0] = *p++;
|
||||
cs[1] = *p++;
|
||||
|
||||
*b++ = (byte)strtol(cs, nullptr, 16);
|
||||
*b++ = (byte)strtol(cs, nullptr, 16);
|
||||
|
||||
// 过滤横杠和空格
|
||||
if(*p == '-' || isspace(*p))
|
||||
if (*p == '-' || isspace(*p))
|
||||
{
|
||||
p++;
|
||||
i++;
|
||||
|
@ -647,9 +664,9 @@ ByteArray String::ToHex() const
|
|||
|
||||
int String::ToInt() const
|
||||
{
|
||||
if(_Length == 0) return 0;
|
||||
if (_Length == 0) return 0;
|
||||
|
||||
if(_Arr[_Length] == '\0') return atoi(_Arr);
|
||||
if (_Arr[_Length] == '\0') return atoi(_Arr);
|
||||
|
||||
// 非零结尾字符串需要特殊处理
|
||||
String s;
|
||||
|
@ -659,9 +676,9 @@ int String::ToInt() const
|
|||
|
||||
float String::ToFloat() const
|
||||
{
|
||||
if(_Length == 0) return 0;
|
||||
if (_Length == 0) return 0;
|
||||
|
||||
if(_Arr[_Length] == '\0') return (float)atof(_Arr);
|
||||
if (_Arr[_Length] == '\0') return (float)atof(_Arr);
|
||||
|
||||
// 非零结尾字符串需要特殊处理
|
||||
String s;
|
||||
|
@ -671,9 +688,9 @@ float String::ToFloat() const
|
|||
|
||||
double String::ToDouble() const
|
||||
{
|
||||
if(_Length == 0) return 0;
|
||||
if (_Length == 0) return 0;
|
||||
|
||||
if(_Arr[_Length] == '\0') return atof(_Arr);
|
||||
if (_Arr[_Length] == '\0') return atof(_Arr);
|
||||
|
||||
// 非零结尾字符串需要特殊处理
|
||||
String s;
|
||||
|
@ -686,7 +703,7 @@ String& String::ToStr(String& str) const
|
|||
{
|
||||
// 把当前字符串复制到目标字符串后面
|
||||
//str.Copy(*this, str._Length);
|
||||
str += *this;
|
||||
str += *this;
|
||||
|
||||
return (String&)*this;
|
||||
}
|
||||
|
@ -701,12 +718,12 @@ String String::ToString() const
|
|||
void String::Show(bool newLine) const
|
||||
{
|
||||
//if(_Length) debug_printf("%s", _Arr);
|
||||
for(int i=0; i<_Length; i++)
|
||||
for (int i = 0; i < _Length; i++)
|
||||
{
|
||||
//fput(_Arr[i]);
|
||||
debug_printf("%c", _Arr[i]);
|
||||
}
|
||||
if(newLine) debug_printf("\r\n");
|
||||
if (newLine) debug_printf("\r\n");
|
||||
}
|
||||
|
||||
// 格式化字符串,输出到现有字符串后面。方便我们连续格式化多个字符串
|
||||
|
@ -730,12 +747,12 @@ String& String::Format(cstring format, ...)
|
|||
|
||||
int String::IndexOf(const char ch, int startIndex) const
|
||||
{
|
||||
if(startIndex < 0) return -1;
|
||||
if(startIndex >= _Length) return -1;
|
||||
if (startIndex < 0) return -1;
|
||||
if (startIndex >= _Length) return -1;
|
||||
|
||||
for(int i=startIndex; i<_Length; i++)
|
||||
for (int i = startIndex; i < _Length; i++)
|
||||
{
|
||||
if(_Arr[i] == ch) return i;
|
||||
if (_Arr[i] == ch) return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
@ -753,11 +770,11 @@ int String::IndexOf(cstring str, int startIndex) const
|
|||
|
||||
int String::LastIndexOf(const char ch, int startIndex) const
|
||||
{
|
||||
if(startIndex >= _Length) return -1;
|
||||
if (startIndex >= _Length) return -1;
|
||||
|
||||
for(int i=_Length - 1; i>=startIndex; i--)
|
||||
for (int i = _Length - 1; i >= startIndex; i--)
|
||||
{
|
||||
if(_Arr[i] == ch) return i;
|
||||
if (_Arr[i] == ch) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
@ -774,29 +791,29 @@ int String::LastIndexOf(cstring str, int startIndex) const
|
|||
|
||||
int String::Search(cstring str, int len, int startIndex, bool rev) const
|
||||
{
|
||||
if(!str) return -1;
|
||||
if(startIndex < 0) return -1;
|
||||
if (!str) return -1;
|
||||
if (startIndex < 0) return -1;
|
||||
|
||||
// 可遍历的长度
|
||||
int count = _Length - len;
|
||||
if(startIndex > count) return -1;
|
||||
int count = _Length - len;
|
||||
if (startIndex > count) return -1;
|
||||
|
||||
// 遍历源字符串
|
||||
auto s = _Arr + startIndex;
|
||||
auto e = _Arr + _Length - 1;
|
||||
auto p = rev ? e : s;
|
||||
for(int i=0; i<=count; i++)
|
||||
auto s = _Arr + startIndex;
|
||||
auto e = _Arr + _Length - 1;
|
||||
auto p = rev ? e : s;
|
||||
for (int i = 0; i <= count; i++)
|
||||
{
|
||||
// 最大比较个数以目标字符串为准,源字符串确保长度足够
|
||||
if(strncmp(p, str, len) == 0) return p - _Arr;
|
||||
if (strncmp(p, str, len) == 0) return p - _Arr;
|
||||
|
||||
if(rev)
|
||||
if (rev)
|
||||
{
|
||||
if(--p < s) break;
|
||||
if (--p < s) break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(++p > e) break;
|
||||
if (++p > e) break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
@ -808,8 +825,8 @@ bool String::Contains(cstring str) const { return IndexOf(str) >= 0; }
|
|||
|
||||
bool String::StartsWith(const String& str, int startIndex) const
|
||||
{
|
||||
if(!_Arr || !str._Arr) return false;
|
||||
if(str._Length == 0) return false;
|
||||
if (!_Arr || !str._Arr) return false;
|
||||
if (str._Length == 0) return false;
|
||||
if (startIndex + str._Length > _Length) return false;
|
||||
|
||||
return strncmp(&_Arr[startIndex], str._Arr, str._Length) == 0;
|
||||
|
@ -817,9 +834,9 @@ bool String::StartsWith(const String& str, int startIndex) const
|
|||
|
||||
bool String::StartsWith(cstring str, int startIndex) const
|
||||
{
|
||||
if(!_Arr || !str) return false;
|
||||
int slen = strlen(str);
|
||||
if(slen == 0) return false;
|
||||
if (!_Arr || !str) return false;
|
||||
int slen = strlen(str);
|
||||
if (slen == 0) return false;
|
||||
if (startIndex + slen > _Length) return false;
|
||||
|
||||
return strncmp(&_Arr[startIndex], str, slen) == 0;
|
||||
|
@ -827,19 +844,19 @@ bool String::StartsWith(cstring str, int startIndex) const
|
|||
|
||||
bool String::EndsWith(const String& str) const
|
||||
{
|
||||
if(!_Arr || !str._Arr) return false;
|
||||
if(str._Length == 0) return false;
|
||||
if(str._Length > _Length) return false;
|
||||
if (!_Arr || !str._Arr) return false;
|
||||
if (str._Length == 0) return false;
|
||||
if (str._Length > _Length) return false;
|
||||
|
||||
return strncmp(&_Arr[_Length - str._Length], str._Arr, str._Length) == 0;
|
||||
}
|
||||
|
||||
bool String::EndsWith(cstring str) const
|
||||
{
|
||||
if(!_Arr || !str) return false;
|
||||
int slen = strlen(str);
|
||||
if(slen == 0) return false;
|
||||
if(slen > _Length) return false;
|
||||
if (!_Arr || !str) return false;
|
||||
int slen = strlen(str);
|
||||
if (slen == 0) return false;
|
||||
if (slen > _Length) return false;
|
||||
|
||||
return strncmp(&_Arr[_Length - slen], str, slen) == 0;
|
||||
}
|
||||
|
@ -858,9 +875,9 @@ String String::Substring(int start, int len) const
|
|||
{
|
||||
String str;
|
||||
|
||||
if(len < 0) len = _Length - start;
|
||||
if (len < 0) len = _Length - start;
|
||||
//str.Copy(this, _Length, start);
|
||||
if(_Length && start < _Length) str.copy(_Arr + start, len);
|
||||
if (_Length && start < _Length) str.copy(_Arr + start, len);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
@ -869,11 +886,11 @@ void trim(char* buffer, int& len, bool trimBegin, bool trimEnd)
|
|||
{
|
||||
if (!buffer || len == 0) return;
|
||||
char *begin = buffer;
|
||||
if(trimBegin) while (isspace(*begin)) begin++;
|
||||
if (trimBegin) while (isspace(*begin)) begin++;
|
||||
char *end = buffer + len - 1;
|
||||
if(trimEnd) while (isspace(*end) && end >= begin) end--;
|
||||
if (trimEnd) while (isspace(*end) && end >= begin) end--;
|
||||
len = end + 1 - begin;
|
||||
if (begin > buffer && len) Buffer(buffer, len) = begin;
|
||||
if (begin > buffer && len) Buffer(buffer, len) = begin;
|
||||
buffer[len] = 0;
|
||||
}
|
||||
|
||||
|
@ -905,10 +922,10 @@ String String::Replace(char find, char replace) const
|
|||
{
|
||||
String str(*this);
|
||||
|
||||
auto p = (char*)str.GetBuffer();
|
||||
for(int i=0; i<Length(); i++, p++)
|
||||
auto p = (char*)str.GetBuffer();
|
||||
for (int i = 0; i < Length(); i++, p++)
|
||||
{
|
||||
if(*p == find) *p = replace;
|
||||
if (*p == find) *p = replace;
|
||||
}
|
||||
|
||||
return str;
|
||||
|
@ -917,9 +934,9 @@ String String::Replace(char find, char replace) const
|
|||
String String::ToLower() const
|
||||
{
|
||||
String str(*this);
|
||||
auto p = str._Arr;
|
||||
for(int i=0; i<str._Length; i++)
|
||||
p[i] = tolower(p[i]);
|
||||
auto p = str._Arr;
|
||||
for (int i = 0; i < str._Length; i++)
|
||||
p[i] = tolower(p[i]);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
@ -927,9 +944,9 @@ String String::ToLower() const
|
|||
String String::ToUpper() const
|
||||
{
|
||||
String str(*this);
|
||||
auto p = str._Arr;
|
||||
for(int i=0; i<str._Length; i++)
|
||||
p[i] = toupper(p[i]);
|
||||
auto p = str._Arr;
|
||||
for (int i = 0; i < str._Length; i++)
|
||||
p[i] = toupper(p[i]);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
@ -937,11 +954,11 @@ String String::ToUpper() const
|
|||
// 静态比较器。比较两个字符串指针
|
||||
int String::Compare(const void* v1, const void* v2)
|
||||
{
|
||||
if(!v1) return v1 == v2 ? 0 : -1;
|
||||
if(!v2) return 1;
|
||||
if (!v1) return v1 == v2 ? 0 : -1;
|
||||
if (!v2) return 1;
|
||||
|
||||
auto str1 = (cstring)v1;
|
||||
auto str2 = (cstring)v2;
|
||||
auto str1 = (cstring)v1;
|
||||
auto str2 = (cstring)v2;
|
||||
return strncmp((char*)v1, str2, strlen(str1));
|
||||
}
|
||||
|
||||
|
@ -950,7 +967,7 @@ int String::Compare(const void* v1, const void* v2)
|
|||
#ifndef _MSC_VER
|
||||
extern char* itoa(int value, char *string, int radix)
|
||||
{
|
||||
return ltoa(value, string, radix) ;
|
||||
return ltoa(value, string, radix);
|
||||
}
|
||||
|
||||
extern char* ltoa(Int64 value, char* string, int radix)
|
||||
|
@ -962,9 +979,9 @@ extern char* ltoa(Int64 value, char* string, int radix)
|
|||
int sign;
|
||||
char *sp;
|
||||
|
||||
if ( string == nullptr ) return 0 ;
|
||||
if (string == nullptr) return 0;
|
||||
|
||||
if (radix > 36 || radix <= 1) return 0 ;
|
||||
if (radix > 36 || radix <= 1) return 0;
|
||||
|
||||
sign = (radix == 10 && value < 0);
|
||||
if (sign)
|
||||
|
@ -977,7 +994,7 @@ extern char* ltoa(Int64 value, char* string, int radix)
|
|||
i = v % radix;
|
||||
v = v / radix;
|
||||
if (i < 10)
|
||||
*tp++ = i+'0';
|
||||
*tp++ = i + '0';
|
||||
else
|
||||
*tp++ = i + 'a' - 10;
|
||||
}
|
||||
|
@ -995,18 +1012,18 @@ extern char* ltoa(Int64 value, char* string, int radix)
|
|||
|
||||
char* utohex(uint value, byte size, char* string, bool upper)
|
||||
{
|
||||
if (string == nullptr ) return 0;
|
||||
if (string == nullptr) return 0;
|
||||
|
||||
// 字节数乘以2是字符个数
|
||||
size <<= 1;
|
||||
size <<= 1;
|
||||
// 指针提前指向最后一个字符,数字从小往大处理,字符需要倒过来赋值
|
||||
auto tp = string + size;;
|
||||
*tp-- = '\0';
|
||||
char ch = upper ? 'A' : 'a';
|
||||
for(int i=0; i<size; i++)
|
||||
auto tp = string + size;;
|
||||
*tp-- = '\0';
|
||||
char ch = upper ? 'A' : 'a';
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
byte bt = value & 0x0F;
|
||||
value >>= 4;
|
||||
value >>= 4;
|
||||
if (bt < 10)
|
||||
*tp-- = bt + '0';
|
||||
else
|
||||
|
@ -1019,19 +1036,19 @@ char* utohex(uint value, byte size, char* string, bool upper)
|
|||
#ifndef _MSC_VER
|
||||
extern char* utoa(uint value, char* string, int radix)
|
||||
{
|
||||
return ultoa(value, string, radix ) ;
|
||||
return ultoa(value, string, radix);
|
||||
}
|
||||
|
||||
extern char* ultoa(UInt64 value, char* string, int radix)
|
||||
{
|
||||
if (string == nullptr ) return 0;
|
||||
if (string == nullptr) return 0;
|
||||
|
||||
if (radix > 36 || radix <= 1) return 0;
|
||||
|
||||
char tmp[33];
|
||||
auto tp = tmp;
|
||||
auto v = value;
|
||||
char ch = radix < 0 ? 'A' : 'a';
|
||||
auto tp = tmp;
|
||||
auto v = value;
|
||||
char ch = radix < 0 ? 'A' : 'a';
|
||||
while (v || tp == tmp)
|
||||
{
|
||||
auto i = v % radix;
|
||||
|
@ -1051,16 +1068,21 @@ extern char* ultoa(UInt64 value, char* string, int radix)
|
|||
}
|
||||
#endif
|
||||
|
||||
char *dtostrf (double val, byte prec, char* str)
|
||||
static char *dtostrf(double val, byte prec, char* str, int len)
|
||||
{
|
||||
char fmt[20];
|
||||
#if defined(_MSC_VER)
|
||||
sprintf_s(fmt, sizeof(fmt), "%%.%df", prec);
|
||||
len = sprintf_s(str, len, fmt, val);
|
||||
#else
|
||||
sprintf(fmt, "%%.%df", prec);
|
||||
int len = sprintf(str, fmt, val);
|
||||
len = sprintf(str, fmt, val);
|
||||
#endif
|
||||
|
||||
// 干掉后面多余的0
|
||||
for(int i=len; i>=0; i--)
|
||||
for (int i = len; i >= 0; i--)
|
||||
{
|
||||
if(str[i] == '0') str[i] = '\0';
|
||||
if (str[i] == '0') str[i] = '\0';
|
||||
}
|
||||
|
||||
return str;
|
||||
|
@ -1071,9 +1093,9 @@ char *dtostrf (double val, byte prec, char* str)
|
|||
StringSplit::StringSplit(const String& str, cstring sep) :
|
||||
_Str(str)
|
||||
{
|
||||
Sep = sep;
|
||||
Position = -1;
|
||||
Length = 0;
|
||||
Sep = sep;
|
||||
Position = -1;
|
||||
Length = 0;
|
||||
|
||||
// 先算好第一段
|
||||
//int p = _Str.IndexOf(_Sep);
|
||||
|
@ -1082,44 +1104,44 @@ StringSplit::StringSplit(const String& str, cstring sep) :
|
|||
|
||||
const String StringSplit::Next()
|
||||
{
|
||||
cstring ptr = nullptr;
|
||||
int len = 0;
|
||||
cstring ptr = nullptr;
|
||||
int len = 0;
|
||||
|
||||
if(Position >= -1 && Sep)
|
||||
if (Position >= -1 && Sep)
|
||||
{
|
||||
String sp = Sep;
|
||||
String sp = Sep;
|
||||
|
||||
// 从当前段之后开始找一段
|
||||
int s = Position + Length;
|
||||
int s = Position + Length;
|
||||
// 除首次以外,每次都要跳过分隔符
|
||||
if(s < 0)
|
||||
s = 0;
|
||||
if (s < 0)
|
||||
s = 0;
|
||||
else
|
||||
s += sp.Length();
|
||||
s += sp.Length();
|
||||
|
||||
// 检查是否已经越界
|
||||
if(s >= _Str.Length())
|
||||
if (s >= _Str.Length())
|
||||
{
|
||||
Position = -2;
|
||||
Length = 0;
|
||||
Position = -2;
|
||||
Length = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 查找分隔符
|
||||
int p = _Str.IndexOf(Sep, s);
|
||||
int p = _Str.IndexOf(Sep, s);
|
||||
|
||||
int sz = 0;
|
||||
int sz = 0;
|
||||
// 剩余全部长度,如果找不到下一个,那么这个就是最后长度。不用跳过分隔符
|
||||
if(p < 0)
|
||||
sz = _Str.Length() - s;
|
||||
if (p < 0)
|
||||
sz = _Str.Length() - s;
|
||||
else
|
||||
sz = p - s;
|
||||
sz = p - s;
|
||||
|
||||
Position = s;
|
||||
Length = sz;
|
||||
Position = s;
|
||||
Length = sz;
|
||||
|
||||
ptr = _Str.GetBuffer() + Position;
|
||||
len = Length;
|
||||
ptr = _Str.GetBuffer() + Position;
|
||||
len = Length;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
#include "HttpClient.h"
|
||||
|
||||
#include "Socket.h"
|
||||
|
||||
|
||||
HttpClient::HttpClient()
|
||||
{
|
||||
_Socket = nullptr;
|
||||
}
|
||||
|
||||
String HttpClient::Get(const String& uri)
|
||||
{
|
||||
String str;
|
||||
|
||||
return str;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef _HttpClient_H_
|
||||
#define _HttpClient_H_
|
||||
|
||||
#include "Core\SString.h"
|
||||
|
||||
class Socket;
|
||||
|
||||
// Http客户端
|
||||
class HttpClient
|
||||
{
|
||||
public:
|
||||
|
||||
HttpClient();
|
||||
|
||||
String Get(const String& uri);
|
||||
|
||||
private:
|
||||
Socket* _Socket;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,457 +0,0 @@
|
|||
#include "HttpClient.h"
|
||||
|
||||
#define NET_DEBUG DEBUG
|
||||
|
||||
|
||||
bool Callback(TinyIP* tip, void* param, Stream& ms);
|
||||
|
||||
TcpSocket::TcpSocket(TinyIP* tip) : Socket(tip)
|
||||
{
|
||||
Type = IP_TCP;
|
||||
|
||||
Port = 0;
|
||||
RemoteIP = 0;
|
||||
RemotePort = 0;
|
||||
LocalIP = 0;
|
||||
LocalPort = 0;
|
||||
|
||||
// 累加端口
|
||||
static ushort g_tcp_port = 1024;
|
||||
if(g_tcp_port < 1024) g_tcp_port = 1024;
|
||||
BindPort = g_tcp_port++;
|
||||
|
||||
// 我们仅仅递增第二个字节,这将允许我们以256或者512字节来发包
|
||||
static uint seqnum = 0xa;
|
||||
Seq = seqnum << 8;
|
||||
seqnum += 2;
|
||||
|
||||
Ack = 0;
|
||||
|
||||
Status = Closed;
|
||||
|
||||
OnAccepted = nullptr;
|
||||
OnReceived = nullptr;
|
||||
OnDisconnected = nullptr;
|
||||
}
|
||||
|
||||
cstring TcpSocket::ToString()
|
||||
{
|
||||
static char name[10];
|
||||
sprintf(name, "TCP_%d", Port);
|
||||
return name;
|
||||
}
|
||||
|
||||
bool TcpSocket::OnOpen()
|
||||
{
|
||||
if(Port != 0) BindPort = Port;
|
||||
|
||||
if(Port)
|
||||
debug_printf("Tcp::Open %d 过滤 %d\r\n", BindPort, Port);
|
||||
else
|
||||
debug_printf("Tcp::Open %d 监听所有端口TCP数据包\r\n", BindPort);
|
||||
|
||||
Enable = true;
|
||||
return Enable;
|
||||
}
|
||||
|
||||
void TcpSocket::OnClose()
|
||||
{
|
||||
debug_printf("Tcp::Close %d\r\n", BindPort);
|
||||
Enable = false;
|
||||
}
|
||||
|
||||
bool TcpSocket::Process(Stream* ms)
|
||||
{
|
||||
TCP_HEADER* tcp = (TCP_HEADER*)ms->Current();
|
||||
if(!ms->Seek(tcp->Size())) return false;
|
||||
|
||||
Header = tcp;
|
||||
uint len = ms->Remain();
|
||||
|
||||
ushort port = _REV16(tcp->DestPort);
|
||||
ushort remotePort = _REV16(tcp->SrcPort);
|
||||
|
||||
// 仅处理本连接的IP和端口
|
||||
if(Port != 0 && port != Port) return false;
|
||||
//if(RemotePort != 0 && remotePort != RemotePort) return false;
|
||||
//if(RemoteIP != 0 && Tip->RemoteIP != RemoteIP) return false;
|
||||
|
||||
IP_HEADER* ip = tcp->Prev();
|
||||
RemotePort = remotePort;
|
||||
RemoteIP = ip->SrcIP;
|
||||
LocalPort = port;
|
||||
LocalIP = ip->DestIP;
|
||||
|
||||
OnProcess(tcp, *ms);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TcpSocket::OnProcess(TCP_HEADER* tcp, Stream& ms)
|
||||
{
|
||||
// 计算标称的数据长度
|
||||
//uint len = tcp->Size() - sizeof(TCP_HEADER);
|
||||
//assert_param(len <= ms.Remain());
|
||||
// TCP好像没有标识数据长度的字段,但是IP里面有,这样子的话,ms里面的长度是准确的
|
||||
uint len = ms.Remain();
|
||||
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp::Process Flags=0x%02x From ", tcp->Flags);
|
||||
TinyIP::ShowIP(RemoteIP);
|
||||
debug_printf(":%d", RemotePort);
|
||||
debug_printf("\r\n");
|
||||
#endif
|
||||
|
||||
// 下次主动发数据时,用该序列号,因为对方Ack确认期望下次得到这个序列号
|
||||
Seq = _REV(tcp->Ack);
|
||||
Ack = _REV(tcp->Seq) + len + 1;
|
||||
|
||||
// 第一次同步应答
|
||||
if (tcp->Flags & TCP_FLAGS_SYN) // SYN连接请求标志位,为1表示发起连接的请求数据包
|
||||
{
|
||||
if(!(tcp->Flags & TCP_FLAGS_ACK))
|
||||
OnAccept(tcp, len);
|
||||
else
|
||||
Accepted2(tcp, len);
|
||||
}
|
||||
else if(tcp->Flags & (TCP_FLAGS_FIN | TCP_FLAGS_RST))
|
||||
{
|
||||
OnDisconnect(tcp, len);
|
||||
}
|
||||
// 第三次同步应答,三次应答后方可传输数据
|
||||
else if (tcp->Flags & TCP_FLAGS_ACK) // ACK确认标志位,为1表示此数据包为应答数据包
|
||||
{
|
||||
if(len == 0 && tcp->Ack <= 1)
|
||||
Accepted2(tcp, len);
|
||||
else
|
||||
OnDataReceive(tcp, len);
|
||||
}
|
||||
else
|
||||
debug_printf("Tcp::Process 未知标识位 0x%02x \r\n", tcp->Flags);
|
||||
}
|
||||
|
||||
void TcpSocket::OnAccept(TCP_HEADER* tcp, uint len)
|
||||
{
|
||||
if(OnAccepted)
|
||||
OnAccepted(this, tcp, tcp->Next(), len);
|
||||
else
|
||||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp Accept "); // 打印发送方的ip
|
||||
TinyIP::ShowIP(RemoteIP);
|
||||
debug_printf(":%d", RemotePort);
|
||||
debug_printf("\r\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
//第二次同步应答
|
||||
SetSeqAck(tcp, 1, false);
|
||||
SetMss(tcp);
|
||||
|
||||
// 需要用到MSS,所以采用4个字节的可选段
|
||||
//Send(tcp, 4, TCP_FLAGS_SYN | TCP_FLAGS_ACK);
|
||||
// 注意tcp->Size()包括头部的扩展数据,这里不用单独填4
|
||||
Send(tcp, 0, TCP_FLAGS_SYN | TCP_FLAGS_ACK);
|
||||
}
|
||||
|
||||
void TcpSocket::Accepted2(TCP_HEADER* tcp, uint len)
|
||||
{
|
||||
if(OnAccepted)
|
||||
OnAccepted(this, tcp, tcp->Next(), len);
|
||||
else
|
||||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp Accept3 "); // 打印发送方的ip
|
||||
TinyIP::ShowIP(RemoteIP);
|
||||
debug_printf(":%d", RemotePort);
|
||||
debug_printf("\r\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
//第二次同步应答
|
||||
SetSeqAck(tcp, 1, true);
|
||||
|
||||
Send(tcp, 0, TCP_FLAGS_ACK);
|
||||
}
|
||||
|
||||
void TcpSocket::OnDataReceive(TCP_HEADER* tcp, uint len)
|
||||
{
|
||||
// 无数据返回ACK
|
||||
if (len == 0)
|
||||
{
|
||||
if (tcp->Flags & (TCP_FLAGS_FIN | TCP_FLAGS_RST)) //FIN结束连接请求标志位。为1表示是结束连接的请求数据包
|
||||
{
|
||||
SetSeqAck(tcp, 1, true);
|
||||
Send(tcp, 0, TCP_FLAGS_ACK);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp Receive(%d) From ", len);
|
||||
TinyIP::ShowIP(RemoteIP);
|
||||
debug_printf("\r\n");
|
||||
#endif
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if(OnReceived)
|
||||
{
|
||||
// 返回值指示是否向对方发送数据包
|
||||
bool rs = OnReceived(this, tcp, tcp->Next(), len);
|
||||
if(!rs)
|
||||
{
|
||||
// 发送ACK,通知已收到
|
||||
SetSeqAck(tcp, 1, true);
|
||||
Send(tcp, 0, TCP_FLAGS_ACK);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp Receive(%d) From ", len);
|
||||
TinyIP::ShowIP(RemoteIP);
|
||||
debug_printf(" : ");
|
||||
String(tcp->Next(), len).Show(true);
|
||||
#endif
|
||||
}
|
||||
// 发送ACK,通知已收到
|
||||
SetSeqAck(tcp, len, true);
|
||||
//Send(buf, 0, TCP_FLAGS_ACK);
|
||||
|
||||
//TcpSend(buf, len);
|
||||
|
||||
// 响应Ack和发送数据一步到位
|
||||
Send(tcp, len, TCP_FLAGS_ACK | TCP_FLAGS_PUSH);
|
||||
}
|
||||
|
||||
void TcpSocket::OnDisconnect(TCP_HEADER* tcp, uint len)
|
||||
{
|
||||
if(OnDisconnected) OnDisconnected(this, tcp, tcp->Next(), len);
|
||||
|
||||
// RST是对方紧急关闭,这里啥都不干
|
||||
if(tcp->Flags & TCP_FLAGS_FIN)
|
||||
{
|
||||
SetSeqAck(tcp, 1, true);
|
||||
//Close(tcp, 0);
|
||||
Send(tcp, 0, TCP_FLAGS_ACK | TCP_FLAGS_PUSH | TCP_FLAGS_FIN);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp OnDisconnect "); // 打印发送方的ip
|
||||
TinyIP::ShowIP(RemoteIP);
|
||||
debug_printf(":%d Flags=0x%02x", RemotePort, tcp->Flags);
|
||||
debug_printf("\r\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void TcpSocket::Send(TCP_HEADER* tcp, uint len, byte flags)
|
||||
{
|
||||
tcp->SrcPort = _REV16(Port > 0 ? Port : LocalPort);
|
||||
tcp->DestPort = _REV16(RemotePort);
|
||||
tcp->Flags = flags;
|
||||
tcp->WindowSize = _REV16(1024);
|
||||
if(tcp->Length < sizeof(TCP_HEADER) / 4) tcp->Length = sizeof(TCP_HEADER) / 4;
|
||||
|
||||
// 必须在校验码之前设置,因为计算校验码需要地址
|
||||
Tip->RemoteIP = RemoteIP;
|
||||
|
||||
// 网络序是大端
|
||||
tcp->Checksum = 0;
|
||||
tcp->Checksum = _REV16(Tip->CheckSum((byte*)tcp, tcp->Size() + len, 2));
|
||||
|
||||
debug_printf("SendTcp: Flags=0x%02x, len=%d(0x%x) %d => %d \r\n", flags, tcp->Length, tcp->Length, _REV16(tcp->SrcPort), _REV16(tcp->DestPort));
|
||||
|
||||
// 注意tcp->Size()包括头部的扩展数据
|
||||
Tip->SendIP(IP_TCP, (byte*)tcp, tcp->Size() + len);
|
||||
}
|
||||
|
||||
void TcpSocket::SetSeqAck(TCP_HEADER* tcp, uint ackNum, bool opSeq)
|
||||
{
|
||||
/*
|
||||
第一次握手:主机A发送位码为SYN=1,随机产生Seq=x的数据包到服务器,主机B由SYN=1知道,A要求建立联机
|
||||
第二次握手:主机B收到请求后要确认联机信息,向A发送Ack=(A.Seq+1),SYN=1,ACK=1,随机产生Seq=y的包
|
||||
第三次握手:主机A收到后检查Ack是否正确,即A.Seq+1,以及位码ACK是否为1,
|
||||
若正确,主机A会再发送Ack=(B.Seq+1),ACK=1,主机B收到后确认Seq值与ACK=1则连接建立成功。
|
||||
完成三次握手,主机A与主机B开始传送数据。
|
||||
|
||||
Seq 序列号。每一个字节都编号,本报文所发送数据的第一个字节的序号。
|
||||
Ack 确认号。期望收到对方的下一个报文的数据的第一个字节的序号。
|
||||
*/
|
||||
//TCP_HEADER* tcp = Header;
|
||||
uint ack = tcp->Ack;
|
||||
tcp->Ack = _REV(_REV(tcp->Seq) + ackNum);
|
||||
if (!opSeq)
|
||||
{
|
||||
tcp->Seq = _REV(Seq);
|
||||
}
|
||||
else
|
||||
{
|
||||
tcp->Seq = ack;
|
||||
//tcp->Seq = _REV(Seq);
|
||||
}
|
||||
}
|
||||
|
||||
void TcpSocket::SetMss(TCP_HEADER* tcp)
|
||||
{
|
||||
tcp->Length = sizeof(TCP_HEADER) / 4;
|
||||
// 头部后面可能有可选数据,Length决定头部总长度(4的倍数)
|
||||
//if (mss)
|
||||
{
|
||||
uint* p = (uint*)tcp->Next();
|
||||
// 使用可选域设置 MSS 到 1460:0x5b4
|
||||
p[0] = _REV(0x020405b4);
|
||||
p[1] = _REV(0x01030302);
|
||||
p[2] = _REV(0x01010402);
|
||||
|
||||
tcp->Length += 3;
|
||||
}
|
||||
}
|
||||
|
||||
TCP_HEADER* TcpSocket::Create()
|
||||
{
|
||||
return (TCP_HEADER*)(Tip->Buffer + sizeof(ETH_HEADER) + sizeof(IP_HEADER));
|
||||
}
|
||||
|
||||
void TcpSocket::SendAck(uint len)
|
||||
{
|
||||
TCP_HEADER* tcp = Create();
|
||||
tcp->Init(true);
|
||||
Send(tcp, len, TCP_FLAGS_ACK | TCP_FLAGS_PUSH);
|
||||
}
|
||||
|
||||
void TcpSocket::Disconnect()
|
||||
{
|
||||
debug_printf("Tcp::Disconnect ");
|
||||
Tip->ShowIP(RemoteIP);
|
||||
debug_printf(":%d \r\n", RemotePort);
|
||||
|
||||
TCP_HEADER* tcp = Create();
|
||||
tcp->Init(true);
|
||||
Send(tcp, 0, TCP_FLAGS_ACK | TCP_FLAGS_PUSH | TCP_FLAGS_FIN);
|
||||
}
|
||||
|
||||
void TcpSocket::Send(const byte* buf, uint len)
|
||||
{
|
||||
debug_printf("Tcp::Send ");
|
||||
Tip->ShowIP(RemoteIP);
|
||||
debug_printf(":%d buf=0x%08x len=%d ...... \r\n", RemotePort, buf, len);
|
||||
|
||||
TCP_HEADER* tcp = Create();
|
||||
tcp->Init(true);
|
||||
byte* end = Tip->Buffer + Tip->BufferSize;
|
||||
if(buf < tcp->Next() || buf >= end)
|
||||
{
|
||||
// 复制数据,确保数据不会溢出
|
||||
uint len2 = Tip->BufferSize - tcp->Offset() - tcp->Size();
|
||||
assert_param(len <= len2);
|
||||
|
||||
Buffer(tcp->Next(), len) = buf;
|
||||
}
|
||||
|
||||
// 发送的时候采用LocalPort
|
||||
LocalPort = BindPort;
|
||||
|
||||
//SetSeqAck(tcp, len, true);
|
||||
tcp->Seq = _REV(Seq);
|
||||
tcp->Ack = _REV(Ack);
|
||||
// 发送数据的时候,需要同时带PUSH和ACK
|
||||
Send(tcp, len, TCP_FLAGS_PUSH | TCP_FLAGS_ACK);
|
||||
|
||||
Tip->LoopWait(Callback, this, 5000);
|
||||
|
||||
if(tcp->Flags & TCP_FLAGS_ACK)
|
||||
debug_printf("发送成功!\r\n");
|
||||
else
|
||||
debug_printf("发送失败!\r\n");
|
||||
}
|
||||
|
||||
// 连接远程服务器,记录远程服务器IP和端口,后续发送数据和关闭连接需要
|
||||
bool TcpSocket::Connect(IPAddress ip, ushort port)
|
||||
{
|
||||
debug_printf("Tcp::Connect ");
|
||||
Tip->ShowIP(ip);
|
||||
debug_printf(":%d ...... \r\n", port);
|
||||
|
||||
RemoteIP = ip;
|
||||
RemotePort = port;
|
||||
|
||||
TCP_HEADER* tcp = Create();
|
||||
tcp->Init(true);
|
||||
tcp->Seq = 0; // 仅仅是为了Ack=0,tcp->Seq还是会被Socket的顺序Seq替代
|
||||
SetSeqAck(tcp, 0, false);
|
||||
SetMss(tcp);
|
||||
|
||||
Status = SynSent;
|
||||
Send(tcp, 0, TCP_FLAGS_SYN);
|
||||
|
||||
if(Tip->LoopWait(Callback, this, 5000))
|
||||
{
|
||||
//if(tcp->Flags & TCP_FLAGS_SYN)
|
||||
if(Status = SynAck)
|
||||
{
|
||||
Status = Established;
|
||||
debug_printf("连接成功!\r\n");
|
||||
return true;
|
||||
}
|
||||
Status = Closed;
|
||||
debug_printf("拒绝连接!\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
Status = Closed;
|
||||
debug_printf("连接超时!\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Callback(TinyIP* tip, void* param, Stream& ms)
|
||||
{
|
||||
auto eth = ms.Retrieve<ETH_HEADER>();
|
||||
if(eth->Type != ETH_IP) return false;
|
||||
|
||||
auto _ip = ms.Retrieve<IP_HEADER>();
|
||||
if(_ip->Protocol != IP_TCP) return false;
|
||||
|
||||
auto socket = (TcpSocket*)param;
|
||||
|
||||
// 这里不移动数据流,方便后面调用Process
|
||||
auto tcp = (TCP_HEADER*)_ip->Next();
|
||||
|
||||
// 检查端口
|
||||
ushort port = _REV16(tcp->DestPort);
|
||||
if(port != socket->Port) return false;
|
||||
|
||||
socket->Header = tcp;
|
||||
|
||||
if(socket->Status == TcpSocket::SynSent)
|
||||
{
|
||||
if(tcp->Flags & TCP_FLAGS_ACK)
|
||||
{
|
||||
socket->Status = TcpSocket::SynAck;
|
||||
|
||||
// 处理。如果对方回发第二次握手包,或者终止握手
|
||||
//Stream ms(tip->Buffer, tip->BufferSize);
|
||||
socket->Process(&ms);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TcpSocket::OnWrite(const byte* buf, uint len)
|
||||
{
|
||||
Send(buf, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
uint TcpSocket::OnRead(byte* buf, uint len)
|
||||
{
|
||||
// 暂时不支持
|
||||
assert_param(false);
|
||||
return 0;
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
#ifndef _TinyIP_HttpClient_H_
|
||||
#define _TinyIP_HttpClient_H_
|
||||
|
||||
#include "TinyIP.h"
|
||||
|
||||
// Http客户端
|
||||
class HttpClient : public TcpSocket
|
||||
{
|
||||
public:
|
||||
|
||||
HttpClient(TinyIP* tip);
|
||||
|
||||
// 处理数据包
|
||||
virtual bool Process(Stream* ms);
|
||||
|
||||
bool Connect(IPAddress ip, ushort port); // 连接远程服务器,记录远程服务器IP和端口,后续发送数据和关闭连接需要
|
||||
void Send(const byte* buf, uint len); // 向Socket发送数据,可能是外部数据包
|
||||
void Disconnect(); // 关闭Socket
|
||||
|
||||
// 收到Tcp数据时触发,传递结构体和负载数据长度。返回值指示是否向对方发送数据包
|
||||
typedef bool (*TcpHandler)(TcpSocket* socket, TCP_HEADER* tcp, byte* buf, uint len);
|
||||
TcpHandler OnAccepted;
|
||||
TcpHandler OnReceived;
|
||||
TcpHandler OnDisconnected;
|
||||
|
||||
virtual cstring ToString();
|
||||
|
||||
protected:
|
||||
void SendAck(uint len);
|
||||
|
||||
void SetSeqAck(TCP_HEADER* tcp, uint ackNum, bool cp_seq);
|
||||
void SetMss(TCP_HEADER* tcp);
|
||||
void Send(TCP_HEADER* tcp, uint len, byte flags);
|
||||
|
||||
virtual void OnProcess(TCP_HEADER* tcp, Stream& ms);
|
||||
virtual void OnAccept(TCP_HEADER* tcp, uint len);
|
||||
virtual void Accepted2(TCP_HEADER* tcp, uint len);
|
||||
virtual void OnDataReceive(TCP_HEADER* tcp, uint len);
|
||||
virtual void OnDisconnect(TCP_HEADER* tcp, uint len);
|
||||
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose();
|
||||
|
||||
virtual bool OnWrite(const byte* buf, uint len);
|
||||
virtual uint OnRead(byte* buf, uint len);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -56,7 +56,7 @@ void Setup(ushort code, cstring name, COM message, int baudRate)
|
|||
Config::Current = &Config::CreateFlash();
|
||||
}
|
||||
|
||||
int Fix2401(const Buffer& bs)
|
||||
static int Fix2401(const Buffer& bs)
|
||||
{
|
||||
//auto& bs = *(Buffer*)param;
|
||||
// 微网指令特殊处理长度
|
||||
|
|
|
@ -213,7 +213,7 @@ void Token::Setup(ushort code, cstring name, COM message, int baudRate)
|
|||
Config::Current = &Config::CreateFlash();
|
||||
}
|
||||
|
||||
int Fix2401(const Buffer& bs)
|
||||
static int Fix2401(const Buffer& bs)
|
||||
{
|
||||
//auto& bs = *(Buffer*)param;
|
||||
// 微网指令特殊处理长度
|
||||
|
|
|
@ -36,6 +36,8 @@
|
|||
<ClCompile Include="..\Board\IOK0612.cpp" />
|
||||
<ClCompile Include="..\Board\NH3_0317.cpp" />
|
||||
<ClCompile Include="..\Board\Pandora.cpp" />
|
||||
<ClCompile Include="..\BootConfig.cpp" />
|
||||
<ClCompile Include="..\Config.cpp" />
|
||||
<ClCompile Include="..\Core\Array.cpp" />
|
||||
<ClCompile Include="..\Core\Buffer.cpp" />
|
||||
<ClCompile Include="..\Core\ByteArray.cpp" />
|
||||
|
@ -105,6 +107,7 @@
|
|||
<ClCompile Include="..\Net\Blu40.cpp" />
|
||||
<ClCompile Include="..\Net\Dhcp.cpp" />
|
||||
<ClCompile Include="..\Net\DNS.cpp" />
|
||||
<ClCompile Include="..\Net\HttpClient.cpp" />
|
||||
<ClCompile Include="..\Net\IPAddress.cpp" />
|
||||
<ClCompile Include="..\Net\IPEndPoint.cpp" />
|
||||
<ClCompile Include="..\Net\ITransport.cpp" />
|
||||
|
@ -121,6 +124,31 @@
|
|||
<ClCompile Include="..\Security\RC6.cpp" />
|
||||
<ClCompile Include="..\Security\RSA.cpp" />
|
||||
<ClCompile Include="..\Storage\Storage.cpp" />
|
||||
<ClCompile Include="..\Test\ADCTest.cpp" />
|
||||
<ClCompile Include="..\Test\ArrayTest.cpp" />
|
||||
<ClCompile Include="..\Test\AT45DBTest.cpp" />
|
||||
<ClCompile Include="..\Test\BufferTest.cpp" />
|
||||
<ClCompile Include="..\Test\CrcTest.cpp" />
|
||||
<ClCompile Include="..\Test\DateTimeTest.cpp" />
|
||||
<ClCompile Include="..\Test\DictionaryTest.cpp" />
|
||||
<ClCompile Include="..\Test\EthernetTest.cpp" />
|
||||
<ClCompile Include="..\Test\FlashTest.cpp" />
|
||||
<ClCompile Include="..\Test\InvokeTest.cpp" />
|
||||
<ClCompile Include="..\Test\IRTest.cpp" />
|
||||
<ClCompile Include="..\Test\JsonTest.cpp" />
|
||||
<ClCompile Include="..\Test\ListTest.cpp" />
|
||||
<ClCompile Include="..\Test\MessageTest.cpp" />
|
||||
<ClCompile Include="..\Test\NRF24L01Test.cpp" />
|
||||
<ClCompile Include="..\Test\PulsePortTest.cpp" />
|
||||
<ClCompile Include="..\Test\SerialTest.cpp" />
|
||||
<ClCompile Include="..\Test\StringTest.cpp" />
|
||||
<ClCompile Include="..\Test\ThreadTest.cpp" />
|
||||
<ClCompile Include="..\Test\TimerTest.cpp" />
|
||||
<ClCompile Include="..\TinyIP\Arp.cpp" />
|
||||
<ClCompile Include="..\TinyIP\Icmp.cpp" />
|
||||
<ClCompile Include="..\TinyIP\Tcp.cpp" />
|
||||
<ClCompile Include="..\TinyIP\TinyIP.cpp" />
|
||||
<ClCompile Include="..\TinyIP\Udp.cpp" />
|
||||
<ClCompile Include="..\TinyNet\DataMessage.cpp" />
|
||||
<ClCompile Include="..\TinyNet\JoinMessage.cpp" />
|
||||
<ClCompile Include="..\TinyNet\PingMessage.cpp" />
|
||||
|
|
|
@ -462,5 +462,85 @@
|
|||
<ClCompile Include="..\TokenNet\TokenConfig.cpp">
|
||||
<Filter>TokenNet</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Config.cpp" />
|
||||
<ClCompile Include="..\BootConfig.cpp" />
|
||||
<ClCompile Include="..\TinyIP\Icmp.cpp">
|
||||
<Filter>TinyIP</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\TinyIP\Tcp.cpp">
|
||||
<Filter>TinyIP</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\TinyIP\TinyIP.cpp">
|
||||
<Filter>TinyIP</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\TinyIP\Udp.cpp">
|
||||
<Filter>TinyIP</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\TinyIP\Arp.cpp">
|
||||
<Filter>TinyIP</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\TimerTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\ADCTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\ArrayTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\AT45DBTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\BufferTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\CrcTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\DateTimeTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\DictionaryTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\EthernetTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\FlashTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\InvokeTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\IRTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\JsonTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\ListTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\MessageTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\NRF24L01Test.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\PulsePortTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\SerialTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\StringTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Test\ThreadTest.cpp">
|
||||
<Filter>Test</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Net\HttpClient.cpp">
|
||||
<Filter>Net</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue