Sockets.hpp
Go to the documentation of this file.
1 
4 #ifndef GOSU_SOCKETS_HPP
5 #define GOSU_SOCKETS_HPP
6 
7 #include <Gosu/TR1.hpp>
8 #include <cstddef>
9 #include <string>
10 #include <Gosu/Platform.hpp>
11 
12 namespace Gosu
13 {
15  typedef std::tr1::uint32_t SocketAddress;
16 
18  typedef std::tr1::uint16_t SocketPort;
19 
22  const SocketPort anyPort = 0;
23 
28  SocketAddress stringToAddress(const std::string& s);
30  std::string addressToString(SocketAddress address);
31 
37  {
38  struct Impl;
39  const GOSU_UNIQUE_PTR<Impl> pimpl;
40 #if defined(GOSU_CPP11_ENABLED)
41  MessageSocket(const MessageSocket&) = delete;
42  MessageSocket& operator=(const MessageSocket&) = delete;
43  MessageSocket(MessageSocket&&) = delete;
44  MessageSocket& operator=(MessageSocket&&) = delete;
45 #endif
46 
47  public:
51  explicit MessageSocket(SocketPort port);
53 
55  SocketAddress address() const;
57  SocketPort port() const;
60  std::size_t maxMessageSize() const;
61 
64  void update();
65 
68  void send(SocketAddress address, SocketPort port,
69  const void* buffer, std::size_t size);
70  /*void broadcast(SocketPort port, const void* buffer,
71  std::size_t size);*/
72 
74  std::tr1::function<void (SocketAddress, SocketPort, const void*,
75  std::size_t)> onReceive;
76  };
77 
80  enum CommMode
81  {
83  //cmLines,
85  };
86 
87  class Socket;
88 
91  class CommSocket
92  {
93  struct Impl;
94  const GOSU_UNIQUE_PTR<Impl> pimpl;
95 #if defined(GOSU_CPP11_ENABLED)
96  CommSocket(const CommSocket&) = delete;
97  CommSocket& operator=(const CommSocket&) = delete;
98  CommSocket(CommSocket&&) = delete;
99  CommSocket& operator=(CommSocket&&) = delete;
100 #endif
101 
102  public:
103  CommSocket(CommMode mode, SocketAddress targetAddress,
104  SocketPort targetPort);
105  CommSocket(CommMode mode, Socket& socket);
106  ~CommSocket();
107 
108  SocketAddress address() const;
109  SocketPort port() const;
111  SocketPort remotePort() const;
112  CommMode mode() const;
113 
114  bool connected() const;
115  void disconnect();
116  bool keepAlive() const;
117  void setKeepAlive(bool value);
118 
119  void update();
120  void send(const void* buffer, std::size_t size);
121  void sendPendingData();
122  std::size_t pendingBytes() const;
123 
124  std::tr1::function<void (const void*, std::size_t)> onReceive;
125  std::tr1::function<void ()> onDisconnection;
126  };
127 
131  {
132  struct Impl;
133  const GOSU_UNIQUE_PTR<Impl> pimpl;
134 #if defined(GOSU_CPP11_ENABLED)
135  ListenerSocket(const ListenerSocket&) = delete;
136  ListenerSocket& operator=(const ListenerSocket&) = delete;
137  ListenerSocket(ListenerSocket&&) = delete;
138  ListenerSocket& operator=(ListenerSocket&&) = delete;
139 #endif
140 
141  public:
143  ~ListenerSocket();
144 
145  SocketAddress address() const;
146  SocketPort port() const;
147 
148  void update();
149 
152  std::tr1::function<void (Socket&)> onConnection;
153  };
154 }
155 
156 #endif
Wraps a TCP socket that is used for one part of bi-directional communication.
Definition: Sockets.hpp:91
Includes all parts of C++03 (TR1) that are relevant for Gosu.
void setKeepAlive(bool value)
std::size_t maxMessageSize() const
Returns the maximum size, in bytes, of a packet that can be sent from this socket.
std::tr1::uint16_t SocketPort
Ports are returned from and given to Gosu functions in host byte order.
Definition: Sockets.hpp:18
SocketPort port() const
MessageSocket(SocketPort port)
Opens a message socket for listening at the specified port.
SocketAddress address() const
SocketPort port() const
ListenerSocket(SocketPort port)
std::string addressToString(SocketAddress address)
Converts an address into a dotted IP4 string.
void update()
Collects all the packets that were sent to this socket and calls onReceive for each of them...
std::tr1::function< void()> onDisconnection
Definition: Sockets.hpp:125
std::tr1::uint32_t SocketAddress
Addresses are returned from and given to Gosu functions in host byte order.
Definition: Sockets.hpp:15
Wraps an UDP socket.
Definition: Sockets.hpp:36
SocketPort port() const
Returns the local port of the socket.
const SocketPort anyPort
Constant that can be used as a placeholder for an arbitrary port, e.g.
Definition: Sockets.hpp:22
std::tr1::function< void(SocketAddress, SocketPort, const void *, std::size_t)> onReceive
If assigned, will be called by update for every packet received.
Definition: Sockets.hpp:75
SocketAddress remoteAddress() const
std::tr1::function< void(Socket &)> onConnection
This signal is fired by update() whenever someone connects to the port which is currently listened on...
Definition: Sockets.hpp:152
std::size_t pendingBytes() const
void send(SocketAddress address, SocketPort port, const void *buffer, std::size_t size)
Sends something to the given port of the computer identified by the address.
CommMode
Defines the way in which data is collected until the onReceive event is called for CommSockets...
Definition: Sockets.hpp:80
Wraps a TCP socket that waits on a specific port and can create CommSocket instances via its onConnec...
Definition: Sockets.hpp:130
SocketAddress address() const
Returns the local address of the socket.
bool connected() const
Macros and utility functions to facilitate programming on all of Gosu&#39;s supported platforms...
SocketAddress address() const
CommSocket(CommMode mode, SocketAddress targetAddress, SocketPort targetPort)
SocketPort remotePort() const
void sendPendingData()
SocketAddress stringToAddress(const std::string &s)
Tries to convert a dotted IP4 string into an address suitable for socket functions.
void send(const void *buffer, std::size_t size)
CommMode mode() const
bool keepAlive() const
std::tr1::function< void(const void *, std::size_t)> onReceive
Definition: Sockets.hpp:124