1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_IP_ENDPOINT_I_H
6 #define ICE_IP_ENDPOINT_I_H
7 
8 #include <IceUtil/Config.h>
9 #include <IceUtil/Shared.h>
10 #include <IceUtil/Thread.h>
11 #include <IceUtil/Monitor.h>
12 #include <Ice/IPEndpointIF.h>
13 #include <Ice/EndpointI.h>
14 #include <Ice/Network.h>
15 #include <Ice/ProtocolInstanceF.h>
16 #include <Ice/ObserverHelper.h>
17 
18 #ifndef ICE_OS_UWP
19 #   include <deque>
20 #endif
21 
22 namespace IceInternal
23 {
24 
25 class ICE_API IPEndpointInfoI : public Ice::IPEndpointInfo
26 {
27 public:
28 
29     IPEndpointInfoI(const EndpointIPtr&);
30     virtual ~IPEndpointInfoI();
31 
32     virtual Ice::Short type() const ICE_NOEXCEPT;
33     virtual bool datagram() const ICE_NOEXCEPT;
34     virtual bool secure() const ICE_NOEXCEPT;
35 
36 private:
37 
38     const EndpointIPtr _endpoint;
39 };
40 
41 class ICE_API IPEndpointI : public EndpointI
42 #ifdef ICE_CPP11_MAPPING
43                           , public std::enable_shared_from_this<IPEndpointI>
44 #endif
45 {
46 public:
47 
48     virtual void streamWriteImpl(Ice::OutputStream*) const;
49 
50     virtual Ice::EndpointInfoPtr getInfo() const ICE_NOEXCEPT;
51     virtual Ice::Short type() const;
52     virtual const std::string& protocol() const;
53     virtual bool secure() const;
54 
55     virtual const std::string& connectionId() const;
56     virtual EndpointIPtr connectionId(const ::std::string&) const;
57 
58     virtual void connectors_async(Ice::EndpointSelectionType, const EndpointI_connectorsPtr&) const;
59     virtual std::vector<EndpointIPtr> expandIfWildcard() const;
60     virtual std::vector<EndpointIPtr> expandHost(EndpointIPtr&) const;
61     virtual bool equivalent(const EndpointIPtr&) const;
62     virtual ::Ice::Int hash() const;
63     virtual std::string options() const;
64 
65 #ifdef ICE_CPP11_MAPPING
66     virtual bool operator==(const Ice::Endpoint&) const;
67     virtual bool operator<(const Ice::Endpoint&) const;
68 #else
69     virtual bool operator==(const Ice::LocalObject&) const;
70     virtual bool operator<(const Ice::LocalObject&) const;
71 #endif
72 
73     virtual std::vector<ConnectorPtr> connectors(const std::vector<Address>&, const NetworkProxyPtr&) const;
74 
75     virtual void hashInit(Ice::Int&) const;
76     virtual void fillEndpointInfo(Ice::IPEndpointInfo*) const;
77 
78     using EndpointI::connectionId;
79 
80     virtual void initWithOptions(std::vector<std::string>&, bool);
81 
82 protected:
83 
84     friend class EndpointHostResolver;
85 
86     virtual bool checkOption(const std::string&, const std::string&, const std::string&);
87 
88     virtual ConnectorPtr createConnector(const Address& address, const NetworkProxyPtr&) const = 0;
89     virtual IPEndpointIPtr createEndpoint(const std::string&, int, const std::string&) const = 0;
90 
91     IPEndpointI(const ProtocolInstancePtr&, const std::string&, int, const Address&, const std::string&);
92     IPEndpointI(const ProtocolInstancePtr&);
93     IPEndpointI(const ProtocolInstancePtr&, Ice::InputStream*);
94 
95     const ProtocolInstancePtr _instance;
96     const std::string _host;
97     const int _port;
98     const Address _sourceAddr;
99     const std::string _connectionId;
100 
101 private:
102 
103     mutable bool _hashInitialized;
104     mutable Ice::Int _hashValue;
105 };
106 
107 #ifndef ICE_OS_UWP
108 class ICE_API EndpointHostResolver : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
109 #else
110 class ICE_API EndpointHostResolver : public IceUtil::Shared
111 #endif
112 {
113 public:
114 
115     EndpointHostResolver(const InstancePtr&);
116 
117     void resolve(const std::string&, int, Ice::EndpointSelectionType, const IPEndpointIPtr&,
118                  const EndpointI_connectorsPtr&);
119     void destroy();
120 
121     virtual void run();
122     void updateObserver();
123 
124 private:
125 
126 #ifndef ICE_OS_UWP
127     struct ResolveEntry
128     {
129         std::string host;
130         int port;
131         Ice::EndpointSelectionType selType;
132         IPEndpointIPtr endpoint;
133         EndpointI_connectorsPtr callback;
134         Ice::Instrumentation::ObserverPtr observer;
135     };
136 
137     const InstancePtr _instance;
138     const IceInternal::ProtocolSupport _protocol;
139     const bool _preferIPv6;
140     bool _destroyed;
141     std::deque<ResolveEntry> _queue;
142     ObserverHelperT<Ice::Instrumentation::ThreadObserver> _observer;
143 #else
144     const InstancePtr _instance;
145 #endif
146 };
147 
148 #ifndef ICE_CPP11_MAPPING
149 inline bool operator==(const IPEndpointI& l, const IPEndpointI& r)
150 {
151     return static_cast<const ::Ice::LocalObject&>(l) == static_cast<const ::Ice::LocalObject&>(r);
152 }
153 
154 inline bool operator<(const IPEndpointI& l, const IPEndpointI& r)
155 {
156     return static_cast<const ::Ice::LocalObject&>(l) < static_cast<const ::Ice::LocalObject&>(r);
157 }
158 #endif
159 
160 }
161 
162 #endif
163