Browse Source

linkedlist instead of vector

Chris Mullins 8 years ago
parent
commit
0d8fcecf31
3 changed files with 27 additions and 10 deletions
  1. 2 1
      lib/Udp/MiLightUdpServer.cpp
  2. 20 7
      lib/Udp/V6MiLightUdpServer.cpp
  3. 5 2
      lib/Udp/V6MiLightUdpServer.h

+ 2 - 1
lib/Udp/MiLightUdpServer.cpp

@@ -1,6 +1,7 @@
 #include <MiLightUdpServer.h>
 #include <V5MiLightUdpServer.h>
 #include <V6MiLightUdpServer.h>
+#include <ESP8266WiFi.h>
 
 MiLightUdpServer::MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
   : client(client), 
@@ -14,7 +15,7 @@ MiLightUdpServer::~MiLightUdpServer() {
 }
 
 void MiLightUdpServer::begin() {
-  socket.begin(this->port);
+  socket.begin(port);
 }
 
 void MiLightUdpServer::stop() {

+ 20 - 7
lib/Udp/V6MiLightUdpServer.cpp

@@ -28,6 +28,16 @@ size_t size(T(&)[sz]) {
     return sz;
 }
 
+V6MiLightUdpServer::~V6MiLightUdpServer() {
+  V6Session* cur = firstSession;
+  
+  while (cur != NULL) {
+    V6Session* next = cur->next;
+    delete cur;
+    cur = next;
+  }
+}
+
 template <typename T>
 T V6MiLightUdpServer::readInt(uint8_t* packet) {
   size_t numBytes = sizeof(T);
@@ -54,8 +64,10 @@ uint8_t* V6MiLightUdpServer::writeInt(const T& value, uint8_t* packet) {
 uint16_t V6MiLightUdpServer::beginSession() {
   const uint16_t id = sessionId++;
   
-  V6Session session(socket.remoteIP(), socket.remotePort(), id);
-  sessions.push_back(session);
+  V6Session* session = new V6Session(socket.remoteIP(), socket.remotePort(), id);
+  session->next = firstSession;
+  firstSession = session;
+  
   return id;
 }
 
@@ -73,15 +85,16 @@ void V6MiLightUdpServer::handleStartSession() {
 }
   
 void V6MiLightUdpServer::sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize) {
-  V6Session* session = NULL;
+  V6Session* session = firstSession;
   
-  for (size_t i = 0; i < sessions.size(); i++) {
-    if (sessions[i].sessionId == sessionId) {
-      session = &sessions[i];
+  while (session != NULL) {
+    if (session->sessionId == sessionId) {
+      break;
     }
+    session = session->next;
   }
   
-  if (session == NULL) {
+  if (session == NULL || session->sessionId != sessionId) {
     Serial.print("Tried to send response to untracked session id: ");
     Serial.println(sessionId);
     return;

+ 5 - 2
lib/Udp/V6MiLightUdpServer.h

@@ -38,9 +38,12 @@ class V6MiLightUdpServer : public MiLightUdpServer {
 public:
   V6MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
     : MiLightUdpServer(client, port, deviceId),
-      sessionId(0)
+      sessionId(0),
+      firstSession(NULL)
   { }
   
+  ~V6MiLightUdpServer();
+  
   // Should return size of the response packet
   virtual void handlePacket(uint8_t* packet, size_t packetSize);
   
@@ -56,7 +59,7 @@ protected:
   static uint8_t COMMAND_HEADER[];
   static uint8_t COMMAND_RESPONSE[];
   
-  Vector<V6Session> sessions;
+  V6Session* firstSession;
   uint16_t sessionId;
   
   uint16_t beginSession();