Sockets.hpp
Go to the documentation of this file.
1 
2 
3 
4 #ifndef GOSU_SOCKETS_HPP
5 #define GOSU_SOCKETS_HPP
6 
7 #include <Gosu/TR1.hpp>
8 #include <cstddef>
9 #include <string>
10 
11 namespace Gosu
12 {
14  typedef std::tr1::uint32_t SocketAddress;
15 
17  typedef std::tr1::uint16_t SocketPort;
18 
21  const SocketPort anyPort = 0;
22 
27  SocketAddress stringToAddress(const std::string& s);
29  std::string addressToString(SocketAddress address);
30 
36  {
37  struct Impl;
38  const std::auto_ptr<Impl> pimpl;
39 
40  public:
44  explicit MessageSocket(SocketPort port);
46 
48  SocketAddress address() const;
50  SocketPort port() const;
53  std::size_t maxMessageSize() const;
54 
57  void update();
58 
61  void send(SocketAddress address, SocketPort port,
62  const void* buffer, std::size_t size);
63  /*void broadcast(SocketPort port, const void* buffer,
64  std::size_t size);*/
65 
67  std::tr1::function<void (SocketAddress, SocketPort, const void*,
68  std::size_t)> onReceive;
69  };
70 
73  enum CommMode
74  {
76  //cmLines,
78  };
79 
80  class Socket;
81 
84  class CommSocket
85  {
86  struct Impl;
87  const std::auto_ptr<Impl> pimpl;
88 
89  public:
90  CommSocket(CommMode mode, SocketAddress targetAddress,
91  SocketPort targetPort);
92  CommSocket(CommMode mode, Socket& socket);
93  ~CommSocket();
94 
95  SocketAddress address() const;
96  SocketPort port() const;
98  SocketPort remotePort() const;
99  CommMode mode() const;
100 
101  bool connected() const;
102  void disconnect();
103  bool keepAlive() const;
104  void setKeepAlive(bool value);
105 
106  void update();
107  void send(const void* buffer, std::size_t size);
108  void sendPendingData();
109  std::size_t pendingBytes() const;
110 
111  std::tr1::function<void (const void*, std::size_t)> onReceive;
112  std::tr1::function<void ()> onDisconnection;
113  };
114 
118  {
119  struct Impl;
120  const std::auto_ptr<Impl> pimpl;
121 
122  public:
124  ~ListenerSocket();
125 
126  SocketAddress address() const;
127  SocketPort port() const;
128 
129  void update();
130 
133  std::tr1::function<void (Socket&)> onConnection;
134  };
135 }
136 
137 #endif