Skip to content

Commit

Permalink
make connect by hostname able to run async
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev committed Aug 19, 2016
1 parent 6271c62 commit cc4d1ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
40 changes: 29 additions & 11 deletions src/ESPAsyncTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
extern "C"{
#include "lwip/opt.h"
#include "lwip/tcp.h"
//#include "lwip/tcp_impl.h"
#include "lwip/inet.h"
#include "lwip/dns.h"
}

#ifdef ESP8266
Expand Down Expand Up @@ -54,13 +54,13 @@ AsyncClient::AsyncClient(tcp_pcb* pcb):
, _recv_cb_arg(0)
, _timeout_cb(0)
, _timeout_cb_arg(0)
, _refcnt(0)
, _pcb_busy(false)
, _pcb_sent_at(0)
, _close_pcb(false)
, _ack_pcb(true)
, _rx_last_packet(0)
, _rx_since_timeout(0)
, _connect_port(0)
, prev(NULL)
, next(NULL)
{
Expand Down Expand Up @@ -112,6 +112,26 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
return true;
}

bool AsyncClient::connect(const char* host, uint16_t port) {
IPAddress remote_addr;
ip_addr_t addr;
remote_addr = static_cast<uint32_t>(0);

if(remote_addr.fromString(host)) {
return connect(remote_addr, port);
}

err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_s_dns_found, this);
if(err == ERR_OK) {
remote_addr = addr.addr;
return connect(remote_addr, port);
} else if(err == ERR_INPROGRESS) {
_connect_port = port;
return true;
}
return false;
}

AsyncClient& AsyncClient::operator=(const AsyncClient& other){
if (_pcb)
_close();
Expand Down Expand Up @@ -336,6 +356,13 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){

// lWIP Callbacks

void AsyncClient::_s_dns_found(const char *name, ip_addr_t *ipaddr, void *arg){
AsyncClient* c = reinterpret_cast<AsyncClient*>(arg);
IPAddress remote_addr;
remote_addr = ipaddr->addr;
c->connect(remote_addr, c->_connect_port);
}

int8_t AsyncClient::_s_poll(void *arg, struct tcp_pcb *tpcb) {
return reinterpret_cast<AsyncClient*>(arg)->_poll(tpcb);
}
Expand Down Expand Up @@ -371,15 +398,6 @@ AsyncClient & AsyncClient::operator+=(const AsyncClient &other) {
return *this;
}


// Make this async
bool AsyncClient::connect(const char* host, uint16_t port){
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr))
return connect(remote_addr, port);
return false;
}

void AsyncClient::setRxTimeout(uint32_t timeout){
_rx_since_timeout = timeout;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ESPAsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler

struct tcp_pcb;
struct pbuf;
struct ip_addr;

class AsyncClient {
protected:
Expand All @@ -62,14 +63,14 @@ class AsyncClient {
void* _timeout_cb_arg;
AcConnectHandler _poll_cb;
void* _poll_cb_arg;
int _refcnt;
bool _pcb_busy;
uint32_t _pcb_sent_at;
bool _close_pcb;
bool _ack_pcb;
uint32_t _rx_ack_len;
uint32_t _rx_last_packet;
uint32_t _rx_since_timeout;
uint16_t _connect_port;

int8_t _close();
int8_t _connected(void* pcb, int8_t err);
Expand All @@ -82,6 +83,7 @@ class AsyncClient {
static void _s_error(void *arg, int8_t err);
static int8_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len);
static int8_t _s_connected(void* arg, void* tpcb, int8_t err);
static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg);

public:
AsyncClient* prev;
Expand Down

1 comment on commit cc4d1ac

@omersiar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this can be applied to <ESPAsyncUDP.h>? Or am I delusional? My Small NTP library crashes ESP because WiFi.byHostName blocks Async call.

// send an NTP request to the time server at the given address
time_t ICACHE_FLASH_ATTR NtpClient::getNtpTime() {
	
	memset(NTPpacket, 0, sizeof(NTPpacket));
	NTPpacket[0] = 0b11100011;
	NTPpacket[1] = 0;
	NTPpacket[2] = 6;
	NTPpacket[3] = 0xEC;
	NTPpacket[12] = 49;
	NTPpacket[13] = 0x4E;
	NTPpacket[14] = 49;
	NTPpacket[15] = 52;
        WiFi.HostByName(TimeServerName,timeServer);
	if (udpListener.connect(timeServer, 123)) {
		udpListener.onPacket([](AsyncUDPPacket packet) {
			unsigned long highWord = word(packet.data()[40], packet.data()[41]);
			unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
			time_t UnixUTCtime = (highWord << 16 | lowWord) - 2208988800UL;
			setTime(UnixUTCtime);
		});
	}
	else {

	}
	udpListener.write(NTPpacket, sizeof(NTPpacket));
	// ugly
	return 0;
}

Please sign in to comment.