0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
IOHandlerData.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #ifndef AsyncComm_IOHandlerData_h
29 #define AsyncComm_IOHandlerData_h
30 
31 #include "CommBuf.h"
32 #include "IOHandler.h"
33 
34 #include <Common/Error.h>
35 
36 #include <list>
37 
38 extern "C" {
39 #include <netdb.h>
40 #include <string.h>
41 }
42 
43 namespace Hypertable {
44 
51  class IOHandlerData : public IOHandler {
52 
53  public:
54 
61  IOHandlerData(int sd, const InetAddr &addr,
62  const DispatchHandlerPtr &dhp, bool connected=false)
63  : IOHandler(sd, dhp) {
64  memcpy(&m_addr, &addr, sizeof(InetAddr));
65  m_connected = connected;
67  }
68 
70  virtual ~IOHandlerData() { }
71 
75  void disconnect() override {
76  EventPtr event = std::make_shared<Event>(Event::DISCONNECT, m_addr, m_proxy, m_error);
77  deliver_event(event);
78  }
79 
83  m_got_header = false;
84  m_event.reset();
87  m_message = 0;
88  m_message_ptr = 0;
90  m_message_aligned = false;
91  }
92 
99  free(m_message);
100  else
101  delete [] m_message;
102  m_message = 0;
103  }
104 
119  int send_message(CommBufPtr &cbp, uint32_t timeout_ms=0,
120  DispatchHandler *disp_handler=nullptr);
121 
136  int flush_send_queue();
137 
160  bool handle_event(struct pollfd *event,
161  ClockT::time_point arrival_time) override;
162 
163 #if defined(__APPLE__) || defined(__FreeBSD__)
164 
186  bool handle_event(struct kevent *event,
187  ClockT::time_point arrival_time) override;
188 #elif defined(__linux__)
189 
213  bool handle_event(struct epoll_event *event,
214  ClockT::time_point arrival_time) override;
215 #elif defined(__sun__)
216 
239  bool handle_event(port_event_t *event,
240  ClockT::time_point arrival_time) override;
241 #else
243 #endif
244 
263  bool handle_write_readiness();
264 
265  private:
266 
277  void handle_message_header(ClockT::time_point arrival_time);
278 
292  void handle_message_body();
293 
296  void handle_disconnect();
297 
299  bool m_connected {};
300 
302  bool m_got_header {};
303 
306 
309 
311  uint8_t m_message_header[64];
312 
314  uint8_t *m_message_header_ptr {};
315 
318 
320  uint8_t *m_message {};
321 
323  uint8_t *m_message_ptr {};
324 
327 
329  std::list<CommBufPtr> m_send_queue;
330  };
332 }
333 
334 #endif // AsyncComm_IOHandlerData_h
std::list< CommBufPtr > m_send_queue
Send queue.
void disconnect() override
Disconnects handler by delivering Event::DISCONNECT via default dispatch handler. ...
Definition: IOHandlerData.h:75
size_t m_message_header_remaining
Amount of header remaining to be read.
chrono::time_point< fast_clock > time_point
Definition: fast_clock.h:42
Abstract base class for application dispatch handlers registered with AsyncComm.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
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.
bool handle_event(struct pollfd *event, ClockT::time_point arrival_time) override
Handle poll() interface events.
static const size_t FIXED_LENGTH
Definition: CommHeader.h:46
bool m_connected
Flag indicating if socket connection has been completed.
void handle_message_body()
Processes a message body.
Encapsulate an internet address.
Definition: InetAddr.h:66
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
void reset_incoming_message_state()
Resets incoming message buffer state in preparation for next message.
Definition: IOHandlerData.h:82
Base class for socket descriptor I/O handlers.
Definition: IOHandler.h:76
virtual ~IOHandlerData()
Destructor.
Definition: IOHandlerData.h:70
I/O handler for TCP sockets.
Definition: IOHandlerData.h:51
Connection disconnected event.
Definition: Event.h:62
IOHandlerData(int sd, const InetAddr &addr, const DispatchHandlerPtr &dhp, bool connected=false)
Constructor.
Definition: IOHandlerData.h:61
Hypertable definitions
bool m_got_header
Flag indicating if message header has been completely received.
uint8_t * m_message_header_ptr
Pointer to next write position in m_message_header.
bool handle_write_readiness()
Handles write readiness by completing connection and flushing send queue.
size_t m_message_remaining
Amount of message payload remaining to be read.
Declarations for CommBuf.
EventPtr m_event
Pointer to Event object holding message to deliver to application.
String m_proxy
Proxy name for this connection.
Definition: IOHandler.h:454
int32_t m_error
Error code.
Definition: IOHandler.h:451
std::shared_ptr< DispatchHandler > DispatchHandlerPtr
Smart pointer to DispatchHandler.
void deliver_event(EventPtr &event, DispatchHandler *dh=0)
Convenience method for delivering event to application.
Definition: IOHandler.h:169
int flush_send_queue()
Flushes send queue.
void free_message_buffer()
Frees the message buffer (m_message).
Definition: IOHandlerData.h:97
uint8_t * m_message
Poiner to message payload buffer.
void handle_disconnect()
Decomissions the handler.
InetAddr m_addr
Handler socket address.
Definition: IOHandler.h:457
Error codes, Exception handling, error logging.
uint8_t * m_message_ptr
Pointer to next write position in m_message.
uint8_t m_message_header[64]
Message header buffer.
Declarations for IOHandler.
bool m_message_aligned
Flag indicating if message buffer was allocated with posix_memalign()
void handle_message_header(ClockT::time_point arrival_time)
Processes a message header.