Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TCPSocket.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  * Socket.h
17  * The Socket interfaces
18  * Copyright (C) 2005 Simon Newton
19  *
20  * - UDPSocket, allows sending and receiving UDP datagrams
21  * - TCPSocket, this represents a TCP connection to a remote endpoint
22  *
23  * AcceptingSocket is the interface that defines sockets which can spawn new
24  * ConnectedDescriptors. TCPAcceptingSocket is the only subclass and provides
25  * the accept() functionality.
26  */
27 
28 #ifndef INCLUDE_OLA_NETWORK_TCPSOCKET_H_
29 #define INCLUDE_OLA_NETWORK_TCPSOCKET_H_
30 
31 #include <stdint.h>
32 
33 #include <ola/base/Macro.h>
34 #include <ola/io/Descriptor.h>
37 
38 namespace ola {
39 namespace network {
40 
41 /*
42  * A TCPSocket
43  */
45  public:
46  explicit TCPSocket(int sd);
47 
48  ~TCPSocket() { Close(); }
49 
50  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
51  ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
52  bool Close();
53 
54  GenericSocketAddress GetLocalAddress() const;
56 
57  static TCPSocket* Connect(const SocketAddress &endpoint);
58 
59  bool SetNoDelay();
60 
61  protected:
62  bool IsSocket() const { return true; }
63 
64  private:
65  ola::io::DescriptorHandle m_handle;
66 
67  DISALLOW_COPY_AND_ASSIGN(TCPSocket);
68 };
69 
70 
71 /*
72  * A TCP accepting socket
73  */
75  public:
76  explicit TCPAcceptingSocket(class TCPSocketFactoryInterface *factory);
78  bool Listen(const SocketAddress &endpoint, int backlog = 10);
79  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
80  bool Close();
81  void PerformRead();
82 
83  void SetFactory(class TCPSocketFactoryInterface *factory) {
84  m_factory = factory;
85  }
86 
88 
89  private:
90  ola::io::DescriptorHandle m_handle;
91  class TCPSocketFactoryInterface *m_factory;
92 
94 };
95 } // namespace network
96 } // namespace ola
97 #endif // INCLUDE_OLA_NETWORK_TCPSOCKET_H_