From 1dd8d62bae5a26f3dc5a7d51eb85cfe1fc7b8b1e Mon Sep 17 00:00:00 2001 From: iotcat Date: Fri, 19 Jul 2019 17:39:53 +0800 Subject: [PATCH] test ok --- lora-socket.h | 17 ++++++------ stringVec.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 stringVec.h diff --git a/lora-socket.h b/lora-socket.h index c173315..8033e37 100644 --- a/lora-socket.h +++ b/lora-socket.h @@ -3,6 +3,7 @@ #include #include "vector.h" +#include "stringVec.h" #ifndef LORA_SOCKET_IP #define LORA_SOCKET_IP "0.0.0.0" @@ -36,7 +37,7 @@ class LoRaSocket { private: - static Vector tcp_sendingStack, tcp_receiveStack; + static StringVec tcp_sendingStack, tcp_receiveStack; static Vector tcp_sendingTryTimes; /* LoRa Functions */ static void LoRa_tx_mode(); @@ -73,9 +74,9 @@ class LoRaSocket { return decode(s.substring(left + 1, right)); }; inline static const String getTcpKey(const String& s){ - unsigned short left = s.indexOf('|', s.indexOf('|', s.indexOf('|', s.indexOf('|') + 1) + 1) + 1); - unsigned short right = s.indexOf('|', left + 1); - return decode(s.substring(left + 1, right)); + int left = s.indexOf('|', s.indexOf('|', s.indexOf('|', s.indexOf('|') + 1) + 1) + 1); + int right = s.indexOf('|', left + 1); + return s.substring(left + 1, right); }; /* receive Functions */ static void getMsg(const String& msg); @@ -141,8 +142,7 @@ void LoRaSocket::getMsg(const String& msg){ onReceived(getContent(msg), getFromIP(msg), getToIP(msg), "tcp"); receiveStackClassify(); } - if(getType(msg) == "rtcp"){Serial.println(msg); - Serial.print("good rtcp"); + if(getType(msg) == "rtcp"){ removeByKey(getContent(msg)); } @@ -151,7 +151,6 @@ void LoRaSocket::getMsg(const String& msg){ void LoRaSocket::udp(const String& msg, const String& to){ String fin = "udp|"+ getIPHeader(to) + encode(msg) + "|"; fin += hash(fin); - Serial.println(fin); send(fin); }; @@ -173,7 +172,7 @@ void LoRaSocket::rtcp(const String& msg){ send(fin); } -Vector LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack; +StringVec LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack; Vector LoRaSocket::tcp_sendingTryTimes; void LoRaSocket::ini() { @@ -231,7 +230,7 @@ const String LoRaSocket::hash(const String& s){ void LoRaSocket::removeByKey(const String& key){ - for(unsigned int i = 0; i < tcp_sendingStack.Size(); i++){Serial.print(getTcpKey(tcp_sendingStack[i])); + for(unsigned int i = 0; i < tcp_sendingStack.Size(); i++){ if(getTcpKey(tcp_sendingStack[i]) == key) { tcp_sendingStack.Erase(i); return; diff --git a/stringVec.h b/stringVec.h new file mode 100644 index 0000000..08f4cf2 --- /dev/null +++ b/stringVec.h @@ -0,0 +1,77 @@ +#ifndef __STRINGVEC__ +#define __STRINGVEC__ + + + +#ifndef MAX_STRINGVEC_SIZE +#define MAX_STRINGVEC_SIZE 10 +#endif + +class StringVec{ +public: + StringVec():_size(0){ + this->clear(); + }; + + inline void PushBack(const String& ss){ + if(this->Size() == MAX_STRINGVEC_SIZE){ + this->shift(); + } + this->_s[this->Size()] = ss; + this->_size ++; + } + + const int Find(const String& ss){ + for(unsigned i = 0; i < this->Size(); i ++){ + if(this->_s[i] == ss){ + return i; + } + } + return -1; + } + + void Erase(const int& pos){ + if(pos < 0 || pos >= this->Size()) return; + for(unsigned int i = pos+1; i < this->Size(); i ++){ + this->_s[i-1] = this->_s[i]; + } + this->_size --; + } + + const String shift(){ + String s = this->_s[0]; + if(this->Size() != 0){ + for(unsigned int i = 1; i < this->Size(); i ++){ + this->_s[i-1] = this->_s[i]; + } + this->_size --; + } + return s; + } + + void clear(){ + for(unsigned int i = 0; i < this->Size(); i ++){ + this->_s[i] = ""; + } + } + + inline const int Size(){ + return this->_size; + } + + + String& operator[](int i){ + if(i < 0 || i >= this->Size()) return this->_s[0]; + return this->_s[i]; + } + +private: + String _s[MAX_STRINGVEC_SIZE]; + int _size; + +}; + + + + +#endif //__STRINGVEC__ \ No newline at end of file