示例#1
0
 public void DoneReading(IAsyncResult readOutputAsyncResult)
 {
     try
     {
         int bytesRead = bufferRead.EndInvoke(readOutputAsyncResult);
         if (bytesRead > 0)
         {
             bufferWrite(inputBuffer, 0, bytesRead);
             if (!IsClosed)
             {
                 bufferRead.BeginInvoke(inputBuffer, 0, inputBuffer.Length, DoneReading, null);
             }
         }
         else
         {
             SetComplete();
         }
     }
     catch (Exception ex)
     {
         SetComplete(ex);
     }
 }
        public void DoneReadingPreamble(IAsyncResult readOutputAsyncResult)
        {
            try
            {
                int bytesRead = bufferRead.EndInvoke(readOutputAsyncResult);
                if (bytesRead > 0)
                {
                    if (bytesRead == 1)
                    {
                        bytesRead += bufferRead(preambleBuffer, 1, preambleBuffer.Length - 1);
                    }
#if VERBOSE
                    Trace.TraceInformation("Input, read preamble: {0}", bytesRead);
#endif

                    int    connectionId = BitConverter.ToInt32(preambleBuffer, 0);
                    ushort frameSize    = BitConverter.ToUInt16(preambleBuffer, sizeof(Int32));

                    // we have to get the frame off the wire irrespective of
                    // whether we can dispatch it
                    if (frameSize > 0)
                    {
                        // read the block synchronously
                        bytesRead = 0;
                        do
                        {
                            bytesRead += bufferRead(inputBuffer, bytesRead, frameSize - bytesRead);
                        }while (bytesRead < frameSize);
#if VERBOSE
                        Trace.TraceInformation("Input, read data {0}", frameSize);
#endif
                    }

                    MultiplexedConnection connection;

                    lock (connectionLock)
                    {
                        if (!connections.TryGetValue(connectionId, out connection))
                        {
                            try
                            {
                                connection = connectionFactory(connectionId, callbackState);
                                if (connection != null)
                                {
                                    connections.Add(connectionId, connection);
                                }
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Unable to establish multiplexed connection: {0}", ex.Message);
                                connection = null;
                            }
                        }
                    }

                    if (connection != null)
                    {
                        bool shutdownConnection = (frameSize == 0);
                        if (frameSize > 0)
                        {
                            try
                            {
                                connection.Write(inputBuffer, 0, frameSize);
#if VERBOSE
                                Trace.TraceInformation("Connection write: {0}", frameSize);
#endif
                            }
                            catch (IOException ioe)
                            {
                                if (ioe.InnerException is SocketException &&
                                    (((SocketException)ioe.InnerException).ErrorCode == 10004 ||
                                     ((SocketException)ioe.InnerException).ErrorCode == 10054))
                                {
                                    Trace.TraceInformation(
                                        "Socket cancelled with code {0} during pending read: {1}",
                                        ((SocketException)ioe.InnerException).ErrorCode,
                                        ioe.Message);
                                }
                                else
                                {
                                    Trace.TraceError("Unable to write to multiplexed connection: {0}", ioe.Message);
                                }
                                shutdownConnection = true;
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("Unable to write to multiplexed connection: {0}", ex.Message);
                                shutdownConnection = true;
                            }
                        }

                        if (shutdownConnection)
                        {
                            connection.Dispose();
                            lock (connectionLock)
                            {
                                if (connections.ContainsKey(connectionId))
                                {
                                    connections.Remove(connectionId);
                                }
                            }
                        }
                    }

                    if (!stopped)
                    {
                        bufferRead.BeginInvoke(preambleBuffer, 0, preambleBuffer.Length, DoneReadingPreamble, null);
                    }
                }
                else
                {
                    OnCompleted();
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error starting multiplex pump : {0}", ex.Message);
                OnCompleted();
            }
        }
示例#3
0
        public void DoneReading(IAsyncResult readOutputAsyncResult)
        {
            int bytesRead;

            try
            {
                try
                {
                    bytesRead = bufferRead.EndInvoke(readOutputAsyncResult);
#if VERBOSE
                    Trace.TraceInformation("Output, read bytes: {0}", bytesRead);
#endif
                }
                catch (IOException ioe)
                {
                    if (ioe.InnerException is SocketException &&
                        (((SocketException)ioe.InnerException).ErrorCode == 10004 ||
                         ((SocketException)ioe.InnerException).ErrorCode == 10054))
                    {
                        Trace.TraceInformation(
                            "Socket cancelled with code {0} during pending read: {1}",
                            ((SocketException)ioe.InnerException).ErrorCode,
                            ioe.Message);
                    }
                    else
                    {
                        Trace.TraceError("Unable to read from source: {0}", ioe.Message);
                    }
                    bytesRead = 0;
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Unable to read from source: {0}", ex.Message);
                    bytesRead = 0;
                }

                if (bytesRead > 0)
                {
                    lock (threadLock)
                    {
                        byte[] connectionIdPreamble = BitConverter.GetBytes(connectionId);
                        Buffer.BlockCopy(connectionIdPreamble, 0, inputBuffer, 0, sizeof(int));
                        byte[] sizePreamble = BitConverter.GetBytes((ushort)bytesRead);
                        Buffer.BlockCopy(sizePreamble, 0, inputBuffer, sizeof(int), sizeof(ushort));

                        bufferWrite(inputBuffer, 0, bytesRead + preambleSize);
#if VERBOSE
                        Trace.TraceInformation("Output, wrote preamble: {0}", bytesRead + preambleSize);
#endif
                    }

                    if (!IsClosed)
                    {
                        try
                        {
                            bufferRead.BeginInvoke(inputBuffer, preambleSize, inputBuffer.Length - preambleSize, DoneReading, null);
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError("Can't start reading from source: {0}", ex.Message);
                            SetComplete(ex);
                        }
                    }
                }
                else
                {
                    lock (threadLock)
                    {
                        byte[] connectionIdPreamble = BitConverter.GetBytes(connectionId);
                        Buffer.BlockCopy(connectionIdPreamble, 0, inputBuffer, 0, sizeof(int));
                        byte[] sizePreamble = BitConverter.GetBytes((ushort)0);
                        Buffer.BlockCopy(sizePreamble, 0, inputBuffer, sizeof(int), sizeof(ushort));

                        bufferWrite(inputBuffer, 0, preambleSize);
#if VERBOSE
                        Trace.TraceInformation("Output, wrote preamble: {0}", bytesRead + preambleSize);
#endif
                    }
                    SetComplete();
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Unable to write to target: {0}", ex.Message);
                SetComplete(ex);
            }
        }