Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TCPTransport.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program 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
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * TCPTransport.h
17  * Copyright (C) 2012 Simon Newton
18  *
19  * This defines the OutgoingStreamTransport and IncomingStreamTransport for
20  * sending and receiving PDUs over stream connections.
21  *
22  * When receiving, the BaseInflator is passed a header containing the source IP
23  * & port (since many higher layer protocols require this). When using the
24  * IncomingStreamTransport you need to provide a fake ip:port pair.
25  *
26  * It's unlikely you want to use IncomingTCPTransport directly, since all
27  * real world connections are TCP (rather than pipes etc.). The
28  * IncomingStreamTransport is separate because it assists in testing.
29  */
30 
31 #ifndef PLUGINS_E131_E131_TCPTRANSPORT_H_
32 #define PLUGINS_E131_E131_TCPTRANSPORT_H_
33 
34 #include <memory>
35 #include "ola/io/OutputBuffer.h"
36 #include "ola/io/OutputStream.h"
37 #include "ola/io/Descriptor.h"
38 #include "ola/network/TCPSocket.h"
39 #include "plugins/e131/e131/PDU.h"
40 #include "plugins/e131/e131/Transport.h"
41 #include "plugins/e131/e131/TransportHeader.h"
42 
43 namespace ola {
44 namespace plugin {
45 namespace e131 {
46 
47 
53  public:
54  IncomingStreamTransport(class BaseInflator *inflator,
55  ola::io::ConnectedDescriptor *descriptor,
56  const ola::network::IPV4SocketAddress &source);
58 
59  bool Receive();
60 
61  private:
62  // The receiver is a state machine.
63  typedef enum {
64  WAITING_FOR_PREAMBLE,
65  WAITING_FOR_PDU_FLAGS,
66  WAITING_FOR_PDU_LENGTH,
67  WAITING_FOR_PDU,
68  } RXState;
69 
70  typedef enum {
71  TWO_BYTES = 2,
72  THREE_BYTES = 3,
73  } PDULengthSize;
74 
75  TransportHeader m_transport_header;
76  class BaseInflator *m_inflator;
77  ola::io::ConnectedDescriptor *m_descriptor;
78 
79  // end points to the byte after the data
80  uint8_t *m_buffer_start, *m_buffer_end, *m_data_end;
81  // the amount of data we need before we can move to the next stage
82  unsigned int m_outstanding_data;
83  // the state we're currently in
84  RXState m_state;
85  unsigned int m_block_size;
86  unsigned int m_consumed_block_size;
87  bool m_stream_valid;
88  PDULengthSize m_pdu_length_size;
89  unsigned int m_pdu_size;
90 
91  void HandlePreamble();
92  void HandlePDUFlags();
93  void HandlePDULength();
94  void HandlePDU();
95 
96  void IncreaseBufferSize(unsigned int new_size);
97  void ReadRequiredData();
98  void EnterWaitingForPreamble();
99  void EnterWaitingForPDU();
100 
104  inline unsigned int FreeSpace() const {
105  return m_buffer_start ?
106  static_cast<unsigned int>(m_buffer_end - m_data_end) : 0u;
107  }
108 
112  inline unsigned int DataLength() const {
113  return m_buffer_start ?
114  static_cast<unsigned int>(m_data_end - m_buffer_start) : 0u;
115  }
116 
120  inline unsigned int BufferSize() const {
121  return static_cast<unsigned int>(m_buffer_end - m_buffer_start);
122  }
123 
124  static const unsigned int INITIAL_SIZE;
125  static const unsigned int PDU_BLOCK_SIZE = 4;
126 };
127 
128 
133  public:
134  IncomingTCPTransport(class BaseInflator *inflator,
135  ola::network::TCPSocket *socket);
137 
138  bool Receive() { return m_transport->Receive(); }
139 
140  private:
141  std::auto_ptr<IncomingStreamTransport> m_transport;
142 };
143 } // namespace e131
144 } // namespace plugin
145 } // namespace ola
146 #endif // PLUGINS_E131_E131_TCPTRANSPORT_H_