Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2005,2006 INRIA | 3 * Copyright (c) 2005,2006 INRIA |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 5 * This program is free software; you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 2 as | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with this program; if not, write to the Free Software | 15 * along with this program; if not, write to the Free Software |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 * | 17 * |
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> | 18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
19 */ | 19 */ |
20 #include "packet.h" | 20 #include "packet.h" |
21 #include "ns3/assert.h" | 21 #include "ns3/assert.h" |
22 #include "ns3/log.h" | 22 #include "ns3/log.h" |
23 #include "ns3/simulator.h" | |
23 #include "ns3/test.h" | 24 #include "ns3/test.h" |
24 #include <string> | 25 #include <string> |
25 #include <stdarg.h> | 26 #include <stdarg.h> |
26 | 27 |
27 NS_LOG_COMPONENT_DEFINE ("Packet"); | 28 NS_LOG_COMPONENT_DEFINE ("Packet"); |
28 | 29 |
29 namespace ns3 { | 30 namespace ns3 { |
30 | 31 |
31 uint32_t Packet::m_globalUid = 0; | 32 uint32_t Packet::m_globalUid = 0; |
32 | 33 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 return m_data->tid; | 106 return m_data->tid; |
106 } | 107 } |
107 void· | 108 void· |
108 PacketTagIterator::Item::GetTag (Tag &tag) const | 109 PacketTagIterator::Item::GetTag (Tag &tag) const |
109 { | 110 { |
110 NS_ASSERT (tag.GetInstanceTypeId () == m_data->tid); | 111 NS_ASSERT (tag.GetInstanceTypeId () == m_data->tid); |
111 tag.Deserialize (TagBuffer ((uint8_t*)m_data->data, (uint8_t*)m_data->data+PAC KET_TAG_MAX_SIZE)); | 112 tag.Deserialize (TagBuffer ((uint8_t*)m_data->data, (uint8_t*)m_data->data+PAC KET_TAG_MAX_SIZE)); |
112 } | 113 } |
113 | 114 |
114 | 115 |
115 void· | |
116 Packet::Ref (void) const | |
117 { | |
118 m_refCount++; | |
119 } | |
120 void· | |
121 Packet::Unref (void) const | |
122 { | |
123 m_refCount--; | |
124 if (m_refCount == 0) | |
125 { | |
126 delete this; | |
127 } | |
128 } | |
129 | |
130 Ptr<Packet>· | 116 Ptr<Packet>· |
131 Packet::Copy (void) const | 117 Packet::Copy (void) const |
132 { | 118 { |
133 // we need to invoke the copy constructor directly | 119 // we need to invoke the copy constructor directly |
134 // rather than calling Create because the copy constructor | 120 // rather than calling Create because the copy constructor |
135 // is private. | 121 // is private. |
136 return Ptr<Packet> (new Packet (*this), false); | 122 return Ptr<Packet> (new Packet (*this), false); |
137 } | 123 } |
138 | 124 |
139 Packet::Packet () | 125 Packet::Packet () |
140 : m_buffer (), | 126 : m_buffer (), |
141 m_byteTagList (), | 127 m_byteTagList (), |
142 m_packetTagList (), | 128 m_packetTagList (), |
143 m_metadata (m_globalUid, 0), | 129 m_metadata ((uint64_t)Simulator::GetSystemId () << 32 | m_globalUid, 0), |
144 m_nixVector (0), | 130 m_nixVector (0) |
145 m_refCount (1) | |
146 { | 131 { |
147 m_globalUid++; | 132 m_globalUid++; |
148 } | 133 } |
149 | 134 |
150 Packet::Packet (const Packet &o) | 135 Packet::Packet (const Packet &o) |
151 : m_buffer (o.m_buffer), | 136 : m_buffer (o.m_buffer), |
152 m_byteTagList (o.m_byteTagList), | 137 m_byteTagList (o.m_byteTagList), |
153 m_packetTagList (o.m_packetTagList), | 138 m_packetTagList (o.m_packetTagList), |
154 m_metadata (o.m_metadata), | 139 m_metadata (o.m_metadata) |
155 m_refCount (1) | |
156 { | 140 { |
157 o.m_nixVector ? m_nixVector = o.m_nixVector->Copy ()· | 141 o.m_nixVector ? m_nixVector = o.m_nixVector->Copy ()· |
158 : m_nixVector = 0; | 142 : m_nixVector = 0; |
159 } | 143 } |
160 | 144 |
161 Packet & | 145 Packet & |
162 Packet::operator = (const Packet &o) | 146 Packet::operator = (const Packet &o) |
163 { | 147 { |
164 if (this == &o) | 148 if (this == &o) |
165 { | 149 { |
166 return *this; | 150 return *this; |
167 } | 151 } |
168 m_buffer = o.m_buffer; | 152 m_buffer = o.m_buffer; |
169 m_byteTagList = o.m_byteTagList; | 153 m_byteTagList = o.m_byteTagList; |
170 m_packetTagList = o.m_packetTagList; | 154 m_packetTagList = o.m_packetTagList; |
171 m_metadata = o.m_metadata; | 155 m_metadata = o.m_metadata; |
172 o.m_nixVector ? m_nixVector = o.m_nixVector->Copy ()· | 156 o.m_nixVector ? m_nixVector = o.m_nixVector->Copy ()· |
173 : m_nixVector = 0; | 157 : m_nixVector = 0; |
174 return *this; | 158 return *this; |
175 } | 159 } |
176 | 160 |
177 Packet::Packet (uint32_t size) | 161 Packet::Packet (uint32_t size) |
178 : m_buffer (size), | 162 : m_buffer (size), |
179 m_byteTagList (), | 163 m_byteTagList (), |
180 m_packetTagList (), | 164 m_packetTagList (), |
181 m_metadata (m_globalUid, size), | 165 m_metadata ((uint64_t)Simulator::GetSystemId () << 32 | m_globalUid, size), |
182 m_nixVector (0), | 166 m_nixVector (0) |
183 m_refCount (1) | |
184 { | 167 { |
185 m_globalUid++; | 168 m_globalUid++; |
186 } | 169 } |
187 Packet::Packet (uint32_t mpiRank, uint32_t uid, uint32_t size) | 170 Packet::Packet (uint8_t const *buffer, uint32_t size, bool magic) |
faker.moatamri
2009/11/06 14:22:30
same thing here, please factorize the copy and pas
Josh Pelkey
2009/11/09 18:50:21
Again, not sure what you mean. I was following th
faker.moatamri
2009/11/09 21:17:26
Yes I know probably not you fault but actually tho
| |
188 : m_buffer (size), | 171 : m_buffer (0, false), |
189 m_byteTagList (), | 172 m_byteTagList (), |
190 m_packetTagList (), | 173 m_packetTagList (), |
191 m_metadata (mpiRank, uid, size), | 174 m_metadata (0,0), |
192 m_nixVector (0), | 175 m_nixVector (0) |
193 m_refCount (1) | 176 { |
194 {} | 177 NS_ASSERT (magic); |
178 Deserialize (buffer, size); | |
179 } | |
195 | 180 |
196 Packet::Packet (uint8_t const*buffer, uint32_t size) | 181 Packet::Packet (uint8_t const*buffer, uint32_t size) |
197 : m_buffer (), | 182 : m_buffer (), |
198 m_byteTagList (), | 183 m_byteTagList (), |
199 m_packetTagList (), | 184 m_packetTagList (), |
200 m_metadata (m_globalUid, size), | 185 m_metadata ((uint64_t)Simulator::GetSystemId () << 32 | m_globalUid, size), |
201 m_nixVector (0), | 186 m_nixVector (0) |
202 m_refCount (1) | |
203 { | 187 { |
204 m_globalUid++; | 188 m_globalUid++; |
205 m_buffer.AddAtStart (size); | 189 m_buffer.AddAtStart (size); |
206 Buffer::Iterator i = m_buffer.Begin (); | 190 Buffer::Iterator i = m_buffer.Begin (); |
207 i.Write (buffer, size); | 191 i.Write (buffer, size); |
208 } | 192 } |
209 | 193 |
210 Packet::Packet (const Buffer &buffer, const ByteTagList &byteTagList,· | 194 Packet::Packet (const Buffer &buffer, const ByteTagList &byteTagList,· |
211 const PacketTagList &packetTagList, const PacketMetadata &metada ta) | 195 const PacketTagList &packetTagList, const PacketMetadata &metada ta) |
212 : m_buffer (buffer), | 196 : m_buffer (buffer), |
213 m_byteTagList (byteTagList), | 197 m_byteTagList (byteTagList), |
214 m_packetTagList (packetTagList), | 198 m_packetTagList (packetTagList), |
215 m_metadata (metadata), | 199 m_metadata (metadata), |
216 m_nixVector (0), | 200 m_nixVector (0) |
217 m_refCount (1) | |
218 {} | 201 {} |
219 | 202 |
220 Ptr<Packet> | 203 Ptr<Packet> |
221 Packet::CreateFragment (uint32_t start, uint32_t length) const | 204 Packet::CreateFragment (uint32_t start, uint32_t length) const |
222 { | 205 { |
223 NS_LOG_FUNCTION (this << start << length); | 206 NS_LOG_FUNCTION (this << start << length); |
224 Buffer buffer = m_buffer.CreateFragment (start, length); | 207 Buffer buffer = m_buffer.CreateFragment (start, length); |
225 NS_ASSERT (m_buffer.GetSize () >= start + length); | 208 NS_ASSERT (m_buffer.GetSize () >= start + length); |
226 uint32_t end = m_buffer.GetSize () - (start + length); | 209 uint32_t end = m_buffer.GetSize () - (start + length); |
227 PacketMetadata metadata = m_metadata.CreateFragment (start, end); | 210 PacketMetadata metadata = m_metadata.CreateFragment (start, end); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 uint32_t newStart = m_buffer.GetCurrentStartOffset (); | 355 uint32_t newStart = m_buffer.GetCurrentStartOffset (); |
373 · | 356 · |
374 // Update tag offsets if buffer offsets were changed | 357 // Update tag offsets if buffer offsets were changed |
375 const_cast<ByteTagList &>(m_byteTagList).AddAtStart (newStart - oldStart, newS tart); | 358 const_cast<ByteTagList &>(m_byteTagList).AddAtStart (newStart - oldStart, newS tart); |
376 return data; | 359 return data; |
377 } | 360 } |
378 | 361 |
379 uint32_t· | 362 uint32_t· |
380 Packet::CopyData (uint8_t *buffer, uint32_t size) const | 363 Packet::CopyData (uint8_t *buffer, uint32_t size) const |
381 { | 364 { |
382 Buffer::Iterator i = m_buffer.Begin (); | 365 return m_buffer.CopyData (buffer, size); |
383 uint32_t cur = 0; | |
384 while (!i.IsEnd () && cur < size) | |
385 { | |
386 buffer[cur] = i.ReadU8 (); | |
387 cur++; | |
388 } | |
389 return cur; | |
390 } | 366 } |
391 | 367 |
392 void | 368 void |
393 Packet::CopyData(std::ostream *os, uint32_t size) const | 369 Packet::CopyData(std::ostream *os, uint32_t size) const |
394 { | 370 { |
395 return m_buffer.CopyData (os, size); | 371 return m_buffer.CopyData (os, size); |
396 } | 372 } |
397 | 373 |
398 uint32_t· | 374 uint64_t· |
399 Packet::GetUid (void) const | 375 Packet::GetUid (void) const |
400 { | 376 { |
401 return m_metadata.GetUid (); | 377 return m_metadata.GetUid (); |
402 } | |
403 | |
404 uint32_t· | |
405 Packet::GetMpiRank (void) const | |
406 { | |
407 return m_metadata.GetMpiRank (); | |
408 } | 378 } |
409 | 379 |
410 void· | 380 void· |
411 Packet::PrintByteTags (std::ostream &os) const | 381 Packet::PrintByteTags (std::ostream &os) const |
412 { | 382 { |
413 ByteTagIterator i = GetByteTagIterator (); | 383 ByteTagIterator i = GetByteTagIterator (); |
414 while (i.HasNext ()) | 384 while (i.HasNext ()) |
415 { | 385 { |
416 ByteTagIterator::Item item = i.Next (); | 386 ByteTagIterator::Item item = i.Next (); |
417 os << item.GetTypeId ().GetName () << " [" << item.GetStart () << "-" << i tem.GetEnd () << "]"; | 387 os << item.GetTypeId ().GetName () << " [" << item.GetStart () << "-" << i tem.GetEnd () << "]"; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
567 PacketMetadata::Enable (); | 537 PacketMetadata::Enable (); |
568 } | 538 } |
569 | 539 |
570 void | 540 void |
571 Packet::EnableChecking (void) | 541 Packet::EnableChecking (void) |
572 { | 542 { |
573 NS_LOG_FUNCTION_NOARGS (); | 543 NS_LOG_FUNCTION_NOARGS (); |
574 PacketMetadata::EnableChecking (); | 544 PacketMetadata::EnableChecking (); |
575 } | 545 } |
576 | 546 |
577 Buffer | 547 uint32_t Packet::GetSerializedSize (void) const |
578 Packet::SerializeForMpi (void) const | 548 { |
579 { | 549 uint32_t size = 0; |
580 /* | 550 |
581 This function stores the following· | 551 if (m_nixVector) |
582 information in the buffer to be· | 552 { |
583 transmitted via MPI: | 553 // increment total size by the size of the nix-vector |
584 | 554 // ensuring 4-byte boundary |
585 ------------------------------------ | 555 size += ((m_nixVector->GetSerializedSize () + 3) & (~3)); |
586 | UID (4B) | | 556 |
587 ------------------------------------ | 557 // add 4-bytes for entry of total length of nix-vector |
588 | rank (4B) | | 558 size += 4; |
589 ------------------------------------ | 559 } |
590 | Nix-vector Length (4B) | | 560 else |
591 ------------------------------------ | 561 { |
592 | Nix-vector (x4B) | | 562 // if no nix-vector, still have to add 4-bytes |
593 ------------------------------------ | 563 // to account for the entry of total size for· |
594 | Tags | | 564 // nix-vector in the buffer |
595 ------------------------------------ | 565 size += 4; |
596 | Meta-data | | 566 } |
597 ------------------------------------ | 567 |
598 | Zero-byte data Length (4B) | | 568 //Tag size |
599 ------------------------------------ | 569 //XXX |
600 | Packet (start) buffer Length (4B) | | 570 //size += m_tags.GetSerializedSize (); |
601 ------------------------------------ | 571 |
602 | Packet (start) buffer | | 572 // increment total size by size of meta-data· |
603 ------------------------------------ | 573 // ensuring 4-byte boundary |
604 | Packet (end) buffer Length (4B) | | 574 size += ((m_metadata.GetSerializedSize () + 3) & (~3)); |
605 ------------------------------------ | 575 |
606 | Packet (end) buffer | | 576 // add 4-bytes for entry of total length of meta-data |
607 ------------------------------------ | 577 size += 4; |
608 | 578 |
609 */ | 579 // increment total size by size of buffer· |
610 | 580 // ensuring 4-byte boundary |
581 size += ((m_buffer.GetSerializedSize () + 3) & (~3)); | |
582 | |
583 // add 4-bytes for entry of total length of buffer· | |
584 size += 4; | |
585 ·· | |
586 return size; | |
587 } | |
588 ·· | |
589 uint32_t· | |
590 Packet::Serialize (uint8_t* buffer, uint32_t maxSize) const | |
591 { | |
592 uint32_t* p = (uint32_t*)buffer; | |
593 uint32_t size = 0; | |
594 | |
595 // if nix-vector exists, serialize it | |
596 if (m_nixVector) | |
597 { | |
598 uint32_t nixSize = m_nixVector->GetSerializedSize (); | |
599 if (size + nixSize <= maxSize) | |
600 { | |
601 // put the total length of nix-vector in the | |
602 // buffer. this includes 4-bytes for total· | |
603 // length itself | |
604 *p++ = nixSize + 4; | |
605 size += nixSize; | |
606 | |
607 // serialize the nix-vector | |
608 uint32_t serialized =· | |
609 m_nixVector->Serialize (p, nixSize); | |
610 if (serialized) | |
611 { | |
612 // increment p by nixSize bytes | |
613 // ensuring 4-byte boundary | |
614 p += ((nixSize+3) & (~3)) / 4; | |
615 } | |
616 else | |
617 { | |
618 return 0; | |
619 } | |
620 } | |
621 else· | |
622 { | |
623 return 0; | |
624 } | |
625 } | |
626 else | |
627 {· | |
628 // no nix vector, set zero length,· | |
629 // ie 4-bytes, since it must include· | |
630 // length for itself | |
631 if (size + 4 <= maxSize) | |
632 { | |
633 size += 4; | |
634 *p++ = 4; | |
635 } | |
636 else | |
637 { | |
638 return 0; | |
639 } | |
640 } | |
641 | |
642 // Serialize Tags | |
643 // XXX | |
644 | |
645 // Serialize Metadata | |
646 uint32_t metaSize = m_metadata.GetSerializedSize (); | |
647 if (size + metaSize <= maxSize) | |
648 { | |
649 // put the total length of metadata in the | |
650 // buffer. this includes 4-bytes for total· | |
651 // length itself | |
652 *p++ = metaSize + 4; | |
653 size += metaSize; | |
654 | |
655 // serialize the metadata | |
656 uint32_t serialized =· | |
657 m_metadata.Serialize ((uint8_t*)p, metaSize);· | |
658 if (serialized) | |
659 { | |
660 // increment p by metaSize bytes | |
661 // ensuring 4-byte boundary | |
662 p += ((metaSize+3) & (~3)) / 4; | |
663 } | |
664 else | |
665 { | |
666 return 0; | |
667 } | |
668 } | |
669 else | |
670 { | |
671 return 0; | |
672 } | |
673 | |
674 // Serialize the packet contents | |
675 uint32_t bufSize = m_buffer.GetSerializedSize (); | |
676 if (size + bufSize <= maxSize) | |
677 { | |
678 // put the total length of the buffer in the | |
679 // buffer. this includes 4-bytes for total· | |
680 // length itself | |
681 *p++ = bufSize + 4; | |
682 size += bufSize; | |
683 | |
684 // serialize the buffer | |
685 uint32_t serialized =· | |
686 m_buffer.Serialize ((uint8_t*)p, bufSize); | |
687 if (serialized) | |
688 { | |
689 // increment p by bufSize bytes | |
690 // ensuring 4-byte boundary | |
691 p += ((bufSize+3) & (~3)) / 4; | |
692 } | |
693 else· | |
694 { | |
695 return 0; | |
696 } | |
697 } | |
698 else | |
699 { | |
700 return 0; | |
701 } | |
702 | |
703 // Serialized successfully | |
704 return 1; | |
705 } | |
706 | |
707 uint32_t· | |
708 Packet::Deserialize (uint8_t const*buffer, uint32_t size) | |
709 { | |
611 NS_LOG_FUNCTION (this); | 710 NS_LOG_FUNCTION (this); |
612 Buffer writeBuffer; | 711 |
613 | 712 uint32_t* p = (uint32_t*)buffer; |
614 // write Packet byte buffer | |
615 writeBuffer = m_buffer.CreateLimitedCopy (); | |
616 | |
617 // write Meta-data | |
618 uint32_t metaReserve = m_metadata.GetSerializedSize (); | |
619 writeBuffer.AddAtStart (metaReserve); | |
620 m_metadata.Serialize (writeBuffer.Begin (), metaReserve); | |
621 | |
622 // write Tags | |
623 //XXX | |
faker.moatamri
2009/11/06 14:22:30
fix that or remove commented code, same for all co
Josh Pelkey
2009/11/09 18:50:21
I was following the format of Serialize and Deseri
faker.moatamri
2009/11/09 21:17:26
commented code should not be included in code base
| |
624 //reserve = m_tags.GetSerializedSize (); | |
625 //buffer.AddAtStart (reserve); | |
626 //m_tags.Serialize (buffer.Begin (), reserve); | |
627 | |
628 // write Nix-vector | |
629 if (m_nixVector) | |
630 { | |
631 uint32_t nixReserve = m_nixVector->GetSerializedSize (); | |
632 writeBuffer.AddAtStart (nixReserve); | |
633 m_nixVector->Serialize (writeBuffer.Begin(), nixReserve); | |
634 } | |
635 else | |
636 { | |
637 writeBuffer.AddAtStart (4); | |
638 writeBuffer.Begin ().WriteU32 (0); | |
639 } | |
640 | |
641 // write Rank | |
642 writeBuffer.AddAtStart (4); | |
643 if (MPIInterface::IsEnabled ()) | |
644 writeBuffer.Begin ().WriteU32 (MPIInterface::Rank ()); | |
645 else | |
646 writeBuffer.Begin ().WriteU32 (0); | |
647 | |
648 // write UID | |
649 writeBuffer.AddAtStart (4); | |
650 writeBuffer.Begin ().WriteU32 (GetUid ()); | |
651 | |
652 return writeBuffer; | |
653 } | |
654 | |
655 void· | |
656 Packet::DeserializeForMpi (Buffer buffer) | |
657 { | |
658 NS_LOG_FUNCTION (this); | |
659 | |
660 // uid and rank have already been stripped· | |
661 // off immediately before calling this· | |
662 // function. So we start with the nix-vector. | |
663 | 713 |
664 // read nix-vector | 714 // read nix-vector |
665 NS_ASSERT (!m_nixVector); | 715 NS_ASSERT (!m_nixVector); |
666 if (buffer.Begin ().ReadU32 ()) | 716 uint32_t nixSize = *p++; |
717 size -= nixSize; | |
718 | |
719 // if size less than zero, the buffer· | |
720 // will be overrun, assert | |
721 NS_ASSERT (size >= 0); | |
722 | |
723 if (nixSize > 4) | |
667 { | 724 { |
668 Ptr<NixVector> nix = CreateObject<NixVector> (); | 725 Ptr<NixVector> nix = CreateObject<NixVector> (); |
669 uint32_t nixDeserialized = nix->Deserialize (buffer.Begin ()); | 726 uint32_t nixDeserialized = nix->Deserialize (p, nixSize); |
727 if (!nixDeserialized) | |
728 { | |
729 // nix-vector not deserialized· | |
730 // completely | |
731 return 0; | |
732 } | |
670 m_nixVector = nix; | 733 m_nixVector = nix; |
671 buffer.RemoveAtStart (nixDeserialized); | 734 // increment p by nixSize ensuring· |
672 } | 735 // 4-byte boundary |
673 else | 736 p += ((((nixSize - 4) + 3) & (~3)) / 4); |
674 buffer.RemoveAtStart (4); | 737 } |
675 | 738 |
676 // read tags | 739 // read tags |
677 //XXX | 740 //XXX |
678 //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ()); | 741 //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ()); |
679 //buffer.RemoveAtStart (tagsDeserialized); | 742 //buffer.RemoveAtStart (tagsDeserialized); |
680 | 743 |
681 // read metadata | 744 // read metadata |
745 uint32_t metaSize = *p++; | |
746 size -= metaSize; | |
747 | |
748 // if size less than zero, the buffer· | |
749 // will be overrun, assert | |
750 NS_ASSERT (size >= 0); | |
751 | |
682 uint32_t metadataDeserialized =· | 752 uint32_t metadataDeserialized =· |
683 m_metadata.Deserialize (buffer.Begin ()); | 753 m_metadata.Deserialize ((uint8_t*)p, metaSize); |
684 buffer.RemoveAtStart (metadataDeserialized); | 754 if (!metadataDeserialized) |
685 | 755 { |
686 // read zero-byte length | 756 // meta-data not deserialized· |
687 uint32_t zeroByteLength = buffer.Begin ().ReadU32 (); | 757 // completely |
688 Buffer zeroByteBuffer (zeroByteLength); | 758 return 0; |
689 buffer.RemoveAtStart (4); | 759 } |
690 | 760 // increment p by metaSize ensuring· |
691 // read (start) packet buffer | 761 // 4-byte boundary |
692 uint32_t startLength = buffer.Begin ().ReadU32 (); | 762 p += ((((metaSize - 4) + 3) & (~3)) / 4); |
693 buffer.RemoveAtStart (4); | 763 |
694 Buffer startBuffer = buffer; | 764 // read buffer contents |
695 buffer.RemoveAtStart (startLength); | 765 uint32_t bufSize = *p++; |
696 | 766 size -= bufSize; |
697 // read (end) packet buffer | 767 |
698 uint32_t endLength = buffer.Begin ().ReadU32 (); | 768 // if size less than zero, the buffer· |
699 buffer.RemoveAtStart (4); | 769 // will be overrun, assert |
700 | 770 NS_ASSERT (size >= 0); |
701 // remove end buffer from start buffer | 771 |
702 startBuffer.RemoveAtEnd (endLength+4); | 772 uint32_t bufferDeserialized = |
703 | 773 m_buffer.Deserialize ((uint8_t*)p, bufSize); |
704 // fill in m_buffer with everything in· | 774 if (!bufferDeserialized) |
705 // the right order | 775 { |
706 // something strange happened when I tried to use· | 776 // buffer not deserialized· |
707 // AddAtEnd to add the end data (after the zero-byte), | 777 // completely |
708 // so i used Write instead | 778 return 0; |
709 zeroByteBuffer.AddAtStart (startLength); | 779 } |
710 zeroByteBuffer.Begin ().Write (startBuffer.PeekData (), startLength); | |
711 zeroByteBuffer.AddAtEnd (endLength); | |
712 zeroByteBuffer.End ().Write (buffer.PeekData (), endLength); | |
713 buffer.RemoveAtStart (endLength); | |
714 m_buffer = zeroByteBuffer; | |
715 } | |
716 | |
717 Buffer· | |
718 Packet::Serialize (void) const | |
719 { | |
720 NS_LOG_FUNCTION (this); | |
721 Buffer buffer; | |
722 uint32_t reserve; | |
723 | |
724 // write metadata | |
725 reserve = m_metadata.GetSerializedSize (); | |
726 buffer.AddAtStart (reserve); | |
727 m_metadata.Serialize (buffer.Begin (), reserve); | |
728 | |
729 // write tags | |
730 //XXX | |
731 //reserve = m_tags.GetSerializedSize (); | |
732 //buffer.AddAtStart (reserve); | |
733 //m_tags.Serialize (buffer.Begin (), reserve); | |
734 ·· | 780 ·· |
735 // aggregate byte buffer, metadata, and tags | 781 // return zero if did not deserialize the· |
736 Buffer tmp = m_buffer.CreateFullCopy (); | 782 // number of expected bytes |
737 tmp.AddAtEnd (buffer); | 783 return (size == 0); |
738 ·· | |
739 // write byte buffer size. | |
740 tmp.AddAtStart (4); | |
741 tmp.Begin ().WriteU32 (m_buffer.GetSize ()); | |
742 | |
743 return tmp; | |
744 } | |
745 void· | |
746 Packet::Deserialize (Buffer buffer) | |
747 { | |
748 NS_LOG_FUNCTION (this); | |
749 Buffer buf = buffer; | |
750 // read size | |
751 uint32_t packetSize = buf.Begin ().ReadU32 (); | |
752 buf.RemoveAtStart (4); | |
753 | |
754 // read buffer. | |
755 buf.RemoveAtEnd (buf.GetSize () - packetSize); | |
756 m_buffer = buf; | |
757 buffer.RemoveAtStart (4 + packetSize); | |
758 | |
759 | |
760 // read tags | |
761 //XXX | |
762 //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ()); | |
763 //buffer.RemoveAtStart (tagsDeserialized); | |
764 | |
765 // read metadata | |
766 uint32_t metadataDeserialized =· | |
767 m_metadata.Deserialize (buffer.Begin ()); | |
768 buffer.RemoveAtStart (metadataDeserialized); | |
769 } | 784 } |
770 | 785 |
771 void· | 786 void· |
772 Packet::AddByteTag (const Tag &tag) const | 787 Packet::AddByteTag (const Tag &tag) const |
773 { | 788 { |
774 NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSeriali zedSize ()); | 789 NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSeriali zedSize ()); |
775 ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList); | 790 ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList); |
776 TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),· | 791 TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),· |
777 m_buffer.GetCurrentStartOffset (), | 792 m_buffer.GetCurrentStartOffset (), |
778 m_buffer.GetCurrentEndOffset ()); | 793 m_buffer.GetCurrentEndOffset ()); |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1277 PacketTestSuite::PacketTestSuite () | 1292 PacketTestSuite::PacketTestSuite () |
1278 : TestSuite ("packet", UNIT) | 1293 : TestSuite ("packet", UNIT) |
1279 { | 1294 { |
1280 AddTestCase (new PacketTest); | 1295 AddTestCase (new PacketTest); |
1281 } | 1296 } |
1282 | 1297 |
1283 PacketTestSuite g_packetTestSuite; | 1298 PacketTestSuite g_packetTestSuite; |
1284 | 1299 |
1285 | 1300 |
1286 } // namespace ns3 | 1301 } // namespace ns3 |
LEFT | RIGHT |