BaseTransportHeaders.cs

  1. // ==++==
  2. // 
  3. //   
  4. //    Copyright (c) 2002 Microsoft Corporation.  All rights reserved.
  5. //   
  6. //    The use and distribution terms for this software are contained in the file
  7. //    named license.txt, which can be found in the root of this distribution.
  8. //    By using this software in any fashion, you are agreeing to be bound by the
  9. //    terms of this license.
  10. //   
  11. //    You must not remove this notice, or any other, from this software.
  12. //   
  13. // 
  14. // ==--==
  15. //==========================================================================
  16. //  File:       BaseTransportHeaders.cs
  17. //
  18. //  Summary:    Base class for special-cased transport headers implementations.
  19. //
  20. //==========================================================================
  21. using System;
  22. using System.Collections;
  23. using System.Globalization;
  24. using System.Net;
  25. namespace System.Runtime.Remoting.Channels
  26. {
  27.     [Serializable]
  28.     internal class BaseTransportHeaders : ITransportHeaders
  29.     {
  30.         // IMPORTANT: If a new wellknown header is added, MapHeaderNameToIndex,
  31.         //   GetValueFromHeaderIndex, and SetValueFromHeaderIndex methods must
  32.         //   be updated (as well as WellknownHeaderCount)!!!
  33.         internal const int WellknownHeaderCount = 4;
  34.         private Object _connectionId; // 0) CommonTransportKeys.ConnectionId
  35.         private Object _ipAddress;    // 1) CommonTransportKeys.IPAddress
  36.     
  37.         private String _requestUri;   // 2) CommonTransportKeys.RequestUri
  38.         private String _contentType;  // 3) "Content-Type"
  39.         // transport headers structure is for non well-known headers
  40.         private ITransportHeaders _otherHeaders;
  41.         public BaseTransportHeaders()
  42.         {
  43.             _otherHeaders = null;
  44.         }
  45.         public String RequestUri
  46.         {
  47.             get { return _requestUri; }
  48.             set { _requestUri = value; }
  49.         } // RequestUri
  50.         public String ContentType
  51.         {
  52.             get { return _contentType; }
  53.             set { _contentType = value; }
  54.         } // ContentType
  55.         public Object ConnectionId
  56.         {
  57.             set { _connectionId = value; }
  58.         }
  59.         public IPAddress IPAddress
  60.         {
  61.             set { _ipAddress = value; }
  62.         }        
  63.         //
  64.         // ITransportHeaders implementation
  65.         //
  66.         public Object this[Object key]
  67.         {
  68.             get 
  69.             {
  70.                 String strKey = key as String;
  71.                 if (strKey != null)
  72.                 {
  73.                     int index = MapHeaderNameToIndex(strKey);
  74.                     if (index != -1)
  75.                         return GetValueFromHeaderIndex(index);
  76.                 }
  77.             
  78.                 if (_otherHeaders != null)
  79.                     return _otherHeaders[key];
  80.                 return null;
  81.             } // get
  82.             set
  83.             {
  84.                 bool bSet = false;
  85.             
  86.                 String strKey = key as String;
  87.                 if (strKey != null)
  88.                 {
  89.                     int index = MapHeaderNameToIndex(strKey);
  90.                     if (index != -1)     
  91.                     {
  92.                         SetValueFromHeaderIndex(index, value);
  93.                         bSet = true;
  94.                     }
  95.                 }
  96.                 if (!bSet)
  97.                 {
  98.                     if (_otherHeaders == null)
  99.                         _otherHeaders = new TransportHeaders();
  100.                     _otherHeaders[key] = value;
  101.                 }
  102.             } // set
  103.         } // Object this[Object key]
  104.         public IEnumerator GetEnumerator() 
  105.         {
  106.             return new BaseTransportHeadersEnumerator(this);
  107.         } // GetEnumerator
  108.         internal IEnumerator GetOtherHeadersEnumerator()
  109.         {
  110.             if (_otherHeaders == null)
  111.                 return null;
  112.             return _otherHeaders.GetEnumerator();            
  113.         } // GetOtherHeadersEnumerator
  114.         internal int MapHeaderNameToIndex(String headerName)
  115.         {
  116.             // 0) CommonTransportKeys.ConnectionId
  117.             // 1) CommonTransportKeys.IPAddress
  118.             // 2) CommonTransportKeys.RequestUri
  119.             // 3) "Content-Type"
  120.             
  121.             if (String.Compare(headerName, CommonTransportKeys.ConnectionId, true, CultureInfo.InvariantCulture) == 0)
  122.                 return 0;
  123.             else
  124.             if (String.Compare(headerName, CommonTransportKeys.IPAddress, true, CultureInfo.InvariantCulture) == 0)
  125.                 return 1;
  126.             else
  127.             if (String.Compare(headerName, CommonTransportKeys.RequestUri, true, CultureInfo.InvariantCulture) == 0)
  128.                 return 2;
  129.             else
  130.             if (String.Compare(headerName, "Content-Type"true, CultureInfo.InvariantCulture) == 0)
  131.                 return 3;
  132.             return -1;
  133.         } // MapHeaderNameToIndex
  134.         internal String MapHeaderIndexToName(int index)
  135.         {
  136.             // 0) CommonTransportKeys.ConnectionId
  137.             // 1) CommonTransportKeys.IPAddress
  138.             // 2) CommonTransportKeys.RequestUri
  139.             // 3) "Content-Type"
  140.             switch (index)
  141.             {
  142.             case 0: return CommonTransportKeys.ConnectionId;
  143.             case 1: return CommonTransportKeys.IPAddress;            
  144.             case 2: return CommonTransportKeys.RequestUri;
  145.             case 3: return "Content-Type";
  146.             
  147.             defaultreturn null;
  148.             }
  149.             
  150.         } // MapHeaderNameToIndex
  151.         internal Object GetValueFromHeaderIndex(int index)
  152.         {
  153.             // NOTE: If this method returns the null, then that indicates the header has no
  154.             //   value (i.e. isn't in the "dictionary"). For the purposes of iteration, this
  155.             //   means that the header should be skipped.
  156.         
  157.             // 0) CommonTransportKeys.ConnectionId
  158.             // 1) CommonTransportKeys.IPAddress
  159.             // 2) CommonTransportKeys.RequestUri
  160.             // 3) "Content-Type"
  161.             switch (index)
  162.             {
  163.             case 0: return _connectionId;
  164.             case 1: return _ipAddress;            
  165.             case 2: return _requestUri;
  166.             case 3: return _contentType;
  167.             
  168.             defaultreturn null;
  169.             }
  170.             
  171.         } // MapHeaderIndexToValue
  172.         internal void SetValueFromHeaderIndex(int index, Object value)
  173.         {
  174.             // NOTE: If this method returns the null, then that indicates the header has no
  175.             //   value (i.e. isn't in the "dictionary"). For the purposes of iteration, this
  176.             //   means that the header should be skipped.
  177.         
  178.             // 0) CommonTransportKeys.ConnectionId
  179.             // 1) CommonTransportKeys.IPAddress
  180.             // 2) CommonTransportKeys.RequestUri
  181.             // 3) "Content-Type"
  182.             switch (index)
  183.             {
  184.             case 0: _connectionId = value; break;
  185.             case 1: _ipAddress = value; break;        
  186.             case 2: _requestUri = (String)value; break;
  187.             case 3: _contentType = (String)value; break;
  188.             default
  189.             {
  190.                 InternalRemotingServices.RemotingAssert(false"someone forgot to update this method"); 
  191.                 break;
  192.             }
  193.             
  194.             } // switch (index)
  195.             
  196.         } // MapHeaderIndexToValue
  197.         
  198.     
  199.     } // class BaseTransportHeaders
  200.     internal class BaseTransportHeadersEnumerator : IEnumerator
  201.     {
  202.         private BaseTransportHeaders _headers;
  203.         private bool _bStarted;
  204.         private int _currentIndex;
  205.         private IEnumerator _otherHeadersEnumerator;
  206.     
  207.         public BaseTransportHeadersEnumerator(BaseTransportHeaders headers)
  208.         {
  209.             _headers = headers;
  210.             Reset();
  211.         } // BaseTransportHeadersEnumerator
  212.     
  213.         public bool MoveNext()
  214.         {
  215.             if (_currentIndex != -1)
  216.             {
  217.                 if (_bStarted)
  218.                     _currentIndex++;
  219.                 else
  220.                     _bStarted = true;
  221.                 while (_currentIndex != -1)
  222.                 {
  223.                     if (_currentIndex >= BaseTransportHeaders.WellknownHeaderCount)
  224.                     {
  225.                         _otherHeadersEnumerator = _headers.GetOtherHeadersEnumerator();
  226.                         _currentIndex = -1;
  227.                     }
  228.                     else
  229.                     {
  230.                         if (_headers.GetValueFromHeaderIndex(_currentIndex) != null)
  231.                             return true;
  232.                 
  233.                         _currentIndex++;
  234.                     }
  235.                 }
  236.             }
  237.             
  238.             if (_otherHeadersEnumerator != null)
  239.             {
  240.                 if (!_otherHeadersEnumerator.MoveNext())
  241.                 {
  242.                     _otherHeadersEnumerator = null;
  243.                     return false;                   
  244.                 }
  245.                 else
  246.                     return true;                    
  247.             }
  248.             return false;
  249.         } // MoveNext
  250.         
  251.         public void Reset()
  252.         {
  253.             _bStarted = false;
  254.             _currentIndex = 0;
  255.             _otherHeadersEnumerator = null;
  256.         } // Reset
  257.         public Object Current  
  258.         {
  259.             get 
  260.             {
  261.                 if (!_bStarted)
  262.                     return null;
  263.             
  264.                 if (_currentIndex != -1)
  265.                 {
  266.                     return 
  267.                         new DictionaryEntry(
  268.                             _headers.MapHeaderIndexToName(_currentIndex),
  269.                             _headers.GetValueFromHeaderIndex(_currentIndex));
  270.                 }
  271.                 if (_otherHeadersEnumerator != null)
  272.                 {
  273.                     return _otherHeadersEnumerator.Current;
  274.                 }
  275.                 return null;
  276.             }
  277.         } // Current
  278.         
  279.     } // class BaseTransportHeadersEnumerator
  280. // namespace System.Runtime.Remoting.Channels
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值