I/O handler for TCP sockets. More...
#include <IOHandlerData.h>
Public Member Functions | |
IOHandlerData (int sd, const InetAddr &addr, const DispatchHandlerPtr &dhp, bool connected=false) | |
Constructor. More... | |
virtual | ~IOHandlerData () |
Destructor. More... | |
void | disconnect () override |
Disconnects handler by delivering Event::DISCONNECT via default dispatch handler. More... | |
void | reset_incoming_message_state () |
Resets incoming message buffer state in preparation for next message. More... | |
void | free_message_buffer () |
Frees the message buffer (m_message). More... | |
int | send_message (CommBufPtr &cbp, uint32_t timeout_ms=0, DispatchHandler *disp_handler=nullptr) |
Sends message pointed to by cbp over socket associated with this I/O handler. More... | |
int | flush_send_queue () |
Flushes send queue. More... | |
bool | handle_event (struct pollfd *event, ClockT::time_point arrival_time) override |
Handle poll() interface events. More... | |
bool | handle_write_readiness () |
Handles write readiness by completing connection and flushing send queue. More... | |
Public Member Functions inherited from Hypertable::IOHandler | |
IOHandler (int sd, const DispatchHandlerPtr &dhp, Reactor::Priority rp=Reactor::Priority::NORMAL) | |
Constructor. More... | |
IOHandler (int sd) | |
Constructor. More... | |
virtual | ~IOHandler () |
Destructor. More... | |
void | deliver_event (EventPtr &event, DispatchHandler *dh=0) |
Convenience method for delivering event to application. More... | |
int | start_polling (int mode=PollEvent::READ) |
Start polling on the handler with the poll interest specified in mode . More... | |
int | add_poll_interest (int mode) |
Adds the poll interest specified in mode to the polling interface for this handler. More... | |
int | remove_poll_interest (int mode) |
Removes the poll interest specified in mode to the polling interface for this handler. More... | |
int | reset_poll_interest () |
Resets poll interest by adding m_poll_interest to the polling interface for this handler. More... | |
InetAddr | get_address () |
Gets the handler socket address. More... | |
InetAddr | get_local_address () |
Get local socket address for connection. More... | |
void | set_proxy (const String &proxy) |
Sets the proxy name for this connection. More... | |
const String & | get_proxy () |
Gets the proxy name for this connection. More... | |
int | get_sd () |
Gets the socket descriptor for this connection. More... | |
void | get_reactor (ReactorPtr &reactor) |
Get the reactor that this handler is assigned to. More... | |
void | display_event (struct pollfd *event) |
Display polling event from poll() interface to stderr. More... | |
Public Attributes | |
ImplementMe | |
Private Member Functions | |
void | handle_message_header (ClockT::time_point arrival_time) |
Processes a message header. More... | |
void | handle_message_body () |
Processes a message body. More... | |
void | handle_disconnect () |
Decomissions the handler. More... | |
Private Attributes | |
bool | m_connected |
Flag indicating if socket connection has been completed. More... | |
bool | m_got_header {} |
Flag indicating if message header has been completely received. More... | |
bool | m_message_aligned {} |
Flag indicating if message buffer was allocated with posix_memalign() More... | |
EventPtr | m_event |
Pointer to Event object holding message to deliver to application. More... | |
uint8_t | m_message_header [64] |
Message header buffer. More... | |
uint8_t * | m_message_header_ptr {} |
Pointer to next write position in m_message_header. More... | |
size_t | m_message_header_remaining |
Amount of header remaining to be read. More... | |
uint8_t * | m_message {} |
Poiner to message payload buffer. More... | |
uint8_t * | m_message_ptr {} |
Pointer to next write position in m_message. More... | |
size_t | m_message_remaining {} |
Amount of message payload remaining to be read. More... | |
std::list< CommBufPtr > | m_send_queue |
Send queue. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from Hypertable::IOHandler | |
bool | test_and_set_error (int32_t error) |
Sets m_error to error if it has not already been set. More... | |
int32_t | get_error () |
Returns first error code encountered by handler. More... | |
InetAddr | get_alias () |
Get alias address for this connection. More... | |
void | set_alias (const InetAddr &alias) |
Set alias address for this connection. More... | |
void | increment_reference_count () |
Increment reference count. More... | |
void | decrement_reference_count () |
Decrement reference count. More... | |
size_t | reference_count () |
Return reference count. More... | |
void | decomission () |
Decomission handler. More... | |
bool | is_decomissioned () |
Checks to see if handler is decomissioned. More... | |
short | poll_events (int mode) |
Return poll() interface events corresponding to the normalized polling interest in mode . More... | |
void | stop_polling () |
Stops polling by removing socket from polling interface. More... | |
Protected Attributes inherited from Hypertable::IOHandler | |
std::mutex | m_mutex |
Mutex for serializing concurrent access More... | |
size_t | m_reference_count |
Reference count. More... | |
uint32_t | m_free_flag |
Free flag (for testing) More... | |
int32_t | m_error |
Error code. More... | |
String | m_proxy |
Proxy name for this connection. More... | |
InetAddr | m_addr |
Handler socket address. More... | |
InetAddr | m_local_addr |
Local address of connection. More... | |
InetAddr | m_alias |
Address alias for connection. More... | |
int | m_sd |
Socket descriptor. More... | |
DispatchHandlerPtr | m_dispatch_handler |
Default dispatch hander for connection. More... | |
ReactorPtr | m_reactor |
Reactor to which this handler is assigned. More... | |
int | m_poll_interest |
Current polling interest. More... | |
bool | m_decomissioned |
Decomissioned flag. More... | |
bool m_socket_internally_created | true |
Socket was internally created and should be closed on destroy. More... | |
I/O handler for TCP sockets.
Definition at line 51 of file IOHandlerData.h.
|
inline |
Constructor.
sd | Socket descriptor |
addr | Address of remote end of connection |
dhp | Default dispatch handler for connection |
connected | Initial connection state for handler |
Definition at line 61 of file IOHandlerData.h.
|
inlinevirtual |
Destructor.
Definition at line 70 of file IOHandlerData.h.
|
inlineoverridevirtual |
Disconnects handler by delivering Event::DISCONNECT via default dispatch handler.
Reimplemented from Hypertable::IOHandler.
Definition at line 75 of file IOHandlerData.h.
int Hypertable::IOHandlerData::flush_send_queue | ( | ) |
Flushes send queue.
When messages are sent, they are first added to a send queue (m_send_queue) and then the messages in the send queue are written over the socket by calling this method. This method attempts to write all of the messages in the queue and stops under the following conditions:
|
inline |
Frees the message buffer (m_message).
If m_message was allocated with posix_memalign(), as indicated by m_message_aligned, the free() function is used to deallocate the memory. Otherwise, the buffer is deallocated with delete []
Definition at line 97 of file IOHandlerData.h.
|
private |
Decomissions the handler.
Definition at line 641 of file IOHandlerData.cc.
|
overridevirtual |
Handle poll()
interface events.
This method is called by its reactor thread to handle I/O events. It handles POLLOUT
events with a call to handle_write_readiness. If handle_write_readiness returns true the handler is disconnected with a call to handle_disconnect and true is returned. POLLIN
events are handled by reading message data off the socket. First the message header is read and decoded with handle_message_header and then the message payload is read and delivered to the application with handle_message_body. If a read error is encountered, m_error is set to the approprate error code (if not already set) and the handler is disconnected with a call to handle_disconnect and true is returned. EOF, POLLERR
events, and POLLHUP
events are handled by disconnecting the handler with a call to handle_disconnect and true is returned. arrival_time
is passed into handle_message_header to be delivered to the applicaiton via the Event object.
event | Pointer to pollfd structure describing event |
arrival_time | Time of event arrival |
Implements Hypertable::IOHandler.
Definition at line 113 of file IOHandlerData.cc.
|
private |
Processes a message body.
This method is called when a message has been completely received (header + payload). It first checks to see if the message is a proxy update message and if so, it updates its proxy map with a call to HandlerMap::update_proxy_map and returns. Otherwise if it is a response message and the CommHeader::FLAGS_BIT_IGNORE_RESPONSE bit is not set in the header flags, the corresponding dispatch handler is removed form request queue and the message is delivered to the applicaton using that handler. Otherwise, the message is delivered to the application using the default dispatch handler. After the message has been delivered, the message receive state is reset with a call to reset_incoming_message_state.
Definition at line 603 of file IOHandlerData.cc.
|
private |
Processes a message header.
This method is called when the fixed length portion of a header has been completely received. It first checks to see if there is a variable portion of the header that has not yet been read, if so, it adjusts m_message_header_remaining and returns. If the header has been completely received, it allocates a new Event object and sets m_event pointing to it. It initializes the event object with the message header and arrival_time
. It then allocates the message payload buffer (m_message), initialzes the payload buffer pointers, and sets m_got_header to true.
Definition at line 566 of file IOHandlerData.cc.
bool IOHandlerData::handle_write_readiness | ( | ) |
Handles write readiness by completing connection and flushing send queue.
When a data handler is created after a call to connect
it is in the disconnected state. Once the socket becomes ready for writing, the connection request can be completed. This method handles the completion of the connection when the handler is in the disconnected state by doing the following:
4*32768
bytesDefinition at line 645 of file IOHandlerData.cc.
|
inline |
Resets incoming message buffer state in preparation for next message.
Definition at line 82 of file IOHandlerData.h.
int IOHandlerData::send_message | ( | CommBufPtr & | cbp, |
uint32_t | timeout_ms = 0 , |
||
DispatchHandler * | disp_handler = nullptr |
||
) |
Sends message pointed to by cbp
over socket associated with this I/O handler.
If the message being sent is a request message (has the CommHeader::FLAGS_BIT_REQUEST set) and disp_handler
is not 0, then an entry is added to the reactor's request cache so that disp_handler
will be called to handle the response or receive a TIMEOUT event if the response is not received within timeout_ms
milliseconds.
cbp | Reference to CommBufPtr pointing to message to send |
timeout_ms | Millisecond timeout used for request messages |
disp_handler | Dispatch handler used for request messages |
Definition at line 738 of file IOHandlerData.cc.
Hypertable::IOHandlerData::ImplementMe |
Definition at line 242 of file IOHandlerData.h.
|
private |
Flag indicating if socket connection has been completed.
Definition at line 299 of file IOHandlerData.h.
|
private |
Pointer to Event object holding message to deliver to application.
Definition at line 308 of file IOHandlerData.h.
|
private |
Flag indicating if message header has been completely received.
Definition at line 302 of file IOHandlerData.h.
|
private |
Poiner to message payload buffer.
Definition at line 320 of file IOHandlerData.h.
|
private |
Flag indicating if message buffer was allocated with posix_memalign()
Definition at line 305 of file IOHandlerData.h.
|
private |
Message header buffer.
Definition at line 311 of file IOHandlerData.h.
|
private |
Pointer to next write position in m_message_header.
Definition at line 314 of file IOHandlerData.h.
|
private |
Amount of header remaining to be read.
Definition at line 317 of file IOHandlerData.h.
|
private |
Pointer to next write position in m_message.
Definition at line 323 of file IOHandlerData.h.
|
private |
Amount of message payload remaining to be read.
Definition at line 326 of file IOHandlerData.h.
|
private |
Send queue.
Definition at line 329 of file IOHandlerData.h.