]> Dogcows Code - chaz/yoink/blob - src/stlplus/portability/udp_sockets.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / udp_sockets.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Daniel Milton adapted by Andy Rushton
4 // Copyright: (c) Daniel Milton, Andy Rushton 2009
5 // License: BSD License, see ../docs/license.html
6
7 ////////////////////////////////////////////////////////////////////////////////
8
9 #include "udp_sockets.hpp"
10
11 ////////////////////////////////////////////////////////////////////////////////
12
13 namespace stlplus
14 {
15
16 ////////////////////////////////////////////////////////////////////////////////
17 // UDP client
18 ////////////////////////////////////////////////////////////////////////////////
19
20 // create an uninitialised socket
21 UDP_client::UDP_client(void) : IP_socket(UDP)
22 {
23 }
24
25 // Send/Receive datagram packets to/from the given address/remote port on the local port.
26 // Enables default send to remote address/port
27 // - remote_address: IP name or number of remote host
28 // - remote_port: port number of remote host
29 // - local_port: port number to receive on - 0 to get an ephemeral port.
30 UDP_client::UDP_client(const std::string& remote_address, unsigned short remote_port, unsigned short local_port) :
31 IP_socket(UDP)
32 {
33 initialise(remote_address, remote_port, local_port);
34 }
35
36 // Send/Receive datagram packets to/from the given address/remote port on the given local port
37 // Enables default send to remote address/port
38 // - remote_address: IP address of remote host - pre-looked-up using ip_lookup
39 // - remote_port: port number of remote host
40 // - local_port: port number to receive on - 0 to get an ephemeral port.
41 UDP_client::UDP_client(unsigned long remote_address, unsigned short remote_port, unsigned short local_port) :
42 IP_socket(UDP)
43 {
44 initialise(remote_address, remote_port, local_port);
45 }
46
47 // Send/Receive datagram packets to/from the given address/remote port on the local port.
48 // Enables default send to remote address/port
49 // - remote_address: IP name or number of remote host
50 // - remote_port: port number of remote host
51 // - local_port: port number to receive on - 0 to get an ephemeral port.
52 // - returns a success flag
53 bool UDP_client::initialise(const std::string& address, unsigned short remote_port, unsigned short local_port)
54 {
55 // lookup the address and convert it into an IP number
56 unsigned long remote_address = IP_socket::ip_lookup(address);
57 if (!remote_address) return false;
58 return initialise(remote_address, remote_port, local_port);
59 }
60
61 // Send/Receive datagram packets to/from the given address/remote port on the given local port
62 // Enables default send to remote address/port
63 // - remote_address: IP address of remote host - pre-looked-up using ip_lookup
64 // - remote_port: port number of remote host
65 // - local_port: port number to receive on - 0 to get an ephemeral port.
66 // - returns a success flag
67 bool UDP_client::initialise(unsigned long remote_address, unsigned short remote_port, unsigned short local_port)
68 {
69 if (!IP_socket::bind(remote_address, local_port)) return false;
70 return IP_socket::connect(remote_address, remote_port);
71 }
72
73 // send to the remote address/port setup in initialise, from the local port also setup in initialise.
74 // send data through the socket as a single datagram
75 // - packet: string containing data to be sent - if data is successfully sent it is removed
76 // - returns success flag
77 bool UDP_client::send(std::string& packet)
78 {
79 return IP_socket::send_packet(packet);
80 }
81
82 // datagram receive
83 // - packet: string to receive data from datagram - if data is successfully sent it is appended
84 // - returns success flag - i.e. packet successfully received
85 bool UDP_client::receive(std::string& packet)
86 {
87 return IP_socket::receive_packet(packet);
88 }
89
90 ////////////////////////////////////////////////////////////////////////////////
91 // UDP Server
92 ////////////////////////////////////////////////////////////////////////////////
93
94 // create an uninitialised socket
95 UDP_server::UDP_server(void) : IP_socket(UDP)
96 {
97 }
98
99 // Initialise socket.
100 // Receive datagram packets from any address on provided local receiving port.
101 // No default send possible.
102 // - local_port: port number to receive on - 0 to get an ephemeral port.
103 UDP_server::UDP_server(unsigned short local_port) : IP_socket(UDP)
104 {
105 initialise(local_port);
106 }
107
108 // Initialise socket.
109 // Receive datagram packets from any address on provided local receiving port.
110 // No default send possible.
111 // - local_port: port number to receive on - 0 to get an ephemeral port.
112 // - returns a success flag
113 bool UDP_server::initialise(unsigned short local_port)
114 {
115 return IP_socket::bind_any(local_port);
116 }
117
118 // send to the address/port given here, from the local port setup in initialise.
119 // send data through the socket as a single datagram
120 // - packet: string containing data to be sent - if data is successfully sent it is removed
121 // - remote_address: IP name (stlplus.sourceforge.net) or dotted number (216.34.181.96)
122 // - remote_port: port number of remote host
123 // - returns success flag
124 bool UDP_server::send(std::string& packet, const std::string& remote_address, unsigned short remote_port)
125 {
126 unsigned long ip_address = ip_lookup(remote_address);
127 if (ip_address == 0) return false;
128 return send(packet, ip_address, remote_port);
129 }
130
131 // send to the address/port given here, from the local port setup in initialise.
132 // send data through the socket as a single datagram
133 // - packet: string containing data to be sent - if data is successfully sent it is removed
134 // - remote_address: pre-looked-up IP address of remote host
135 // - remote_port: port number of remote host
136 // - returns success flag
137 bool UDP_server::send(std::string& packet, unsigned long remote_address, unsigned short remote_port)
138 {
139 return IP_socket::send_packet(packet, remote_address, remote_port);
140 }
141
142 // datagram receive
143 // - packet: string to receive data from datagram - if data is successfully sent it is appended
144 // - remote_address: the address of the client that sent the packet, can then be used to reply
145 // - remote_port: the port of the client that sent the packet, can then be used to reply
146 // - returns success flag - i.e. packet successfully received
147 bool UDP_server::receive(std::string& packet, unsigned long& remote_address, unsigned short& remote_port)
148 {
149 return IP_socket::receive_packet(packet, remote_address, remote_port);
150 }
151
152 /////////////////////////////////////////////////////////////////////////////
153 // fire and forget UDP client packet send function
154 ////////////////////////////////////////////////////////////////////////////////
155
156 bool UDP_send(const std::string& packet,
157 const std::string& remote_address, unsigned short remote_port, unsigned short local_port)
158 {
159 UDP_client client(remote_address, remote_port, local_port);
160 if (!client.initialised()) return false;
161 std::string packet_copy = packet;
162 return client.send(packet_copy);
163 }
164
165 /////////////////////////////////////////////////////////////////////////////
166
167 } // end namespace stlplus
This page took 0.039204 seconds and 4 git commands to generate.