1
#pragma once
2

            
3
#include "envoy/buffer/buffer.h"
4
#include "envoy/network/connection.h"
5

            
6
namespace Envoy {
7
namespace Tcp {
8

            
9
/*
10
 * AsyncTcpClientOptions for the creation of async tcp client;
11
 */
12
struct AsyncTcpClientOptions {
13
37
  AsyncTcpClientOptions(bool enable_half_close) : enable_half_close(enable_half_close) {};
14
  bool enable_half_close;
15
};
16

            
17
using AsyncTcpClientOptionsConstSharedPtr = std::shared_ptr<const AsyncTcpClientOptions>;
18

            
19
/*
20
 * AsyncTcpClientCallbacks for peer connection callbacks and data.
21
 */
22
class AsyncTcpClientCallbacks : public Network::ConnectionCallbacks {
23
public:
24
81
  ~AsyncTcpClientCallbacks() override = default;
25

            
26
  /*
27
   * Invoked when data is delivered from the peer connection.
28
   * @param data supplies data from the peer.
29
   * @param end_stream whether the data is the last data frame.
30
   */
31
  virtual void onData(Buffer::Instance& data, bool end_stream) PURE;
32
};
33

            
34
/**
35
 * An async TCP client with connection to peers.
36
 */
37
class AsyncTcpClient {
38
public:
39
82
  virtual ~AsyncTcpClient() = default;
40

            
41
  /**
42
   * Connect to a remote host. Errors or connection events are reported via the
43
   * event callback registered via setAsyncTcpClientCallbacks(). If the callbacks
44
   * needs to be changed before reconnecting, it is required to set the callbacks
45
   * again, before calling to connect() to attempting to reconnect.
46
   * @returns true if a new client has created and the connection is in progress.
47
   * @returns false if an underlying client exists and is connected or connecting.
48
   */
49
  virtual bool connect() PURE;
50

            
51
  /**
52
   * Close the client. It closes the connection based on close type.
53
   * The underlying connection will be defer deleted when a Close event is received.
54
   * Abrt/NoFlush will abortively closes the connection discarding any unsent data.
55
   * @param type the connection close type.
56
   */
57
  virtual void close(Network::ConnectionCloseType type) PURE;
58

            
59
  /**
60
   * @return the detected close type from socket.
61
   */
62
  virtual StreamInfo::DetectedCloseType detectedCloseType() const PURE;
63

            
64
  /**
65
   * Write data through the client.
66
   * @param data the bufferred data.
67
   * @param end_stream indicates if this is the end of the stream, half close
68
   * should be enabled by setting end_stream to true.
69
   */
70
  virtual void write(Buffer::Instance& data, bool end_stream) PURE;
71

            
72
  /**
73
   * Disable socket reads on the connection, applying external back pressure.
74
   * @param disable supplies TRUE is reads should be disabled, FALSE if they
75
   * should be enabled.
76
   */
77
  virtual void readDisable(bool disable) PURE;
78

            
79
  /**
80
   * Add AsyncTcpClientCallbacks to the client
81
   * @param callbacks the client callbacks.
82
   */
83
  virtual void setAsyncTcpClientCallbacks(AsyncTcpClientCallbacks& callbacks) PURE;
84

            
85
  /**
86
   * @return Event::Dispatcher& the dispatcher backing this client.
87
   */
88
  virtual Event::Dispatcher& dispatcher() PURE;
89

            
90
  /**
91
   * @return if the client connects to a peer host.
92
   */
93
  virtual bool connected() PURE;
94

            
95
  /**
96
   * @return the streamInfo of the current connection if there is any.
97
   */
98
  virtual OptRef<StreamInfo::StreamInfo> getStreamInfo() PURE;
99
};
100

            
101
using AsyncTcpClientPtr = std::unique_ptr<AsyncTcpClient>;
102

            
103
} // namespace Tcp
104
} // namespace Envoy