|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2017 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#include "transport.hpp" |
| 19 | +#include "common.hpp" |
| 20 | + |
| 21 | +namespace uplink { |
| 22 | + |
| 23 | +Header Header::parse(const char* data) |
| 24 | +{ |
| 25 | + Expects(data != nullptr); |
| 26 | + return Header{ |
| 27 | + static_cast<Transport_code>(data[0]), |
| 28 | + *(reinterpret_cast<const uint32_t*>(&data[1])) |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +Transport_parser::Transport_parser(Transport_complete cb) |
| 33 | + : on_complete{std::move(cb)}, on_header{nullptr}, transport_{nullptr} |
| 34 | +{ |
| 35 | + Expects(on_complete != nullptr); |
| 36 | +} |
| 37 | + |
| 38 | +void Transport_parser::parse(const char* data, size_t len) |
| 39 | +{ |
| 40 | + if(transport_ != nullptr) |
| 41 | + { |
| 42 | + transport_->load_cargo(data, len); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + transport_ = std::make_unique<Transport>(Header::parse(data)); |
| 47 | + |
| 48 | + if(on_header) |
| 49 | + on_header(transport_->header()); |
| 50 | + |
| 51 | + len -= sizeof(Header); |
| 52 | + |
| 53 | + transport_->load_cargo(data + sizeof(Header), len); |
| 54 | + } |
| 55 | + |
| 56 | + if(transport_->is_complete()) |
| 57 | + on_complete(std::move(transport_)); |
| 58 | +} |
| 59 | + |
| 60 | +} |
| 61 | + |
0 commit comments