Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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_