Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef AbstractSocketH
00033 #define AbstractSocketH
00034
00035
00036 #include <zeusbase/System/ZObject.h>
00037
00038 #if defined(ENABLE_LINUX_BINDING)
00039 #include <sys/types.h>
00040 #include <sys/socket.h>
00041 #include <netinet/in.h>
00042 #include <arpa/inet.h>
00043 #include <netinet/tcp.h>
00044 #elif defined(ENABLE_WIN32_BINDING)
00045 #if defined(USE_WINDOWS_H)
00046 #include <windows.h>
00047 #endif
00048
00049 #if defined(ENABLE_IPV6_BINDING)
00050 #include <winsock2.h>
00051 #else
00052 #include <winsock.h>
00053 #endif
00054
00055 #if __BCPLUSPLUS__ >= 0x0610
00056 #pragma link "Ws2_32.lib"
00057 #endif
00058
00059 #endif
00060
00061
00062 BEGIN_NAMESPACE_Zeus
00063
00064 class TIPAddress;
00065 class TCriticalSection;
00066
00067
00073
00074 zeusbase_class TAbstractSocket : public TZObject
00075 {
00076 public:
00077 TAbstractSocket(TIPAddress& rAddress);
00078 TAbstractSocket(const TString& rstrAddr, Uint uiPort);
00079 TAbstractSocket(Int iFD, struct sockaddr_in& rAddress);
00080
00081 virtual Retval connect() = 0;
00082 virtual Retval disconnect();
00083
00084 TString getAddress() const;
00085 TString getHostName() const;
00086 Uint getPort() const;
00087 Int getReceiveBufferSize() const;
00088 Int getSendBufferSize() const;
00089 Int getSocketDescripitor() const;
00090 bool isConnected() const;
00091 bool isBlockable() const;
00092 bool isReUsable() const;
00093 bool isValid() const;
00094
00095 void setBlockable(bool bMode);
00096 void setReceiveBufferSize(Int iSize);
00097 void setReUsable(bool bMode);
00098 void setSendBufferSize(Int iSize);
00099 void setTCPBufferEnable(bool bMode);
00100
00101 protected:
00102 TAbstractSocket();
00103 virtual ~TAbstractSocket();
00104 virtual void openSocket() = 0;
00105
00106 static Retval getErrorCode(bool bHandleBlockedAsError);
00107 static bool getBoolSocketOption(Int iFD, Int iOption, Int iLevel);
00108 static Int getIntSocketOption(Int iFD, Int iOption, Int iLevel);
00109 static void setBoolSocketOption(Int iFD, Int iOption, Int iLevel, bool bValue);
00110 static void setIntSocketOption(Int iFD, Int iOption, Int iLevel, Int iValue);
00111
00113 TIPAddress* m_pAddress;
00115 bool m_bConnected;
00117 bool m_bBlockable;
00119 bool m_bReUsable;
00120
00122 Int m_iSocketFD;
00124 TCriticalSection& m_rSocketLock;
00125
00126 private:
00128 static Int m_iObjectCount;
00129
00130 void initSocket();
00131 };
00132
00133
00134
00135
00138
00139 inline Int TAbstractSocket::getSocketDescripitor() const
00140 {
00141 return m_iSocketFD;
00142 }
00143
00144
00148
00149 inline Int TAbstractSocket::getReceiveBufferSize() const
00150 {
00151 return TAbstractSocket::getIntSocketOption(m_iSocketFD, SO_RCVBUF, SOL_SOCKET);
00152 }
00153
00154
00158
00159 inline Int TAbstractSocket::getSendBufferSize() const
00160 {
00161 return TAbstractSocket::getIntSocketOption(m_iSocketFD, SO_SNDBUF, SOL_SOCKET);
00162 }
00163
00164
00169
00170 inline bool TAbstractSocket::isBlockable() const
00171 {
00172 return m_bBlockable;
00173 }
00174
00175
00180
00181 inline bool TAbstractSocket::isReUsable() const
00182 {
00183 return TAbstractSocket::getBoolSocketOption(m_iSocketFD, SO_REUSEADDR, SOL_SOCKET);
00184 }
00185
00186
00190
00191 inline void TAbstractSocket::setReceiveBufferSize(Int iSize)
00192 {
00193 if (iSize > 0)
00194 {
00195 TAbstractSocket::setIntSocketOption(m_iSocketFD, SO_RCVBUF, SOL_SOCKET, iSize);
00196 }
00197 }
00198
00199
00205
00206 inline void TAbstractSocket::setReUsable(bool bMode)
00207 {
00208 m_bReUsable = bMode;
00209 TAbstractSocket::setBoolSocketOption(m_iSocketFD, SO_REUSEADDR, SOL_SOCKET, bMode);
00210 }
00211
00212
00216
00217 inline void TAbstractSocket::setSendBufferSize(Int iSize)
00218 {
00219 if (iSize > 0)
00220 {
00221 TAbstractSocket::setIntSocketOption(m_iSocketFD, SO_SNDBUF, SOL_SOCKET, iSize);
00222 }
00223 }
00224
00225
00230
00231 inline void TAbstractSocket::setTCPBufferEnable(bool bMode)
00232 {
00233 TAbstractSocket::setBoolSocketOption(m_iSocketFD, TCP_NODELAY, IPPROTO_TCP, !bMode);
00234 }
00235
00236
00237
00238 END_NAMESPACE_Zeus
00239
00240
00241 #endif