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 #include "nn/nlib/UniquePtr.h"
12 
13 NLIB_NAMESPACE_BEGIN
14 
15 struct TcpOutputStreamSettings {
16  void* buf;
17  size_t buffer_size;
18  TcpOutputStreamSettings() : buf(NULL), buffer_size(8192) {}
19 };
20 
21 #ifdef _MSC_VER
22 class TcpOutputStream NLIB_FINAL : public OutputStream {
23 #else
24 class NLIB_VIS_PUBLIC TcpOutputStream NLIB_FINAL : public OutputStream {
25 #endif
26 
27  public:
28  TcpOutputStream() NLIB_NOEXCEPT : m_Socket(NLIB_SOCKET_INVALID), m_Buf(NULL), m_BufSize(0) {}
29  NLIB_VIS_PUBLIC virtual ~TcpOutputStream() NLIB_NOEXCEPT NLIB_OVERRIDE;
30  errno_t Init(nlib_sock sockfd) NLIB_NOEXCEPT {
31  TcpOutputStreamSettings settings;
32  return this->Init(sockfd, settings);
33  }
34  NLIB_VIS_PUBLIC errno_t Init(nlib_sock sockfd,
35  const TcpOutputStreamSettings& settings) NLIB_NOEXCEPT;
36 
37  private:
38  NLIB_VIS_PUBLIC virtual bool PushBuffer_(const void* p, size_t nBytes,
39  bool doFlush) NLIB_NOEXCEPT NLIB_OVERRIDE;
40  NLIB_VIS_PUBLIC virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
41 
42  private:
43  nlib_sock m_Socket;
44  uint8_t* m_Buf;
45  size_t m_BufSize;
46  UniquePtr<uint8_t[]> m_BufferHolder;
47 };
48 
49 NLIB_NAMESPACE_END
50 
51 #endif
52 #endif // INCLUDE_NN_NLIB_TCPOUTPUTSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Defines that class that is corresponding to std::unique_ptr.
C-based declaration of the basic API.
A file that contains the configuration information for each development environment.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
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