MySensors Library & Examples
2.3.2-62-ge298769
hal
transport
PJON
driver
interfaces
ARDUINO
TCPHelper_ARDUINO.h
1
#pragma once
2
3
#ifdef PJON_ESP
4
#if defined(ESP32)
5
#include <WiFi.h>
6
#else
7
#include <ESP8266WiFi.h>
8
#endif
9
typedef
WiFiServer
TCPHelperServer
;
10
typedef
WiFiClient
TCPHelperClient
;
11
#define min(a,b) (a<b?a:b)
12
#define PJON_GET_MAC WiFi.macAddress
13
#else
14
#ifdef PJON_ETHERNET2
15
#include <Ethernet2.h>
16
#else
17
#include <Ethernet.h>
18
#endif
19
// #include <utility/socket.h> // May be required if using outdated IDE
20
typedef
EthernetServer
TCPHelperServer
;
21
typedef
EthernetClient
TCPHelperClient
;
22
#define PJON_GET_MAC EthernetClass::MACAddress
23
#endif
24
25
/*
26
// The following classes make it posssible to use the same non-blocking connect logic
27
// on Arduino as on POSIX. It works, allowing better communication with POSIX devices,
28
// but requires that the official EthernetClient and EthernetServer classes are changed
29
// so that private members are made protected, making it possible to add
30
// functionality to them.
31
32
class TCPHelperClient : public EthernetClient {
33
public:
34
bool prepare_connect(IPAddress ip, uint16_t port) {
35
if (_sock != MAX_SOCK_NUM)
36
stop();
37
38
if (_sock != MAX_SOCK_NUM)
39
return false;
40
41
for (int i = 0; i < MAX_SOCK_NUM; i++) {
42
uint8_t s = socketStatus(i);
43
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
44
_sock = i;
45
break;
46
}
47
}
48
49
if (_sock == MAX_SOCK_NUM)
50
return false;
51
52
_srcport++;
53
if (_srcport == 0) _srcport = 49152; //Use IANA recommended ephemeral port range 49152-65535
54
socket(_sock, SnMR::TCP, _srcport, 0);
55
56
if (!::connect(_sock, rawIPAddress(ip), port)) {
57
_sock = MAX_SOCK_NUM;
58
return false;
59
}
60
61
return true;
62
}
63
64
int try_connect() {
65
if (status() == SnSR::ESTABLISHED) return 1;
66
if (status() == SnSR::CLOSED) {
67
_sock = MAX_SOCK_NUM;
68
return -1;
69
}
70
return 0;
71
}
72
73
TCPHelperClient() : EthernetClient() { }
74
75
TCPHelperClient(uint16_t sock) : EthernetClient(sock) { }
76
77
TCPHelperClient(EthernetClient client) : EthernetClient(client ){ }
78
};
79
80
class TCPHelperServer : public EthernetServer {
81
public:
82
TCPHelperServer(uint16_t sock) : EthernetServer(sock) { }
83
84
TCPHelperClient available() {
85
EthernetClient client = EthernetServer::available();
86
return TCPHelperClient(client);
87
}
88
};
89
*/
EthernetClient
Definition:
EthernetClient.h:43
TCPHelperClient
Definition:
TCPHelper_POSIX.h:31
EthernetServer
EthernetServer class.
Definition:
EthernetServer.h:42
TCPHelperServer
Definition:
TCPHelper_POSIX.h:350
Copyright (C) 2013-2019 Sensnology AB. Generated by
doxygen
1.8.17