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 ReadMarshalStream& operator >> (std::pair<void *, unsigned> v) {
00075 memcpy( v.first, start+size, v.second);
00076 size += v.second;
00077 return *this;
00078 }
00079
00080 inline ReadMarshalStream& operator >> (std::string &v) {
00081 uint32_t s_size;
00082 *this >> s_size;
00083 char data[s_size];
00084 memcpy(data, start+size, s_size);
00085 size += s_size;
00086 v = std::string(data, s_size);
00087 return *this;
00088 }
00089
00090 };
00091
00092 class WriteMarshalStream {
00093
00094 awds::BasePacket * const packet;
00095 const size_t startPacketOffset;
00096
00097 char * const start;
00098 size_t size;
00099
00100 public:
00101
00102 WriteMarshalStream(void * _start) :
00103 packet(0),
00104 startPacketOffset(0),
00105 start(static_cast<char *>(_start)),
00106 size(0)
00107 { }
00108
00109 explicit WriteMarshalStream(awds::Flood flood) :
00110 packet(&flood.packet),
00111 startPacketOffset(awds::Flood::FloodHeaderEnd),
00112 start(flood.packet.buffer + startPacketOffset),
00113 size(0)
00114 { }
00115
00116 explicit WriteMarshalStream(awds::UnicastPacket uni) :
00117 packet(&uni.packet),
00118 startPacketOffset(awds::UnicastPacket::UnicastPacketEnd),
00119 start(uni.packet.buffer + startPacketOffset),
00120 size(0)
00121 { }
00122
00123
00124 inline WriteMarshalStream& operator <<(unsigned char c) {
00125 start[size++] = static_cast<char>(c);
00126 return *this;
00127 }
00128
00129 inline WriteMarshalStream& operator <<( uint32_t v ) {
00130 toArray<uint32_t>(v, start + size);
00131 size += 4;
00132 return *this;
00133 }
00134
00135 inline WriteMarshalStream& operator <<( uint16_t v ) {
00136 toArray<uint16_t>(v, start + size);
00137 size += 2;
00138 return *this;
00139 }
00140
00141 inline WriteMarshalStream& operator <<(const awds::NodeId& id) {
00142 id.toArray(start + size);
00143 size += awds::NodeId::size;
00144 return *this;
00145 }
00146
00147 inline WriteMarshalStream& operator <<(const gea::AbsTime& t) {
00148 t.toArray( (void *)(start + size) );
00149 size += gea::AbsTime::size;
00150 return *this;
00151 }
00152
00153 inline WriteMarshalStream& operator <<(const gea::Duration& t) {
00154 t.toArray( (void *)(start + size) );
00155 size += gea::Duration::size;
00156 return *this;
00157 }
00158
00159 inline WriteMarshalStream& operator <<(std::pair<const void *, unsigned> v) {
00160 memcpy(start+size, v.first, v.second);
00161 size += v.second;
00162 return *this;
00163 }
00164
00165 inline WriteMarshalStream& operator <<(const std::string &v) {
00166 *this << (uint32_t) v.size();
00167 memcpy(start+size, v.data(), v.size());
00168 size += v.size();
00169 return *this;
00170 }
00171
00172 size_t getStreamSize() const { return this->size; }
00173
00174 void storePacketSize() {
00175 assert(this->packet);
00176 this->packet->size = this->startPacketOffset + this->getStreamSize();
00177 }
00178
00179
00180 };
00181
00182
00183 #endif //MARSHALSTREAM_H__
00184
00185
00186
00187
00188
00189