Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OPCServer.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  * OPCServer.h
17  * The Open Pixel Server.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_OPENPIXELCONTROL_OPCSERVER_H_
22 #define PLUGINS_OPENPIXELCONTROL_OPCSERVER_H_
23 
24 #include <string>
25 #include <map>
26 #include <memory>
27 #include "ola/Constants.h"
28 #include "ola/DmxBuffer.h"
29 #include "ola/io/SelectServerInterface.h"
31 #include "ola/network/TCPSocket.h"
32 #include "ola/network/TCPSocketFactory.h"
33 #include "plugins/openpixelcontrol/OPCConstants.h"
34 
35 namespace ola {
36 namespace plugin {
37 namespace openpixelcontrol {
38 
44 class OPCServer {
45  public:
51 
58  const ola::network::IPV4SocketAddress &listen_addr);
59 
63  ~OPCServer();
64 
70  bool Init();
71 
78  void SetCallback(uint8_t channel, ChannelCallback *callback);
79 
86 
87  private:
88  struct RxState {
89  public:
90  unsigned int offset;
91  uint16_t expected_size;
92  uint8_t *data;
93  unsigned int buffer_size;
94 
95  RxState()
96  : offset(0),
97  expected_size(0) {
98  buffer_size = OPC_FRAME_SIZE,
99  data = new uint8_t[buffer_size];
100  }
101 
102  ~RxState() {
103  delete[] data;
104  }
105 
106  void CheckSize();
107  };
108 
109  typedef std::map<ola::network::TCPSocket*, RxState*> ClientMap;
110 
111  ola::io::SelectServerInterface* const m_ss;
112  const ola::network::IPV4SocketAddress m_listen_addr;
113  ola::network::TCPSocketFactory m_tcp_socket_factory;
114 
115  std::auto_ptr<ola::network::TCPAcceptingSocket> m_listening_socket;
116  ClientMap m_clients;
117  std::map<uint8_t, ChannelCallback*> m_callbacks;
118 
119  void NewTCPConnection(ola::network::TCPSocket *socket);
120  void SocketReady(ola::network::TCPSocket *socket, RxState *rx_state);
121  void SocketClosed(ola::network::TCPSocket *socket);
122 
123  DISALLOW_COPY_AND_ASSIGN(OPCServer);
124 };
125 } // namespace openpixelcontrol
126 } // namespace plugin
127 } // namespace ola
128 #endif // PLUGINS_OPENPIXELCONTROL_OPCSERVER_H_