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 SYNCCLIENT_H_
23
#define SYNCCLIENT_H_
24
 
25
#include "Client.h"
26
// Needed for Arduino core releases prior to 2.5.0, because of changes
27
// made to accommodate Arduino core 2.5.0
28
// CONST was 1st defined in Core 2.5.0 in IPAddress.h
29
#ifndef CONST
30
#define CONST
31
#endif
32
#include <async_config.h>
33
class cbuf;
34
class AsyncClient;
35
 
36
class SyncClient: public Client {
37
  private:
38
    AsyncClient *_client;
39
    cbuf *_tx_buffer;
40
    size_t _tx_buffer_size;
41
    cbuf *_rx_buffer;
42
    int *_ref;
43
 
44
    size_t _sendBuffer();
45
    void _onData(void *data, size_t len);
46
    void _onConnect(AsyncClient *c);
47
    void _onDisconnect();
48
    void _attachCallbacks();
49
    void _attachCallbacks_Disconnect();
50
    void _attachCallbacks_AfterConnected();
51
    void _release();
52
 
53
  public:
54
    SyncClient(size_t txBufLen = TCP_MSS);
55
    SyncClient(AsyncClient *client, size_t txBufLen = TCP_MSS);
56
    virtual ~SyncClient();
57
 
58
    int ref();
59
    int unref();
60
    operator bool(){ return connected(); }
61
    SyncClient & operator=(const SyncClient &other);
62
 
63
#if ASYNC_TCP_SSL_ENABLED
64
    int _connect(const IPAddress& ip, uint16_t port, bool secure);
65
    int connect(CONST IPAddress& ip, uint16_t port, bool secure){
66
      return _connect(ip, port, secure);
67
    }
68
    int connect(IPAddress ip, uint16_t port, bool secure){
69
      return _connect(reinterpret_cast<const IPAddress&>(ip), port, secure);
70
    }
71
    int connect(const char *host, uint16_t port, bool secure);
72
    int connect(CONST IPAddress& ip, uint16_t port){
73
      return _connect(ip, port, false);
74
    }
75
    int connect(IPAddress ip, uint16_t port){
76
      return _connect(reinterpret_cast<const IPAddress&>(ip), port, false);
77
    }
78
    int connect(const char *host, uint16_t port){
79
      return connect(host, port, false);
80
    }
81
#else
82
    int _connect(const IPAddress& ip, uint16_t port);
83
    int connect(CONST IPAddress& ip, uint16_t port){
84
      return _connect(ip, port);
85
    }
86
    int connect(IPAddress ip, uint16_t port){
87
      return _connect(reinterpret_cast<const IPAddress&>(ip), port);
88
    }
89
    int connect(const char *host, uint16_t port);
90
#endif
91
    void setTimeout(uint32_t seconds);
92
 
93
    uint8_t status();
94
    uint8_t connected();
95
 
96
    bool stop(unsigned int maxWaitMs);
97
    bool flush(unsigned int maxWaitMs);
98
    void stop() { (void)stop(0);}
99
    void flush() { (void)flush(0);}
100
    size_t write(uint8_t data);
101
    size_t write(const uint8_t *data, size_t len);
102
 
103
    int available();
104
    int peek();
105
    int read();
106
    int read(uint8_t *data, size_t len);
107
};
108
 
109
#endif /* SYNCCLIENT_H_ */