1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 namespace IceInternal
6 {
7     using System.Collections.Generic;
8     using System.Globalization;
9     using System.Diagnostics;
10 
11     sealed class OpaqueEndpointI : EndpointI
12     {
OpaqueEndpointI(List<string> args)13         public OpaqueEndpointI(List<string> args)
14         {
15             _type = -1;
16             _rawEncoding = Ice.Util.Encoding_1_0;
17             _rawBytes = new byte[0];
18 
19             initWithOptions(args);
20 
21             if(_type < 0)
22             {
23                 throw new Ice.EndpointParseException("no -t option in endpoint " + ToString());
24             }
25             if(_rawBytes.Length == 0)
26             {
27                 throw new Ice.EndpointParseException("no -v option in endpoint " + ToString());
28             }
29 
30             calcHashValue();
31         }
32 
OpaqueEndpointI(short type, Ice.InputStream s)33         public OpaqueEndpointI(short type, Ice.InputStream s)
34         {
35             _type = type;
36             _rawEncoding = s.getEncoding();
37             int sz = s.getEncapsulationSize();
38             _rawBytes = new byte[sz];
39             s.readBlob(_rawBytes);
40 
41             calcHashValue();
42         }
43 
44         //
45         // Marshal the endpoint
46         //
streamWrite(Ice.OutputStream s)47         public override void streamWrite(Ice.OutputStream s)
48         {
49             s.startEncapsulation(_rawEncoding, Ice.FormatType.DefaultFormat);
50             s.writeBlob(_rawBytes);
51             s.endEncapsulation();
52         }
53 
streamWriteImpl(Ice.OutputStream s)54         public override void streamWriteImpl(Ice.OutputStream s)
55         {
56             Debug.Assert(false);
57         }
58 
59         //
60         // Convert the endpoint to its string form
61         //
ice_toString_()62         public override string ice_toString_()
63         {
64             string val = System.Convert.ToBase64String(_rawBytes);
65             return "opaque -t " + _type + " -e " + Ice.Util.encodingVersionToString(_rawEncoding) + " -v " + val;
66         }
67 
68         private sealed class InfoI : Ice.OpaqueEndpointInfo
69         {
InfoI(short type, Ice.EncodingVersion rawEncoding, byte[] rawBytes)70             public InfoI(short type, Ice.EncodingVersion rawEncoding, byte[] rawBytes) :
71                 base(null, -1, false, rawEncoding, rawBytes)
72             {
73                 _type = type;
74             }
75 
type()76             override public short type()
77             {
78                 return _type;
79             }
80 
datagram()81             override public bool datagram()
82             {
83                 return false;
84             }
85 
secure()86             override public bool secure()
87             {
88                 return false;
89             }
90 
91             private readonly short _type;
92         }
93 
94         //
95         // Return the endpoint information.
96         //
getInfo()97         public override Ice.EndpointInfo getInfo()
98         {
99             return new InfoI(_type, _rawEncoding, _rawBytes);
100         }
101 
102         //
103         // Return the endpoint type
104         //
type()105         public override short type()
106         {
107             return _type;
108         }
109 
110         //
111         // Return the protocol name;
112         //
protocol()113         public override string protocol()
114         {
115             return "opaque";
116         }
117 
118         //
119         // Return the timeout for the endpoint in milliseconds. 0 means
120         // non-blocking, -1 means no timeout.
121         //
timeout()122         public override int timeout()
123         {
124             return -1;
125         }
126 
127         //
128         // Return a new endpoint with a different timeout value, provided
129         // that timeouts are supported by the endpoint. Otherwise the same
130         // endpoint is returned.
131         //
timeout(int t)132         public override EndpointI timeout(int t)
133         {
134             return this;
135         }
136 
connectionId()137         public override string connectionId()
138         {
139             return "";
140         }
141 
142         //
143         // Return a new endpoint with a different connection id.
144         //
connectionId(string id)145         public override EndpointI connectionId(string id)
146         {
147             return this;
148         }
149 
150         //
151         // Return true if the endpoints support bzip2 compress, or false
152         // otherwise.
153         //
compress()154         public override bool compress()
155         {
156             return false;
157         }
158 
159         //
160         // Return a new endpoint with a different compression value,
161         // provided that compression is supported by the
162         // endpoint. Otherwise the same endpoint is returned.
163         //
compress(bool compress)164         public override EndpointI compress(bool compress)
165         {
166             return this;
167         }
168 
169         //
170         // Return true if the endpoint is datagram-based.
171         //
datagram()172         public override bool datagram()
173         {
174             return false;
175         }
176 
177         //
178         // Return true if the endpoint is secure.
179         //
secure()180         public override bool secure()
181         {
182             return false;
183         }
184 
185         //
186         // Get the encoded endpoint.
187         //
rawBytes()188         public byte[] rawBytes()
189         {
190             return _rawBytes;
191         }
192 
193         //
194         // Return a server side transceiver for this endpoint, or null if a
195         // transceiver can only be created by an acceptor.
196         //
transceiver()197         public override Transceiver transceiver()
198         {
199             return null;
200         }
201 
202         //
203         // Return connectors for this endpoint, or empty list if no connector
204         // is available.
205         //
connectors_async(Ice.EndpointSelectionType endSel, EndpointI_connectors callback)206         public override void connectors_async(Ice.EndpointSelectionType endSel, EndpointI_connectors callback)
207         {
208             callback.connectors(new List<Connector>());
209         }
210 
211         //
212         // Return an acceptor for this endpoint, or null if no acceptors
213         // is available.
214         //
acceptor(string adapterName)215         public override Acceptor acceptor(string adapterName)
216         {
217             return null;
218         }
219 
220         //
221         // Expand endpoint out in to separate endpoints for each local
222         // host if listening on INADDR_ANY on server side or if no host
223         // was specified on client side.
224         //
expandIfWildcard()225         public override List<EndpointI> expandIfWildcard()
226         {
227             List<EndpointI> endps = new List<EndpointI>();
228             endps.Add(this);
229             return endps;
230         }
231 
expandHost(out EndpointI publishedEndpoint)232         public override List<EndpointI> expandHost(out EndpointI publishedEndpoint)
233         {
234             publishedEndpoint = null;
235             List<EndpointI> endps = new List<EndpointI>();
236             endps.Add(this);
237             return endps;
238         }
239 
240         //
241         // Check whether the endpoint is equivalent to another one.
242         //
equivalent(EndpointI endpoint)243         public override bool equivalent(EndpointI endpoint)
244         {
245             return false;
246         }
247 
GetHashCode()248         public override int GetHashCode()
249         {
250             return _hashCode;
251         }
252 
options()253         public override string options()
254         {
255             string s = "";
256             if(_type > -1)
257             {
258                 s += " -t " + _type;
259             }
260             s += " -e " + Ice.Util.encodingVersionToString(_rawEncoding);
261             if(_rawBytes.Length > 0)
262             {
263                 s += " -v " + System.Convert.ToBase64String(_rawBytes);
264             }
265             return s;
266         }
267 
268         //
269         // Compare endpoints for sorting purposes
270         //
CompareTo(EndpointI obj)271         public override int CompareTo(EndpointI obj)
272         {
273             if(!(obj is OpaqueEndpointI))
274             {
275                 return type() < obj.type() ? -1 : 1;
276             }
277 
278             OpaqueEndpointI p = (OpaqueEndpointI)obj;
279             if(this == p)
280             {
281                 return 0;
282             }
283 
284             if(_type < p._type)
285             {
286                 return -1;
287             }
288             else if(p._type < _type)
289             {
290                 return 1;
291             }
292 
293             if(_rawEncoding.major < p._rawEncoding.major)
294             {
295                 return -1;
296             }
297             else if(p._rawEncoding.major < _rawEncoding.major)
298             {
299                 return 1;
300             }
301 
302             if(_rawEncoding.minor < p._rawEncoding.minor)
303             {
304                 return -1;
305             }
306             else if(p._rawEncoding.minor < _rawEncoding.minor)
307             {
308                 return 1;
309             }
310 
311             if(_rawBytes.Length < p._rawBytes.Length)
312             {
313                 return -1;
314             }
315             else if(p._rawBytes.Length < _rawBytes.Length)
316             {
317                 return 1;
318             }
319             for(int i = 0; i < _rawBytes.Length; i++)
320             {
321                 if(_rawBytes[i] < p._rawBytes[i])
322                 {
323                     return -1;
324                 }
325                 else if(p._rawBytes[i] < _rawBytes[i])
326                 {
327                     return 1;
328                 }
329             }
330 
331             return 0;
332         }
333 
checkOption(string option, string argument, string endpoint)334         protected override bool checkOption(string option, string argument, string endpoint)
335         {
336             switch(option[1])
337             {
338             case 't':
339             {
340                 if(_type > -1)
341                 {
342                     throw new Ice.EndpointParseException("multiple -t options in endpoint " + endpoint);
343                 }
344                 if(argument == null)
345                 {
346                     throw new Ice.EndpointParseException("no argument provided for -t option in endpoint " + endpoint);
347                 }
348 
349                 int t;
350                 try
351                 {
352                     t = System.Int32.Parse(argument, CultureInfo.InvariantCulture);
353                 }
354                 catch(System.FormatException)
355                 {
356                     throw new Ice.EndpointParseException("invalid type value `" + argument + "' in endpoint " +
357                                                          endpoint);
358                 }
359 
360                 if(t < 0 || t > 65535)
361                 {
362                     throw new Ice.EndpointParseException("type value `" + argument + "' out of range in endpoint " +
363                                                          endpoint);
364                 }
365 
366                 _type = (short)t;
367                 return true;
368             }
369 
370             case 'v':
371             {
372                 if(_rawBytes.Length > 0)
373                 {
374                     throw new Ice.EndpointParseException("multiple -v options in endpoint " + endpoint);
375                 }
376                 if(argument == null)
377                 {
378                     throw new Ice.EndpointParseException("no argument provided for -v option in endpoint " + endpoint);
379                 }
380 
381                 try
382                 {
383                     _rawBytes = System.Convert.FromBase64String(argument);
384                 }
385                 catch(System.FormatException ex)
386                 {
387                     throw new Ice.EndpointParseException("Invalid Base64 input in endpoint " + endpoint, ex);
388                 }
389 
390                 return true;
391             }
392 
393             case 'e':
394             {
395                 if(argument == null)
396                 {
397                     throw new Ice.EndpointParseException("no argument provided for -e option in endpoint " + endpoint);
398                 }
399 
400                 try
401                 {
402                     _rawEncoding = Ice.Util.stringToEncodingVersion(argument);
403                 }
404                 catch(Ice.VersionParseException e)
405                 {
406                     throw new Ice.EndpointParseException("invalid encoding version `" + argument +
407                                                          "' in endpoint " + endpoint + ":\n" + e.str);
408                 }
409                 return true;
410             }
411 
412             default:
413             {
414                 return false;
415             }
416             }
417         }
418 
calcHashValue()419         private void calcHashValue()
420         {
421             int h = 5381;
422             HashUtil.hashAdd(ref h, _type);
423             HashUtil.hashAdd(ref h, _rawEncoding);
424             HashUtil.hashAdd(ref h, _rawBytes);
425             _hashCode = h;
426         }
427 
428         private short _type;
429         private Ice.EncodingVersion _rawEncoding;
430         private byte[] _rawBytes;
431         private int _hashCode;
432     }
433 
434 }
435