mirror of
https://github.com/me-no-dev/AsyncTCP.git
synced 2025-07-30 02:37:32 +02:00
Initial Import
This commit is contained in:
37
.travis.yml
Normal file
37
.travis.yml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
sudo: false
|
||||||
|
language: bash
|
||||||
|
os:
|
||||||
|
- linux
|
||||||
|
|
||||||
|
script:
|
||||||
|
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
|
||||||
|
- sleep 3
|
||||||
|
- export DISPLAY=:1.0
|
||||||
|
- wget http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz
|
||||||
|
- tar xf arduino-1.6.5-linux64.tar.xz
|
||||||
|
- mv arduino-1.6.5 $HOME/arduino_ide
|
||||||
|
- export PATH="$HOME/arduino_ide:$PATH"
|
||||||
|
- which arduino
|
||||||
|
- mkdir -p $HOME/Arduino/libraries
|
||||||
|
- cp -r $TRAVIS_BUILD_DIR $HOME/Arduino/libraries/ESPAsyncTCP
|
||||||
|
- cd $HOME/arduino_ide/hardware
|
||||||
|
- mkdir esp8266com
|
||||||
|
- cd esp8266com
|
||||||
|
- git clone https://github.com/esp8266/Arduino.git esp8266
|
||||||
|
- cd esp8266/tools
|
||||||
|
- python get.py
|
||||||
|
- source $TRAVIS_BUILD_DIR/travis/common.sh
|
||||||
|
- arduino --board esp8266com:esp8266:generic --save-prefs
|
||||||
|
- arduino --get-pref sketchbook.path
|
||||||
|
- build_sketches arduino $HOME/Arduino/libraries/ESPAsyncTCP esp8266
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
on_success: change
|
||||||
|
on_failure: change
|
||||||
|
webhooks:
|
||||||
|
urls:
|
||||||
|
- https://webhooks.gitter.im/e/60e65d0c78ea0a920347
|
||||||
|
on_success: change # options: [always|never|change] default: always
|
||||||
|
on_failure: always # options: [always|never|change] default: always
|
||||||
|
on_start: false # default: false
|
11
README.md
11
README.md
@ -1,2 +1,11 @@
|
|||||||
# AsyncTCP
|
# AsyncTCP
|
||||||
Async TCP Library for ESP32
|
Async TCP Library for ESP32 Arduino
|
||||||
|
|
||||||
|
[](https://gitter.im/me-no-dev/ESPAsyncWebServer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-connection network environment for Espressif's ESP32 MCUs.
|
||||||
|
|
||||||
|
This library is the base for [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)
|
||||||
|
|
||||||
|
## AsyncClient and AsyncServer
|
||||||
|
The base classes on which everything else is built. They expose all possible scenarios, but are really raw and require more skills to use.
|
||||||
|
3
component.mk
Normal file
3
component.mk
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
COMPONENT_ADD_INCLUDEDIRS := src
|
||||||
|
COMPONENT_SRCDIRS := src
|
||||||
|
CXXFLAGS += -fno-rtti
|
22
library.json
Normal file
22
library.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name":"AsyncTCP",
|
||||||
|
"description":"Asynchronous TCP Library for ESP32",
|
||||||
|
"keywords":"async,tcp",
|
||||||
|
"authors":
|
||||||
|
{
|
||||||
|
"name": "Hristo Gochkov",
|
||||||
|
"maintainer": true
|
||||||
|
},
|
||||||
|
"repository":
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/me-no-dev/AsyncTCP.git"
|
||||||
|
},
|
||||||
|
"version": "1.0.0",
|
||||||
|
"license": "LGPL-3.0",
|
||||||
|
"frameworks": "arduino",
|
||||||
|
"platforms":"espressif",
|
||||||
|
"examples": [
|
||||||
|
"examples/*/*.ino"
|
||||||
|
]
|
||||||
|
}
|
9
library.properties
Normal file
9
library.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
name=AsyncTCP
|
||||||
|
version=1.0.0
|
||||||
|
author=Me-No-Dev
|
||||||
|
maintainer=Me-No-Dev
|
||||||
|
sentence=Async TCP Library for ESP32
|
||||||
|
paragraph=Async TCP Library for ESP32
|
||||||
|
category=Other
|
||||||
|
url=https://github.com/me-no-dev/AsyncTCP
|
||||||
|
architectures=*
|
1007
src/AsyncTCP.cpp
Normal file
1007
src/AsyncTCP.cpp
Normal file
File diff suppressed because it is too large
Load Diff
188
src/AsyncTCP.h
Normal file
188
src/AsyncTCP.h
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
Asynchronous TCP library for Espressif MCUs
|
||||||
|
|
||||||
|
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
|
||||||
|
This file is part of the esp8266 core for Arduino environment.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ASYNCTCP_H_
|
||||||
|
#define ASYNCTCP_H_
|
||||||
|
|
||||||
|
#include "IPAddress.h"
|
||||||
|
#include <functional>
|
||||||
|
extern "C" {
|
||||||
|
#include "freertos/semphr.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
class AsyncClient;
|
||||||
|
|
||||||
|
#define ASYNC_MAX_ACK_TIME 5000
|
||||||
|
#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given)
|
||||||
|
#define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react.
|
||||||
|
|
||||||
|
typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
|
||||||
|
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
|
||||||
|
typedef std::function<void(void*, AsyncClient*, int8_t error)> AcErrorHandler;
|
||||||
|
typedef std::function<void(void*, AsyncClient*, void *data, size_t len)> AcDataHandler;
|
||||||
|
typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler;
|
||||||
|
|
||||||
|
struct tcp_pcb;
|
||||||
|
struct pbuf;
|
||||||
|
struct _ip_addr;
|
||||||
|
|
||||||
|
class AsyncClient {
|
||||||
|
protected:
|
||||||
|
tcp_pcb* _pcb;
|
||||||
|
|
||||||
|
AcConnectHandler _connect_cb;
|
||||||
|
void* _connect_cb_arg;
|
||||||
|
AcConnectHandler _discard_cb;
|
||||||
|
void* _discard_cb_arg;
|
||||||
|
AcAckHandler _sent_cb;
|
||||||
|
void* _sent_cb_arg;
|
||||||
|
AcErrorHandler _error_cb;
|
||||||
|
void* _error_cb_arg;
|
||||||
|
AcDataHandler _recv_cb;
|
||||||
|
void* _recv_cb_arg;
|
||||||
|
AcTimeoutHandler _timeout_cb;
|
||||||
|
void* _timeout_cb_arg;
|
||||||
|
AcConnectHandler _poll_cb;
|
||||||
|
void* _poll_cb_arg;
|
||||||
|
|
||||||
|
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;
|
||||||
|
uint32_t _ack_timeout;
|
||||||
|
uint16_t _connect_port;
|
||||||
|
|
||||||
|
int8_t _close();
|
||||||
|
int8_t _connected(void* pcb, int8_t err);
|
||||||
|
void _error(int8_t err);
|
||||||
|
int8_t _poll(tcp_pcb* pcb);
|
||||||
|
int8_t _sent(tcp_pcb* pcb, uint16_t len);
|
||||||
|
void _dns_found(struct _ip_addr *ipaddr);
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
AsyncClient* prev;
|
||||||
|
AsyncClient* next;
|
||||||
|
|
||||||
|
AsyncClient(tcp_pcb* pcb = 0);
|
||||||
|
~AsyncClient();
|
||||||
|
|
||||||
|
AsyncClient & operator=(const AsyncClient &other);
|
||||||
|
AsyncClient & operator+=(const AsyncClient &other);
|
||||||
|
|
||||||
|
bool operator==(const AsyncClient &other);
|
||||||
|
|
||||||
|
bool operator!=(const AsyncClient &other) {
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
bool connect(IPAddress ip, uint16_t port);
|
||||||
|
bool connect(const char* host, uint16_t port);
|
||||||
|
void close(bool now = false);
|
||||||
|
void stop();
|
||||||
|
int8_t abort();
|
||||||
|
bool free();
|
||||||
|
|
||||||
|
bool canSend();//ack is not pending
|
||||||
|
size_t space();
|
||||||
|
size_t add(const char* data, size_t size, uint8_t apiflags=0);//add for sending
|
||||||
|
bool send();//send all data added with the method above
|
||||||
|
size_t ack(size_t len); //ack data that you have not acked using the method below
|
||||||
|
void ackLater(){ _ack_pcb = false; } //will not ack the current packet. Call from onData
|
||||||
|
|
||||||
|
size_t write(const char* data);
|
||||||
|
size_t write(const char* data, size_t size, uint8_t apiflags=0); //only when canSend() == true
|
||||||
|
|
||||||
|
uint8_t state();
|
||||||
|
bool connecting();
|
||||||
|
bool connected();
|
||||||
|
bool disconnecting();
|
||||||
|
bool disconnected();
|
||||||
|
bool freeable();//disconnected or disconnecting
|
||||||
|
|
||||||
|
uint16_t getMss();
|
||||||
|
uint32_t getRxTimeout();
|
||||||
|
void setRxTimeout(uint32_t timeout);//no RX data timeout for the connection in seconds
|
||||||
|
uint32_t getAckTimeout();
|
||||||
|
void setAckTimeout(uint32_t timeout);//no ACK timeout for the last sent packet in milliseconds
|
||||||
|
void setNoDelay(bool nodelay);
|
||||||
|
bool getNoDelay();
|
||||||
|
uint32_t getRemoteAddress();
|
||||||
|
uint16_t getRemotePort();
|
||||||
|
uint32_t getLocalAddress();
|
||||||
|
uint16_t getLocalPort();
|
||||||
|
|
||||||
|
IPAddress remoteIP();
|
||||||
|
uint16_t remotePort();
|
||||||
|
IPAddress localIP();
|
||||||
|
uint16_t localPort();
|
||||||
|
|
||||||
|
void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect
|
||||||
|
void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected
|
||||||
|
void onAck(AcAckHandler cb, void* arg = 0); //ack received
|
||||||
|
void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error
|
||||||
|
void onData(AcDataHandler cb, void* arg = 0); //data received
|
||||||
|
void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout
|
||||||
|
void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected
|
||||||
|
|
||||||
|
const char * errorToString(int8_t error);
|
||||||
|
const char * stateToString();
|
||||||
|
|
||||||
|
int8_t _recv(tcp_pcb* pcb, pbuf* pb, int8_t err);
|
||||||
|
|
||||||
|
static int8_t _s_poll(void *arg, struct tcp_pcb *tpcb);
|
||||||
|
static int8_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, int8_t err);
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
class AsyncServer {
|
||||||
|
protected:
|
||||||
|
uint16_t _port;
|
||||||
|
IPAddress _addr;
|
||||||
|
bool _noDelay;
|
||||||
|
tcp_pcb* _pcb;
|
||||||
|
AcConnectHandler _connect_cb;
|
||||||
|
void* _connect_cb_arg;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
AsyncServer(IPAddress addr, uint16_t port);
|
||||||
|
AsyncServer(uint16_t port);
|
||||||
|
~AsyncServer();
|
||||||
|
void onClient(AcConnectHandler cb, void* arg);
|
||||||
|
void begin();
|
||||||
|
void end();
|
||||||
|
void setNoDelay(bool nodelay);
|
||||||
|
bool getNoDelay();
|
||||||
|
uint8_t status();
|
||||||
|
|
||||||
|
static int8_t _s_accept(void *arg, tcp_pcb* newpcb, int8_t err);
|
||||||
|
protected:
|
||||||
|
int8_t _accept(tcp_pcb* newpcb, int8_t err);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* ASYNCTCP_H_ */
|
23
travis/common.sh
Executable file
23
travis/common.sh
Executable file
@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
function build_sketches()
|
||||||
|
{
|
||||||
|
local arduino=$1
|
||||||
|
local srcpath=$2
|
||||||
|
local platform=$3
|
||||||
|
local sketches=$(find $srcpath -name *.ino)
|
||||||
|
for sketch in $sketches; do
|
||||||
|
local sketchdir=$(dirname $sketch)
|
||||||
|
if [[ -f "$sketchdir/.$platform.skip" ]]; then
|
||||||
|
echo -e "\n\n ------------ Skipping $sketch ------------ \n\n";
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
echo -e "\n\n ------------ Building $sketch ------------ \n\n";
|
||||||
|
$arduino --verify $sketch;
|
||||||
|
local result=$?
|
||||||
|
if [ $result -ne 0 ]; then
|
||||||
|
echo "Build failed ($1)"
|
||||||
|
return $result
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
Reference in New Issue
Block a user