00001 00026 #ifndef ODVA_ETHERNETIP_ENCAP_PACKET_H 00027 #define ODVA_ETHERNETIP_ENCAP_PACKET_H 00028 00029 #include <boost/shared_ptr.hpp> 00030 00031 #include "odva_ethernetip/eip_types.h" 00032 00033 #include "odva_ethernetip/encap_header.h" 00034 #include "odva_ethernetip/serialization/reader.h" 00035 #include "odva_ethernetip/serialization/writer.h" 00036 #include "odva_ethernetip/serialization/serializable.h" 00037 00038 00039 using boost::shared_ptr; 00040 00041 namespace eip { 00042 00043 using serialization::Serializable; 00044 using serialization::Reader; 00045 using serialization::Writer; 00046 00050 class EncapPacket : public Serializable 00051 { 00052 public: 00053 00057 EncapPacket(EIP_UINT command = 0, EIP_UDINT session_handle = 0) 00058 : header_(command, session_handle) { } 00059 00066 EncapPacket(EIP_UINT command, EIP_UDINT session_handle, 00067 shared_ptr<Serializable> payload) 00068 : header_(command, session_handle), payload_(payload) 00069 { 00070 updateLength(); 00071 } 00072 00073 00078 EncapHeader& getHeader() 00079 { 00080 return header_; 00081 } 00082 00087 shared_ptr<Serializable> getPayload() const 00088 { 00089 return payload_; 00090 } 00091 00096 void setPayload(shared_ptr<Serializable> payload) 00097 { 00098 payload_ = payload; 00099 updateLength(); 00100 } 00101 00106 void getPayloadAs(Serializable& result); 00107 00112 size_t getLength() const 00113 { 00114 size_t l = header_.getLength(); 00115 if (NULL != payload_) 00116 { 00117 l += payload_->getLength(); 00118 } 00119 return l; 00120 } 00121 00126 void updateLength() 00127 { 00128 header_.length = payload_ ? payload_->getLength() : 0; 00129 } 00130 00136 Writer& serialize(Writer& writer) const; 00137 00145 Reader& deserialize(Reader& reader, size_t length); 00146 00153 Reader& deserialize(Reader& reader); 00154 00155 private: 00156 EncapHeader header_; 00157 shared_ptr<Serializable> payload_; 00158 }; 00159 00160 } // namespace eip 00161 00162 #endif // ODVA_ETHERNETIP_ENCAP_PACKET_H