This commit is contained in:
2024-12-04 12:32:03 +03:00
parent a642e913ed
commit 2fb1f546fa
6 changed files with 221 additions and 40 deletions

View File

@ -2,30 +2,29 @@
#include "utility/w5500.h"
#include "utility/socket.h"
std::list<EthernetMaketClient*>& EthernetMaketClient::getInstances() {
static std::list<EthernetMaketClient*> instances; // Список создаётся при первом доступе
return instances;
}
////////////////////////////////////////////////////////////////
EthernetMaketClient::EthernetMaketClient() {
_sock = MAX_SOCK_NUM;
getInstances().push_back(this);
// Serial.print("SOCK ?? "); Serial.println(_sock);
}
EthernetMaketClient::EthernetMaketClient(uint8_t sock) {
_sock = sock;
getInstances().push_back(this);
}
EthernetMaketClient::~EthernetMaketClient() {
getInstances().remove(this);
}
void EthernetMaketClient::tick()
{
forEach(
[](EthernetMaketClient& obj) {
obj.tick_();
}
);
void EthernetMaketClient::tick() {
for (EthernetMaketClient* client : getInstances()) {
client->tick_();
}
}
}
////////////////////////////////////////////////////////////////
ConnectionStatusSimple EthernetMaketClient::connectNonBlock(IPAddress ip, uint16_t port) {
dstIP = ip;
@ -58,6 +57,7 @@ ConnectionStatusSimple EthernetMaketClient::connectNonBlock(IPAddress ip, uint16
uint8_t s = w5500.readSnSR(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
_sock = i;
Serial.print("SOCK <- "); Serial.println(_sock);
break;
}
}
@ -85,8 +85,8 @@ ConnectionStatusSimple EthernetMaketClient::connectNonBlock(IPAddress ip, uint16
}
bool EthernetMaketClient::isConnected() const {
return connectStatus == CONNECT_CONNECTED;
bool EthernetMaketClient::canWriteIntoConnection(size_t size) const {
return connectStatus == CONNECT_CONNECTED /* && w5500.getTXFreeSize(_sock) >= size */;
}
void EthernetMaketClient::disconnect(){
@ -103,6 +103,11 @@ void EthernetMaketClient::close(){
}
void EthernetMaketClient::tick_(){
if(_sock != MAX_SOCK_NUM && connectStatus!=CONNECT_CONNECTED && connectStatus!=CONNECT_STOP_WAITING&&connectStatus!=CONNECT_CONNECTING){
Serial.print("tick_() sock = "); Serial.println(_sock);
}
switch (connectStatus)
{
case CONNECT_START:
@ -241,9 +246,13 @@ void EthernetMaketClient::dataWrite(uint8_t* data, uint16_t dataSize, bool overr
if (this->dataSize == 0 || override) {
this->dataPtr = data;
this->dataSize = dataSize;
if(isConnected()){
dataWrite();
}
activityUpdate();
Serial.println("Data buffer Saved.");
// if(canWrite(dataSize)){
// dataWrite();
// } else {
// Serial.println("W5500 Buffer is full. Write operation is ignored.");
// }
} else {
Serial.println("Data buffer is not empty. Write operation is ignored.");
}
@ -297,6 +306,15 @@ void EthernetMaketClient::startConnection(IPAddress ip, uint16_t port, bool nonB
dstPort = port;
isNonBlocking = nonBlock;
connectStatus = CONNECT_START;
// if (nonBlock)
// {
// connectNonBlock(dstIP, dstPort);
// }
// else
// {
// (ConnectionStatusSimple)connect(dstIP, dstPort);
// }
};
void EthernetMaketClient::activityUpdate(){
@ -329,3 +347,6 @@ EthernetMaketClient& EthernetMaketClient::operator=(const EthernetMaketClient& o
return *this;
}
ConnectionStatusSimple EthernetMaketClient::getConnectionStatus() const {
return connectStatus;
}