00001 00026 #include <vector> 00027 #include <boost/make_shared.hpp> 00028 00029 #include "odva_ethernetip/encap_packet.h" 00030 #include "odva_ethernetip/serialization/serializable_buffer.h" 00031 #include "odva_ethernetip/serialization/buffer_reader.h" 00032 #include "odva_ethernetip/serialization/buffer_writer.h" 00033 #include "odva_ethernetip/serialization/copy_serializable.h" 00034 00035 using std::vector; 00036 using boost::make_shared; 00037 00038 namespace eip { 00039 00040 using serialization::SerializableBuffer; 00041 using serialization::BufferReader; 00042 using serialization::BufferWriter; 00043 00044 void EncapPacket::getPayloadAs(Serializable& result) 00045 { 00046 serialization::copy_serializable(result, *payload_); 00047 } 00048 00049 Writer& EncapPacket::serialize(Writer& writer) const 00050 { 00051 // fixme: need to set length before this! 00052 // header_.length = (NULL == payload_) ? 0 : payload_->getLength(); 00053 header_.serialize(writer); 00054 if (payload_) 00055 { 00056 payload_->serialize(writer); 00057 } 00058 return writer; 00059 } 00060 00061 Reader& EncapPacket::deserialize(Reader& reader) 00062 { 00063 header_.deserialize(reader); 00064 if (header_.length > 0) 00065 { 00066 shared_ptr<SerializableBuffer> sb = make_shared<SerializableBuffer>(); 00067 sb->deserialize(reader, header_.length); 00068 payload_ = sb; 00069 } 00070 else 00071 { 00072 payload_ = shared_ptr<Serializable>(); 00073 } 00074 return reader; 00075 } 00076 00077 Reader& EncapPacket::deserialize(Reader& reader, size_t length) 00078 { 00079 if (length < header_.getLength()) 00080 { 00081 throw std::length_error("Deserialization size less than header size"); 00082 } 00083 header_.deserialize(reader); 00084 if (length != header_.getLength() + header_.length) 00085 { 00086 throw std::length_error("Packet length different than given to deserialize"); 00087 } 00088 if (header_.length > 0) 00089 { 00090 payload_ = make_shared<SerializableBuffer>(); 00091 payload_->deserialize(reader, header_.length); 00092 } 00093 else 00094 { 00095 payload_ = shared_ptr<Serializable>(); 00096 } 00097 return reader; 00098 } 00099 00100 } // namespace eip