summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/include/pgm/ip/pgm_endpoint.hh
blob: 1114719ef9fd18dbe9115f7da2aae58fc2ad1f6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
 * 
 * PGM endpoint
 *
 * Copyright (c) 2010 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __PGM_IP_PGM_ENDPOINT_HH__
#define __PGM_IP_PGM_ENDPOINT_HH__

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#	pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

namespace pgm {
#define restrict
#include <pgm/pgm.h>
}

namespace ip {

template <typename InternetProtocol>
class pgm_endpoint
{
public:
	/// The protocol type associated with the endpoint.
	typedef InternetProtocol protocol_type;

	typedef struct cpgm::pgm_sockaddr_t data_type;

	/// Default constructor.
	pgm_endpoint()
	: data_()
	{
		data_.sa_port = 0;
		cpgm::pgm_tsi_t tmp_addr = PGM_TSI_INIT;
		data_.sa_addr = tmp_addr;
	}

	/// Construct an endpoint using a port number, specified in host byte
	/// order.  The GSI will be generated from the node name.
	/**
	 * @par examples
	 * To initialise a PGM endpoint for port 7500, use:
	 * @code
	 * ip::pgm::endpoint ep (7500);
	 * @endcode
	 */
	pgm_endpoint (unsigned short port_num)
	: data_()
	{
		data_.sa_port = port_num;
		data_.sa_addr.sport = 0;
		pgm_gsi_create_from_hostname (&data_.sa_addr.gsi, NULL);
	}

	/// Construct an endpoint using a port number and a TSI.
	pgm_endpoint (const cpgm::pgm_tsi_t& tsi, unsigned short port_num)
	: data_()
	{
		data_.sa_port = port_num;
		data_.sa_addr = tsi;
	}

	/// Construct an endpoint using a port number and a memory area.
	pgm_endpoint (const void* src, std::size_t len, unsigned short port_num)
	: data_()
	{
		data_.sa_port = port_num;
		data_.sa_addr.sport = 0;
		pgm_gsi_create_from_data (&data_.sa_addr.gsi, src, len);
	}

	/// Copy constructor.
	pgm_endpoint (const pgm_endpoint& other)
	: data_ (other.data_)
	{
	}

	/// Assign from another endpoint.
	pgm_endpoint& operator= (const pgm_endpoint& other)
	{
		data_ = other.data_;
		return *this;
	}

	/// Get the underlying endpoint in the native type.
	const data_type* data() const
	{
		return &data_;
	}

	/// Get the underlying size of the endpoint in the native type.
	std::size_t size() const
	{
		return sizeof(data_type);
	}

	/// Get the port associated with the endpoint. The port number is always in
	/// the host's byte order.
	unsigned short port() const
	{
		return data_.sa_port;
	}

	/// Set the port associated with the endpoint. The port number is always in
	/// the host's byte order.
	void port (unsigned short port_num)
	{
		data_.sa_port = port_num;
	}

	/// Get the TSI associated with the endpoint.
	const cpgm::pgm_tsi_t* address() const
	{
		return &data_.sa_addr;
	}

	/// Set the TSI associated with the endpoint.
	void address (cpgm::pgm_tsi_t& addr)
	{
		data_.sa_addr = addr;
	}

	/// Compare two endpoints for equality.
	friend bool operator== (const pgm_endpoint<InternetProtocol>& e1,
		const pgm_endpoint<InternetProtocol>& e2)
	{
		return e1.address() == e2.address() && e1.port() == e2.port();
	}

	/// Compare two endpoints for inequality.
	friend bool operator!= (const pgm_endpoint<InternetProtocol>& e1,
		const pgm_endpoint<InternetProtocol>& e2)
	{
		return e1.address() != e2.address() || e1.port() != e2.port();
	}

	/// Compare endpoints for ordering.
	friend bool operator<(const pgm_endpoint<InternetProtocol>& e1,
		const pgm_endpoint<InternetProtocol>& e2)
	{
		if (e1.address() < e2.address())
			return true;
		if (e1.address() != e2.address())
			return false;
		return e1.port() < e2.port();
	}

private:
	// The underlying PGM socket address.
	data_type data_;
};

} // namespace ip

#endif /* __PGM_IP_PGM_ENDPOINT_HH__ */