|
MemCacheClient
|
00001 00006 #ifndef INCLUDED_Socket 00007 #define INCLUDED_Socket 00008 00009 #if defined(_MSC_VER) && (_MSC_VER >= 1020) 00010 # pragma once 00011 #endif 00012 00013 #include <vector> 00014 00015 #ifdef _WIN32 00016 # include <winsock2.h> 00017 #else 00018 # include <sys/socket.h> 00019 # include <arpa/inet.h> 00020 #endif 00021 00022 #ifdef CROSSBASE_API 00023 # include <Core/clplatform.h> 00024 # include <Trace/cltrace.h> 00025 START_CL_NAMESPACE 00026 #else 00027 # include "Matilda.h" 00028 #endif 00029 00032 class CROSSBASE_CLASS_API Socket 00033 { 00034 protected: 00035 const static int MAXBUF = 1024; 00036 #ifndef _WIN32 00037 typedef int SOCKET; 00038 #endif 00039 00040 ClTrace mTrace; 00041 SOCKET mSocket; 00042 char mBuf[MAXBUF]; 00043 int mIdx; 00044 int mBufLen; 00045 00046 public: 00047 int mConnectTimeout; 00048 int mSendTimeout; 00049 int mRecvTimeout; 00050 int mBufferSize; 00051 00052 private: 00054 Socket(const Socket &); 00055 00057 Socket & operator=(const Socket &); 00058 00059 void connectSocket(SOCKET aSocket, const sockaddr_in * aServer); 00060 void waitForConnect(SOCKET aSocket); 00061 void setSocketOptions(SOCKET aSocket); 00062 void setSocketBufferSize(SOCKET aSocket); 00063 void setSocketBlocking(SOCKET aSocket, bool aBlocking); 00064 int setSocketTimeout(SOCKET aSocket, int aType, int aTimeout); 00065 00066 protected: 00073 int ReceiveBytes(char * a_pszBuf, int a_nBufSiz); 00074 00075 public: 00077 enum ErrorCategory { 00078 ERR_OTHER = 0, 00079 ERR_DNS, // connection error 00080 ERR_CONNECT, // connection error 00081 ERR_SEND, // comms error 00082 ERR_RECV, // comms error 00083 }; 00084 00086 class Exception : public std::exception { 00087 public: 00088 ErrorCategory mType; 00089 int mCode; 00090 const char * mDetail; 00091 00095 Exception(ErrorCategory aType = ERR_OTHER, int aCode = 0, const char * aDetail = "") 00096 : mType(aType), mCode(aCode), mDetail(aDetail) 00097 { } 00098 }; 00099 00107 static bool Resolve(const char * aHost, std::vector<std::string> & aAddress); // throw Exception 00108 00110 static void GetLastError( 00111 int aError, 00112 WideString & aErrorMsg 00113 ); 00114 00115 public: 00117 Socket(const ClTrace & aTrace); 00118 00120 ~Socket(); 00121 00126 void Connect(const char * aIpAddress, int aPort); // throw Exception 00127 00129 bool IsConnected() const; 00130 00132 void Disconnect(); 00133 00143 void SendBytes(const char * a_pszBuf, size_t a_nBufSiz); // throw Exception 00144 00151 int GetBytes(char * a_pszBuf, int a_nBufSiz); // throw Exception 00152 00157 void DiscardBytes(int a_nBytes); // throw Exception 00158 00163 char GetByte(); 00164 00169 void ReceiveLine(std::string & a_sLine, bool aTrim); // throw Exception 00170 }; 00171 00172 #ifdef CROSSBASE_API 00173 END_CL_NAMESPACE 00174 #endif 00175 00176 #endif // INCLUDED_Socket
1.7.3