00001
00002
00003
00004 #ifndef GOSU_SOCKETS_HPP
00005 #define GOSU_SOCKETS_HPP
00006
00007 #include <boost/cstdint.hpp>
00008 #include <boost/function.hpp>
00009 #include <boost/scoped_ptr.hpp>
00010 #include <cstddef>
00011 #include <string>
00012
00013 namespace Gosu
00014 {
00015
00016
00017 typedef boost::uint32_t SocketAddress;
00018 typedef boost::uint16_t SocketPort;
00019 const SocketPort anyPort = 0;
00020
00025 SocketAddress stringToAddress(const std::string& s);
00027 std::string addressToString(SocketAddress address);
00028
00033 class MessageSocket
00034 {
00035 struct Impl;
00036 boost::scoped_ptr<Impl> pimpl;
00037
00038 public:
00042 explicit MessageSocket(SocketPort port);
00043 ~MessageSocket();
00044
00046 SocketAddress address() const;
00048 SocketPort port() const;
00051 std::size_t maxMessageSize() const;
00052
00055 void update();
00056
00059 void send(SocketAddress address, SocketPort port,
00060 const void* buffer, std::size_t size);
00061
00062
00063
00065 boost::function<void (SocketAddress, SocketPort, const void*,
00066 std::size_t)> onReceive;
00067 };
00068
00069 enum CommMode
00070 {
00071 cmRaw,
00072
00073 cmManaged
00074 };
00075
00076 class Socket;
00077
00078 class CommSocket
00079 {
00080 struct Impl;
00081 boost::scoped_ptr<Impl> pimpl;
00082
00083 public:
00084 CommSocket(CommMode mode, SocketAddress targetAddress,
00085 SocketPort targetPort);
00086 CommSocket(CommMode mode, Socket& socket);
00087 ~CommSocket();
00088
00089 SocketAddress address() const;
00090 SocketPort port() const;
00091 SocketAddress remoteAddress() const;
00092 SocketPort remotePort() const;
00093 CommMode mode() const;
00094
00095 bool connected() const;
00096 void disconnect();
00097 bool keepAlive() const;
00098 void setKeepAlive(bool value);
00099
00100 void update();
00101 void send(const void* buffer, std::size_t size);
00102 void sendPendingData();
00103 std::size_t pendingBytes() const;
00104
00105 boost::function<void (const void*, std::size_t)> onReceive;
00106 boost::function<void ()> onDisconnection;
00107 };
00108
00109 class ListenerSocket
00110 {
00111 struct Impl;
00112 boost::scoped_ptr<Impl> pimpl;
00113
00114 public:
00115 ListenerSocket(SocketPort port);
00116 ~ListenerSocket();
00117
00118 SocketAddress address() const;
00119 SocketPort port() const;
00120
00121 void update();
00122
00123
00124
00125 boost::function<void (Socket&)> onConnection;
00126 };
00127 }
00128
00129 #endif