Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Socket.h
17  * The Socket interfaces
18  * Copyright (C) 2005-2009 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 #ifdef WIN32
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
36 #else
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #endif
40 
41 #include <ola/io/Descriptor.h>
44 
45 
46 namespace ola {
47 namespace network {
48 
49 
50 /*
51  * A TCPSocket
52  */
54  public:
55  explicit TCPSocket(int sd)
56  : m_sd(sd) {
57  SetNoSigPipe(sd);
58  }
59 
60  ~TCPSocket() { Close(); }
61 
62  int ReadDescriptor() const { return m_sd; }
63  int WriteDescriptor() const { return m_sd; }
64  bool Close();
65 
66  GenericSocketAddress GetLocalAddress() const;
68 
69  static TCPSocket* Connect(const SocketAddress &endpoint);
70 
71  bool SetNoDelay();
72 
73  protected:
74  bool IsSocket() const { return true; }
75 
76  private:
77  int m_sd;
78 
79  TCPSocket(const TCPSocket &other);
80  TCPSocket& operator=(const TCPSocket &other);
81 };
82 
83 
84 /*
85  * A TCP accepting socket
86  */
88  public:
89  explicit TCPAcceptingSocket(class TCPSocketFactoryInterface *factory);
91  bool Listen(const SocketAddress &endpoint, int backlog = 10);
92  int ReadDescriptor() const { return m_sd; }
93  bool Close();
94  void PerformRead();
95 
96  void SetFactory(class TCPSocketFactoryInterface *factory) {
97  m_factory = factory;
98  }
99 
101 
102  private:
103  int m_sd;
104  class TCPSocketFactoryInterface *m_factory;
105 
107  TCPAcceptingSocket& operator=(const TCPAcceptingSocket &other);
108 };
109 } // namespace network
110 } // namespace ola
111 #endif // INCLUDE_OLA_NETWORK_TCPSOCKET_H_