Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNetStream.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cNetStream_H
7 #define _INC_cNetStream_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cNetSocket.h"
14 
15 namespace GrayLib
16 {
18 
20  {
26 
27  typedef cNetStream THIS_t;
28  typedef cStream SUPER_t;
29 
30  public:
32  bool m_bAsync;
37 
38  private:
39  cThreadLockFast m_Lock;
40 
41  protected:
42 #ifdef _WIN32
43  virtual void OnClose() // similar to MFC
44  {
47  Close();
48  }
49 #endif
50 
51  public:
52  cNetStream(SOCKET hSocket = INVALID_SOCKET, bool bAsync = false, size_t nRxBufferLimit = (cStream::k_FILE_BLOCK_SIZE / 2));
53  virtual ~cNetStream(void);
54 
55  bool isConnected() const
56  {
58  return m_Socket.isConnected();
59  }
60  bool isConnecting() const noexcept
61  {
63  return m_Socket.isConnecting();
64  }
65  bool isValidSocket() const noexcept
66  {
68  return m_Socket.isValidSocket();
69  }
70 
71  HASHCODE_t get_HashCode() const noexcept
72  {
74  DEBUG_CHECK(isValidSocket());
75  return m_Socket.get_HashCode();
76  }
77 
78  bool isInputDesired() const
79  {
81  return(m_binp.get_GrowSizeChunk() > 0);
82  }
83  bool isInputEmpty() const
84  {
86  if (!m_bAsync || m_bInpThrottled)
87  {
88  if (m_Socket.get_ReadReady() != 0)
89  return false;
90  }
91  return m_binp.isEmptyQ();
92  }
93  bool isOutputEmpty() const
94  {
96  return m_bout.isEmptyQ();
97  }
99  {
100  if (!m_bAsync || m_bInpThrottled)
101  {
102  return m_Socket.get_ReadReady() + m_binp.get_ReadQty();
103  }
104  return m_binp.get_ReadQty();
105  }
107  {
108  return m_bout.get_ReadQty();
109  }
110 
112  virtual HRESULT onEventWrite();
113  virtual HRESULT onEventRead();
114 
115 #ifdef _WIN32
116  void OnEvent(WORD wEvent, LSTATUS wError);
117 #endif
118  HRESULT UpdateQWrite();
119  HRESULT UpdateQRead();
120 
122  {
123  return &m_bout;
124  }
126  {
127  return &m_binp;
128  }
129 
130  // cStream
131  virtual HRESULT WriteX(const void* pData, size_t nDataSize) override;
132  virtual HRESULT ReadX(void* pData, size_t nDataSize) override;
133  virtual size_t SetSeekSizeMin(size_t nSizeMin = k_FILE_BLOCK_SIZE) override;
134  virtual STREAM_SEEKRET_t Seek(STREAM_OFFSET_t iOffset, SEEK_ORIGIN_TYPE eSeekOrigin) override;
135  virtual void Close();
136 
138  {
140  m_StatInp.m_tLast.InitTimeNow();
141  m_Socket.SetStateEstablished();
142  }
143 
145  };
146 };
147 #endif // _INC_cNetStream_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
bool isValidSocket() const noexcept
Definition: cNetSocket.h:109
Definition: cNetSocket.h:185
bool isConnecting() const noexcept
Definition: cNetSocket.h:302
HASHCODE_t get_HashCode() const noexcept
Definition: cNetSocket.h:345
bool isConnected() const noexcept
Definition: cNetSocket.h:311
SOCKETSTATE_TYPE m_eState
What is the state of the socket. e.g. SOCKETSTATE_Disconnected.
Definition: cNetSocket.h:195
void SetStateEstablished() noexcept
Definition: cNetSocket.h:331
int get_ReadReady() const
Definition: cNetSocket.cpp:716
Definition: cNetStream.h:20
bool isInputEmpty() const
Definition: cNetStream.h:83
bool isInputDesired() const
Definition: cNetStream.h:78
bool isConnecting() const noexcept
Definition: cNetStream.h:60
ITERATE_t get_InputQty() const
Definition: cNetStream.h:98
bool isValidSocket() const noexcept
Definition: cNetStream.h:65
bool m_bInpThrottled
The m_bAsync input was throttled. we cant rely on m_bAsync OnEvent to notify us again....
Definition: cNetStream.h:33
HASHCODE_t get_HashCode() const noexcept
Definition: cNetStream.h:71
UNITTEST_FRIEND(cNetStream)
ITERATE_t get_OutputQty() const
Definition: cNetStream.h:106
bool isConnected() const
Definition: cNetStream.h:55
cStreamQueue m_binp
raw input buffer from m_Socket. can read from this externally if m_bAsync.
Definition: cNetStream.h:35
cStreamQueue m_bout
raw output buffer to m_Socket. can write to this externally if m_bAsync.
Definition: cNetStream.h:34
cStreamOutput * get_StreamOut()
Definition: cNetStream.h:121
cStreamInput * get_StreamInp()
Definition: cNetStream.h:125
cNetSocket m_Socket
the raw TCP SOCKET we are buffering to/from.
Definition: cNetStream.h:31
bool isOutputEmpty() const
Definition: cNetStream.h:93
HRESULT m_hResultRx
The status of the connect or disconnect. S_OK = connected. else how to it close ? don't return this u...
Definition: cNetStream.h:36
bool m_bAsync
Will this socket be using OnEvent FD_READ FD_WRITE events ?
Definition: cNetStream.h:32
void SetStateEstablished()
Definition: cNetStream.h:137
ITERATE_t get_ReadQty() const
Definition: cQueue.h:60
bool isEmptyQ() const noexcept
Definition: cQueue.h:41
ITERATE_t get_GrowSizeChunk() const noexcept
Definition: cQueue.h:640
static const size_t k_FILE_BLOCK_SIZE
default arbitrary transfer block size. more than this is NOT more efficient.
Definition: cStream.h:89
Definition: cStream.h:306
Definition: cStream.h:126
Definition: cStreamQueue.h:19
Definition: cStream.h:66
Definition: cStream.h:456
Definition: cThreadLock.h:205
Definition: cMesh.h:22
@ SOCKETSTATE_Disconnected
error indicates a remote disconnect. dead socket. not functional socket.
Definition: cNetSocket.h:49
UNITTEST2_PREDEF(cQuadtree)
LONG_PTR STREAM_OFFSET_t
Might be 64 or 32 bit. TODO SET USE_FILE_POS64.
Definition: cOSHandle.h:52
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
LONG_PTR STREAM_SEEKRET_t
return from Seek()
Definition: cOSHandle.h:53
int ITERATE_t
like size_t but signed
Definition: Index.h:28
UINT_PTR HASHCODE_t
could hold a pointer converted to a number? maybe 64 or 32 bit ? same as size_t.
Definition: GrayCore.h:116
SEEK_ORIGIN_TYPE
Definition: cOSHandle.h:34