45 #ifndef INCLUDE_OLA_IO_DESCRIPTOR_H_
46 #define INCLUDE_OLA_IO_DESCRIPTOR_H_
51 #include <ola/io/IOQueue.h>
66 GENERIC_DESCRIPTOR = 0,
72 struct DescriptorHandle {
79 DescriptorType m_type;
85 uint32_t* m_read_data_size;
88 : m_type(GENERIC_DESCRIPTOR),
91 m_read_data_size(NULL) {
96 static DescriptorHandle INVALID_DESCRIPTOR;
97 static const size_t READ_DATA_BUFFER_SIZE = 1024;
98 bool operator!=(
const DescriptorHandle &lhs,
const DescriptorHandle &rhs);
99 bool operator==(
const DescriptorHandle &lhs,
const DescriptorHandle &rhs);
100 bool operator<(
const DescriptorHandle &lhs,
const DescriptorHandle &rhs);
101 std::ostream&
operator<<(std::ostream &stream,
const DescriptorHandle &data);
103 typedef int DescriptorHandle;
104 static DescriptorHandle INVALID_DESCRIPTOR = -1;
115 virtual DescriptorHandle ReadDescriptor()
const = 0;
118 bool ValidReadDescriptor()
const {
119 return ReadDescriptor() != INVALID_DESCRIPTOR;
123 virtual void PerformRead() = 0;
133 virtual DescriptorHandle WriteDescriptor()
const = 0;
136 bool ValidWriteDescriptor()
const {
137 return WriteDescriptor() != INVALID_DESCRIPTOR;
141 virtual void PerformWrite() = 0;
172 m_on_write = on_write;
193 DescriptorHandle ReadDescriptor()
const {
return m_handle; }
194 DescriptorHandle WriteDescriptor()
const {
return m_handle; }
196 bool Close() {
return true; }
199 DescriptorHandle m_handle;
209 return d1->ReadDescriptor() < d2->ReadDescriptor();
226 virtual ssize_t Send(
const uint8_t *buffer,
unsigned int size);
227 virtual ssize_t Send(
IOQueue *data);
229 virtual int Receive(uint8_t *buffer,
231 unsigned int &data_read);
233 virtual bool SetReadNonBlocking() {
234 return SetNonBlocking(ReadDescriptor());
237 virtual bool Close() = 0;
238 int DataRemaining()
const;
239 bool IsClosed()
const;
247 m_on_close = on_close;
266 static bool SetNonBlocking(DescriptorHandle fd);
269 virtual bool IsSocket()
const = 0;
270 bool SetNoSigPipe(DescriptorHandle fd);
276 OnCloseCallback *m_on_close;
287 m_handle_pair[0] = INVALID_DESCRIPTOR;
288 m_handle_pair[1] = INVALID_DESCRIPTOR;
290 memset(m_read_data, 0, READ_DATA_BUFFER_SIZE);
291 m_read_data_size = 0;
296 DescriptorHandle ReadDescriptor()
const {
return m_handle_pair[0]; }
297 DescriptorHandle WriteDescriptor()
const {
return m_handle_pair[1]; }
302 bool IsSocket()
const {
return false; }
305 DescriptorHandle m_handle_pair[2];
309 uint8_t m_read_data[READ_DATA_BUFFER_SIZE];
310 uint32_t m_read_data_size;
323 m_in_pair[0] = m_in_pair[1] = INVALID_DESCRIPTOR;
324 m_out_pair[0] = m_out_pair[1] = INVALID_DESCRIPTOR;
326 memset(m_read_data, 0, READ_DATA_BUFFER_SIZE);
327 m_read_data_size = 0;
334 DescriptorHandle ReadDescriptor()
const {
return m_in_pair[0]; }
335 DescriptorHandle WriteDescriptor()
const {
return m_out_pair[1]; }
340 bool IsSocket()
const {
return false; }
343 DescriptorHandle m_in_pair[2];
344 DescriptorHandle m_out_pair[2];
347 DescriptorHandle out_pair[2],
349 m_in_pair[0] = in_pair[0];
350 m_in_pair[1] = in_pair[1];
351 m_out_pair[0] = out_pair[0];
352 m_out_pair[1] = out_pair[1];
353 m_other_end = other_end;
355 m_in_pair[0].m_read_data = m_read_data;
356 m_in_pair[0].m_read_data_size = &m_read_data_size;
362 uint8_t m_read_data[READ_DATA_BUFFER_SIZE];
363 uint32_t m_read_data_size;
374 m_handle = INVALID_DESCRIPTOR;
380 DescriptorHandle ReadDescriptor()
const {
return m_handle; }
381 DescriptorHandle WriteDescriptor()
const {
return m_handle; }
386 bool IsSocket()
const {
return true; }
389 DescriptorHandle m_handle;
393 m_handle.m_handle.m_fd = socket;
397 m_other_end = other_end;
411 DescriptorHandle ReadDescriptor()
const {
return m_handle; }
412 DescriptorHandle WriteDescriptor()
const {
return m_handle; }
416 bool IsSocket()
const {
return false; }
419 DescriptorHandle m_handle;
425 #endif // INCLUDE_OLA_IO_DESCRIPTOR_H_