PageRenderTime 92ms CodeModel.GetById 73ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llhost.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 151 lines | 91 code | 27 blank | 33 comment | 12 complexity | 9e3ee3ad05bbb7c5c451d44a79a9c483 MD5 | raw file
  1/** 
  2 * @file llhost.h
  3 * @brief a LLHost uniquely defines a host (Simulator, Proxy or other)
  4 * across the network
  5 *
  6 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#ifndef LL_LLHOST_H
 29#define LL_LLHOST_H
 30
 31#include <iostream>
 32#include <string>
 33
 34#include "net.h"
 35
 36const U32 INVALID_PORT = 0;
 37const U32 INVALID_HOST_IP_ADDRESS = 0x0;
 38
 39class LLHost {
 40protected:
 41	U32			mPort;
 42	U32         mIP;
 43public:
 44	
 45	static LLHost invalid;
 46
 47	// CREATORS
 48	LLHost()
 49	:	mPort(INVALID_PORT),
 50		mIP(INVALID_HOST_IP_ADDRESS)
 51	{ } // STL's hash_map expect this T()
 52
 53	LLHost( U32 ipv4_addr, U32 port )
 54	:	mPort( port ) 
 55	{
 56		mIP = ipv4_addr;
 57	}
 58
 59	LLHost( const std::string& ipv4_addr, U32 port )
 60	:	mPort( port )
 61	{ 
 62		mIP = ip_string_to_u32(ipv4_addr.c_str());
 63	}
 64
 65	explicit LLHost(const U64 ip_port)
 66	{
 67		U32 ip = (U32)(ip_port >> 32);
 68		U32 port = (U32)(ip_port & (U64)0xFFFFFFFF);
 69		mIP = ip;
 70		mPort = port;
 71	}
 72
 73	explicit LLHost(const std::string& ip_and_port);
 74
 75	~LLHost()
 76	{ }
 77
 78	// MANIPULATORS
 79	void	set( U32 ip, U32 port )				{ mIP = ip; mPort = port; }
 80	void	set( const std::string& ipstr, U32 port )	{ mIP = ip_string_to_u32(ipstr.c_str()); mPort = port; }
 81	void	setAddress( const std::string& ipstr )		{ mIP = ip_string_to_u32(ipstr.c_str()); }
 82	void	setAddress( U32 ip )				{ mIP = ip; }
 83	void	setPort( U32 port )					{ mPort = port; }
 84	BOOL    setHostByName(const std::string& hname);
 85
 86	LLHost&	operator=(const LLHost &rhs);
 87	void    invalidate()                        { mIP = INVALID_HOST_IP_ADDRESS; mPort = INVALID_PORT;};
 88
 89	// READERS
 90	U32		getAddress() const							{ return mIP; }
 91	U32		getPort() const								{ return mPort; }
 92	BOOL	isOk() const								{ return (mIP != INVALID_HOST_IP_ADDRESS) && (mPort != INVALID_PORT); }
 93	size_t	hash() const								{ return (mIP << 16) | (mPort & 0xffff); }
 94	std::string getString() const;
 95	std::string getIPString() const;
 96	std::string getHostName() const;
 97	std::string getIPandPort() const;
 98
 99	friend std::ostream& operator<< (std::ostream& os, const LLHost &hh);
100
101	// This operator is not well defined. does it expect a
102	// "192.168.1.1:80" notation or "int int" format? Phoenix 2007-05-18
103	//friend std::istream& operator>> (std::istream& is, LLHost &hh);
104
105	friend bool operator==( const LLHost &lhs, const LLHost &rhs );
106	friend bool operator!=( const LLHost &lhs, const LLHost &rhs );
107	friend bool operator<(const LLHost &lhs, const LLHost &rhs);
108};
109
110
111// Function Object required for STL templates using LLHost as key 
112class LLHostHash
113{
114public:
115	size_t operator() (const LLHost &hh) const { return hh.hash(); }
116};
117
118
119inline bool operator==( const LLHost &lhs, const LLHost &rhs )
120{
121	return (lhs.mIP == rhs.mIP) && (lhs.mPort == rhs.mPort);
122}
123
124inline bool operator!=( const LLHost &lhs, const LLHost &rhs )
125{
126	return (lhs.mIP != rhs.mIP) || (lhs.mPort != rhs.mPort);
127}
128
129inline bool operator<(const LLHost &lhs, const LLHost &rhs)
130{
131	if (lhs.mIP < rhs.mIP)
132	{
133		return true;
134	}
135	if (lhs.mIP > rhs.mIP)
136	{
137		return false;
138	}
139
140	if (lhs.mPort < rhs.mPort)
141	{
142		return true;
143	}
144	else
145	{
146		return false;
147	}
148}
149
150
151#endif // LL_LLHOST_H