mirror of
https://github.com/Show-maket/EthernetMaket.git
synced 2025-05-03 14:50:22 +00:00
upd
This commit is contained in:
parent
2fb1f546fa
commit
daf17f1ac6
@ -22,10 +22,10 @@ EthernetMaketClient EthernetMaketServer::available(int sock)
|
||||
{
|
||||
if (client.available())
|
||||
{
|
||||
return EthernetMaketClient(client);
|
||||
return std::move(EthernetMaketClient(client));
|
||||
}
|
||||
}
|
||||
return EthernetMaketClient(MAX_SOCK_NUM);
|
||||
return std::move(EthernetMaketClient(MAX_SOCK_NUM));
|
||||
}
|
||||
|
||||
void EthernetMaketServer::accept(int sock)
|
||||
@ -60,19 +60,32 @@ void EthernetMaketServer::begin(int sock)
|
||||
socket(sock, SnMR::TCP, _port, 0);
|
||||
listen(sock);
|
||||
EthernetClass::_server_port[sock] = _port;
|
||||
}
|
||||
serverSocket = sock;
|
||||
return;
|
||||
}
|
||||
// Serial.println(F("EthernetMaketServer::begin() FAIL"));
|
||||
}
|
||||
|
||||
void EthernetMaketServer::begin()
|
||||
{
|
||||
EthernetServer::begin();
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||||
EthernetClient client(sock);
|
||||
if (client.status() == SnSR::CLOSED) {
|
||||
socket(sock, SnMR::TCP, _port, 0);
|
||||
listen(sock);
|
||||
EthernetClass::_server_port[sock] = _port;
|
||||
serverSocket = sock;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Serial.println(F("EthernetMaketServer::begin() FAIL"));
|
||||
}
|
||||
|
||||
void EthernetMaketServer::tick()
|
||||
{
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
|
||||
// for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
|
||||
{
|
||||
EthernetMaketClient sclient = available(sock);
|
||||
EthernetMaketClient sclient = available(serverSocket);
|
||||
if (sclient.connected() && sclient.available() > 0)
|
||||
{
|
||||
serverHandler(sclient);
|
||||
|
@ -12,6 +12,7 @@ private:
|
||||
void accept(int sock);
|
||||
std::function<void(EthernetMaketClient&)> serverHandler = [](EthernetMaketClient &sclient) {};
|
||||
bool wasConnected[MAX_SOCK_NUM];
|
||||
SOCKET serverSocket = MAX_SOCK_NUM;
|
||||
|
||||
public:
|
||||
using EthernetServer::EthernetServer;
|
||||
|
@ -13,6 +13,9 @@ EthernetMaketClient::EthernetMaketClient() {
|
||||
EthernetMaketClient::EthernetMaketClient(uint8_t sock) {
|
||||
_sock = sock;
|
||||
}
|
||||
EthernetMaketClient::EthernetMaketClient(EthernetClient &client) {
|
||||
_sock = client.getSocketNumber();
|
||||
}
|
||||
|
||||
void EthernetMaketClient::tick()
|
||||
{
|
||||
@ -116,6 +119,35 @@ void EthernetMaketClient::tick_(){
|
||||
connectStatus = CONNECT_CONNECTING;
|
||||
close();
|
||||
Serial.println("CONNECT_CONNECTING...");
|
||||
{
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = w5500.readSnSR(i);
|
||||
String state = "";
|
||||
switch (s) {
|
||||
case 0x00: state = "CLOSED"; break;
|
||||
case 0x13: state = "INIT"; break;
|
||||
case 0x14: state = "LISTEN"; break;
|
||||
case 0x15: state = "SYNSENT"; break;
|
||||
case 0x16: state = "SYNRECV"; break;
|
||||
case 0x17: state = "ESTABLISHED"; break;
|
||||
case 0x18: state = "FIN_WAIT"; break;
|
||||
case 0x1A: state = "CLOSING"; break;
|
||||
case 0x1B: state = "TIME_WAIT"; break;
|
||||
case 0x1C: state = "CLOSE_WAIT"; break;
|
||||
case 0x1D: state = "LAST_ACK"; break;
|
||||
case 0x22: state = "UDP"; break;
|
||||
case 0x32: state = "IPRAW"; break;
|
||||
case 0x42: state = "MACRAW"; break;
|
||||
case 0x5F: state = "PPPOE"; break;
|
||||
default: state = "UNKNOWN"; break;
|
||||
}
|
||||
|
||||
Serial.print("SOCK <- ");
|
||||
Serial.print(i);
|
||||
Serial.print(" State: ");
|
||||
Serial.println(state);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CONNECT_CONNECTING:
|
||||
if (millis() - startConnectionTime >= connectionTimeout)
|
||||
@ -230,8 +262,7 @@ bool EthernetMaketClient::dataWrite(){
|
||||
if(dataSize) {
|
||||
lastActivityTime = millis();
|
||||
if(write(dataPtr, dataSize)){
|
||||
dataPtr = nullptr;
|
||||
dataSize = 0; // Очищаем размер данных
|
||||
clearData();
|
||||
ret = true;
|
||||
}
|
||||
lastActivityTime = millis();
|
||||
@ -242,8 +273,16 @@ bool EthernetMaketClient::dataWrite(){
|
||||
return ret;
|
||||
}
|
||||
|
||||
void EthernetMaketClient::clearData(){
|
||||
this->dataPtr = nullptr;
|
||||
this->dataSize = 0;
|
||||
}
|
||||
bool EthernetMaketClient::isDataEmpty() const{
|
||||
return this->dataPtr == nullptr;
|
||||
}
|
||||
|
||||
void EthernetMaketClient::dataWrite(uint8_t* data, uint16_t dataSize, bool override) {
|
||||
if (this->dataSize == 0 || override) {
|
||||
if (this->dataPtr == nullptr || override) {
|
||||
this->dataPtr = data;
|
||||
this->dataSize = dataSize;
|
||||
activityUpdate();
|
||||
|
@ -33,6 +33,25 @@ protected:
|
||||
public:
|
||||
EthernetMaketClient();
|
||||
EthernetMaketClient(uint8_t sock);
|
||||
EthernetMaketClient(EthernetClient &client);
|
||||
|
||||
// // Move constructor
|
||||
// EthernetMaketClient(EthernetMaketClient&& other) noexcept
|
||||
// : EthernetClient(std::move(other)), // Move base class
|
||||
// lastActivityTime(other.lastActivityTime),
|
||||
// startConnectionTime(other.startConnectionTime),
|
||||
// stopStartTime(other.stopStartTime),
|
||||
// onSendSuccess(std::move(other.onSendSuccess)),
|
||||
// isNonBlocking(other.isNonBlocking),
|
||||
// dataPtr(other.dataPtr),
|
||||
// dataSize(other.dataSize),
|
||||
// dstIP(std::move(other.dstIP)),
|
||||
// dstPort(other.dstPort),
|
||||
// connectStatus(other.connectStatus)
|
||||
// {
|
||||
// other.dataPtr = nullptr; // Reset the source object's pointer
|
||||
// other.dataSize = 0;
|
||||
// }
|
||||
|
||||
EthernetMaketClient& operator=(const EthernetMaketClient& other);
|
||||
|
||||
@ -40,7 +59,8 @@ public:
|
||||
void startConnection(IPAddress ip, uint16_t port, bool nonBlock = true);
|
||||
|
||||
void dataWrite(uint8_t* data, uint16_t dataSize, bool override = false);
|
||||
// bool send(uint8_t* data, uint16_t dataSize, IPAddress ip, uint16_t port);
|
||||
void clearData();
|
||||
bool isDataEmpty() const;
|
||||
|
||||
void disconnect();
|
||||
void close();
|
||||
|
15
src/EthernetOverride/TCPSession.h
Normal file
15
src/EthernetOverride/TCPSession.h
Normal file
@ -0,0 +1,15 @@
|
||||
#include "TCP.h"
|
||||
|
||||
static EthernetMaketClient tcpSock[MAX_SOCK_NUM];
|
||||
|
||||
using TCP_SESSION = SOCKET;
|
||||
|
||||
TCP_SESSION newConnection(IPAddress ip, uint16_t port, bool nonBlock = true){
|
||||
for(int i = 0; i < MAX_SOCK_NUM; i++){
|
||||
if(tcpSock[i].status() == SnSR::CLOSED){
|
||||
tcpSock[i].startConnection(ip, port, nonBlock);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return MAX_SOCK_NUM;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user