1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 #pragma once 10 11 #include <folly/SocketAddress.h> 12 13 namespace proxygen { 14 15 /** 16 * Simple class representing an endpoint by (hostname, port, http/s). 17 * Optionally used by SessionHolder if caller wants to store sessions from 18 * different endpoints in a single SessionPool. 19 */ 20 class Endpoint { 21 public: Endpoint(const std::string & hostname,uint16_t port,bool isSecure)22 explicit Endpoint(const std::string& hostname, uint16_t port, bool isSecure) 23 : hostname_(hostname), port_(port), isSecure_(isSecure) { 24 hash_ = 25 std::hash<std::string>()(hostname_) ^ (port_ << 1) ^ (isSecure_ << 17); 26 }; 27 Endpoint(const folly::SocketAddress & addr,bool isSecure)28 explicit Endpoint(const folly::SocketAddress& addr, bool isSecure) 29 : Endpoint(addr.getAddressStr(), addr.getPort(), isSecure){}; 30 getHostname()31 const std::string& getHostname() const { 32 return hostname_; 33 }; 34 getPort()35 uint16_t getPort() const { 36 return port_; 37 }; 38 isSecure()39 bool isSecure() const { 40 return isSecure_; 41 } 42 getHash()43 size_t getHash() const { 44 return hash_; 45 } 46 describe(std::ostream & os)47 void describe(std::ostream& os) const { 48 os << hostname_ << ":" << port_ << ":" << (isSecure_ ? "" : "in") 49 << "secure"; 50 }; 51 52 private: 53 std::string hostname_; 54 uint16_t port_{0}; 55 std::size_t hash_{0}; 56 bool isSecure_{false}; 57 }; 58 59 struct EndpointHash { operatorEndpointHash60 std::size_t operator()(const Endpoint& e) const { 61 return e.getHash(); 62 } 63 }; 64 65 struct EndpointEqual { operatorEndpointEqual66 bool operator()(const Endpoint& lhs, const Endpoint& rhs) const { 67 return lhs.getHostname() == rhs.getHostname() && 68 lhs.getPort() == rhs.getPort() && lhs.isSecure() == rhs.isSecure(); 69 } 70 }; 71 72 } // namespace proxygen 73