#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/OS/Network/Packet.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVLNETPACKET_HEADER #define RAVLNETPACKET_HEADER 1 ///////////////////////////////////////////////////////// //! rcsid="$Id: Packet.hh,v 1.5 2001/10/11 14:35:44 craftit Exp $" //! file="Ravl/OS/Network/Packet.hh" //! lib=RavlNet //! author="Charles Galambos" //! date="13/12/2000" //! docentry="Ravl.OS.Network" #include "Ravl/BinStream.hh" #include "Ravl/BufStream.hh" namespace RavlN { class NetMessageC; //! userlevel=Normal //: Packet of stream encoded data. class NetPacketC { public: NetPacketC() {} //: Default constructor. NetPacketC(IntT size); //: Construct a packet of 'size' bytes. NetPacketC(SArray1dC &arr) : data(arr) {} //: Construct a packet from an array inline NetPacketC(BinIStreamC &strm); //: Construct a packet of 'size' bytes. SArray1dC &Data() { return data; } //: Access data in packet. const SArray1dC &Data() const { return data; } //: Access data in packet. IStreamC DecodeStream() const { return BufIStreamC(data); } //: Create a decoding stream. //BinOStreamC EncodeStream() // { return BinOStreamC(OStrStreamC(data)); } //: Create an encoding stream. inline bool Transmit(BinOStreamC &strm) const; //: Transmit packet. bool IsValid() const { return data.Size() > 0; } //: Is packet valid ? void Dump(ostream &out) const; //: Dump packet in human readable form. UIntT Size() const { return data.Size(); } //: Get the size of the packet. protected: SArray1dC data; }; //: Construct a packet of 'size' bytes. inline NetPacketC::NetPacketC(BinIStreamC &strm) { UIntT size; strm >> size; if(!strm.Stream() || size <= 0) return ; SArray1dC buf(size); strm.IBuff(&buf[0],size); data = buf; } //: Transmit packet. inline bool NetPacketC::Transmit(BinOStreamC &strm) const { strm << data.Size(); strm.OBuff(&data[0],data.Size()); return true; } inline BinOStreamC &operator<<(BinOStreamC &strm,const NetPacketC &pkt) { pkt.Transmit(strm); return strm; } inline BinIStreamC &operator>>(BinIStreamC &strm,NetPacketC &pkt) { strm >> pkt.Data(); return strm; } }; #endif