Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Explaination:
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 
39 namespace ola {
40 namespace io {
41 
58  public:
72  ola::io::MemoryBlockPool *memory_pool,
73  unsigned int max_buffer_size = DEFAULT_MAX_BUFFER_SIZE);
74 
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;
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  DISALLOW_COPY_AND_ASSIGN(NonBlockingSender);
126 };
127 } // namespace io
128 } // namespace ola
129 #endif // INCLUDE_OLA_IO_NONBLOCKINGSENDER_H_
~NonBlockingSender()
Destructor.
Definition: NonBlockingSender.cpp:43
static const unsigned int DEFAULT_MAX_BUFFER_SIZE
The default max internal buffer size.
Definition: NonBlockingSender.h:113
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
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
The interface for the SelectServer.
Definition: SelectServerInterface.h:42
bool LimitReached() const
Check if the limit for the internal buffer has been reached.
Definition: NonBlockingSender.cpp:50
Definition: IOQueue.h:39
bool SendMessage(class IOStack *stack)
Send the contents of an IOStack on the ConnectedDescriptor.
Definition: NonBlockingSender.cpp:54
Definition: IOStack.h:39
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
MemoryBlockPool. This class is not thread safe.
Definition: MemoryBlockPool.h:35
Write data to ConnectedDescriptors without blocking or losing data.
Definition: NonBlockingSender.h:57