18d51ab70SShuo Chen#pragma once
28d51ab70SShuo Chen
38d51ab70SShuo Chen#include "TlsContext.h"
48d51ab70SShuo Chen
58d51ab70SShuo Chenclass InetAddress;
68d51ab70SShuo Chen
78d51ab70SShuo Chenclass TlsStream;
88d51ab70SShuo Chentypedef std::unique_ptr<TlsStream> TlsStreamPtr;
98d51ab70SShuo Chen
108d51ab70SShuo Chen// A blocking TLS stream
118d51ab70SShuo Chenclass TlsStream : noncopyable
128d51ab70SShuo Chen{
138d51ab70SShuo Chen public:
148d51ab70SShuo Chen  explicit TlsStream(TlsContext&& context)
158d51ab70SShuo Chen    : context_(std::move(context))  // must be established
168d51ab70SShuo Chen  {
177db0aea6SShuo Chen    LOG_INFO << context_.cipher();
188d51ab70SShuo Chen  }
198d51ab70SShuo Chen
208d51ab70SShuo Chen  ~TlsStream() = default;
218d51ab70SShuo Chen  TlsStream(TlsStream&&) = default;
228d51ab70SShuo Chen  // TlsStream& operator=(TlsStream&&) = default;
238d51ab70SShuo Chen
248d51ab70SShuo Chen  static TlsStreamPtr connect(TlsConfig* config, const char* hostport, const char* servername = nullptr);
258d51ab70SShuo Chen
268d51ab70SShuo Chen  // NOT thread safe
278d51ab70SShuo Chen  int receiveAll(void* buf, int len);  // read len bytes, unless error happens
288d51ab70SShuo Chen  int receiveSome(void* buf, int len); // read len or less bytes
298d51ab70SShuo Chen
308d51ab70SShuo Chen  int sendAll(const void* buf, int len);  // send len bytes, unless error happens
318d51ab70SShuo Chen  int sendSome(const void* buf, int len); // send len or less bytes
328d51ab70SShuo Chen
338d51ab70SShuo Chen private:
348d51ab70SShuo Chen  TlsContext context_;
358d51ab70SShuo Chen};
368d51ab70SShuo Chen
37