Open Lighting Architecture: include/ola/io/NonBlockingSender.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
NonBlockingSender.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * NonBlockingSender.h
17 * Copyright (C) 2013 Simon Newton
18 *
19 * Explanation:
20 * If we just write IOStacks directly to ConnectedDescriptors, we may not be
21 * able to write the entire message. This can happen if the remote end is slow
22 * to ack and data builds up in the kernel socket buffer.
23 *
24 * This class abstracts the caller from having to deal with this situation. At
25 * construction time we specify the maximum number of message bytes we want to
26 * buffer. Once the buffer reaches this size subsequent calls to SendMessage
27 * will return false.
28 */
29
30#ifndef INCLUDE_OLA_IO_NONBLOCKINGSENDER_H_
31#define INCLUDE_OLA_IO_NONBLOCKINGSENDER_H_
32
33#include <ola/io/Descriptor.h>
34#include <ola/io/IOQueue.h>
35#include <ola/io/MemoryBlockPool.h>
36#include <ola/io/OutputBuffer.h>
37#include <ola/io/SelectServerInterface.h>
38
39namespace ola {
40namespace io {
41
57 class NonBlockingSender {
58 public:
70 NonBlockingSender(ola::io::ConnectedDescriptor *descriptor,
71 ola::io::SelectServerInterface *ss,
72 ola::io::MemoryBlockPool *memory_pool,
73 unsigned int max_buffer_size = DEFAULT_MAX_BUFFER_SIZE);
74
78 ~NonBlockingSender();
79
84 bool LimitReached() const;
85
93 bool SendMessage(class IOStack *stack);
94
102 bool SendMessage(IOQueue *queue);
103
113 static const unsigned int DEFAULT_MAX_BUFFER_SIZE;
114
115 private:
116 ola::io::ConnectedDescriptor *m_descriptor;
117 ola::io::SelectServerInterface *m_ss;
118 ola::io::IOQueue m_output_buffer;
119 bool m_associated;
120 unsigned int m_max_buffer_size;
121
122 void PerformWrite();
123 void AssociateIfRequired();
124
125 NonBlockingSender(const NonBlockingSender &) = delete;
126 const NonBlockingSender &operator=(const NonBlockingSender &) = delete;
127};
128} // namespace io
129} // namespace ola
130#endif // INCLUDE_OLA_IO_NONBLOCKINGSENDER_H_
ola::io::ConnectedDescriptor
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition Descriptor.h:283
ola::io::IOQueue
Definition IOQueue.h:41
ola::io::IOStack
Definition IOStack.h:41
ola::io::MemoryBlockPool
MemoryBlockPool. This class is not thread safe.
Definition MemoryBlockPool.h:35
ola::io::NonBlockingSender::~NonBlockingSender
~NonBlockingSender()
Destructor.
Definition NonBlockingSender.cpp:43
ola::io::NonBlockingSender::LimitReached
bool LimitReached() const
Check if the limit for the internal buffer has been reached.
Definition NonBlockingSender.cpp:50
ola::io::NonBlockingSender::DEFAULT_MAX_BUFFER_SIZE
static const unsigned int DEFAULT_MAX_BUFFER_SIZE
The default max internal buffer size.
Definition NonBlockingSender.h:113
ola::io::NonBlockingSender::SendMessage
bool SendMessage(class IOStack *stack)
Send the contents of an IOStack on the ConnectedDescriptor.
Definition NonBlockingSender.cpp:54
ola::io::NonBlockingSender::NonBlockingSender
NonBlockingSender(ola::io::ConnectedDescriptor *descriptor, ola::io::SelectServerInterface *ss, ola::io::MemoryBlockPool *memory_pool, unsigned int max_buffer_size=DEFAULT_MAX_BUFFER_SIZE)
Create a new NonBlockingSender.
Definition NonBlockingSender.cpp:30
ola::io::SelectServerInterface
The interface for the SelectServer.
Definition SelectServerInterface.h:42
Descriptor.h
ola::io
Classes for general I/O and event handling.
Definition Descriptor.cpp:49
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44