1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #include "StreamTransceiver.h"
6 #include "StreamEndpointI.h"
7 #include "StreamConnector.h"
8 
9 #include <Ice/Network.h>
10 #include <Ice/UniqueRef.h>
11 #include <Ice/Exception.h>
12 #include <Ice/Properties.h>
13 #include <Ice/NetworkProxy.h>
14 
15 #include <CoreFoundation/CoreFoundation.h>
16 
17 using namespace std;
18 using namespace Ice;
19 using namespace IceInternal;
20 
21 TransceiverPtr
connect()22 IceObjC::StreamConnector::connect()
23 {
24     UniqueRef<CFReadStreamRef> readStream;
25     UniqueRef<CFWriteStreamRef> writeStream;
26     UniqueRef<CFStringRef> h(CFStringCreateWithCString(ICE_NULLPTR, _host.c_str(), kCFStringEncodingUTF8));
27     UniqueRef<CFHostRef> host(CFHostCreateWithName(ICE_NULLPTR, h.get()));
28     CFStreamCreatePairWithSocketToCFHost(ICE_NULLPTR, host.get(), _port, &readStream.get(), &writeStream.get());
29     _instance->setupStreams(readStream.get(), writeStream.get(), false, _host);
30     return new StreamTransceiver(_instance, readStream.release(), writeStream.release(), _host, _port);
31 }
32 
33 Short
type() const34 IceObjC::StreamConnector::type() const
35 {
36     return _instance->type();
37 }
38 
39 string
toString() const40 IceObjC::StreamConnector::toString() const
41 {
42     string proxyHost = _instance->proxyHost();
43     ostringstream os;
44     if(!proxyHost.empty())
45     {
46         os << proxyHost << ":" << _instance->proxyPort();
47     }
48     else
49     {
50         os << _host << ":" << _port;
51     }
52     return os.str();
53 }
54 
55 bool
operator ==(const IceInternal::Connector & r) const56 IceObjC::StreamConnector::operator==(const IceInternal::Connector& r) const
57 {
58     const StreamConnector* p = dynamic_cast<const StreamConnector*>(&r);
59     if(!p)
60     {
61         return false;
62     }
63 
64     if(_timeout != p->_timeout)
65     {
66         return false;
67     }
68 
69     if(_connectionId != p->_connectionId)
70     {
71         return false;
72     }
73 
74     if(_host != p->_host)
75     {
76         return false;
77     }
78 
79     if(_port != p->_port)
80     {
81         return false;
82     }
83 
84     return true;
85 }
86 
87 bool
operator <(const IceInternal::Connector & r) const88 IceObjC::StreamConnector::operator<(const IceInternal::Connector& r) const
89 {
90     const StreamConnector* p = dynamic_cast<const StreamConnector*>(&r);
91     if(!p)
92     {
93         return type() < r.type();
94     }
95 
96     if(_timeout < p->_timeout)
97     {
98         return true;
99     }
100     else if(p->_timeout < _timeout)
101     {
102         return false;
103     }
104 
105     if(_connectionId < p->_connectionId)
106     {
107         return true;
108     }
109     else if(p->_connectionId < _connectionId)
110     {
111         return false;
112     }
113 
114     if(_host < p->_host)
115     {
116         return true;
117     }
118     else if(p->_host < _host)
119     {
120         return false;
121     }
122 
123     return _port < p->_port;
124 }
125 
StreamConnector(const InstancePtr & instance,const string & host,Ice::Int port,Ice::Int timeout,const string & connectionId)126 IceObjC::StreamConnector::StreamConnector(const InstancePtr& instance,
127                                           const string& host,
128                                           Ice::Int port,
129                                           Ice::Int timeout,
130                                           const string& connectionId) :
131     _instance(instance),
132     _host(host.empty() ? string("127.0.0.1") : host),
133     _port(port),
134     _timeout(timeout),
135     _connectionId(connectionId)
136 {
137 }
138 
~StreamConnector()139 IceObjC::StreamConnector::~StreamConnector()
140 {
141 }
142