当前位置: 首页>>代码示例>>C#>>正文


C# ISocket.Read方法代码示例

本文整理汇总了C#中ISocket.Read方法的典型用法代码示例。如果您正苦于以下问题:C# ISocket.Read方法的具体用法?C# ISocket.Read怎么用?C# ISocket.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ISocket的用法示例。


在下文中一共展示了ISocket.Read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BufferHeadersInternal

        // this is horribly inefficient right now.
        static IEnumerable<object> BufferHeadersInternal(ISocket socket)
        {
            LinkedList<ArraySegment<byte>> result = new LinkedList<ArraySegment<byte>>();

            int bufferPosition = 0, totalBytesRead = 0;
            byte[] buffer = null;

            while (true)
            {
                if (buffer == null || bufferPosition > BufferSize / 2)
                {
                    buffer = new byte[BufferSize];
                    bufferPosition = 0;
                }

                int bytesRead = 0;

                //Trace.Write("About to read header chunk.");
                var read = new ContinuationState<int>(socket.Read(buffer, bufferPosition, buffer.Length - bufferPosition));
                yield return read;

                bytesRead = read.Result;

                //Trace.Write("Read {0} bytes.", bytesRead);

                result.AddLast(new ArraySegment<byte>(buffer, bufferPosition, bytesRead));

                if (bytesRead == 0)
                    break;

                bufferPosition += bytesRead;
                totalBytesRead += bytesRead;

                // TODO: would be nice to have a state machine and only parse once. for now
                // let's just hope to catch it on the first go-round.
                var bodyDataPosition = IndexOfAfterCRLFCRLF(result);

                if (bodyDataPosition != -1)
                {
                    var last = result.Last.Value;
                    result.RemoveLast();
                    var overlapLength = totalBytesRead - bodyDataPosition;
                    result.AddLast(new ArraySegment<byte>(last.Array, last.Offset, last.Count - overlapLength));
                    result.AddLast(new ArraySegment<byte>(last.Array, last.Offset + last.Count - overlapLength, overlapLength));
                    break;
                }

                // TODO test this
                if (totalBytesRead > MaxHeaderLength)
                    throw new Exception("Request headers data exceeds max header length.");
            }

            yield return result;
        }
开发者ID:markrendle,项目名称:kayak,代码行数:55,代码来源:Parsing.cs

示例2: CreateReadBody

        public Action<byte[], int, int, Action<int>, Action<Exception>> CreateReadBody(
            ISocket socket,
            ArraySegment<byte> bodyDataReadWithHeaders)
        {
            return (buffer, offset, count, r, e) =>
                {
                    if (bodyDataReadWithHeaders.Count > 0)
                    {
                        int bytesRead;

                        bytesRead = Math.Min(bodyDataReadWithHeaders.Count, count);
                        Buffer.BlockCopy(bodyDataReadWithHeaders.Array, bodyDataReadWithHeaders.Offset, buffer, offset, bytesRead);

                        if (bytesRead < bodyDataReadWithHeaders.Count)
                            bodyDataReadWithHeaders =
                                new ArraySegment<byte>(
                                    bodyDataReadWithHeaders.Array,
                                    bodyDataReadWithHeaders.Offset + bytesRead,
                                    bodyDataReadWithHeaders.Count - bytesRead);
                        else
                            bodyDataReadWithHeaders = default(ArraySegment<byte>);

                        r(bytesRead);
                    }
                    else if (socket != null)
                    {
                        socket.Read(buffer, offset, count)(r, e);
                    }
                    else
                    {
                        r(0);
                    }
                };
        }
开发者ID:lovewitty,项目名称:OstrichNet,代码行数:34,代码来源:HttpSupport.cs

示例3: ProxyThis

 private void ProxyThis(ISocket instream, ISocket outstream, AutoResetEvent readyToConnect)
 {
     Logger.Debug("Starting Proxy from " + instream + " to " + outstream);
     int read = 1;
     Closed = false;
     try
     {
         var inBuffer = new byte[_bufferSize];
         // listen for incoming connection
         if (!instream.Connected)
         {
             readyToConnect.Set();
             Logger.Debug("Listening for incoming connections");
             instream.ListenOnce();
             Logger.Debug("Connected incoming");
         }
         // connect to outgoing endpoint and start the reverse proxy
         if (!outstream.Connected)
         {
             outstream.Connect();
             Logger.Debug("Connected outgoing");
             _outRunner.Start();
             Logger.Debug("Started Reverse Proxy");
         }
         while (read > 0)
         {
             // Read from instream
             if (read > 0)
             {
                 try
                 {
                     read = instream.Read(inBuffer, _bufferSize);
     #if DEBUG
     //                            Logger.Debug("Read " + read + " bytes from " + instream);
     #endif
                 }
                 catch (Exception ex)
                 {
                     Logger.Error("Failed to read data from stream (" + instream + "), closing proxy : " + ex.Message);
                     break;
                 }
                 if (read > 0)
                 {
                     try
                     {
                         outstream.Send(inBuffer, read);
     #if DEBUG
                         //Logger.Debug("Wrote " + read + " bytes to " + outstream);
     #endif
                     }
                     catch (Exception ex)
                     {
                         Logger.Error("Failed to send data through stream (" + instream + ") , closing proxy : " + ex.Message);
                         break;
                     }
                 }
             }
             else
             {
                 Logger.Warn("Read no data from (" + instream + "), closing proxy");
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Error("Thread caught exception while processing : " + ex.Message, ex);
     }
     finally
     {
         try
         {
             instream.Close();
         }
         catch (Exception ex)
         {
             Logger.Error("Caught Error closing stream (" + instream + ") : " + ex.Message, ex);
         }
         try
         {
             outstream.Close();
         }
         catch (Exception ex)
         {
             Logger.Error("Caught Error closing stream (" + outstream + ") : " + ex.Message, ex);
         }
     }
 }
开发者ID:gwupe,项目名称:Gwupe,代码行数:87,代码来源:StreamProxy.cs


注:本文中的ISocket.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。