Base class for socket descriptor I/O handlers. More...
#include <IOHandler.h>
Public Member Functions | |
IOHandler (int sd, const DispatchHandlerPtr &dhp, Reactor::Priority rp=Reactor::Priority::NORMAL) | |
Constructor. More... | |
IOHandler (int sd) | |
Constructor. More... | |
virtual bool | handle_event (struct pollfd *event, ClockT::time_point arrival_time)=0 |
Event handler method for Unix poll interface. 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... | |
Protected Member Functions | |
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... | |
virtual void | disconnect () |
Disconnect connection. 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 | |
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... | |
Friends | |
class | HandlerMap |
Base class for socket descriptor I/O handlers.
When a socket is created, an I/O handler object is allocated to handle events that occur on the socket. Events are encapsulated in the Event class and are delivered to the application through a DispatchHandler. For example, a TCP socket will have an associated IOHandlerData object that reads messages off the socket and sends then to the application via the installed DispatchHandler object.
Definition at line 76 of file IOHandler.h.
|
inline |
Constructor.
Initializes the I/O handler, assigns it a Reactor, and sets m_local_addr to the locally bound address (IPv4:port) of sd
(see getsockname
).
sd | Socket descriptor |
dhp | Dispatch handler |
Definition at line 87 of file IOHandler.h.
|
inline |
Constructor.
Initializes handler for raw I/O. Assigns it a Reactor and sets m_local_addr to the locally bound address (IPv4:port) of sd
.
sd | Socket descriptor |
Definition at line 103 of file IOHandler.h.
|
inlinevirtual |
Destructor.
If #m_socket_internally_created is set to true, closes the socket descriptor m_sd.
Definition at line 152 of file IOHandler.h.
int Hypertable::IOHandler::add_poll_interest | ( | int | mode | ) |
Adds the poll interest specified in mode
to the polling interface for this handler.
This method adds the poll interest, specified in mode
, to the polling interface for this handler and merges mode
into m_poll_interest using bitwise OR (|). If an error is encountered, m_error is set to the approprate error code.
|
inlineprotected |
Decomission handler.
This method decomissions the handler by setting the m_decomissioned flag to true. If the reference count is 0, the handler is also scheduled for removal with a call to m_reactor->schedule_removal(this)
.
Definition at line 382 of file IOHandler.h.
|
inlineprotected |
Decrement reference count.
If reference count drops to 0 and the handler is decomissioned then it is scheduled for removal with a call to m_reactor->schedule_removal(this)
.
Definition at line 355 of file IOHandler.h.
|
inline |
Convenience method for delivering event to application.
This method will deliver event
to the application via the event handler dh
if supplied, otherwise the event will be delivered via the default event handler, or no default event handler exists, it will just log the event. This method is (and should always) by called from a reactor thread.
event | pointer to Event (deleted by this method) |
dh | Event handler via which to deliver event |
Definition at line 169 of file IOHandler.h.
|
inlineprotectedvirtual |
Disconnect connection.
Reimplemented in Hypertable::IOHandlerData.
Definition at line 399 of file IOHandler.h.
void IOHandler::display_event | ( | struct pollfd * | event | ) |
Display polling event from poll()
interface to stderr.
event | Pointer to pollfd structure describing poll() event. |
Definition at line 60 of file IOHandler.cc.
|
inline |
Gets the handler socket address.
The socket address is the address of the remote end of the connection for data (TCP) handlers, and the local socket address for datagram and accept handlers.
Definition at line 229 of file IOHandler.h.
|
inlineprotected |
Get alias address for this connection.
Definition at line 327 of file IOHandler.h.
|
inlineprotected |
Returns first error code encountered by handler.
When an error is encountered during handler methods, the first error code that is encountered is recorded in m_error. This method returns that error or Error::OK if no error has been encountered.
Definition at line 319 of file IOHandler.h.
|
inline |
Get local socket address for connection.
Definition at line 234 of file IOHandler.h.
|
inline |
Gets the proxy name for this connection.
Definition at line 247 of file IOHandler.h.
|
inline |
Get the reactor that this handler is assigned to.
reactor | Reference to returned reactor pointer |
Definition at line 260 of file IOHandler.h.
|
inline |
Gets the socket descriptor for this connection.
Definition at line 255 of file IOHandler.h.
|
pure virtual |
Event handler method for Unix poll interface.
event | Pointer to pollfd structure describing event |
arrival_time | Arrival time of event |
Implemented in Hypertable::IOHandlerData, Hypertable::IOHandlerDatagram, Hypertable::IOHandlerRaw, and Hypertable::IOHandlerAccept.
|
inlineprotected |
Increment reference count.
Definition at line 343 of file IOHandler.h.
|
inlineprotected |
Checks to see if handler is decomissioned.
Definition at line 393 of file IOHandler.h.
|
inlineprotected |
Return poll()
interface events corresponding to the normalized polling interest in mode
.
mode
is some bitwise combination of the flags PollEvent::READ and PollEvent::WRITE.
poll()
events correspond to polling interest specified in mode
. Definition at line 408 of file IOHandler.h.
|
inlineprotected |
Return reference count.
Definition at line 368 of file IOHandler.h.
int Hypertable::IOHandler::remove_poll_interest | ( | int | mode | ) |
Removes the poll interest specified in mode
to the polling interface for this handler.
This method removes the poll interest, specified in mode
, from the polling interface for this handler and strips mode
from m_poll_interest using boolean operations. If an error is encountered, m_error is set to the approprate error code.
|
inline |
Resets poll interest by adding m_poll_interest to the polling interface for this handler.
If an error is encountered, m_error is set to the approprate error code.
Definition at line 220 of file IOHandler.h.
|
inlineprotected |
Set alias address for this connection.
alias | Reference to return alias address. |
Definition at line 334 of file IOHandler.h.
|
inline |
Sets the proxy name for this connection.
proxy | Proxy name to set for this connection. |
Definition at line 239 of file IOHandler.h.
int IOHandler::start_polling | ( | int | mode = PollEvent::READ | ) |
Start polling on the handler with the poll interest specified in mode
.
This method registers the poll interest, specified in mode
, with the polling interface and sets m_poll_interest to mode
. If an error is encountered, m_error is set to the approprate error code.
Definition at line 91 of file IOHandler.cc.
|
inlineprotected |
Stops polling by removing socket from polling interface.
Clears m_poll_interest.
Definition at line 420 of file IOHandler.h.
|
inlineprotected |
Sets m_error to error
if it has not already been set.
This method checks to see if m_error is set to Error::OK and if so, it sets m_error to error
and returns true. Otherwise it does nothing and returns false.
error
, false otherwise. Definition at line 303 of file IOHandler.h.
|
friend |
Definition at line 292 of file IOHandler.h.
|
protected |
Handler socket address.
Definition at line 457 of file IOHandler.h.
|
protected |
Address alias for connection.
Definition at line 463 of file IOHandler.h.
|
protected |
Decomissioned flag.
Calls to methods that reference this member must be mutex protected by caller.
Definition at line 483 of file IOHandler.h.
|
protected |
Default dispatch hander for connection.
Definition at line 469 of file IOHandler.h.
|
protected |
Error code.
Definition at line 451 of file IOHandler.h.
|
protected |
Free flag (for testing)
Definition at line 448 of file IOHandler.h.
|
protected |
Local address of connection.
Definition at line 460 of file IOHandler.h.
|
protected |
Mutex for serializing concurrent access
Definition at line 440 of file IOHandler.h.
|
protected |
Current polling interest.
The polling interest is some bitwise combination of the flags PollEvent::READ and PollEvent::WRITE.
Definition at line 478 of file IOHandler.h.
|
protected |
Proxy name for this connection.
Definition at line 454 of file IOHandler.h.
|
protected |
Reactor to which this handler is assigned.
Definition at line 472 of file IOHandler.h.
|
protected |
Reference count.
Calls to methods that reference this member must be mutex protected by caller.
Definition at line 445 of file IOHandler.h.
|
protected |
Socket descriptor.
Definition at line 466 of file IOHandler.h.
|
protected |
Socket was internally created and should be closed on destroy.
Definition at line 486 of file IOHandler.h.