This commit is contained in:
2024-10-28 17:55:14 +03:00
commit f3e1dd6ee8
11 changed files with 505 additions and 0 deletions

View File

@ -0,0 +1,48 @@
#include "TCP.h"
#include "utility/w5500.h"
#include "utility/socket.h"
ConnectionStatusSimple EthernetMaketClient::connectNonBlock(IPAddress ip, uint16_t port) {
if (_sock != MAX_SOCK_NUM) {
// Serial.println(F("connectNonBlock"));
uint8_t status_ = status();
switch (status_) {
case SnSR::CLOSED:
// Serial.println(F("CLOSED"));
_sock = MAX_SOCK_NUM;
return CONNECT_FAIL;
break;
case SnSR::ESTABLISHED:
// Serial.println(F("ESTABLISHED"));
return CONNECT_SUCCESS;
break;
default:
// Serial.print(F("status "));
// Serial.println(status_,HEX);
return CONNECT_CONNECTING;
break;
}
}
for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = w5500.readSnSR(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
_sock = i;
break;
}
}
if (_sock == MAX_SOCK_NUM)
return CONNECT_FAIL;
_srcport++;
if (_srcport == 0) _srcport = 1024;
socket(_sock, SnMR::TCP, _srcport, 0);
if (!::connect(_sock, rawIPAddress(ip), port)) {
_sock = MAX_SOCK_NUM;
return CONNECT_FAIL;
}
return CONNECT_CONNECTING;
}