00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : DatagramPacket 00006 * Package : Zeus.ZeusBase.Net 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef DatagramPacketH 00033 #define DatagramPacketH 00034 00035 #include <zeusbase/System/ZObject.h> 00036 #include <zeusbase/System/ArrayPtr.hpp> 00037 #include <zeusbase/System/ByteArray.hpp> 00038 00039 BEGIN_NAMESPACE_Zeus 00040 00041 class TIPAddress; 00042 00043 /***************************************************************************/ 00048 /***************************************************************************/ 00049 zeusbase_class TDatagramPacket 00050 { 00051 public: 00052 TDatagramPacket(); 00053 TDatagramPacket(Int iSize); 00054 TDatagramPacket(const TByteArray& rData); 00055 TDatagramPacket(const TString& rTargetAddress, Uint uiPort, Int iSize = 128); 00056 00057 virtual ~TDatagramPacket(); 00058 00060 //this method is used to access the data buffer for sockets 00061 // use setData to change the content 00062 char* getData(); 00064 00065 const char* getData() const; 00066 Int getDataSize() const; 00067 Retval getAddress(TIPAddress*& rpAddress) const; 00068 00069 void setAddress(TIPAddress& rAddress); 00070 void setAddress(const TString& rTargetAddress, Uint uiPort); 00071 void setData(const TByteArray& rData); 00072 00073 protected: 00074 00075 private: 00077 TArrayPtr<char> m_ptrData; 00079 TIPAddress* m_pAddress; 00080 00081 void allocDataBuffer(Int iSize); 00082 }; 00083 00084 //INLINE METHODS 00085 /***************************************************************************/ 00089 /***************************************************************************/ 00090 inline char* TDatagramPacket::getData() 00091 { 00092 return m_ptrData; 00093 } 00094 00095 /***************************************************************************/ 00098 /***************************************************************************/ 00099 inline const char* TDatagramPacket::getData() const 00100 { 00101 return m_ptrData; 00102 } 00103 00104 /***************************************************************************/ 00107 /***************************************************************************/ 00108 inline Int TDatagramPacket::getDataSize() const 00109 { 00110 return m_ptrData.getSize(); 00111 } 00112 00113 /***************************************************************************/ 00116 /***************************************************************************/ 00117 inline void TDatagramPacket::allocDataBuffer(Int iSize) 00118 { 00119 if (iSize <= 0) 00120 { 00121 iSize = 128; 00122 } 00123 m_ptrData.assign(new char[iSize], iSize); 00124 } 00125 00126 00127 END_NAMESPACE_Zeus 00128 00129 #endif