From 91229367f155f7ad1db4a343ea427f8a0187c927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4X2?= Date: Mon, 27 Feb 2017 17:57:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=BC=96=E8=AF=91=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=EF=BC=8CHttpClient=E7=A7=BB=E5=88=B0Net=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Board/AP0103.cpp | 4 +- Board/AP0104.cpp | 4 +- Board/AP0801.cpp | 4 +- Board/AP0802.cpp | 4 +- Board/IOK0612.cpp | 2 +- Board/NH3_0317.cpp | 2 +- Core/String.cpp | 378 +++++++++++++++--------------- Net/HttpClient.cpp | 16 ++ Net/HttpClient.h | 21 ++ TinyIP/HttpClient.cpp | 457 ------------------------------------- TinyIP/HttpClient.h | 48 ---- TinyNet/Tiny.cpp | 2 +- TokenNet/Token.cpp | 2 +- vs/SmartOS.vcxproj | 28 +++ vs/SmartOS.vcxproj.filters | 80 +++++++ 15 files changed, 357 insertions(+), 695 deletions(-) create mode 100644 Net/HttpClient.cpp create mode 100644 Net/HttpClient.h delete mode 100644 TinyIP/HttpClient.cpp delete mode 100644 TinyIP/HttpClient.h diff --git a/Board/AP0103.cpp b/Board/AP0103.cpp index 0127d8cd..4cc82ece 100644 --- a/Board/AP0103.cpp +++ b/Board/AP0103.cpp @@ -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; diff --git a/Board/AP0104.cpp b/Board/AP0104.cpp index f04f8ac8..b11edaee 100644 --- a/Board/AP0104.cpp +++ b/Board/AP0104.cpp @@ -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; diff --git a/Board/AP0801.cpp b/Board/AP0801.cpp index bea54426..770f983e 100644 --- a/Board/AP0801.cpp +++ b/Board/AP0801.cpp @@ -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; diff --git a/Board/AP0802.cpp b/Board/AP0802.cpp index 5403bb4a..ba9ba92f 100644 --- a/Board/AP0802.cpp +++ b/Board/AP0802.cpp @@ -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; // 微网指令特殊处理长度 diff --git a/Board/IOK0612.cpp b/Board/IOK0612.cpp index fb4694bc..193b3168 100644 --- a/Board/IOK0612.cpp +++ b/Board/IOK0612.cpp @@ -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); diff --git a/Board/NH3_0317.cpp b/Board/NH3_0317.cpp index b93a5457..aaef653a 100644 --- a/Board/NH3_0317.cpp +++ b/Board/NH3_0317.cpp @@ -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); diff --git a/Core/String.cpp b/Core/String.cpp index c17ced84..4808eefb 100644 --- a/Core/String.cpp +++ b/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 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>= 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; } } diff --git a/Net/HttpClient.cpp b/Net/HttpClient.cpp new file mode 100644 index 00000000..8841cb4c --- /dev/null +++ b/Net/HttpClient.cpp @@ -0,0 +1,16 @@ +#include "HttpClient.h" + +#include "Socket.h" + + +HttpClient::HttpClient() +{ + _Socket = nullptr; +} + +String HttpClient::Get(const String& uri) +{ + String str; + + return str; +} diff --git a/Net/HttpClient.h b/Net/HttpClient.h new file mode 100644 index 00000000..64b993f8 --- /dev/null +++ b/Net/HttpClient.h @@ -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 diff --git a/TinyIP/HttpClient.cpp b/TinyIP/HttpClient.cpp deleted file mode 100644 index 69c68e97..00000000 --- a/TinyIP/HttpClient.cpp +++ /dev/null @@ -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(); - if(eth->Type != ETH_IP) return false; - - auto _ip = ms.Retrieve(); - 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; -} diff --git a/TinyIP/HttpClient.h b/TinyIP/HttpClient.h deleted file mode 100644 index 84d328cc..00000000 --- a/TinyIP/HttpClient.h +++ /dev/null @@ -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 diff --git a/TinyNet/Tiny.cpp b/TinyNet/Tiny.cpp index 95122c89..54f2edd4 100644 --- a/TinyNet/Tiny.cpp +++ b/TinyNet/Tiny.cpp @@ -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; // 微网指令特殊处理长度 diff --git a/TokenNet/Token.cpp b/TokenNet/Token.cpp index facee846..d3b47ca9 100644 --- a/TokenNet/Token.cpp +++ b/TokenNet/Token.cpp @@ -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; // 微网指令特殊处理长度 diff --git a/vs/SmartOS.vcxproj b/vs/SmartOS.vcxproj index ed4ada00..10990c43 100644 --- a/vs/SmartOS.vcxproj +++ b/vs/SmartOS.vcxproj @@ -36,6 +36,8 @@ + + @@ -105,6 +107,7 @@ + @@ -121,6 +124,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/vs/SmartOS.vcxproj.filters b/vs/SmartOS.vcxproj.filters index 5c5ba060..188a73fe 100644 --- a/vs/SmartOS.vcxproj.filters +++ b/vs/SmartOS.vcxproj.filters @@ -462,5 +462,85 @@ TokenNet + + + + TinyIP + + + TinyIP + + + TinyIP + + + TinyIP + + + TinyIP + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Test + + + Net + \ No newline at end of file