TcpClientSocketManager.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:       TcpClientSocketManager.cs
  17. //
  18. //  Summary:    Class for managing a socket connection.
  19. //
  20. //==========================================================================
  21. using System;
  22. using System.Collections;
  23. using System.IO;
  24. using System.Net;
  25. using System.Net.Sockets;
  26. using System.Runtime.Remoting.Messaging;
  27. using System.Text;
  28. using System.Threading;
  29. namespace System.Runtime.Remoting.Channels.Tcp
  30. {
  31.     // A client socket manager instance should encapsulate the socket
  32.     //   for the purpose of reading a response
  33.     internal class TcpClientSocketHandler : TcpSocketHandler
  34.     {    
  35.         // prebaked bytes
  36.         private static byte[] s_endOfLineBytes = Encoding.ASCII.GetBytes("/r/n");
  37.         
  38.     
  39.         private String _machineAndPort; // "machineName:port"                     
  40.     
  41.         // connection state information
  42.         private bool _bOneWayRequest = false;  // was the request made OneWay?
  43.         private bool _bChunked;    
  44.         private int  _contentLength;
  45.         private Stream _requestStream; // the request stream that we return from GetRequestStream()
  46.         private TcpReadingStream _responseStream; // the stream that we returned from GetResponseStream()
  47.         
  48.         public TcpClientSocketHandler(Socket socket, String machineAndPort) :
  49.             base(socket)            
  50.         {          
  51.             _machineAndPort = machineAndPort;
  52.         } // TcpClientSocketHandler
  53.         
  54.         // Prepare for reading a new request off of the same socket
  55.         protected override void PrepareForNewMessage()
  56.         {
  57.             _requestStream = null;
  58.             _responseStream = null;
  59.         } // PrepareForNewRequest
  60.         public override void OnInputStreamClosed()
  61.         {       
  62.             // make sure we read to the end of the response stream
  63.             if (_responseStream != null)
  64.             {
  65.                 _responseStream.ReadToEnd();
  66.                 _responseStream = null;                
  67.             }
  68.         
  69.             // return socket to the cache
  70.             ReturnToCache();
  71.         } // OnInputStreamClosed
  72.         public BaseTransportHeaders ReadHeaders()
  73.         {           
  74.             BaseTransportHeaders headers = new BaseTransportHeaders();
  75.             UInt16 operation;
  76.             ReadVersionAndOperation(out operation);
  77.             // At this point, we're always expecting a Reply, so check for that.
  78.             if (operation != TcpOperations.Reply)
  79.             {
  80.                 throw new RemotingException(
  81.                     String.Format(
  82.                         CoreChannel.GetResourceString("Remoting_Tcp_ExpectingReplyOp"),
  83.                         operation.ToString()));
  84.             }                        
  85.                    
  86.             // content length must come next (may be chunked or a specific length)
  87.             ReadContentLength(out _bChunked, out _contentLength);
  88.             
  89.             // read to end of headers  
  90.             ReadToEndOfHeaders(headers); 
  91.                                
  92.             return headers;
  93.         } // ReadHeaders  
  94.         public Stream GetRequestStream(IMessage msg, int contentLength,
  95.                                        ITransportHeaders headers)
  96.         {
  97.             IMethodCallMessage mcm = (IMethodCallMessage)msg;        
  98.             String uri = mcm.Uri;
  99.             _bOneWayRequest = RemotingServices.IsOneWay(mcm.MethodBase);
  100.             ChunkedMemoryStream headerStream = new ChunkedMemoryStream(CoreChannel.BufferPool);
  101.             // output preamble and version
  102.             WritePreambleAndVersion(headerStream);
  103.             // output opcode 
  104.             if (!_bOneWayRequest)
  105.                 WriteUInt16(TcpOperations.Request, headerStream);
  106.             else
  107.                 WriteUInt16(TcpOperations.OneWayRequest, headerStream);            
  108.             // output content delimiter style
  109.             WriteUInt16(TcpContentDelimiter.ContentLength, headerStream);
  110.             WriteInt32(contentLength, headerStream);
  111.             
  112.             // output request uri
  113.             WriteUInt16(TcpHeaders.RequestUri, headerStream);
  114.             WriteByte(TcpHeaderFormat.CountedString, headerStream);
  115.             WriteCountedString(uri, headerStream);         
  116.             // output rest of headers
  117.             WriteHeaders(headers, headerStream);   
  118.             
  119.             headerStream.WriteTo(NetStream);
  120.             headerStream.Close();
  121.             _requestStream = NetStream;
  122.             return _requestStream;
  123.         } // GetRequestStream
  124.         public void SendRequest(IMessage msg, ITransportHeaders headers, Stream contentStream)
  125.         {
  126.             int requestLength = (int)contentStream.Length;
  127.             GetRequestStream(msg, requestLength, headers);
  128.             StreamHelper.CopyStream(contentStream, NetStream);           
  129.             contentStream.Close();
  130.         } // SendRequest
  131.         public Stream GetResponseStream()
  132.         {
  133.             if (!_bChunked)
  134.                 _responseStream = new TcpFixedLengthReadingStream(this, _contentLength);
  135.             else
  136.                 _responseStream = new TcpChunkedReadingStream(this);
  137.             
  138.             return _responseStream;
  139.         } // GetResponseStream
  140.         public bool OneWayRequest
  141.         {
  142.             get { return _bOneWayRequest; }
  143.         }
  144.         public void ReturnToCache()
  145.         {
  146.             TcpClientTransportSink.ClientSocketCache.ReleaseSocket(
  147.                 _machineAndPort, this);
  148.         }
  149.     
  150.     } // TcpClientSocketHandler
  151. // namespace System.Runtime.Remoting.Channels
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值