master
iotcat 5 years ago
parent 9399312d9b
commit 1dd8d62bae
  1. 17
      lora-socket.h
  2. 77
      stringVec.h

@ -3,6 +3,7 @@
#include <LoRa.h> #include <LoRa.h>
#include "vector.h" #include "vector.h"
#include "stringVec.h"
#ifndef LORA_SOCKET_IP #ifndef LORA_SOCKET_IP
#define LORA_SOCKET_IP "0.0.0.0" #define LORA_SOCKET_IP "0.0.0.0"
@ -36,7 +37,7 @@ class LoRaSocket {
private: private:
static Vector<String> tcp_sendingStack, tcp_receiveStack; static StringVec tcp_sendingStack, tcp_receiveStack;
static Vector<unsigned int> tcp_sendingTryTimes; static Vector<unsigned int> tcp_sendingTryTimes;
/* LoRa Functions */ /* LoRa Functions */
static void LoRa_tx_mode(); static void LoRa_tx_mode();
@ -73,9 +74,9 @@ class LoRaSocket {
return decode(s.substring(left + 1, right)); return decode(s.substring(left + 1, right));
}; };
inline static const String getTcpKey(const String& s){ inline static const String getTcpKey(const String& s){
unsigned short left = s.indexOf('|', s.indexOf('|', s.indexOf('|', s.indexOf('|') + 1) + 1) + 1); int left = s.indexOf('|', s.indexOf('|', s.indexOf('|', s.indexOf('|') + 1) + 1) + 1);
unsigned short right = s.indexOf('|', left + 1); int right = s.indexOf('|', left + 1);
return decode(s.substring(left + 1, right)); return s.substring(left + 1, right);
}; };
/* receive Functions */ /* receive Functions */
static void getMsg(const String& msg); static void getMsg(const String& msg);
@ -141,8 +142,7 @@ void LoRaSocket::getMsg(const String& msg){
onReceived(getContent(msg), getFromIP(msg), getToIP(msg), "tcp"); onReceived(getContent(msg), getFromIP(msg), getToIP(msg), "tcp");
receiveStackClassify(); receiveStackClassify();
} }
if(getType(msg) == "rtcp"){Serial.println(msg); if(getType(msg) == "rtcp"){
Serial.print("good rtcp");
removeByKey(getContent(msg)); removeByKey(getContent(msg));
} }
@ -151,7 +151,6 @@ void LoRaSocket::getMsg(const String& msg){
void LoRaSocket::udp(const String& msg, const String& to){ void LoRaSocket::udp(const String& msg, const String& to){
String fin = "udp|"+ getIPHeader(to) + encode(msg) + "|"; String fin = "udp|"+ getIPHeader(to) + encode(msg) + "|";
fin += hash(fin); fin += hash(fin);
Serial.println(fin);
send(fin); send(fin);
}; };
@ -173,7 +172,7 @@ void LoRaSocket::rtcp(const String& msg){
send(fin); send(fin);
} }
Vector<String> LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack; StringVec LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack;
Vector<unsigned int> LoRaSocket::tcp_sendingTryTimes; Vector<unsigned int> LoRaSocket::tcp_sendingTryTimes;
void LoRaSocket::ini() { void LoRaSocket::ini() {
@ -231,7 +230,7 @@ const String LoRaSocket::hash(const String& s){
void LoRaSocket::removeByKey(const String& key){ 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) { if(getTcpKey(tcp_sendingStack[i]) == key) {
tcp_sendingStack.Erase(i); tcp_sendingStack.Erase(i);
return; return;

@ -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__
Loading…
Cancel
Save