client-cpp  0.9.0
KaaTcpParser.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef KAATCPPARSER_HPP_
18 #define KAATCPPARSER_HPP_
19 
20 #include <cstdint>
21 #include <boost/noncopyable.hpp>
22 #include <boost/shared_array.hpp>
25 #include <list>
26 
27 namespace kaa {
28 
29 typedef std::pair<KaaTcpMessageType, std::pair<boost::shared_array<char>, std::uint32_t>> MessageRecord;
30 typedef std::list<MessageRecord> MessageRecordList;
31 
32 enum class KaaTcpParserState : std::uint8_t
33 {
34  NONE = 0x00,
35  PROCESSING_LENGTH = 0x01,
36  PROCESSING_PAYLOAD = 0x02,
37 };
38 
39 class KaaTcpParser : boost::noncopyable
40 {
41 public:
43  state_(KaaTcpParserState::NONE), messageLength_(0), processedPayloadLength_(0)
44  , lenghtMultiplier_(1), messageType_(KaaTcpMessageType::MESSAGE_UNKNOWN), context_(context) { }
46 
47  void parseBuffer(const char *buffer, std::uint32_t size);
48 
49  boost::shared_array<char> getCurrentPayload() const { return messagePayload_; }
50  std::uint32_t getCurrentPayloadLength() const { return messageLength_; }
51  KaaTcpMessageType getCurrentMessageType() const { return messageType_; }
52 
54 
55  void resetParser();
56 
57 private:
58  void processByte(char byte);
59  void retrieveMessageType(char byte);
60  void onMessageDone();
61 
62 private:
63 
64  KaaTcpParserState state_;
65  std::uint32_t messageLength_;
66  std::uint32_t processedPayloadLength_;
67  std::uint32_t lenghtMultiplier_;
68  KaaTcpMessageType messageType_;
69  boost::shared_array<char> messagePayload_;
70  MessageRecordList messages_;
71  IKaaClientContext &context_;
72 };
73 
74 }
75 
76 
77 #endif /* KAATCPPARSER_HPP_ */
std::pair< KaaTcpMessageType, std::pair< boost::shared_array< char >, std::uint32_t > > MessageRecord
KaaTcpParser(IKaaClientContext &context)
KaaTcpParserState
MessageRecordList releaseMessages()
boost::shared_array< char > getCurrentPayload() const
std::uint32_t getCurrentPayloadLength() const
std::list< MessageRecord > MessageRecordList
KaaTcpMessageType getCurrentMessageType() const
void parseBuffer(const char *buffer, std::uint32_t size)
KaaTcpMessageType