Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
/*
2
  Asynchronous TCP library for Espressif MCUs
3
 
4
  Copyright (c) 2016 Hristo Gochkov. All rights reserved.
5
  This file is part of the esp8266 core for Arduino environment.
6
 
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public
9
  License as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
 
12
  This library is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
  Lesser General Public License for more details.
16
 
17
  You should have received a copy of the GNU Lesser General Public
18
  License along with this library; if not, write to the Free Software
19
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
 
22
#ifndef ASYNCPRINTER_H_
23
#define ASYNCPRINTER_H_
24
 
25
#include "Arduino.h"
26
#include "ESPAsyncTCP.h"
27
#include "cbuf.h"
28
 
29
class AsyncPrinter;
30
 
31
typedef std::function<void(void*, AsyncPrinter*, uint8_t*, size_t)> ApDataHandler;
32
typedef std::function<void(void*, AsyncPrinter*)> ApCloseHandler;
33
 
34
class AsyncPrinter: public Print {
35
  private:
36
    AsyncClient *_client;
37
    ApDataHandler _data_cb;
38
    void *_data_arg;
39
    ApCloseHandler _close_cb;
40
    void *_close_arg;
41
    cbuf *_tx_buffer;
42
    size_t _tx_buffer_size;
43
 
44
    void _onConnect(AsyncClient *c);
45
  public:
46
    AsyncPrinter *next;
47
 
48
    AsyncPrinter();
49
    AsyncPrinter(AsyncClient *client, size_t txBufLen = TCP_MSS);
50
    virtual ~AsyncPrinter();
51
 
52
    int connect(IPAddress ip, uint16_t port);
53
    int connect(const char *host, uint16_t port);
54
 
55
    void onData(ApDataHandler cb, void *arg);
56
    void onClose(ApCloseHandler cb, void *arg);
57
 
58
    operator bool();
59
    AsyncPrinter & operator=(const AsyncPrinter &other);
60
 
61
    size_t write(uint8_t data);
62
    size_t write(const uint8_t *data, size_t len);
63
 
64
    bool connected();
65
    void close();
66
 
67
    size_t _sendBuffer();
68
    void _onData(void *data, size_t len);
69
    void _on_close();
70
    void _attachCallbacks();
71
};
72
 
73
#endif /* ASYNCPRINTER_H_ */