Pārlūkot izejas kodu

limit number of sessions

Chris Mullins 8 gadi atpakaļ
vecāks
revīzija
b2cab871e8
2 mainītis faili ar 19 papildinājumiem un 0 dzēšanām
  1. 14 0
      lib/Udp/V6MiLightUdpServer.cpp
  2. 5 0
      lib/Udp/V6MiLightUdpServer.h

+ 14 - 0
lib/Udp/V6MiLightUdpServer.cpp

@@ -1,5 +1,6 @@
 #include <V6MiLightUdpServer.h>
 #include <ESP8266WiFi.h>
+#include <Arduino.h>
   
 uint8_t V6MiLightUdpServer::START_SESSION_COMMAND[] = {
   0x20, 0x00, 0x00, 0x00, 0x16, 0x02, 0x62, 0x3A, 0xD5, 0xED, 0xA3, 0x01, 0xAE, 
@@ -68,6 +69,19 @@ uint16_t V6MiLightUdpServer::beginSession() {
   session->next = firstSession;
   firstSession = session;
   
+  if (numSessions >= V6_MAX_SESSIONS) {
+    V6Session* cur = firstSession;
+    
+    for (size_t i = 1; i < V6_MAX_SESSIONS; i++) {
+      cur = cur->next;
+    }
+    
+    delete cur->next;
+    cur->next = NULL;
+  } else {
+    numSessions++;
+  }
+  
   return id;
 }
 

+ 5 - 0
lib/Udp/V6MiLightUdpServer.h

@@ -8,6 +8,7 @@
 #include <Vector.h>
 
 #define V6_COMMAND_LEN 8
+#define V6_MAX_SESSIONS 10
 
 #ifndef _V6_MILIGHT_UDP_SERVER
 #define _V6_MILIGHT_UDP_SERVER 
@@ -39,6 +40,7 @@ public:
   V6MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
     : MiLightUdpServer(client, port, deviceId),
       sessionId(0),
+      numSessions(0),
       firstSession(NULL)
   { }
   
@@ -58,8 +60,11 @@ protected:
   static uint8_t START_SESSION_RESPONSE[];
   static uint8_t COMMAND_HEADER[];
   static uint8_t COMMAND_RESPONSE[];
+  static uint8_t SEARCH_COMMAND[];
+  static uint8_t LOCAL_SEARCH_COMMAND[];
   
   V6Session* firstSession;
+  size_t numSessions;
   uint16_t sessionId;
   
   uint16_t beginSession();