|
MemCacheClient
|
Socket connection, disconnection, and buffered data receives. More...
#include <Socket.h>
Classes | |
| class | Exception |
| Exception thrown on any send or receive error. More... | |
Public Types | |
| enum | ErrorCategory { ERR_OTHER = 0, ERR_DNS, ERR_CONNECT, ERR_SEND, ERR_RECV } |
Public Member Functions | |
| Socket (const ClTrace &aTrace) | |
| constructor | |
| ~Socket () | |
| destructor | |
| void | Connect (const char *aIpAddress, int aPort) |
| connect the socket to an address | |
| bool | IsConnected () const |
| Determine if we are currently connected to a server. | |
| void | Disconnect () |
| Disconnect from the server. | |
| void | SendBytes (const char *a_pszBuf, size_t a_nBufSiz) |
| Send the supplied bytes to the server. | |
| int | GetBytes (char *a_pszBuf, int a_nBufSiz) |
| Receive a block of data from the server. | |
| void | DiscardBytes (int a_nBytes) |
| Receive and discard bytes. | |
| char | GetByte () |
| Receive a single byte. | |
| void | ReceiveLine (std::string &a_sLine, bool aTrim) |
| Receive a line of text ending in or . | |
Static Public Member Functions | |
| static bool | Resolve (const char *aHost, std::vector< std::string > &aAddress) |
| Resolve a name to an IPv4 IP address. This will return a global address in preference to returning a localhost loopback address. It will throw an exception on error, or when multiple global IP addresses are present. | |
| static void | GetLastError (int aError, WideString &aErrorMsg) |
Public Attributes | |
| int | mConnectTimeout |
| timeout for connect in millisec | |
| int | mSendTimeout |
| timeout for send in millisec | |
| int | mRecvTimeout |
| timeout for recv in millisec | |
| int | mBufferSize |
| minimum socket buffer size in bytes | |
Protected Types | |
| typedef int | SOCKET |
Protected Member Functions | |
| int | ReceiveBytes (char *a_pszBuf, int a_nBufSiz) |
| Receive data into a buffer with as many bytes as possible up to the size of the buffer. | |
Protected Attributes | |
| ClTrace | mTrace |
| trace output | |
| SOCKET | mSocket |
| socket being abstracted | |
| char | mBuf [MAXBUF] |
| internal data buffer | |
| int | mIdx |
| current read index for mBuf | |
| int | mBufLen |
| current end index for mBuf | |
Static Protected Attributes | |
| static const int | MAXBUF = 1024 |
| size of internal data buffer | |
Private Member Functions | |
| Socket (const Socket &) | |
| copy constructor is disabled | |
| Socket & | operator= (const Socket &) |
| copy operator is disabled | |
| void | connectSocket (SOCKET aSocket, const sockaddr_in *aServer) |
| void | waitForConnect (SOCKET aSocket) |
| void | setSocketOptions (SOCKET aSocket) |
| void | setSocketBufferSize (SOCKET aSocket) |
| void | setSocketBlocking (SOCKET aSocket, bool aBlocking) |
| int | setSocketTimeout (SOCKET aSocket, int aType, int aTimeout) |
Socket connection, disconnection, and buffered data receives.
| void Socket::Connect | ( | const char * | aIpAddress, |
| int | aPort | ||
| ) |
connect the socket to an address
| a_nIpAddress | Server IP address (must be IPv4 dotted numeric) |
| a_nPort | Server port |
Definition at line 419 of file Socket.cpp.
References Disconnect(), GetLastError(), mBufferSize, Socket::Exception::mCode, Socket::Exception::mDetail, mRecvTimeout, mSendTimeout, mSocket, and mTrace.
Referenced by MemCacheClient::Server::Connect().
| void Socket::DiscardBytes | ( | int | a_nBytes | ) |
Receive and discard bytes.
| a_nBytes | Number of bytes to be received and ignored. |
| Exception | on socket error |
Definition at line 600 of file Socket.cpp.
References MAXBUF, mBuf, mBufLen, mIdx, and ReceiveBytes().
Referenced by MemCacheClient::HandleGetResponse().
| char Socket::GetByte | ( | ) |
Receive a single byte.
| Exception | on socket error |
Definition at line 561 of file Socket.cpp.
References MAXBUF, mBuf, mBufLen, mIdx, mTrace, and ReceiveBytes().
Referenced by MemCacheClient::FlushAll(), MemCacheClient::HandleGetResponse(), MemCacheClient::IncDec(), and ReceiveLine().
| int Socket::GetBytes | ( | char * | a_pszBuf, |
| int | a_nBufSiz | ||
| ) |
Receive a block of data from the server.
| a_pszBuf | Buffer to receive the data into |
| a_nBufSiz | Maximum number of bytes to be received |
| Exception | on socket error |
Definition at line 574 of file Socket.cpp.
References mBuf, mBufLen, mIdx, mTrace, and ReceiveBytes().
Referenced by MemCacheClient::HandleGetResponse().
| void Socket::GetLastError | ( | int | aError, |
| WideString & | aErrorMsg | ||
| ) | [static] |
return the last socket error
Definition at line 643 of file Socket.cpp.
Referenced by Connect(), ReceiveBytes(), and SendBytes().
| int Socket::ReceiveBytes | ( | char * | a_pszBuf, |
| int | a_nBufSiz | ||
| ) | [protected] |
Receive data into a buffer with as many bytes as possible up to the size of the buffer.
| a_pszBuf | Buffer to be filled |
| a_nBufSiz | Maximum number of bytes to receive |
Definition at line 524 of file Socket.cpp.
References Disconnect(), GetLastError(), mSocket, and mTrace.
Referenced by DiscardBytes(), GetByte(), and GetBytes().
| void Socket::ReceiveLine | ( | std::string & | a_sLine, |
| bool | aTrim | ||
| ) |
Receive a line of text ending in
or
.
| aTrim | Remove all trailing whitespace from the returned data. |
| Exception | on socket error |
Definition at line 622 of file Socket.cpp.
References GetByte().
Referenced by MemCacheClient::HandleDelResponse(), MemCacheClient::HandleGetResponse(), and MemCacheClient::HandleStoreResponse().
| bool Socket::Resolve | ( | const char * | aHost, |
| std::vector< std::string > & | aAddress | ||
| ) | [static] |
Resolve a name to an IPv4 IP address. This will return a global address in preference to returning a localhost loopback address. It will throw an exception on error, or when multiple global IP addresses are present.
| aHost | DNS name to resolve |
| aBuf | Buffer to return IP address |
| aBufSiz | Size of buffer |
Definition at line 65 of file Socket.cpp.
| void Socket::SendBytes | ( | const char * | a_pszBuf, |
| size_t | a_nBufSiz | ||
| ) |
Send the supplied bytes to the server.
A blocking send is used, so the function will block until either all bytes are sent or an error occurs.
| a_pszBuf | Bytes to send to the server |
| a_nBufSiz | Number of bytes to send to the server |
| Exception | on socket error |
Definition at line 481 of file Socket.cpp.
References Disconnect(), GetLastError(), mSocket, and mTrace.
Referenced by MemCacheClient::Combine(), MemCacheClient::FlushAll(), MemCacheClient::IncDec(), and MemCacheClient::Store().
1.7.3