client-cpp  0.7.0
KaaTcpParser.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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>
24 #include <list>
25 
26 namespace kaa {
27 
28 typedef std::pair<KaaTcpMessageType, std::pair<boost::shared_array<char>, std::uint32_t>> MessageRecord;
29 typedef std::list<MessageRecord> MessageRecordList;
30 
31 enum class KaaTcpParserState : std::uint8_t
32 {
33  NONE = 0x00,
34  PROCESSING_LENGTH = 0x01,
35  PROCESSING_PAYLOAD = 0x02,
36 };
37 
38 class KaaTcpParser : boost::noncopyable
39 {
40 public:
42  state_(KaaTcpParserState::NONE), messageLength_(0), processedPayloadLength_(0)
43  , lenghtMultiplier_(1), messageType_(KaaTcpMessageType::MESSAGE_UNKNOWN) { }
45 
46  void parseBuffer(const char *buffer, std::uint32_t size);
47 
48  boost::shared_array<char> getCurrentPayload() const { return messagePayload_; }
49  std::uint32_t getCurrentPayloadLength() const { return messageLength_; }
50  KaaTcpMessageType getCurrentMessageType() const { return messageType_; }
51 
53 
54  void resetParser();
55 
56 private:
57  void processByte(char byte);
58  void retrieveMessageType(char byte);
59  void onMessageDone();
60 
61 private:
62 
63  KaaTcpParserState state_;
64  std::uint32_t messageLength_;
65  std::uint32_t processedPayloadLength_;
66  std::uint32_t lenghtMultiplier_;
67  KaaTcpMessageType messageType_;
68  boost::shared_array<char> messagePayload_;
69  MessageRecordList messages_;
70 };
71 
72 }
73 
74 
75 #endif /* KAATCPPARSER_HPP_ */
std::pair< KaaTcpMessageType, std::pair< boost::shared_array< char >, std::uint32_t > > MessageRecord
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