00001 #ifndef _MARSHALSTREAM_H__
00002 #define _MARSHALSTREAM_H__
00003
00004 #include <awds/UnicastPacket.h>
00005 #include <awds/Flood.h>
00006 #include <awds/toArray.h>
00007 #include <awds/NodeId.h>
00008
00009 #include <stdint.h>
00010 #include <utility>
00011 #include <cstring>
00012
00013 #include <cassert>
00014
00015 struct ReadMarshalStream {
00016 const char * const start;
00017 size_t size;
00018
00019 ReadMarshalStream(void * _start) :
00020 start(static_cast<char *>(_start)),
00021 size(0)
00022 { }
00023
00024 explicit ReadMarshalStream(awds::Flood& flood) :
00025 start(flood.packet.buffer + awds::Flood::FloodHeaderEnd),
00026 size(0)
00027 { }
00028
00029 explicit ReadMarshalStream(awds::UnicastPacket& uni) :
00030 start(uni.packet.buffer + awds::UnicastPacket::UnicastPacketEnd),
00031 size(0)
00032 { }
00033
00034 inline ReadMarshalStream& operator >>(unsigned char& c) {
00035 c = static_cast<unsigned char>(start[size++]);
00036 return *this;
00037 }
00038
00039 inline ReadMarshalStream& operator >>(char& c) {
00040 c = start[size++];
00041 return *this;
00042 }
00043
00044 inline ReadMarshalStream& operator >>(uint32_t& v) {
00045 v = fromArray<uint32_t>(start + size);
00046 size += 4;
00047 return *this;
00048 }
00049
00050 inline ReadMarshalStream& operator >>(uint16_t& v) {
00051 v = fromArray<uint16_t>(start + size);
00052 size += 2;
00053 return *this;
00054 }
00055
00056 inline ReadMarshalStream& operator >>(awds::NodeId& id) {
00057 id.fromArray(start + size);
00058 size += awds::NodeId::size;
00059 return *this;
00060 }
00061
00062 inline ReadMarshalStream& operator >>(gea::AbsTime& t) {
00063 t.fromArray( (void *)(start + size) );
00064 size += gea::AbsTime::size;
00065 return *this;
00066 }
00067
00068 inline ReadMarshalStream& operator >>(gea::Duration& t) {
00069 t.fromArray( (void *)(start + size) );
00070 size += gea::Duration::size;
00071 return *this;
00072 }
00073
00074
00075
00076 ReadMarshalStream& operator >> (std::pair<void *, unsigned> v) {
00077 memcpy( v.first, start+size, v.second);
00078 size += v.second;
00079 return *this;
00080 }
00081
00082
00083 };
00084
00085 class WriteMarshalStream {
00086
00087 awds::BasePacket * const packet;
00088 const size_t startPacketOffset;
00089
00090 char * const start;
00091 size_t size;
00092
00093 public:
00094
00095 WriteMarshalStream(void * _start) :
00096 packet(0),
00097 startPacketOffset(0),
00098 start(static_cast<char *>(_start)),
00099 size(0)
00100 { }
00101
00102 explicit WriteMarshalStream(awds::Flood flood) :
00103 packet(&flood.packet),
00104 startPacketOffset(awds::Flood::FloodHeaderEnd),
00105 start(flood.packet.buffer + startPacketOffset),
00106 size(0)
00107 { }
00108
00109 explicit WriteMarshalStream(awds::UnicastPacket uni) :
00110 packet(&uni.packet),
00111 startPacketOffset(awds::UnicastPacket::UnicastPacketEnd),
00112 start(uni.packet.buffer + startPacketOffset),
00113 size(0)
00114 { }
00115
00116
00117 inline WriteMarshalStream& operator <<(unsigned char c) {
00118 start[size++] = static_cast<char>(c);
00119 return *this;
00120 }
00121
00122 inline WriteMarshalStream& operator <<( uint32_t v ) {
00123 toArray<uint32_t>(v, start + size);
00124 size += 4;
00125 return *this;
00126 }
00127
00128 inline WriteMarshalStream& operator <<( uint16_t v ) {
00129 toArray<uint16_t>(v, start + size);
00130 size += 2;
00131 return *this;
00132 }
00133
00134 inline WriteMarshalStream& operator <<(const awds::NodeId& id) {
00135 id.toArray(start + size);
00136 size += awds::NodeId::size;
00137 return *this;
00138 }
00139
00140 inline WriteMarshalStream& operator <<(const gea::AbsTime& t) {
00141 t.toArray( (void *)(start + size) );
00142 size += gea::AbsTime::size;
00143 return *this;
00144 }
00145
00146 inline WriteMarshalStream& operator <<(const gea::Duration& t) {
00147 t.toArray( (void *)(start + size) );
00148 size += gea::Duration::size;
00149 return *this;
00150 }
00151
00152 inline WriteMarshalStream& operator <<(std::pair<const void *, unsigned> v) {
00153 memcpy(start+size, v.first, v.second);
00154 size += v.second;
00155 return *this;
00156 }
00157
00158 size_t getStreamSize() const { return this->size; }
00159
00160 void storePacketSize() {
00161 assert(this->packet);
00162 this->packet->size = this->startPacketOffset + this->getStreamSize();
00163 }
00164
00165
00166 };
00167
00168
00169 #endif //MARSHALSTREAM_H__
00170
00171
00172
00173
00174
00175