EMANE  1.2.1
downstreampacket.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014,2016,2018 - Adjacent Link LLC, Bridgewater,
3  * New Jersey
4  * Copyright (c) 2008 - DRS CenGen, LLC, Columbia, Maryland
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of DRS CenGen, LLC nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "emane/downstreampacket.h"
36 #include "emane/utils/vectorio.h"
37 #include "emane/net.h"
38 #include "emane/event.h"
39 
40 #include <string>
41 #include <deque>
42 #include <list>
43 
44 class EMANE::DownstreamPacket::Implementation
45 {
46 public:
47  Implementation():
48  pShared_{std::make_shared<Shared>()}{}
49 
50  Implementation(const PacketInfo & info, const void * buf, size_t size):
51  pShared_{std::make_shared<Shared>()}
52  {
53  const unsigned char * c = static_cast<const unsigned char *>(buf);
54  pShared_->segment_ = PacketSegment(&c[0],&c[size]);
55  pShared_->info_ = info;
56  totalLengthBytes_ += size;
57  }
58 
59  void prepend(const void * buf, size_t size)
60  {
61  const unsigned char * c = static_cast<const unsigned char *>(buf);
62 
63  segments_.emplace_front(&c[0],&c[size]);
64 
65  totalLengthBytes_ += size;
66  }
67 
68  void prependLengthPrefixFraming(std::uint16_t u16Length)
69  {
70  std::uint16_t u16LengthNet{HTONS(u16Length)};
71 
72  auto c = reinterpret_cast<const std::uint8_t *>(&u16LengthNet);
73 
74  segments_.emplace_front(&c[0],&c[sizeof(u16Length)]);
75 
76  totalLengthBytes_ += sizeof(u16Length);
77  }
78 
79  void prependLengthPrefixFramingLong(std::uint32_t u32Length)
80  {
81  std::uint32_t u32LengthNet{HTONL(u32Length)};
82 
83  auto c = reinterpret_cast<const std::uint8_t *>(&u32LengthNet);
84 
85  segments_.emplace_front(&c[0],&c[sizeof(u32Length)]);
86 
87  totalLengthBytes_ += sizeof(u32Length);
88  }
89 
90  size_t length() const
91  {
92  return totalLengthBytes_;
93  }
94 
95  const PacketInfo & getPacketInfo() const
96  {
97  return pShared_->info_;
98  }
99 
101  {
102  Utils::VectorIO vectorIO{};
103 
104  vectorIO.reserve(segments_.size() + 1);
105 
106  for(const auto & segment : segments_)
107  {
108  vectorIO.push_back({reinterpret_cast<std::uint8_t *>(const_cast<char *>(segment.c_str())),
109  segment.size()});
110  }
111 
112  vectorIO.push_back({reinterpret_cast<std::uint8_t *>(const_cast<char *>(pShared_->segment_.c_str())),
113  pShared_->segment_.size()});
114 
115  return vectorIO;
116  }
117 
118  void attachEvent(NEMId nemId, const Event & event)
119  {
120  attachedEvents_.push_back(std::make_tuple(nemId,
121  event.getEventId(),
122  event.serialize()));
123  }
124 
126  {
127  return attachedEvents_;
128  }
129 
130 private:
131  using PacketSegment = std::string;
132  using Segments = std::deque<PacketSegment>;
133  using AttachedEvents = std::list<std::tuple<NEMId,EventId,std::string>>;
134 
135  class Shared
136  {
137  public:
138  PacketSegment segment_{};
139  PacketInfo info_{0,0,0,{}};
140  };
141 
142  Segments segments_{};
143  PacketSegment::size_type totalLengthBytes_{};
144  AttachedEvents attachedEvents_{};
145  std::shared_ptr<Shared> pShared_;
146 };
147 
149  const void * buf,
150  size_t size):
151  pImpl_{new Implementation{info,buf,size}}{}
152 
153 
155  pImpl_{new Implementation{*pkt.pImpl_}}{}
156 
158  pImpl_{std::move(pkt.pImpl_)}{}
159 
161 
163 {
164  pImpl_.reset(new Implementation{*pkt.pImpl_});
165  return *this;
166 }
167 
169 {
170  pImpl_ = std::move(pkt.pImpl_);
171  return *this;
172 }
173 
174 void EMANE::DownstreamPacket::prepend(const void * buf, size_t size)
175 {
176  pImpl_->prepend(buf,size);
177 }
178 
180 {
181  pImpl_->prependLengthPrefixFraming(u16Length);
182 }
183 
185 {
186  pImpl_->prependLengthPrefixFramingLong(u32Length);
187 }
188 
190 {
191  return pImpl_->getVectorIO();
192 }
193 
195 {
196  return pImpl_->length();
197 }
198 
200 {
201  return pImpl_->getPacketInfo();
202 }
203 
205 {
206  pImpl_->attachEvent(nemId,event);
207 }
208 
211 {
212  return pImpl_->getEventSerializations();
213 }
const PacketInfo & getPacketInfo() const
void attachEvent(NEMId nemId, const Event &event)
Event interface is the base for all events.
Definition: event.h:46
void prepend(const void *buf, size_t size)
std::vector< iovec > VectorIO
Definition: vectorio.h:43
Store source, destination, creation time and priority information for a packet.
Definition: packetinfo.h:50
DownstreamPacket & operator=(const DownstreamPacket &pkt)
Specialized packet the allows downstream processing to add layer specific headers as the packet trave...
std::list< std::tuple< EMANE::NEMId, EMANE::EventId, Serialization > > EventSerializations
std::uint16_t NEMId
Definition: types.h:52
constexpr std::uint16_t HTONS(std::uint16_t x)
Definition: net.h:125
DownstreamPacket(const PacketInfo &info, const void *buf, size_t size)
const EventSerializations & getEventSerializations() const
Utils::VectorIO getVectorIO() const
void prependLengthPrefixFraming(std::uint16_t u16Length)
constexpr std::uint32_t HTONL(std::uint32_t x)
Definition: net.h:103
void prependLengthPrefixFramingLong(std::uint32_t u32Length)