MemCacheClient
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions

Socket Class Reference

Socket connection, disconnection, and buffered data receives. More...

#include <Socket.h>

Inheritance diagram for Socket:
MemCacheClient::Server

List of all members.

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
Socketoperator= (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)

Detailed Description

Socket connection, disconnection, and buffered data receives.

Definition at line 32 of file Socket.h.


Member Enumeration Documentation

socket error category

Definition at line 77 of file Socket.h.


Member Function Documentation

void Socket::Connect ( const char *  aIpAddress,
int  aPort 
)

connect the socket to an address

Parameters:
a_nIpAddressServer IP address (must be IPv4 dotted numeric)
a_nPortServer 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.

Parameters:
a_nBytesNumber of bytes to be received and ignored.
Exceptions:
Exceptionon 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.

Returns:
received byte
Exceptions:
Exceptionon 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.

Parameters:
a_pszBufBuffer to receive the data into
a_nBufSizMaximum number of bytes to be received
Returns:
number of bytes that were actually received
Exceptions:
Exceptionon 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.

Parameters:
a_pszBufBuffer to be filled
a_nBufSizMaximum number of bytes to receive
Returns:
number of bytes actually received

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
.

Parameters:
aTrimRemove all trailing whitespace from the returned data.
Exceptions:
Exceptionon 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.

Parameters:
aHostDNS name to resolve
aBufBuffer to return IP address
aBufSizSize 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.

Parameters:
a_pszBufBytes to send to the server
a_nBufSizNumber of bytes to send to the server
Exceptions:
Exceptionon 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().


The documentation for this class was generated from the following files: