1#pragma once
2
3#include "TlsContext.h"
4
5class InetAddress;
6
7class TlsStream;
8typedef std::unique_ptr<TlsStream> TlsStreamPtr;
9
10// A blocking TLS stream
11class TlsStream : noncopyable
12{
13 public:
14  explicit TlsStream(TlsContext&& context)
15    : context_(std::move(context))  // must be established
16  {
17    LOG_INFO << context_.cipher();
18  }
19
20  ~TlsStream() = default;
21  TlsStream(TlsStream&&) = default;
22  // TlsStream& operator=(TlsStream&&) = default;
23
24  static TlsStreamPtr connect(TlsConfig* config, const char* hostport, const char* servername = nullptr);
25
26  // NOT thread safe
27  int receiveAll(void* buf, int len);  // read len bytes, unless error happens
28  int receiveSome(void* buf, int len); // read len or less bytes
29
30  int sendAll(const void* buf, int len);  // send len bytes, unless error happens
31  int sendSome(const void* buf, int len); // send len or less bytes
32
33 private:
34  TlsContext context_;
35};
36
37