Skip to content

Commit

Permalink
LUDP GUDP ETCP use interfaces
Browse files Browse the repository at this point in the history
gioblu committed Jul 16, 2018
1 parent 9c5d50a commit fe6552d
Showing 3 changed files with 25 additions and 25 deletions.
46 changes: 23 additions & 23 deletions src/strategies/EthernetTCP/EthernetLink.h
Original file line number Diff line number Diff line change
@@ -302,7 +302,7 @@ class EthernetLink {
uint16_t timeout_ms = 2000
) {
int32_t total_bytes_read = 0, bytes_read = ETCP_ERROR_READ;
uint32_t start_ms = millis();
uint32_t start_ms = PJON_MILLIS();
/* NOTE: The Arduino standard recv/read functions returns
-1 if no data waiting
0 if socket closed
@@ -313,8 +313,8 @@ class EthernetLink {
while(
client.connected() &&
(avail = client.available()) <= 0 &&
(uint32_t)(millis() - start_ms) < min(1000, timeout_ms)
) delayMicroseconds(250);
(uint32_t)(PJON_MILLIS() - start_ms) < min(1000, timeout_ms)
) PJON_DELAY_MICROSECONDS(250);
if (avail <= 0) continue;
#endif

@@ -327,7 +327,7 @@ class EthernetLink {
} while(
bytes_read != ETCP_ERROR_READ &&
total_bytes_read < length &&
(uint32_t)(millis() - start_ms) < timeout_ms
(uint32_t)(PJON_MILLIS() - start_ms) < timeout_ms
);

if(bytes_read == ETCP_ERROR_READ) {
@@ -343,13 +343,13 @@ class EthernetLink {
// Read a package from a connected client (incoming or outgoing socket) and send ACK
uint16_t receive(TCPHelperClient &client, bool wait) {
uint16_t return_value = PJON_FAIL;
uint32_t start_ms = millis(), avail;
uint32_t start_ms = PJON_MILLIS(), avail;
if (wait) {
while(
client.connected() &&
(avail = client.available()) <= 0 &&
(uint32_t)(millis() - start_ms) < 1000
) delayMicroseconds(250);
(uint32_t)(PJON_MILLIS() - start_ms) < 1000
) PJON_DELAY_MICROSECONDS(250);
} else avail = client.connected() ? client.available() : 0;

if(avail > 0) {
@@ -412,7 +412,7 @@ class EthernetLink {
#endif

return_value = ok ? PJON_ACK : PJON_FAIL;
if (ok) _last_receive_time = millis();
if (ok) _last_receive_time = PJON_MILLIS();
if (_ack_requested && !_receive_and_discard) {
// Write PJON_ACK
int8_t acklen = 0;
@@ -499,15 +499,15 @@ class EthernetLink {
// Use non-blocking calls, and receive and discard incoming packets
if (_client_out.prepare_connect(_remote_ip[pos], _remote_port[pos])) {
int8_t status = 0;
uint32_t start = millis();
uint32_t start = PJON_MILLIS();
do {
if (!_initiate_both_sockets_in_same_direction && !_single_socket && _server) {
TCPHelperClient client = _server->available();
if (client) client.stop();
}
status = _client_out.try_connect();
if (status == 0) delayMicroseconds(rand() % 250); // Avoid misusing CPU while waiting
} while (status == 0 && (uint32_t)(millis()-start)<4000);
if (status == 0) PJON_DELAY_MICROSECONDS(PJON_RANDOM(250)); // Avoid misusing CPU while waiting
} while (status == 0 && (uint32_t)(PJON_MILLIS()-start)<4000);
connected = (status == 1);
}
#endif
@@ -536,10 +536,10 @@ class EthernetLink {
#ifdef ETCP_DEBUG_PRINT
Serial.println("Conn rev..");
#endif
uint32_t start = millis();
uint32_t start = PJON_MILLIS();
do {
connected_rev = _client_in.connect(_remote_ip[pos], _remote_port[pos]);
} while (!connected_rev && (uint32_t)(millis()-start) < 2000);
} while (!connected_rev && (uint32_t)(PJON_MILLIS()-start) < 2000);
#ifdef ETCP_DEBUG_PRINT
Serial.println(connected_rev ? F("Conn rev to srv") : F("Failed rev conn to srv"));
#endif
@@ -556,14 +556,14 @@ class EthernetLink {
// ACK active on both sockets or none in this mode
_ack_requested = _request_ack;
did_connect = true;
_last_receive_time = millis(); // Count the connection as a receive action
_last_receive_time = PJON_MILLIS(); // Count the connection as a receive action
}
}
}
#endif

if (did_connect) { // Gather a litte connection information
_connection_time = millis();
_connection_time = PJON_MILLIS();
_connection_count++;
if (_single_socket) _ack_requested = _request_ack; // Same mode in both directions
}
@@ -576,7 +576,7 @@ class EthernetLink {
#endif
disconnect_out();
_current_device = -1;
delay(10); // Slow down if failure
PJON_DELAY(10); // Slow down if failure
return false; // Server is unreachable or busy
}
else _current_device = id; // Remember who we are connected to
@@ -674,10 +674,10 @@ class EthernetLink {
_request_ack = _ack_requested;

bool connected_reverse = false;
uint32_t start = millis();
uint32_t start = PJON_MILLIS();
do {
_client_out = _server->available();
} while (!_client_out && (uint32_t)(millis()-start) < 2000);
} while (!_client_out && (uint32_t)(PJON_MILLIS()-start) < 2000);

if(_client_out) {
#ifdef ETCP_DEBUG_PRINT
@@ -712,9 +712,9 @@ class EthernetLink {
#endif

if (did_connect) {
_connection_time = millis();
_connection_time = PJON_MILLIS();
_connection_count++;
_last_receive_time = millis(); // Count the connection as a receive action
_last_receive_time = PJON_MILLIS(); // Count the connection as a receive action
}
return connected;
};
@@ -731,7 +731,7 @@ class EthernetLink {
}
};

bool got_receive_timeout() { return (uint32_t)(millis() - _last_receive_time) > ETCP_IDLE_TIMEOUT; }
bool got_receive_timeout() { return (uint32_t)(PJON_MILLIS() - _last_receive_time) > ETCP_IDLE_TIMEOUT; }

bool disconnect_in_if_needed() {
if(_client_in && !_keep_connection) {
@@ -898,7 +898,7 @@ class EthernetLink {

// Read singlesocket header
bool ok = read_until_header(client, ETCP_SINGLE_SOCKET_HEADER);
if (ok) _last_receive_time = millis();
if (ok) _last_receive_time = PJON_MILLIS();
#ifdef ETCP_DEBUG_PRINT
//Serial.print("Read ss head, ok=");
//Serial.println(ok);
@@ -1202,7 +1202,7 @@ class EthernetLink {
uint16_t result = PJON_FAIL;
do {
result = receive();
if (result != PJON_ACK) delayMicroseconds(250);
if (result != PJON_ACK) PJON_DELAY_MICROSECONDS(250);
} while(
result != PJON_ACK &&
(uint32_t)(PJON_MICROS() - start) <= duration_us
2 changes: 1 addition & 1 deletion src/strategies/GlobalUDP/GlobalUDP.h
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ class GlobalUDP {

uint32_t back_off(uint8_t attempts) {
#ifdef PJON_ESP
return 10000ul*attempts + random(10000);
return 10000ul*attempts + PJON_RANDOM(10000);
#elif _WIN32
(void)attempts; // Avoid "unused parameter" warning
return 1000ul + (1000ul * rand()) / RAND_MAX;
2 changes: 1 addition & 1 deletion src/strategies/LocalUDP/LocalUDP.h
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ class LocalUDP {

uint32_t back_off(uint8_t attempts) {
#ifdef PJON_ESP
return 10000ul*attempts + random(10000);
return 10000ul * attempts + PJON_RANDOM(10000);
#elif _WIN32
return 1000ul * attempts + 1000ul*rand()/RAND_MAX;
#else

0 comments on commit fe6552d

Please sign in to comment.