Allolib  1.0
C++ Components For Interactive Multimedia
al_Socket.hpp
1 #ifndef INCLUDE_AL_IO_SOCKET_HPP
2 #define INCLUDE_AL_IO_SOCKET_HPP 1
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Utilties for network socket sending/receiving
42 
43  File author(s):
44  Graham Wakefield, 2010, grrrwaaa@gmail.com
45  Lance Putnam, 2010, putnam.lance@gmail.com
46 */
47 
48 #include <string>
49 
50 #include "al/system/al_Time.hpp"
51 #include "al/types/al_ValueSource.hpp"
52 
53 namespace al {
54 
58 class Socket {
59 public:
61  enum {
62  // IPv4/v6 protocols
63  TCP = 6,
64  UDP = 17,
65  SCTP = 132,
67  // Transmission types
68  STREAM = 1 << 8,
69  DGRAM =
70  2
71  << 8,
73  // Families
74  INET = 1 << 16,
75  INET6 = 2 << 16
76  };
77 
79  Socket();
80 
86  Socket(uint16_t port, const char *address, al_sec timeout, int type);
87 
88  virtual ~Socket();
89 
91  static std::string hostName();
92 
94  static std::string hostIP();
95 
96  static std::string nameToIp(std::string name);
97 
99  bool opened() const;
100 
102  const std::string &address() const;
103 
105  uint16_t port() const;
106 
108  al_sec timeout() const;
109 
111  bool open(uint16_t port, const char *address, al_sec timeout, int type);
112 
114  void close();
115 
117  bool bind();
118 
120 
123  bool connect();
124 
126 
134  void timeout(al_sec t);
135 
137 
142  //
147  size_t recv(char *buffer, size_t maxlen, char *from = nullptr);
148 
150 
154  size_t send(const char *buffer, size_t len);
155 
157 
161  bool listen();
162 
164 
168  bool accept(Socket &sock);
169 
170  ValueSource *valueSource();
171 
172 protected:
173  // Called after a successful call to open
174  virtual bool onOpen() { return true; }
175 
176 private:
177  struct Impl;
178  Impl *mImpl;
179  ValueSource mValueSource;
180 };
181 
183 
189 class SocketClient : public Socket {
190 public:
191  SocketClient() {}
192 
198  SocketClient(uint16_t port, const char *address = "localhost",
199  al_sec timeout = 0, int type = UDP | DGRAM)
200  : Socket(port, address, timeout, type) {
201  SocketClient::onOpen();
202  }
203 
204 protected:
205  virtual bool onOpen();
206 };
207 
209 
215 class SocketServer : public Socket {
216 public:
217  SocketServer() {}
218 
225  SocketServer(uint16_t port, const char *address = "", al_sec timeout = 0,
226  int type = UDP | DGRAM)
227  : Socket(port, address, timeout, type) {
228  SocketServer::onOpen();
229  }
230 
231 protected:
232  virtual bool onOpen();
233 };
234 
236 // typedef SocketClient SocketSend;
237 
239 // typedef SocketServer SocketRecv;
240 
241 } // namespace al
242 
243 #endif /* include guard */
Client socket.
Definition: al_Socket.hpp:189
SocketClient(uint16_t port, const char *address="localhost", al_sec timeout=0, int type=UDP|DGRAM)
Definition: al_Socket.hpp:198
static std::string hostName()
Get name of current host.
al_sec timeout() const
Get timeout duration, in seconds.
void close()
Close the socket.
bool accept(Socket &sock)
Check for an incoming socket connection.
size_t recv(char *buffer, size_t maxlen, char *from=nullptr)
Read data from a network.
void timeout(al_sec t)
Set socket timeout.
const std::string & address() const
Get IP address string.
bool opened() const
Returns whether socket is open.
static std::string hostIP()
IP address of current host.
uint16_t port() const
Get port number.
bool bind()
Bind current (local) address to socket. Called on a server socket.
bool listen()
Listen for incoming connections from remote clients.
Socket()
Create uninitialized socket.
bool open(uint16_t port, const char *address, al_sec timeout, int type)
Open socket (reopening if currently open)
bool connect()
Connect socket to current (remote) address.
Socket(uint16_t port, const char *address, al_sec timeout, int type)
size_t send(const char *buffer, size_t len)
Send data over a network.
Server socket.
Definition: al_Socket.hpp:215
SocketServer(uint16_t port, const char *address="", al_sec timeout=0, int type=UDP|DGRAM)
Definition: al_Socket.hpp:225
Definition: al_App.hpp:23