Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cProtocol.h
Go to the documentation of this file.
1 //
4 //
5 #ifndef _INC_cProtocol_H
6 #define _INC_cProtocol_H
7 #ifndef NO_PRAGMA_ONCE
8 #pragma once
9 #endif
10 
11 #include "../GrayLibBase.h"
18 
19 namespace GrayLib
20 {
22 
23  class GRAYLIB_LINK cProtocolStream : public IUnkObject
24  {
32 
33  public:
35  IUnkObject* const m_pServerConnection;
37 
38  protected:
41 
43 
44  public:
45  cProtocolStream(IProtocolFactory* pProtocolFactory, IUnkObject* pServerConnection, cStreamInput* pStreamInp, cStreamOutput* pStreamOut);
46  virtual ~cProtocolStream();
47 
48  HASHCODE_t get_HashCode() const noexcept
49  {
51  return m_nHashCode;
52  }
53  STDMETHOD_(HASHCODE_t, get_HashCodeX)() const noexcept override
54  {
57  return get_HashCode();
58  }
59  STDMETHOD_(cString, get_Name)() const override
60  {
63  return m_sName;
64  }
65  STDMETHOD_(cStringA, get_SymName)() const override
66  {
68  return StrArg<char>(get_HashCode(), (RADIX_t)0x10);
69  }
70 
71  bool isConnected() const
72  {
73  return(m_pStreamOut != nullptr);
74  }
76  {
77  return m_pStreamInp;
78  }
80  {
81  return m_pStreamOut;
82  }
83  virtual size_t get_ProtocolMaxMessageSize() const;
84 
85  virtual HRESULT DisposeThis()
86  {
89  if (m_pStreamOut != nullptr)
90  {
91  m_pStreamInp = nullptr;
92  m_pStreamOut = nullptr;
93  }
94  ASSERT(!isConnected());
95  return S_OK;
96  }
98  {
100  if (m_pStreamOut == nullptr)
101  {
102  // The protocol terminated itself. isConnected()
103  return HRESULT_WIN32_C(WSAECONNABORTED);
104  }
105  return S_OK;
106  }
107 
108  virtual HRESULT ProcessStreamPacket() // = 0;
109  {
116  ASSERT(m_pStreamInp != nullptr);
117  ASSERT(false); // should not get here.
118  return HRESULT_WIN32_C(ERROR_WRITE_FAULT);
119  }
120  };
121 
123 
125  {
130 
131  public:
133  {}
134  virtual const char* get_ProtocolName() const = 0;
135  virtual size_t get_ProtocolMaxMessageSize() const
136  {
140  return 8 * 1024;
141  }
142  virtual HRESULT TestProtocol(const BYTE* pszPrefixData, size_t iPrefixLen) const = 0;
143  virtual cProtocolStreamPtr CreateProtocolStream(IUnkObject* pServerConnection, cStreamInput* pStreamInp, cStreamOutput* pStreamOut) = 0;
144  };
145 
147  {
150  protected:
152  public:
153  bool AddProtocol(IProtocolFactory* pProtocolFactory);
154  HRESULT TestProtocol(const BYTE* pPrefixData, size_t iPrefixLen, OUT IProtocolFactory*& rpProtocolFactory) const;
155  };
156 };
157 
158 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define HRESULT_WIN32_C(x)
a constant LSTATUS/error_status_t with no check, unlike HRESULT_FROM_WIN32()
Definition: HResult.h:79
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cProtocol.h:147
cArrayPtr< IProtocolFactory > m_aProtocols
array of possible protocols we support.
Definition: cProtocol.h:151
Definition: cProtocol.h:24
cStreamInput * m_pStreamInp
for use with ProcessStreamPacket()
Definition: cProtocol.h:39
cStreamOutput * m_pStreamOut
TX responses back to the remote client go here. (e.g. cNetServerConnection->cNetStream)
Definition: cProtocol.h:40
IUnkObject *const m_pServerConnection
the cNetServerConnection or other top level parent for this protocol.
Definition: cProtocol.h:35
cStreamInput * get_StreamInp() const
Definition: cProtocol.h:75
virtual HRESULT ProcessStreamPacket()
Definition: cProtocol.h:108
virtual HRESULT DisposeThis()
Definition: cProtocol.h:85
HASHCODE_t get_HashCode() const noexcept
Definition: cProtocol.h:48
cString m_sName
a user readable name. Usually the IP address or reverse DNS lookup. can be upgraded as lookup or logi...
Definition: cProtocol.h:42
STDMETHOD_(cString, get_Name)() const override
Definition: cProtocol.h:59
virtual HRESULT OnTickStream()
Definition: cProtocol.h:97
IProtocolFactory *const m_pProtocolFactory
What IProtocolFactory produced this?
Definition: cProtocol.h:34
bool isConnected() const
Definition: cProtocol.h:71
STDMETHOD_(HASHCODE_t, get_HashCodeX)() const noexcept override
Definition: cProtocol.h:53
HASHCODE_t const m_nHashCode
the m_pClient->get_HashCode() id for this client connection/stream.
Definition: cProtocol.h:36
cStreamOutput * get_StreamOut() const
Definition: cProtocol.h:79
STDMETHOD_(cStringA, get_SymName)() const override
Definition: cProtocol.h:65
Definition: cArray.h:864
Definition: cStream.h:306
Definition: cStream.h:126
Definition: cMesh.h:22
cIUnkPtr< cProtocolStream > cProtocolStreamPtr
Definition: cProtocol.h:122
WORD RADIX_t
Base for convert of numbers to strings. e.g. 10 base vs 16 base hex numbers.
Definition: StrChar.h:27
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
const char *__stdcall StrArg< char >(const char *pszStr) noexcept
Definition: StrArg.h:63
Definition: cProtocol.h:125
virtual cProtocolStreamPtr CreateProtocolStream(IUnkObject *pServerConnection, cStreamInput *pStreamInp, cStreamOutput *pStreamOut)=0
virtual ~IProtocolFactory()
Definition: cProtocol.h:132
virtual size_t get_ProtocolMaxMessageSize() const
Definition: cProtocol.h:135
virtual HRESULT TestProtocol(const BYTE *pszPrefixData, size_t iPrefixLen) const =0
virtual const char * get_ProtocolName() const =0