Open Lighting Architecture  Latest Git
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  * TCPSocket.h
17  * The TCP Socket interfaces
18  * Copyright (C) 2005 Simon Newton
19  *
20  * TCPSocket, this represents a TCP connection to a remote endpoint
21  *
22  * AcceptingSocket is the interface that defines sockets which can spawn new
23  * ConnectedDescriptors. TCPAcceptingSocket is the only subclass and provides
24  * the accept() functionality.
25  */
26 
27 #ifndef INCLUDE_OLA_NETWORK_TCPSOCKET_H_
28 #define INCLUDE_OLA_NETWORK_TCPSOCKET_H_
29 
30 #include <stdint.h>
31 
32 #include <ola/base/Macro.h>
33 #include <ola/io/Descriptor.h>
36 
37 namespace ola {
38 namespace network {
39 
40 /*
41  * A TCPSocket
42  */
44  public:
45  explicit TCPSocket(int sd);
46 
47  ~TCPSocket() { Close(); }
48 
49  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
50  ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
51  bool Close();
52 
53  GenericSocketAddress GetLocalAddress() const;
55 
56  static TCPSocket* Connect(const SocketAddress &endpoint);
57 
58  bool SetNoDelay();
59 
60  protected:
61  bool IsSocket() const { return true; }
62 
63  private:
64  ola::io::DescriptorHandle m_handle;
65 
66  DISALLOW_COPY_AND_ASSIGN(TCPSocket);
67 };
68 
69 
70 /*
71  * A TCP accepting socket
72  */
74  public:
75  explicit TCPAcceptingSocket(class TCPSocketFactoryInterface *factory);
77  bool Listen(const SocketAddress &endpoint, int backlog = 10);
78  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
79  bool Close();
80  void PerformRead();
81 
82  void SetFactory(class TCPSocketFactoryInterface *factory) {
83  m_factory = factory;
84  }
85 
87 
88  private:
89  ola::io::DescriptorHandle m_handle;
90  class TCPSocketFactoryInterface *m_factory;
91 
93 };
94 } // namespace network
95 } // namespace ola
96 #endif // INCLUDE_OLA_NETWORK_TCPSOCKET_H_
Represents Socket Addresses.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
Definition: TCPSocketFactory.h:34
GenericSocketAddress GetPeerAddress() const
Definition: TCPSocket.cpp:72
GenericSocketAddress GetLocalAddress(int sd)
Definition: SocketHelper.cpp:39
Definition: TCPSocket.h:43
Represents an IPv4 Address.
void PerformRead()
Called when there is data available on the descriptor.
Definition: Descriptor.cpp:212
Represents a file descriptor that supports reading data.
Definition: Descriptor.h:140
Helper macros.
Definition: TCPSocket.h:73
ola::io::DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition: TCPSocket.h:49
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
The base SocketAddress.
Definition: SocketAddress.h:58
ola::io::DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition: TCPSocket.h:50
ola::io::DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition: TCPSocket.h:78
a Generic Socket Address
Definition: SocketAddress.h:166