| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/tuple/tuple.hpp>
|
|---|
| 4 | #include <boost/noncopyable.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <nsca/nsca_packet.hpp>
|
|---|
| 7 | #include <cryptopp/cryptopp.hpp>
|
|---|
| 8 |
|
|---|
| 9 | #include "handler.hpp"
|
|---|
| 10 |
|
|---|
| 11 | namespace nsca {
|
|---|
| 12 | namespace server {
|
|---|
| 13 | class parser : public boost::noncopyable{
|
|---|
| 14 | unsigned int payload_length_;
|
|---|
| 15 | unsigned int packet_length_;
|
|---|
| 16 |
|
|---|
| 17 | std::string buffer_;
|
|---|
| 18 | boost::shared_ptr<nsca::server::handler> handler_;
|
|---|
| 19 | public:
|
|---|
| 20 | parser(unsigned int payload_length)
|
|---|
| 21 | : payload_length_(payload_length)
|
|---|
| 22 | , packet_length_(nsca::length::get_packet_length(payload_length))
|
|---|
| 23 | {}
|
|---|
| 24 |
|
|---|
| 25 | template <typename InputIterator>
|
|---|
| 26 | boost::tuple<bool, InputIterator> digest(InputIterator begin, InputIterator end) {
|
|---|
| 27 | int count = packet_length_ - buffer_.size();
|
|---|
| 28 | for (; count > 0&& begin != end; ++begin, --count)
|
|---|
| 29 | buffer_.push_back(*begin);
|
|---|
| 30 | return boost::make_tuple(buffer_.size() >= packet_length_, begin);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | void decrypt(nscp::encryption::engine &encryption) {
|
|---|
| 34 | encryption.decrypt_buffer(buffer_);
|
|---|
| 35 | }
|
|---|
| 36 | nsca::packet parse() {
|
|---|
| 37 | nsca::packet packet(payload_length_);
|
|---|
| 38 | packet.parse_data(buffer_.c_str(), buffer_.size());
|
|---|
| 39 | buffer_.clear();
|
|---|
| 40 | return packet;
|
|---|
| 41 | }
|
|---|
| 42 | std::string get_buffer() const {
|
|---|
| 43 | return buffer_;
|
|---|
| 44 | }
|
|---|
| 45 | std::string::size_type size() {
|
|---|
| 46 | return buffer_.size();
|
|---|
| 47 | }
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | }// namespace server
|
|---|
| 51 | } // namespace nsca
|
|---|