]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/portability/udp_sockets.hpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / udp_sockets.hpp
diff --git a/src/stlplus/portability/udp_sockets.hpp b/src/stlplus/portability/udp_sockets.hpp
new file mode 100644 (file)
index 0000000..5097d44
--- /dev/null
@@ -0,0 +1,268 @@
+#ifndef STLPLUS_UDP_SOCKET\r
+#define STLPLUS_UDP_SOCKET\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+// Author:    Andy Rushton\r
+// Copyright: (c) Southampton University 1999-2004\r
+//            (c) Andy Rushton           2004-2009\r
+// License:   BSD License, see ../docs/license.html\r
+\r
+// A platform-independent (Unix and Windows anyway) interface to UDP sockets\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+#include "portability_fixes.hpp"\r
+#include "ip_sockets.hpp"\r
+#include <string>\r
+\r
+namespace stlplus\r
+{\r
+\r
+  //////////////////////////////////////////////////////////////////////////////\r
+  // UDP client - creates a connectioned socket\r
+\r
+  class UDP_client : protected IP_socket\r
+  {\r
+  public:\r
+\r
+    // create an uninitialised socket\r
+    UDP_client(void);\r
+\r
+    // Send/Receive datagram packets to/from the given address/remote port on the local port.\r
+    // - remote_address: IP name or number of remote host\r
+    // - remote_port: port number of remote host\r
+    // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+    UDP_client(const std::string& remote_address, unsigned short remote_port, unsigned short local_port=0);\r
+\r
+    // Send/Receive datagram packets to/from the given address/remote port on the given local port\r
+    // Enables default send to remote address/port\r
+    // - remote_address: IP address of remote host - pre-looked-up using ip_lookup\r
+    // - remote_port: port number of remote host\r
+    // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+    UDP_client(unsigned long remote_address, unsigned short remote_port, unsigned short local_port=0);\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // initialisation, connection\r
+\r
+    // function for performing IP lookup (i.e. gethostbyname)\r
+    // could be standalone but making it a member means that it can use the socket's error handler\r
+    // i.e. if this fails, the sockets error code will be set - clear it to use the socket again\r
+    // - remote_address: IP name (stlplus.sourceforge.net) or dotted number (216.34.181.96)\r
+    // - returns the IP address as a long integer - zero if there's an error\r
+    // unsigned long ip_lookup(const std::string& remote_address);\r
+    using IP_socket::ip_lookup;\r
+\r
+    // Send/Receive datagram packets to/from the given address/remote port on the local port.\r
+    // Enables default send to remote address/port\r
+    // - remote_address: IP name or number of remote host\r
+    // - remote_port: port number of remote host\r
+    // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+    // - returns a success flag\r
+    bool initialise(const std::string& remote_address, unsigned short remote_port, unsigned short local_port=0);\r
+\r
+    // Send/Receive datagram packets to/from the given address/remote port on the given local port\r
+    // Enables default send to remote address/port\r
+    // - remote_address: IP address of remote host - pre-looked-up using ip_lookup\r
+    // - remote_port: port number of remote host\r
+    // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+    // - returns a success flag\r
+    bool initialise(unsigned long remote_address, unsigned short remote_port, unsigned short local_port=0);\r
+\r
+    // test whether this is an initialised socket\r
+    // - returns whether this is initialised\r
+    // bool initialised(void) const;\r
+    using IP_socket::initialised;\r
+\r
+    // close, i.e. disconnect the socket\r
+    // - returns a success flag\r
+    // bool close(void);\r
+    using IP_socket::close;\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // sending/receiving\r
+\r
+    // test whether a socket is connected and ready to send data, returns if ready or on timeout\r
+    // - timeout: how long to wait in microseconds if not connected yet (blocking)\r
+    // - returns status\r
+    // bool send_ready(unsigned timeout = 0);\r
+    using IP_socket::send_ready;\r
+\r
+    // send to the remote address/port setup in initialise, from the local port also setup in initialise.\r
+    // send data through the socket as a single datagram\r
+    // - packet: string containing data to be sent - if data is successfully sent it is removed\r
+    // - returns success flag\r
+    bool send(std::string& packet);\r
+\r
+    // test whether a socket is connected and ready to receive data, returns if ready or on timeout\r
+    // - timeout: how long to wait in microseconds if not connected yet (blocking)\r
+    // - returns status\r
+    // bool receive_ready(unsigned timeout = 0);\r
+    using IP_socket::receive_ready;\r
+\r
+    // datagram receive\r
+    // - packet: string to receive data from datagram - if data is successfully sent it is appended\r
+    // - returns success flag - i.e. packet successfully received\r
+    bool receive(std::string& packet);\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // informational\r
+\r
+    // the local port number of the connection\r
+    // returns the port number, 0 if not bound to a port\r
+    // unsigned short local_port(void) const;\r
+    using IP_socket::local_port;\r
+\r
+    // the remote address of the connection\r
+    // returns the address, 0 if ANY address\r
+    // unsigned long remote_address(void) const;\r
+    using IP_socket::remote_address;\r
+\r
+    // the remote port number of the connection\r
+    // returns the port number, 0 if not bound to a port\r
+    // unsigned short remote_port(void) const;\r
+    using IP_socket::remote_port;\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // error handling\r
+    // errors are set internally\r
+    // an error code of 0 is the test for no error, don't rely on the message being an empty string\r
+    // an error code != 0 means an error, then there will be a message explaining the error\r
+\r
+    // if an error is set it stays set - so you must clear it before further operations\r
+    // void clear_error (void);\r
+    using IP_socket::clear_error ;\r
+\r
+    // get the error code of the last error\r
+    // int error(void) const;\r
+    using IP_socket::error;\r
+\r
+    // get the explanatory message of the last error\r
+    // std::string message(void) const;\r
+    using IP_socket::message;\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+\r
+  private:\r
+    IP_socket m_socket;\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // UDP server - creates a connectionless (multicast) listener socket\r
+\r
+  class UDP_server : protected IP_socket\r
+  {\r
+  public:\r
+\r
+    // create an uninitialised socket\r
+    UDP_server(void);\r
+\r
+    // Initialise socket.\r
+    // Receive datagram packets from any address on provided local receiving port.\r
+    // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+    UDP_server(unsigned short local_port);\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // initialisation, connection\r
+\r
+    // function for performing IP lookup (i.e. gethostbyname)\r
+    // could be standalone but making it a member means that it can use the socket's error handler\r
+    // i.e. if this fails, the sockets error code will be set - clear it to use the socket again\r
+    // - remote_address: IP name (stlplus.sourceforge.net) or dotted number (216.34.181.96)\r
+    // - returns the IP address as a long integer - zero if there's an error\r
+    // unsigned long ip_lookup(const std::string& remote_address);\r
+    using IP_socket::ip_lookup;\r
+\r
+    // Initialise socket.\r
+    // Receive datagram packets from any address on provided local receiving port.\r
+    // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+    // - returns a success flag\r
+    bool initialise(unsigned short local_port);\r
+\r
+    // test whether this is an initialised socket\r
+    // - returns whether this is initialised\r
+    // bool initialised(void) const;\r
+    using IP_socket::initialised;\r
+\r
+    // close, i.e. disconnect the socket\r
+    // - returns a success flag\r
+    // bool close(void);\r
+    using IP_socket::close;\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // sending/receiving\r
+\r
+    // test whether a socket is connected and ready to send data, returns if ready or on timeout\r
+    // - timeout: how long to wait in microseconds if not connected yet (blocking)\r
+    // - returns status\r
+    // bool send_ready(unsigned timeout = 0);\r
+    using IP_socket::send_ready;\r
+\r
+    // send to the address/port given here, from the local port setup in initialise.\r
+    // send data through the socket as a single datagram\r
+    // - packet: string containing data to be sent - if data is successfully sent it is removed\r
+    // - remote_address: IP name (stlplus.sourceforge.net) or dotted number (216.34.181.96)\r
+    // - remote_port: port number of remote host\r
+    // - returns success flag\r
+    bool send(std::string& packet, const std::string& remote_address, unsigned short remote_port);\r
+\r
+    // send to the address/port given here, from the local port setup in initialise.\r
+    // send data through the socket as a single datagram\r
+    // - packet: string containing data to be sent - if data is successfully sent it is removed\r
+    // - remote_address: pre-looked-up IP address of remote host\r
+    // - remote_port: port number of remote host\r
+    // - returns success flag\r
+    bool send(std::string& packet, unsigned long remote_address, unsigned short remote_port);\r
+\r
+    // test whether a socket is connected and ready to receive data, returns if ready or on timeout\r
+    // - timeout: how long to wait in microseconds if not connected yet (blocking)\r
+    // - returns status\r
+    // bool receive_ready(unsigned timeout = 0);\r
+    using IP_socket::receive_ready;\r
+\r
+    // datagram receive\r
+    // - packet: string to receive data from datagram - if data is successfully sent it is appended\r
+    // - remote_address: the address of the client that sent the packet, can then be used to reply\r
+    // - remote_port: the port of the client that sent the packet, can then be used to reply\r
+    // - returns success flag - i.e. packet successfully received\r
+    bool receive(std::string& packet, unsigned long& remote_address, unsigned short& remote_port);\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // informational\r
+\r
+    // the local port number of the connection\r
+    // returns the port number, 0 if not bound to a port\r
+    // unsigned short local_port(void) const;\r
+    using IP_socket::local_port;\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+    // error handling\r
+    // errors are set internally\r
+    // an error code of 0 is the test for no error, don't rely on the message being an empty string\r
+    // an error code != 0 means an error, then there will be a message explaining the error\r
+\r
+    // if an error is set it stays set - so you must clear it before further operations\r
+    // void clear_error(void);\r
+    using IP_socket::clear_error;\r
+\r
+    // get the error code of the last error\r
+    // int error(void) const;\r
+    using IP_socket::error;\r
+\r
+    // get the explanatory message of the last error\r
+    // std::string message(void) const;\r
+    using IP_socket::message;\r
+\r
+    ////////////////////////////////////////////////////////////////////////////\r
+  };\r
+\r
+  /////////////////////////////////////////////////////////////////////////////\r
+  // fire and forget UDP client packet send function\r
+\r
+  bool UDP_send(const std::string& packet,\r
+                const std::string& remote_address, unsigned short remote_port, unsigned short local_port = 0);\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
+\r
+#endif\r
This page took 0.025413 seconds and 4 git commands to generate.