nlib
TcpInputStream.h
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_TCPINPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_TCPINPUTSTREAM_H_
5 
6 #include "nn/nlib/Platform.h"
7 #ifdef NLIB_SOCKET_ENABLED
8 
9 #include "nn/nlib/Config.h"
10 #include "nn/nlib/InputStream.h"
11 #include "nn/nlib/UniquePtr.h"
12 
13 NLIB_NAMESPACE_BEGIN
14 
15 struct TcpInputStreamSettings {
16  void* buf;
17  size_t buffer_size;
18  TcpInputStreamSettings() : buf(NULL), buffer_size(8192) {}
19 };
20 
21 #if defined(NLIB_WINDLL) && defined(NLIB_CXX11_UNIQUEPTR) && defined(NLIB_CXX11_TEMPLATE_ALIAS)
22 # ifdef nx_misc_EXPORTS
23 template class __declspec(dllexport) std::unique_ptr<uint8_t[]>;
24 # else
25 extern template class __declspec(dllimport) std::unique_ptr<uint8_t[]>;
26 # endif
27 #endif
28 
29 #ifdef _MSC_VER
30 class TcpInputStream NLIB_FINAL : public InputStream {
31 #else
32 class NLIB_VIS_PUBLIC TcpInputStream NLIB_FINAL : public InputStream {
33 #endif
34 
35  public:
36  TcpInputStream() NLIB_NOEXCEPT : m_Socket(NLIB_SOCKET_INVALID), m_Buf(NULL), m_BufSize(0) {}
37  NLIB_VIS_PUBLIC virtual ~TcpInputStream() NLIB_NOEXCEPT NLIB_OVERRIDE;
38  errno_t Init(nlib_sock sockfd) NLIB_NOEXCEPT {
39  TcpInputStreamSettings settings;
40  return this->Init(sockfd, settings);
41  }
42  NLIB_VIS_PUBLIC errno_t Init(nlib_sock sockfd,
43  const TcpInputStreamSettings& settings) NLIB_NOEXCEPT;
44 
45  private:
46  NLIB_VIS_PUBLIC virtual size_t FillBuffer_(void* p,
47  size_t nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
48  NLIB_VIS_PUBLIC virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
49  NLIB_VIS_PUBLIC virtual void* GetWorkBuffer_(size_t* nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
50 
51  private:
52  nlib_sock m_Socket;
53  uint8_t* m_Buf;
54  size_t m_BufSize;
55  UniquePtr<uint8_t[]> m_BufferHolder;
56  NLIB_DISALLOW_COPY_AND_ASSIGN(TcpInputStream);
57 };
58 
59 NLIB_NAMESPACE_END
60 
61 #endif
62 
63 #endif // INCLUDE_NN_NLIB_TCPINPUTSTREAM_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_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
Defines the base class for input streams.
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
STL namespace.
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
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24