1*4c44e39aSPhilippe Houdoin //------------------------------------------------------------------------------ 2*4c44e39aSPhilippe Houdoin // Copyright (c) 2001-2002, OpenBeOS 3*4c44e39aSPhilippe Houdoin // 4*4c44e39aSPhilippe Houdoin // Permission is hereby granted, free of charge, to any person obtaining a 5*4c44e39aSPhilippe Houdoin // copy of this software and associated documentation files (the "Software"), 6*4c44e39aSPhilippe Houdoin // to deal in the Software without restriction, including without limitation 7*4c44e39aSPhilippe Houdoin // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8*4c44e39aSPhilippe Houdoin // and/or sell copies of the Software, and to permit persons to whom the 9*4c44e39aSPhilippe Houdoin // Software is furnished to do so, subject to the following conditions: 10*4c44e39aSPhilippe Houdoin // 11*4c44e39aSPhilippe Houdoin // The above copyright notice and this permission notice shall be included in 12*4c44e39aSPhilippe Houdoin // all copies or substantial portions of the Software. 13*4c44e39aSPhilippe Houdoin // 14*4c44e39aSPhilippe Houdoin // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15*4c44e39aSPhilippe Houdoin // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16*4c44e39aSPhilippe Houdoin // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17*4c44e39aSPhilippe Houdoin // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18*4c44e39aSPhilippe Houdoin // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19*4c44e39aSPhilippe Houdoin // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20*4c44e39aSPhilippe Houdoin // DEALINGS IN THE SOFTWARE. 21*4c44e39aSPhilippe Houdoin //------------------------------------------------------------------------------ 22*4c44e39aSPhilippe Houdoin 23*4c44e39aSPhilippe Houdoin #ifndef _NETBUFFER_H 24*4c44e39aSPhilippe Houdoin #define _NETBUFFER_H 25*4c44e39aSPhilippe Houdoin 26*4c44e39aSPhilippe Houdoin #include <BeBuild.h> 27*4c44e39aSPhilippe Houdoin #include <SupportDefs.h> 28*4c44e39aSPhilippe Houdoin #include <Archivable.h> 29*4c44e39aSPhilippe Houdoin #include <sys/socket.h> 30*4c44e39aSPhilippe Houdoin 31*4c44e39aSPhilippe Houdoin 32*4c44e39aSPhilippe Houdoin /* 33*4c44e39aSPhilippe Houdoin * BNetBuffer 34*4c44e39aSPhilippe Houdoin * 35*4c44e39aSPhilippe Houdoin * BNetBuffer is a dynamic buffer useful for storing data to be sent 36*4c44e39aSPhilippe Houdoin * across the network. Data is inserted into and removed from 37*4c44e39aSPhilippe Houdoin * the object using one of the many insert and remove member functions. 38*4c44e39aSPhilippe Houdoin * Access to the raw stored data is possible. The BNetEndpoint class has a 39*4c44e39aSPhilippe Houdoin * send and recv function for use with BNetBuffer. Network byte order conversion 40*4c44e39aSPhilippe Houdoin * is done automatically for all appropriate integral types in the insert and remove 41*4c44e39aSPhilippe Houdoin * functions for that type. 42*4c44e39aSPhilippe Houdoin * 43*4c44e39aSPhilippe Houdoin */ 44*4c44e39aSPhilippe Houdoin 45*4c44e39aSPhilippe Houdoin class NLPacket; 46*4c44e39aSPhilippe Houdoin 47*4c44e39aSPhilippe Houdoin class BNetBuffer : public BArchivable 48*4c44e39aSPhilippe Houdoin { 49*4c44e39aSPhilippe Houdoin public: 50*4c44e39aSPhilippe Houdoin 51*4c44e39aSPhilippe Houdoin BNetBuffer(size_t size = 0); 52*4c44e39aSPhilippe Houdoin virtual ~BNetBuffer(); 53*4c44e39aSPhilippe Houdoin 54*4c44e39aSPhilippe Houdoin /* 55*4c44e39aSPhilippe Houdoin * It is important to call InitCheck() after creating an instance of this class. 56*4c44e39aSPhilippe Houdoin */ 57*4c44e39aSPhilippe Houdoin 58*4c44e39aSPhilippe Houdoin status_t InitCheck(); 59*4c44e39aSPhilippe Houdoin 60*4c44e39aSPhilippe Houdoin BNetBuffer(BMessage *archive); 61*4c44e39aSPhilippe Houdoin virtual status_t Archive(BMessage *into, bool deep = true) const; 62*4c44e39aSPhilippe Houdoin static BArchivable *Instantiate(BMessage *archive); 63*4c44e39aSPhilippe Houdoin 64*4c44e39aSPhilippe Houdoin BNetBuffer(const BNetBuffer &); 65*4c44e39aSPhilippe Houdoin BNetBuffer &operator=(const BNetBuffer &); 66*4c44e39aSPhilippe Houdoin 67*4c44e39aSPhilippe Houdoin 68*4c44e39aSPhilippe Houdoin /* 69*4c44e39aSPhilippe Houdoin * Data insertion member functions. Data is inserted at the end of the internal 70*4c44e39aSPhilippe Houdoin * dynamic buffer. For the short, int, and long versions (and unsigned of 71*4c44e39aSPhilippe Houdoin * each as well) byte order conversion is performed. 72*4c44e39aSPhilippe Houdoin * 73*4c44e39aSPhilippe Houdoin * The terminating null of all strings are inserted as 74*4c44e39aSPhilippe Houdoin * well. 75*4c44e39aSPhilippe Houdoin * 76*4c44e39aSPhilippe Houdoin * Returns B_OK if successful, B_ERROR otherwise. 77*4c44e39aSPhilippe Houdoin */ 78*4c44e39aSPhilippe Houdoin status_t AppendInt8(int8); 79*4c44e39aSPhilippe Houdoin status_t AppendUint8(uint8); 80*4c44e39aSPhilippe Houdoin status_t AppendInt16(int16); 81*4c44e39aSPhilippe Houdoin status_t AppendUint16(uint16); 82*4c44e39aSPhilippe Houdoin status_t AppendInt32(int32); 83*4c44e39aSPhilippe Houdoin status_t AppendUint32(uint32); 84*4c44e39aSPhilippe Houdoin status_t AppendFloat(float); 85*4c44e39aSPhilippe Houdoin status_t AppendDouble(double); 86*4c44e39aSPhilippe Houdoin status_t AppendString(const char *); 87*4c44e39aSPhilippe Houdoin status_t AppendData(const void *, size_t); 88*4c44e39aSPhilippe Houdoin status_t AppendMessage(const BMessage &); 89*4c44e39aSPhilippe Houdoin status_t AppendInt64(int64); 90*4c44e39aSPhilippe Houdoin status_t AppendUint64(uint64); 91*4c44e39aSPhilippe Houdoin 92*4c44e39aSPhilippe Houdoin /* 93*4c44e39aSPhilippe Houdoin * Data extraction member functions. Data is extracted from the start of the 94*4c44e39aSPhilippe Houdoin * internal dynamic buffer. For the short, int, and long versions (and 95*4c44e39aSPhilippe Houdoin * unsigned of each as well) byte order conversion is performed. 96*4c44e39aSPhilippe Houdoin * 97*4c44e39aSPhilippe Houdoin * Returns B_OK if successful, B_ERROR otherwise. 98*4c44e39aSPhilippe Houdoin */ 99*4c44e39aSPhilippe Houdoin status_t RemoveInt8(int8 &); 100*4c44e39aSPhilippe Houdoin status_t RemoveUint8(uint8 &); 101*4c44e39aSPhilippe Houdoin status_t RemoveInt16(int16 &); 102*4c44e39aSPhilippe Houdoin status_t RemoveUint16(uint16 &); 103*4c44e39aSPhilippe Houdoin status_t RemoveInt32(int32 &); 104*4c44e39aSPhilippe Houdoin status_t RemoveUint32(uint32 &); 105*4c44e39aSPhilippe Houdoin status_t RemoveFloat(float &); 106*4c44e39aSPhilippe Houdoin status_t RemoveDouble(double &); 107*4c44e39aSPhilippe Houdoin status_t RemoveString(char *, size_t); 108*4c44e39aSPhilippe Houdoin status_t RemoveData(void *, size_t); 109*4c44e39aSPhilippe Houdoin status_t RemoveMessage(BMessage &); 110*4c44e39aSPhilippe Houdoin status_t RemoveInt64(int64 &); 111*4c44e39aSPhilippe Houdoin status_t RemoveUint64(uint64 &); 112*4c44e39aSPhilippe Houdoin 113*4c44e39aSPhilippe Houdoin /* 114*4c44e39aSPhilippe Houdoin * GetData() returns a pointer to the internal data buffer. This is useful 115*4c44e39aSPhilippe Houdoin * when wanting to pass the data to a function expecting a pointer to data. 116*4c44e39aSPhilippe Houdoin * GetSize() returns the size of the BNetPacket's data, in bytes. 117*4c44e39aSPhilippe Houdoin * GetBytesRemaining() returns the bytes remaining in the BNetPacket available 118*4c44e39aSPhilippe Houdoin * to be extracted via BNetPacket::Remove*(). 119*4c44e39aSPhilippe Houdoin */ 120*4c44e39aSPhilippe Houdoin unsigned char *Data() const; 121*4c44e39aSPhilippe Houdoin size_t Size() const; 122*4c44e39aSPhilippe Houdoin size_t BytesRemaining() const; 123*4c44e39aSPhilippe Houdoin 124*4c44e39aSPhilippe Houdoin inline NLPacket *GetImpl() const; 125*4c44e39aSPhilippe Houdoin 126*4c44e39aSPhilippe Houdoin protected: 127*4c44e39aSPhilippe Houdoin status_t m_init; 128*4c44e39aSPhilippe Houdoin 129*4c44e39aSPhilippe Houdoin private: 130*4c44e39aSPhilippe Houdoin virtual void _ReservedBNetBufferFBCCruft1(); 131*4c44e39aSPhilippe Houdoin virtual void _ReservedBNetBufferFBCCruft2(); 132*4c44e39aSPhilippe Houdoin virtual void _ReservedBNetBufferFBCCruft3(); 133*4c44e39aSPhilippe Houdoin virtual void _ReservedBNetBufferFBCCruft4(); 134*4c44e39aSPhilippe Houdoin virtual void _ReservedBNetBufferFBCCruft5(); 135*4c44e39aSPhilippe Houdoin virtual void _ReservedBNetBufferFBCCruft6(); 136*4c44e39aSPhilippe Houdoin 137*4c44e39aSPhilippe Houdoin int32 __ReservedBNetBufferFBCCruftData[9]; 138*4c44e39aSPhilippe Houdoin 139*4c44e39aSPhilippe Houdoin }; 140*4c44e39aSPhilippe Houdoin 141*4c44e39aSPhilippe Houdoin 142*4c44e39aSPhilippe Houdoin inline NLPacket *BNetBuffer::GetImpl() const 143*4c44e39aSPhilippe Houdoin { 144*4c44e39aSPhilippe Houdoin return NULL; // No Nettle backward compatibility 145*4c44e39aSPhilippe Houdoin } 146*4c44e39aSPhilippe Houdoin 147*4c44e39aSPhilippe Houdoin #endif // _NETBUFFER_H 148*4c44e39aSPhilippe Houdoin 149