diff --git a/examples/Client/Client.ino b/examples/Client/Client.ino index 6b3a8ba..dfd17cd 100644 --- a/examples/Client/Client.ino +++ b/examples/Client/Client.ino @@ -40,7 +40,11 @@ void onSend(){ #define TEST_1 // #define TEST_2 - +class VAR { + public: + uint16_t var = 1337; +}; +VAR vvv; void setup() { Serial.begin(115200); @@ -55,8 +59,12 @@ void setup() // Ethernet.setRtCount(3); // client.setNoDelayedACK(true); // Не ждать ответа от сервера - - client.setOnSendSuccess(onSend); + + // client.setOnSendSuccess(onSend); + client.setOnSendSuccess(std::bind([](VAR* bind_vvv){ + Serial.print("Sending Successfully: "); + Serial.println(bind_vvv->var++); + }, &vvv)); } #ifdef TEST_2 @@ -120,4 +128,41 @@ void loop() dataReady = false; } #endif + +if(client.available()>0){ + byte response[16]; + int len = client.read(response, sizeof(response)); + + if (len == 16) + { + // Проверка контрольной суммы + uint8_t checksum = 0; + for (int i = 0; i < 15; i++) + { + checksum += response[i]; + } + + Serial.print("RX: "); + for (int i = 0; i < 16; i++) + { + Serial.print(response[i], HEX); + Serial.print(" "); + } + + if (response[15] == checksum) + { + Serial.println("Checksum is OK"); + } + else + { + Serial.println("Checksum is ERROR"); + } + client.activityUpdate(); // Обновляем время последней активности + } + else + { + Serial.println("Received an incomplete response"); + } +} + } \ No newline at end of file diff --git a/src/EthernetOverride/TCP.cpp b/src/EthernetOverride/TCP.cpp index b924dcc..fbb49c8 100644 --- a/src/EthernetOverride/TCP.cpp +++ b/src/EthernetOverride/TCP.cpp @@ -212,44 +212,12 @@ void EthernetMaketClient::tick_(){ } //******************************************************** - //? Проверка наличия доступных данных от сервера - if (available() > 0) - { - byte response[16]; - int len = read(response, sizeof(response)); - - if (len == 16) - { - // Проверка контрольной суммы - uint8_t checksum = 0; - for (int i = 0; i < 15; i++) - { - checksum += response[i]; - } - - Serial.print("RX: "); - for (int i = 0; i < 16; i++) - { - Serial.print(response[i], HEX); - Serial.print(" "); - } - - if (response[15] == checksum) - { - Serial.println("Checksum is OK"); - } - else - { - Serial.println("Checksum is ERROR"); - } - lastActivityTime = millis(); // Обновляем время последней активности - } - else - { - Serial.println("Received an incomplete response"); - } - } - //? ------------------------------------------------- + // //? Проверка наличия доступных данных от сервера + // if (available() > 0) + // { + + // } + // //? ------------------------------------------------- } bool EthernetMaketClient::dataWrite(){ @@ -330,3 +298,34 @@ void EthernetMaketClient::startConnection(IPAddress ip, uint16_t port, bool nonB isNonBlocking = nonBlock; connectStatus = CONNECT_START; }; + +void EthernetMaketClient::activityUpdate(){ + lastActivityTime = millis(); +} + +EthernetMaketClient& EthernetMaketClient::operator=(const EthernetMaketClient& other) { + if (this == &other) { + return *this; // Защита от самоприсваивания + } + + // Копируем все данные + this->lastActivityTime = other.lastActivityTime; + this->startConnectionTime = other.startConnectionTime; + this->stopStartTime = other.stopStartTime; + this->isNonBlocking = other.isNonBlocking; + this->connectStatus = other.connectStatus; + // this->timeout = other.timeout; + // this->connectionTimeout = other.connectionTimeout; + // this->stopTimeout = other.stopTimeout; + this->dstIP = other.dstIP; + this->dstPort = other.dstPort; + this->dataPtr = other.dataPtr; + this->dataSize = other.dataSize; + this->onSendSuccess = other.onSendSuccess; + + // Отключаем исполнение старого клиента + const_cast(other).connectStatus = CONNECT_IDLE; + + return *this; +} + diff --git a/src/EthernetOverride/TCP.h b/src/EthernetOverride/TCP.h index 36752b5..04bcb3a 100644 --- a/src/EthernetOverride/TCP.h +++ b/src/EthernetOverride/TCP.h @@ -28,13 +28,15 @@ protected: static std::list& getInstances(); // Ленивое создание списка void tick_(); + ConnectionStatusSimple connectStatus = CONNECT_IDLE; public: - ConnectionStatusSimple connectStatus = CONNECT_IDLE; EthernetMaketClient(); EthernetMaketClient(uint8_t sock); ~EthernetMaketClient(); + EthernetMaketClient& operator=(const EthernetMaketClient& other); + ConnectionStatusSimple connectNonBlock(IPAddress ip, uint16_t port); void startConnection(IPAddress ip, uint16_t port, bool nonBlock = true); @@ -50,6 +52,8 @@ public: void setOnSendSuccess(const std::function& callback); void resetOnSendSuccess(); + + void activityUpdate(); }; #endif // __MAKET_TCP_H__ diff --git a/src/EthernetOverride/UDP.cpp b/src/EthernetOverride/UDP.cpp index b934d47..20765f2 100644 --- a/src/EthernetOverride/UDP.cpp +++ b/src/EthernetOverride/UDP.cpp @@ -60,3 +60,7 @@ void EthernetMaketUDP::endPacketNonBlock() void EthernetMaketUDP::tick(){ sendUDP_tick(_sock, _status); } + +ConnectionStatusSimple EthernetMaketUDP::getState() const{ + return _status; +} diff --git a/src/EthernetOverride/UDP.h b/src/EthernetOverride/UDP.h index ec5a9cc..da0bd78 100644 --- a/src/EthernetOverride/UDP.h +++ b/src/EthernetOverride/UDP.h @@ -10,6 +10,7 @@ public: using EthernetUDP::EthernetUDP; void endPacketNonBlock(); void tick(); + ConnectionStatusSimple getState() const; }; #endif // __MAKET_UDP_H__ \ No newline at end of file