LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes
LogCabin::RPC::MessageSocket Class Reference

A MessageSocket is a message-oriented layer on top of a TCP connection. More...

#include <MessageSocket.h>

List of all members.

Classes

class  Handler
 An interface for handling events generated by a MessageSocket. More...
struct  Header
 This is the header that precedes every message across the TCP socket. More...
struct  Inbound
 This class stages a message while it is being received. More...
struct  Outbound
 This class stages a message while it is being sent. More...
struct  ReceiveSocket
 This class is an Event::File monitor that calls readable() when the socket can be read from without blocking. More...
struct  SendSocket
 This class is an Event::File monitor that calls writable() when the socket can be written to without blocking. More...

Public Types

enum  { MAX_VERSION_SUPPORTED }
 Largest version of the framing protocol supported by this code. More...
typedef uint64_t MessageId
 An opaque identifier for a message.

Public Member Functions

 MessageSocket (Handler &handler, Event::Loop &eventLoop, int fd, uint32_t maxMessageLength)
 Constructor.
 ~MessageSocket ()
 Destructor.
void close ()
 Used when the server wishes to close this socket.
void sendMessage (MessageId messageId, Core::Buffer contents)
 Queue a message to be sent to the other side of this socket.

Private Member Functions

void disconnect ()
 Cleans up and calls onDisconnect() when the socket has an error.
void readable ()
 Called when the socket has data that can be read without blocking.
ssize_t read (void *buf, size_t maxBytes)
 Wrapper around recv(); used by readable().
void writable ()
 Called when the socket may be written to without blocking.
 MessageSocket (const MessageSocket &)
MessageSocketoperator= (const MessageSocket &)

Private Attributes

const uint32_t maxMessageLength
 The maximum number of bytes of payload to allow per message.
Handlerhandler
 Deals with received messages and disconnects.
Event::LoopeventLoop
 Used to find out when the socket is readable or writable.
Inbound inbound
 The current message that is being received.
Core::Mutex outboundQueueMutex
 Protects outboundQueue only from concurrent modification.
std::deque< OutboundoutboundQueue
 A queue of messages waiting to be sent.
ReceiveSocket receiveSocket
 Notifies MessageSocket when the socket can be read from without blocking.
SendSocket sendSocket
 Notifies MessageSocket when the socket can be transmitted on without blocking.
Event::File::Monitor receiveSocketMonitor
 Registers receiveSocket with the event loop.
Event::File::Monitor sendSocketMonitor
 Registers sendSocket with the event loop.

Detailed Description

A MessageSocket is a message-oriented layer on top of a TCP connection.

It sends and receives discrete chunks of data identified by opaque IDs. Higher layers can use this to build an RPC framework, both on the client side and on the server side.

On the wire, this adds a 16-byte header on all messages: | 0xdaf4 | version | length | messageId | See Header for more details. Following the header, the data is sent as an opaque binary string.

Definition at line 47 of file MessageSocket.h.


Member Typedef Documentation

An opaque identifier for a message.

For RPCs, clients can use this to pair up a response with its request, and servers will want to reply with the same ID as the matching request.

Definition at line 54 of file MessageSocket.h.


Member Enumeration Documentation

anonymous enum

Largest version of the framing protocol supported by this code.

Enumerator:
MAX_VERSION_SUPPORTED 

Definition at line 59 of file MessageSocket.h.


Constructor & Destructor Documentation

LogCabin::RPC::MessageSocket::MessageSocket ( Handler handler,
Event::Loop eventLoop,
int  fd,
uint32_t  maxMessageLength 
)

Constructor.

Parameters:
handlerHandles received messages and disconnect events.
eventLoopEvent::Loop that will be used to find out when the socket is readable or writable.
fdConnected file descriptor for the socket. This object will close the file descriptor when it is disconnected.
maxMessageLengthThe maximum number of bytes of payload to allow per message. This exists to limit the amount of buffer space a single socket can use. Attempting to send longer messages will PANIC; attempting to receive longer messages will disconnect the socket.

Definition at line 174 of file MessageSocket.cc.

Destructor.

Definition at line 190 of file MessageSocket.cc.


Member Function Documentation

Used when the server wishes to close this socket.

It invokes Handler::handleDisconnect(), as if the client has disconnected. After this returns, the Handler will not be called again.

Definition at line 195 of file MessageSocket.cc.

Queue a message to be sent to the other side of this socket.

This method is safe to call from any thread.

Parameters:
messageIdAn opaque identifier for the message.
contentsThe data to send. This must be shorter than the maxMessageLength argument given to the constructor.

Definition at line 207 of file MessageSocket.cc.

Cleans up and calls onDisconnect() when the socket has an error.

Only called from event loop handlers.

Definition at line 228 of file MessageSocket.cc.

Called when the socket has data that can be read without blocking.

Definition at line 238 of file MessageSocket.cc.

ssize_t LogCabin::RPC::MessageSocket::read ( void *  buf,
size_t  maxBytes 
) [private]

Wrapper around recv(); used by readable().

Parameters:
bufWhere to store the data received.
maxBytesThe maximum number of bytes to receive and store into buf.
Returns:
The number of bytes read (<= maxBytes), if successful. The value -1 indicates that the socket was disconnected, in which case the caller must be careful not to access this object and immediately return.

Definition at line 312 of file MessageSocket.cc.

Called when the socket may be written to without blocking.

Definition at line 327 of file MessageSocket.cc.

MessageSocket& LogCabin::RPC::MessageSocket::operator= ( const MessageSocket ) [private]

Member Data Documentation

The maximum number of bytes of payload to allow per message.

This exists to limit the amount of buffer space a single socket can use.

Definition at line 297 of file MessageSocket.h.

Deals with received messages and disconnects.

Definition at line 302 of file MessageSocket.h.

Used to find out when the socket is readable or writable.

Definition at line 307 of file MessageSocket.h.

The current message that is being received.

Definition at line 312 of file MessageSocket.h.

Protects outboundQueue only from concurrent modification.

Definition at line 317 of file MessageSocket.h.

A queue of messages waiting to be sent.

The first one may be in the middle of transmission, while the others have not yet started. This queue is protected from concurrent modifications by outboundQueueMutex.

It's important that this remains a std::deque (or std::queue) because writable() holds a pointer to the first element without the lock, while sendMessage() may concurrently push onto the queue. std::deques are guaranteed not to invalidate pointers while elements are pushed and popped from the ends.

Definition at line 330 of file MessageSocket.h.

Notifies MessageSocket when the socket can be read from without blocking.

Definition at line 336 of file MessageSocket.h.

Notifies MessageSocket when the socket can be transmitted on without blocking.

Definition at line 342 of file MessageSocket.h.

Registers receiveSocket with the event loop.

Definition at line 347 of file MessageSocket.h.

Registers sendSocket with the event loop.

Definition at line 352 of file MessageSocket.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines