1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 namespace IceInternal
6 {
7     using System.Collections.Generic;
8     using System.Net;
9     using System.Globalization;
10 
11     sealed class TcpEndpointI : IPEndpointI
12     {
TcpEndpointI(ProtocolInstance instance, string ho, int po, EndPoint sourceAddr, int ti, string conId, bool co)13         public TcpEndpointI(ProtocolInstance instance, string ho, int po, EndPoint sourceAddr, int ti, string conId,
14                             bool co) :
15             base(instance, ho, po, sourceAddr, conId)
16         {
17             _timeout = ti;
18             _compress = co;
19         }
20 
TcpEndpointI(ProtocolInstance instance)21         public TcpEndpointI(ProtocolInstance instance) :
22             base(instance)
23         {
24             _timeout = instance.defaultTimeout();
25             _compress = false;
26         }
27 
TcpEndpointI(ProtocolInstance instance, Ice.InputStream s)28         public TcpEndpointI(ProtocolInstance instance, Ice.InputStream s) :
29             base(instance, s)
30         {
31             _timeout = s.readInt();
32             _compress = s.readBool();
33         }
34 
35         private sealed class InfoI : Ice.TCPEndpointInfo
36         {
InfoI(IPEndpointI e)37             public InfoI(IPEndpointI e)
38             {
39                 _endpoint = e;
40             }
41 
type()42             public override short type()
43             {
44                 return _endpoint.type();
45             }
46 
datagram()47             public override bool datagram()
48             {
49                 return _endpoint.datagram();
50             }
51 
secure()52             public override bool secure()
53             {
54                 return _endpoint.secure();
55             }
56 
57             private IPEndpointI _endpoint;
58         }
59 
streamWriteImpl(Ice.OutputStream s)60         public override void streamWriteImpl(Ice.OutputStream s)
61         {
62             base.streamWriteImpl(s);
63             s.writeInt(_timeout);
64             s.writeBool(_compress);
65         }
66 
getInfo()67         public override Ice.EndpointInfo getInfo()
68         {
69             InfoI info = new InfoI(this);
70             fillEndpointInfo(info);
71             return info;
72         }
73 
timeout()74         public override int timeout()
75         {
76             return _timeout;
77         }
78 
timeout(int timeout)79         public override EndpointI timeout(int timeout)
80         {
81             if(timeout == _timeout)
82             {
83                 return this;
84             }
85             else
86             {
87                 return new TcpEndpointI(instance_, host_, port_, sourceAddr_, timeout, connectionId_, _compress);
88             }
89         }
90 
compress()91         public override bool compress()
92         {
93             return _compress;
94         }
95 
compress(bool compress)96         public override EndpointI compress(bool compress)
97         {
98             if(compress == _compress)
99             {
100                 return this;
101             }
102             else
103             {
104                 return new TcpEndpointI(instance_, host_, port_, sourceAddr_, _timeout, connectionId_, compress);
105             }
106         }
107 
datagram()108         public override bool datagram()
109         {
110             return false;
111         }
112 
transceiver()113         public override Transceiver transceiver()
114         {
115             return null;
116         }
117 
acceptor(string adapterName)118         public override Acceptor acceptor(string adapterName)
119         {
120             return new TcpAcceptor(this, instance_, host_, port_);
121         }
122 
endpoint(TcpAcceptor acceptor)123         public TcpEndpointI endpoint(TcpAcceptor acceptor)
124         {
125             int port = acceptor.effectivePort();
126             if(port == port_)
127             {
128                 return this;
129             }
130             else
131             {
132                 return new TcpEndpointI(instance_, host_, port, sourceAddr_, _timeout, connectionId_, _compress);
133             }
134         }
135 
options()136         public override string options()
137         {
138             //
139             // WARNING: Certain features, such as proxy validation in Glacier2,
140             // depend on the format of proxy strings. Changes to toString() and
141             // methods called to generate parts of the reference string could break
142             // these features. Please review for all features that depend on the
143             // format of proxyToString() before changing this and related code.
144             //
145             string s = base.options();
146 
147             if(_timeout == -1)
148             {
149                 s += " -t infinite";
150             }
151             else
152             {
153                 s += " -t " + _timeout;
154             }
155 
156             if(_compress)
157             {
158                 s += " -z";
159             }
160 
161             return s;
162         }
163 
CompareTo(EndpointI obj)164         public override int CompareTo(EndpointI obj)
165         {
166             if(!(obj is TcpEndpointI))
167             {
168                 return type() < obj.type() ? -1 : 1;
169             }
170 
171             TcpEndpointI p = (TcpEndpointI)obj;
172             if(this == p)
173             {
174                 return 0;
175             }
176 
177             if(_timeout < p._timeout)
178             {
179                 return -1;
180             }
181             else if(p._timeout < _timeout)
182             {
183                 return 1;
184             }
185 
186             if(!_compress && p._compress)
187             {
188                 return -1;
189             }
190             else if(!p._compress && _compress)
191             {
192                 return 1;
193             }
194 
195             return base.CompareTo(p);
196         }
197 
hashInit(ref int h)198         public override void hashInit(ref int h)
199         {
200             base.hashInit(ref h);
201             HashUtil.hashAdd(ref h, _timeout);
202             HashUtil.hashAdd(ref h, _compress);
203         }
204 
fillEndpointInfo(Ice.IPEndpointInfo info)205         public override void fillEndpointInfo(Ice.IPEndpointInfo info)
206         {
207             base.fillEndpointInfo(info);
208             info.timeout = _timeout;
209             info.compress = _compress;
210         }
211 
checkOption(string option, string argument, string endpoint)212         protected override bool checkOption(string option, string argument, string endpoint)
213         {
214             if(base.checkOption(option, argument, endpoint))
215             {
216                 return true;
217             }
218 
219             switch(option[1])
220             {
221                 case 't':
222                 {
223                     if(argument == null)
224                     {
225                         throw new Ice.EndpointParseException("no argument provided for -t option in endpoint " +
226                                                              endpoint);
227                     }
228 
229                     if(argument.Equals("infinite"))
230                     {
231                         _timeout = -1;
232                     }
233                     else
234                     {
235                         try
236                         {
237                             _timeout = int.Parse(argument, CultureInfo.InvariantCulture);
238                             if(_timeout < 1)
239                             {
240                                 Ice.EndpointParseException e = new Ice.EndpointParseException();
241                                 e.str = "invalid timeout value `" + argument + "' in endpoint " + endpoint;
242                                 throw e;
243                             }
244                         }
245                         catch(System.FormatException ex)
246                         {
247                             Ice.EndpointParseException e = new Ice.EndpointParseException(ex);
248                             e.str = "invalid timeout value `" + argument + "' in endpoint " + endpoint;
249                             throw e;
250                         }
251                     }
252 
253                     return true;
254                 }
255 
256                 case 'z':
257                 {
258                     if(argument != null)
259                     {
260                         throw new Ice.EndpointParseException("unexpected argument `" + argument +
261                                                              "' provided for -z option in " + endpoint);
262                     }
263 
264                     _compress = true;
265 
266                     return true;
267                 }
268 
269                 default:
270                 {
271                     return false;
272                 }
273             }
274         }
275 
createConnector(EndPoint addr, NetworkProxy proxy)276         protected override Connector createConnector(EndPoint addr, NetworkProxy proxy)
277         {
278             return new TcpConnector(instance_, addr, proxy, sourceAddr_, _timeout, connectionId_);
279         }
280 
createEndpoint(string host, int port, string connectionId)281         protected override IPEndpointI createEndpoint(string host, int port, string connectionId)
282         {
283             return new TcpEndpointI(instance_, host, port, sourceAddr_, _timeout, connectionId, _compress);
284         }
285 
286         private int _timeout;
287         private bool _compress;
288     }
289 
290     sealed class TcpEndpointFactory : EndpointFactory
291     {
TcpEndpointFactory(ProtocolInstance instance)292         internal TcpEndpointFactory(ProtocolInstance instance)
293         {
294             _instance = instance;
295         }
296 
initialize()297         public void initialize()
298         {
299         }
300 
type()301         public short type()
302         {
303             return _instance.type();
304         }
305 
protocol()306         public string protocol()
307         {
308             return _instance.protocol();
309         }
310 
create(List<string> args, bool oaEndpoint)311         public EndpointI create(List<string> args, bool oaEndpoint)
312         {
313             IPEndpointI endpt = new TcpEndpointI(_instance);
314             endpt.initWithOptions(args, oaEndpoint);
315             return endpt;
316         }
317 
read(Ice.InputStream s)318         public EndpointI read(Ice.InputStream s)
319         {
320             return new TcpEndpointI(_instance, s);
321         }
322 
destroy()323         public void destroy()
324         {
325             _instance = null;
326         }
327 
clone(ProtocolInstance instance)328         public EndpointFactory clone(ProtocolInstance instance)
329         {
330             return new TcpEndpointFactory(instance);
331         }
332 
333         private ProtocolInstance _instance;
334     }
335 
336 }
337