Open Lighting Architecture  0.9.0
 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 
42 namespace ola {
43 namespace plugin {
44 namespace e131 {
45 
46 
52  public:
53  IncomingStreamTransport(class BaseInflator *inflator,
54  ola::io::ConnectedDescriptor *descriptor,
55  const ola::network::IPV4SocketAddress &source);
57 
58  bool Receive();
59 
60  private:
61  // The receiver is a state machine.
62  typedef enum {
63  WAITING_FOR_PREAMBLE,
64  WAITING_FOR_PDU_FLAGS,
65  WAITING_FOR_PDU_LENGTH,
66  WAITING_FOR_PDU,
67  } RXState;
68 
69  typedef enum {
70  TWO_BYTES = 2,
71  THREE_BYTES = 3,
72  } PDULengthSize;
73 
74  TransportHeader m_transport_header;
75  class BaseInflator *m_inflator;
76  ola::io::ConnectedDescriptor *m_descriptor;
77 
78  // end points to the byte after the data
79  uint8_t *m_buffer_start, *m_buffer_end, *m_data_end;
80  // the amount of data we need before we can move to the next stage
81  unsigned int m_outstanding_data;
82  // the state we're currently in
83  RXState m_state;
84  unsigned int m_block_size;
85  unsigned int m_consumed_block_size;
86  bool m_stream_valid;
87  PDULengthSize m_pdu_length_size;
88  unsigned int m_pdu_size;
89 
90  void HandlePreamble();
91  void HandlePDUFlags();
92  void HandlePDULength();
93  void HandlePDU();
94 
95  void IncreaseBufferSize(unsigned int new_size);
96  void ReadRequiredData();
97  void EnterWaitingForPreamble();
98  void EnterWaitingForPDU();
99 
103  inline unsigned int FreeSpace() const {
104  return m_buffer_start ?
105  static_cast<unsigned int>(m_buffer_end - m_data_end) : 0u;
106  }
107 
111  inline unsigned int DataLength() const {
112  return m_buffer_start ?
113  static_cast<unsigned int>(m_data_end - m_buffer_start) : 0u;
114  }
115 
119  inline unsigned int BufferSize() const {
120  return static_cast<unsigned int>(m_buffer_end - m_buffer_start);
121  }
122 
123  static const unsigned int INITIAL_SIZE;
124  static const unsigned int PDU_BLOCK_SIZE = 4;
125 };
126 
127 
132  public:
133  IncomingTCPTransport(class BaseInflator *inflator,
134  ola::network::TCPSocket *socket);
136 
137  bool Receive() { return m_transport->Receive(); }
138 
139  private:
140  std::auto_ptr<IncomingStreamTransport> m_transport;
141 };
142 } // namespace e131
143 } // namespace plugin
144 } // namespace ola
145 #endif // PLUGINS_E131_E131_TCPTRANSPORT_H_