]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/portability/udp_sockets.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / udp_sockets.cpp
diff --git a/src/stlplus/portability/udp_sockets.cpp b/src/stlplus/portability/udp_sockets.cpp
new file mode 100644 (file)
index 0000000..137be3d
--- /dev/null
@@ -0,0 +1,167 @@
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Daniel Milton adapted by Andy Rushton\r
+//   Copyright: (c) Daniel Milton, Andy Rushton 2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+#include "udp_sockets.hpp"\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+namespace stlplus\r
+{\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // UDP client\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  // create an uninitialised socket\r
+  UDP_client::UDP_client(void) : IP_socket(UDP)\r
+  {\r
+  }\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
+  UDP_client::UDP_client(const std::string& remote_address, unsigned short remote_port, unsigned short local_port) :\r
+    IP_socket(UDP)\r
+  {\r
+    initialise(remote_address, remote_port, local_port);\r
+  }\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::UDP_client(unsigned long remote_address, unsigned short remote_port, unsigned short local_port) :\r
+    IP_socket(UDP)\r
+  {\r
+    initialise(remote_address, remote_port, local_port);\r
+  }\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 UDP_client::initialise(const std::string& address, unsigned short remote_port, unsigned short local_port)\r
+  {\r
+    // lookup the address and convert it into an IP number\r
+    unsigned long remote_address = IP_socket::ip_lookup(address);\r
+    if (!remote_address) return false;\r
+    return initialise(remote_address, remote_port, local_port);\r
+  }\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 UDP_client::initialise(unsigned long remote_address, unsigned short remote_port, unsigned short local_port)\r
+  {\r
+    if (!IP_socket::bind(remote_address, local_port)) return false;\r
+    return IP_socket::connect(remote_address, remote_port);\r
+  }\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 UDP_client::send(std::string& packet)\r
+  {\r
+    return IP_socket::send_packet(packet);\r
+  }\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 UDP_client::receive(std::string& packet)\r
+  {\r
+    return IP_socket::receive_packet(packet);\r
+  }\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // UDP Server\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  // create an uninitialised socket\r
+  UDP_server::UDP_server(void) : IP_socket(UDP)\r
+  {\r
+  }\r
+\r
+  // Initialise socket.\r
+  // Receive datagram packets from any address on provided local receiving port.\r
+  // No default send possible.\r
+  // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+  UDP_server::UDP_server(unsigned short local_port) : IP_socket(UDP)\r
+  {\r
+    initialise(local_port);\r
+  }\r
+\r
+  // Initialise socket.\r
+  // Receive datagram packets from any address on provided local receiving port.\r
+  // No default send possible.\r
+  // - local_port: port number to receive on - 0 to get an ephemeral port.\r
+  // - returns a success flag\r
+  bool UDP_server::initialise(unsigned short local_port)\r
+  {\r
+    return IP_socket::bind_any(local_port);\r
+  }\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 UDP_server::send(std::string& packet, const std::string& remote_address, unsigned short remote_port)\r
+  {\r
+    unsigned long ip_address = ip_lookup(remote_address);\r
+    if (ip_address == 0) return false;\r
+    return send(packet, ip_address, remote_port);\r
+  }\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 UDP_server::send(std::string& packet, unsigned long remote_address, unsigned short remote_port)\r
+  {\r
+    return IP_socket::send_packet(packet, remote_address, remote_port);\r
+  }\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 UDP_server::receive(std::string& packet, unsigned long& remote_address, unsigned short& remote_port)\r
+  {\r
+    return IP_socket::receive_packet(packet, remote_address, remote_port);\r
+  }\r
+\r
+  /////////////////////////////////////////////////////////////////////////////\r
+  // fire and forget UDP client packet send function\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  bool UDP_send(const std::string& packet,\r
+                const std::string& remote_address, unsigned short remote_port, unsigned short local_port)\r
+  {\r
+    UDP_client client(remote_address, remote_port, local_port);\r
+    if (!client.initialised()) return false;\r
+    std::string packet_copy = packet;\r
+    return client.send(packet_copy);\r
+  }\r
+\r
+  /////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
This page took 0.023962 seconds and 4 git commands to generate.