nlib
TcpOutputStream.h
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_TCPOUTPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_TCPOUTPUTSTREAM_H_
5 
6 #include "nn/nlib/Platform.h"
7 #ifdef NLIB_SOCKET_ENABLED
8 
9 #include "nn/nlib/Config.h"
10 #include "nn/nlib/OutputStream.h"
11 
12 NLIB_NAMESPACE_BEGIN
13 
14 struct TcpOutputStreamSettings {
15  void* buf;
16  size_t buffer_size;
17  TcpOutputStreamSettings() : buf(NULL), buffer_size(8192) {}
18 };
19 
20 class NLIB_VIS_PUBLIC TcpOutputStream NLIB_FINAL : public OutputStream {
21  public:
22  TcpOutputStream() NLIB_NOEXCEPT : socket_(NLIB_SOCKET_INVALID), buf_(NULL), buf_size_(0),
23  buffer_holder_(NULL) {}
24  virtual ~TcpOutputStream() NLIB_NOEXCEPT NLIB_OVERRIDE;
25  errno_t Init(nlib_sock sockfd) NLIB_NOEXCEPT {
26  TcpOutputStreamSettings settings;
27  return this->Init(sockfd, settings);
28  }
29  errno_t Init(nlib_sock sockfd, const TcpOutputStreamSettings& settings) NLIB_NOEXCEPT;
30 
31  private:
32  virtual bool PushBuffer_(const void* p, size_t nbytes,
33  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE;
34  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
35 
36  private:
37  nlib_sock socket_;
38  uint8_t* buf_;
39  size_t buf_size_;
40  uint8_t* buffer_holder_;
41 };
42 
43 NLIB_NAMESPACE_END
44 
45 #endif
46 #endif // INCLUDE_NN_NLIB_TCPOUTPUTSTREAM_H_
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:210
C-based declaration of the basic API.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
Defines the base class for output streams.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24