Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TCPSocketFactory.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * TCPSocketFactory.h
17  * Factories for creating TCP Sockets
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_TCPSOCKETFACTORY_H_
22 #define INCLUDE_OLA_NETWORK_TCPSOCKETFACTORY_H_
23 
24 #include <ola/Callback.h>
25 #include <ola/network/TCPSocket.h>
26 
27 namespace ola {
28 namespace network {
29 
34  public:
35  virtual ~TCPSocketFactoryInterface() {}
36 
37  virtual void NewTCPSocket(int fd) = 0;
38 };
39 
40 
44 template<class SocketType>
46  public:
48 
49  explicit GenericTCPSocketFactory(NewSocketCallback *on_accept)
50  : m_new_socket(on_accept) {
51  }
52 
54  if (m_new_socket)
55  delete m_new_socket;
56  }
57 
58  void NewTCPSocket(int fd) {
59  SocketType *socket = new SocketType(fd);
60  socket->SetReadNonBlocking();
61  m_new_socket->Run(socket);
62  }
63 
64  private:
65  NewSocketCallback *m_new_socket;
66 };
67 
69 } // namespace network
70 } // namespace ola
71 #endif // INCLUDE_OLA_NETWORK_TCPSOCKETFACTORY_H_