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
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef AbstractSocketH
00041 #define AbstractSocketH
00042
00043
00044 #include <zeusbase/System/ZObject.h>
00045
00046 #if defined(ENABLE_LINUX_BINDING)
00047 #include <sys/types.h>
00048 #include <sys/socket.h>
00049 #include <netinet/in.h>
00050 #include <arpa/inet.h>
00051 #elif defined(ENABLE_WIN32_BINDING)
00052 #if defined(USE_WINDOWS_H)
00053 #include <windows.h>
00054 #if __BCPLUSPLUS__ < 0x0570
00055 #include <winsock.h>
00056 #endif
00057 #endif
00058 #endif
00059
00060
00061 BEGIN_NAMESPACE_Zeus
00062
00063 class TIPAddress;
00064 class TCriticalSection;
00065
00066
00072
00073 zeusbase_class TAbstractSocket : public TZObject
00074 {
00075 public:
00076 TAbstractSocket(TIPAddress& rAddress);
00077 TAbstractSocket(const TString& rstrAddr, Uint uiPort);
00078 TAbstractSocket(Int iFD, struct sockaddr_in& rAddress);
00079
00080 virtual Retval connect() = 0;
00081 virtual Retval disconnect();
00082
00083 TString getAddress() const;
00084 TString getHostName() const;
00085 Uint getPort() const;
00086 Int getReceiveBufferSize() const;
00087 Int getSendBufferSize() const;
00088 bool isConnected() const;
00089 bool isBlockable() const;
00090 bool isReUsable() const;
00091 bool isValid() const;
00092
00093 void setBlockable(bool bMode);
00094 void setReceiveBufferSize(Int iSize);
00095 void setReUsable(bool bMode);
00096 void setSendBufferSize(Int iSize);
00097
00098 protected:
00099 TAbstractSocket();
00100 virtual ~TAbstractSocket();
00101
00102 static Retval getErrorCode(bool bHandleBlockedAsError);
00103 static bool getBoolSocketOption(Int iFD, Int iOption, Int iLevel);
00104 static Int getIntSocketOption(Int iFD, Int iOption, Int iLevel);
00105 static void setBoolSocketOption(Int iFD, Int iOption, Int iLevel, bool bValue);
00106 static void setIntSocketOption(Int iFD, Int iOption, Int iLevel, Int iValue);
00107
00109 TIPAddress* m_pAddress;
00111 bool m_bConnected;
00113 bool m_bBlockable;
00114
00116 Int m_iSocketFD;
00118 TCriticalSection& m_rSocketLock;
00119
00120 private:
00122 static Int m_iObjectCount;
00123
00124 void initSocket();
00125 };
00126
00127
00128
00129
00133
00134 inline Int TAbstractSocket::getReceiveBufferSize() const
00135 {
00136 return TAbstractSocket::getIntSocketOption(m_iSocketFD, SO_RCVBUF, SOL_SOCKET);
00137 }
00138
00139
00143
00144 inline Int TAbstractSocket::getSendBufferSize() const
00145 {
00146 return TAbstractSocket::getIntSocketOption(m_iSocketFD, SO_SNDBUF, SOL_SOCKET);
00147 }
00148
00149
00154
00155 inline bool TAbstractSocket::isBlockable() const
00156 {
00157 return m_bBlockable;
00158 }
00159
00160
00165
00166 inline bool TAbstractSocket::isReUsable() const
00167 {
00168 return TAbstractSocket::getBoolSocketOption(m_iSocketFD, SO_REUSEADDR, SOL_SOCKET);
00169 }
00170
00171
00175
00176 inline void TAbstractSocket::setReceiveBufferSize(Int iSize)
00177 {
00178 if (iSize > 0)
00179 {
00180 TAbstractSocket::setIntSocketOption(m_iSocketFD, SO_RCVBUF, SOL_SOCKET, iSize);
00181 }
00182 }
00183
00184
00190
00191 inline void TAbstractSocket::setReUsable(bool bMode)
00192 {
00193 TAbstractSocket::setBoolSocketOption(m_iSocketFD, SO_REUSEADDR, SOL_SOCKET, bMode);
00194 }
00195
00196
00200
00201 inline void TAbstractSocket::setSendBufferSize(Int iSize)
00202 {
00203 if (iSize > 0)
00204 {
00205 TAbstractSocket::setIntSocketOption(m_iSocketFD, SO_SNDBUF, SOL_SOCKET, iSize);
00206 }
00207 }
00208
00209
00210 END_NAMESPACE_Zeus
00211
00212
00213 #endif