| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: Protocol.h |
|---|
| 5 | // Purpose: Generic protocol support |
|---|
| 6 | // Created: 2003/08/19 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef PROTOCOL__H |
|---|
| 11 | #define PROTOCOL__H |
|---|
| 12 | |
|---|
| 13 | #include <sys/types.h> |
|---|
| 14 | |
|---|
| 15 | #include <memory> |
|---|
| 16 | #include <vector> |
|---|
| 17 | #include <string> |
|---|
| 18 | |
|---|
| 19 | #include "Message.h" |
|---|
| 20 | |
|---|
| 21 | class IOStream; |
|---|
| 22 | |
|---|
| 23 | // default timeout is 15 minutes |
|---|
| 24 | #define PROTOCOL_DEFAULT_TIMEOUT (15*60*1000) |
|---|
| 25 | // 16 default maximum object size -- should be enough |
|---|
| 26 | #define PROTOCOL_DEFAULT_MAXOBJSIZE (16*1024) |
|---|
| 27 | |
|---|
| 28 | // -------------------------------------------------------------------------- |
|---|
| 29 | // |
|---|
| 30 | // Class |
|---|
| 31 | // Name: Protocol |
|---|
| 32 | // Purpose: Generic command / response protocol support |
|---|
| 33 | // Created: 2003/08/19 |
|---|
| 34 | // |
|---|
| 35 | // -------------------------------------------------------------------------- |
|---|
| 36 | class Protocol |
|---|
| 37 | { |
|---|
| 38 | public: |
|---|
| 39 | Protocol(IOStream &rStream); |
|---|
| 40 | virtual ~Protocol(); |
|---|
| 41 | |
|---|
| 42 | private: |
|---|
| 43 | Protocol(const Protocol &rToCopy); |
|---|
| 44 | |
|---|
| 45 | protected: |
|---|
| 46 | // Unsafe to make public, as they may allow sending objects |
|---|
| 47 | // from a different protocol. The derived class prevents this. |
|---|
| 48 | std::auto_ptr<Message> ReceiveInternal(); |
|---|
| 49 | void SendInternal(const Message &rObject); |
|---|
| 50 | |
|---|
| 51 | public: |
|---|
| 52 | void Handshake(); |
|---|
| 53 | std::auto_ptr<IOStream> ReceiveStream(); |
|---|
| 54 | void SendStream(IOStream &rStream); |
|---|
| 55 | |
|---|
| 56 | enum |
|---|
| 57 | { |
|---|
| 58 | NoError = -1, |
|---|
| 59 | UnknownError = 0 |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | // -------------------------------------------------------------------------- |
|---|
| 63 | // |
|---|
| 64 | // Function |
|---|
| 65 | // Name: Protocol::SetTimeout(int) |
|---|
| 66 | // Purpose: Sets the timeout for sending and reciving |
|---|
| 67 | // Created: 2003/08/19 |
|---|
| 68 | // |
|---|
| 69 | // -------------------------------------------------------------------------- |
|---|
| 70 | void SetTimeout(int NewTimeout) {mTimeout = NewTimeout;} |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | // -------------------------------------------------------------------------- |
|---|
| 74 | // |
|---|
| 75 | // Function |
|---|
| 76 | // Name: Protocol::GetTimeout() |
|---|
| 77 | // Purpose: Get current timeout for sending and receiving |
|---|
| 78 | // Created: 2003/09/06 |
|---|
| 79 | // |
|---|
| 80 | // -------------------------------------------------------------------------- |
|---|
| 81 | int GetTimeout() {return mTimeout;} |
|---|
| 82 | |
|---|
| 83 | // -------------------------------------------------------------------------- |
|---|
| 84 | // |
|---|
| 85 | // Function |
|---|
| 86 | // Name: Protocol::SetMaxObjectSize(int) |
|---|
| 87 | // Purpose: Sets the maximum size of an object which will be accepted |
|---|
| 88 | // Created: 2003/08/19 |
|---|
| 89 | // |
|---|
| 90 | // -------------------------------------------------------------------------- |
|---|
| 91 | void SetMaxObjectSize(unsigned int NewMaxObjSize) {mMaxObjectSize = NewMaxObjSize;} |
|---|
| 92 | |
|---|
| 93 | // For Message derived classes |
|---|
| 94 | void Read(void *Buffer, int Size); |
|---|
| 95 | void Read(std::string &rOut, int Size); |
|---|
| 96 | void Read(int64_t &rOut); |
|---|
| 97 | void Read(int32_t &rOut); |
|---|
| 98 | void Read(int16_t &rOut); |
|---|
| 99 | void Read(int8_t &rOut); |
|---|
| 100 | void Read(bool &rOut) {int8_t read; Read(read); rOut = (read == true);} |
|---|
| 101 | void Read(std::string &rOut); |
|---|
| 102 | template<typename type> |
|---|
| 103 | void Read(type &rOut) |
|---|
| 104 | { |
|---|
| 105 | rOut.ReadFromProtocol(*this); |
|---|
| 106 | } |
|---|
| 107 | // -------------------------------------------------------------------------- |
|---|
| 108 | // |
|---|
| 109 | // Function |
|---|
| 110 | // Name: Protocol::ReadVector(std::vector<> &) |
|---|
| 111 | // Purpose: Reads a vector/list of items from the stream |
|---|
| 112 | // Created: 2003/08/19 |
|---|
| 113 | // |
|---|
| 114 | // -------------------------------------------------------------------------- |
|---|
| 115 | template<typename type> |
|---|
| 116 | void ReadVector(std::vector<type> &rOut) |
|---|
| 117 | { |
|---|
| 118 | rOut.clear(); |
|---|
| 119 | int16_t num = 0; |
|---|
| 120 | Read(num); |
|---|
| 121 | for(int16_t n = 0; n < num; ++n) |
|---|
| 122 | { |
|---|
| 123 | type v; |
|---|
| 124 | Read(v); |
|---|
| 125 | rOut.push_back(v); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void Write(const void *Buffer, int Size); |
|---|
| 130 | void Write(int64_t Value); |
|---|
| 131 | void Write(int32_t Value); |
|---|
| 132 | void Write(int16_t Value); |
|---|
| 133 | void Write(int8_t Value); |
|---|
| 134 | void Write(bool Value) {int8_t write = Value; Write(write);} |
|---|
| 135 | void Write(const std::string &rValue); |
|---|
| 136 | template<typename type> |
|---|
| 137 | void Write(const type &rValue) |
|---|
| 138 | { |
|---|
| 139 | rValue.WriteToProtocol(*this); |
|---|
| 140 | } |
|---|
| 141 | template<typename type> |
|---|
| 142 | // -------------------------------------------------------------------------- |
|---|
| 143 | // |
|---|
| 144 | // Function |
|---|
| 145 | // Name: Protocol::WriteVector(const std::vector<> &) |
|---|
| 146 | // Purpose: Writes a vector/list of items from the stream |
|---|
| 147 | // Created: 2003/08/19 |
|---|
| 148 | // |
|---|
| 149 | // -------------------------------------------------------------------------- |
|---|
| 150 | void WriteVector(const std::vector<type> &rValue) |
|---|
| 151 | { |
|---|
| 152 | int16_t num = rValue.size(); |
|---|
| 153 | Write(num); |
|---|
| 154 | for(int16_t n = 0; n < num; ++n) |
|---|
| 155 | { |
|---|
| 156 | Write(rValue[n]); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | public: |
|---|
| 161 | static const uint16_t sProtocolStreamHeaderLengths[256]; |
|---|
| 162 | enum |
|---|
| 163 | { |
|---|
| 164 | ProtocolStreamHeader_EndOfStream = 0, |
|---|
| 165 | ProtocolStreamHeader_MaxEncodedSizeValue = 252, |
|---|
| 166 | ProtocolStreamHeader_SizeIs64k = 253, |
|---|
| 167 | ProtocolStreamHeader_Reserved1 = 254, |
|---|
| 168 | ProtocolStreamHeader_Reserved2 = 255 |
|---|
| 169 | }; |
|---|
| 170 | enum |
|---|
| 171 | { |
|---|
| 172 | ProtocolStream_SizeUncertain = 0xffffffff |
|---|
| 173 | }; |
|---|
| 174 | bool GetLogToSysLog() { return mLogToSysLog; } |
|---|
| 175 | FILE *GetLogToFile() { return mLogToFile; } |
|---|
| 176 | void SetLogToSysLog(bool Log = false) {mLogToSysLog = Log;} |
|---|
| 177 | void SetLogToFile(FILE *File = 0) {mLogToFile = File;} |
|---|
| 178 | |
|---|
| 179 | protected: |
|---|
| 180 | virtual std::auto_ptr<Message> MakeMessage(int ObjType) = 0; |
|---|
| 181 | virtual const char *GetProtocolIdentString() = 0; |
|---|
| 182 | |
|---|
| 183 | void CheckAndReadHdr(void *hdr); // don't use type here to avoid dependency |
|---|
| 184 | |
|---|
| 185 | // Will be used for logging |
|---|
| 186 | virtual void InformStreamReceiving(u_int32_t Size); |
|---|
| 187 | virtual void InformStreamSending(u_int32_t Size); |
|---|
| 188 | |
|---|
| 189 | private: |
|---|
| 190 | void EnsureBufferAllocated(int Size); |
|---|
| 191 | int SendStreamSendBlock(uint8_t *Block, int BytesInBlock); |
|---|
| 192 | |
|---|
| 193 | IOStream &mrStream; |
|---|
| 194 | bool mHandshakeDone; |
|---|
| 195 | unsigned int mMaxObjectSize; |
|---|
| 196 | int mTimeout; |
|---|
| 197 | char *mpBuffer; |
|---|
| 198 | int mBufferSize; |
|---|
| 199 | int mReadOffset; |
|---|
| 200 | int mWriteOffset; |
|---|
| 201 | int mValidDataSize; |
|---|
| 202 | bool mLogToSysLog; |
|---|
| 203 | FILE *mLogToFile; |
|---|
| 204 | }; |
|---|
| 205 | |
|---|
| 206 | class ProtocolContext |
|---|
| 207 | { |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | #endif // PROTOCOL__H |
|---|
| 211 | |
|---|