示例#1
0
        /// <summary>
        /// Sends a specified message over a specified connection.
        /// </summary>
        /// <param name="connection">Duplex Channel Connection</param>
        /// <param name="guid">Unique identifier of the Message</param>
        /// <param name="headers">Remoting transport headers</param>
        /// <param name="message">Stream with raw data of the message</param>
        public static void Send(Connection connection, Guid guid, ITransportHeaders headers, Stream message)
        {
            try
            {
                connection.LockWrite();
                BinaryWriter writer = connection.Writer;

                if (writer == null)
                {
                    // Unexpected connection loss. Connection isn´t working anymore, so close it.
                    connection.ReleaseWrite();
                    connection.Close();
                    connection = null;
                }
                else
                {
                    writer.Write(guid.ToByteArray());

                    var headerStream = TransportHeaderWrapper.Serialize(headers);
                    writer.Write((int)headerStream.Length);
                    writer.Write(headerStream.GetBuffer(), 0, (int)headerStream.Length);

                    writer.Write((int)message.Length);
                    MemoryStream ms = message as MemoryStream;
                    if (ms == null)
                    {
                        byte[] msgBuffer = new byte[message.Length];
                        message.Read(msgBuffer, 0, (int)message.Length);
                        writer.Write(msgBuffer, 0, (int)message.Length);
                    }
                    else
                    {
                        writer.Write(ms.GetBuffer(), 0, (int)message.Length);
                    }

                    writer.Flush();
                }
            }
            catch (ObjectDisposedException)
            {
                // Socket may be closed meanwhile. Connection isn't working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            catch (IOException)
            {
                // Unexpected connection loss. Connection isn't working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            catch (SocketException)
            {
                // Unexpected connection loss. Connection isn't working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            catch (RemotingException)
            {
                // Unexpected connection loss. Connection isn't working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            finally
            {
                if (connection != null)
                {
                    connection.ReleaseWrite();
                }
            }
        }
示例#2
0
文件: Message.cs 项目: yallie/zyan
        /// <summary>
        /// Sends a specified message over a specified connection.
        /// </summary>
        /// <param name="connection">Duplex Channel Connection</param>
        /// <param name="guid">Unique identifier of the Message</param>
        /// <param name="headers">Remoting transport headers</param>
        /// <param name="message">Stream with raw data of the message</param>
        public static void Send(Connection connection, Guid guid, ITransportHeaders headers, Stream message)
        {
            try
            {
                connection.LockWrite();
                BinaryWriter writer = connection.Writer;

                if (writer == null)
                {
                    // Unexpected connection loss. Connection isn´t working anymore, so close it.
                    connection.ReleaseWrite();
                    connection.Close();
                    connection = null;
                }
                else
                {
                    writer.Write(guid.ToByteArray());

                    var headerStream = TransportHeaderWrapper.Serialize(headers);
                    writer.Write((int)headerStream.Length);
                    writer.Write(headerStream.GetBuffer(), 0, (int)headerStream.Length);

                    writer.Write((int)message.Length);
                    MemoryStream ms = message as MemoryStream;
                    if (ms == null)
                    {
                        byte[] msgBuffer = new byte[message.Length];
                        message.Read(msgBuffer, 0, (int)message.Length);
                        writer.Write(msgBuffer, 0, (int)message.Length);
                    }
                    else
                        writer.Write(ms.GetBuffer(), 0, (int)message.Length);

                    writer.Flush();
                }
            }
            catch (ObjectDisposedException)
            {
                // Socket may be closed meanwhile. Connection isn´t working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            catch (IOException)
            {
                // Unexpected connection loss. Connection isn´t working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            catch (SocketException)
            {
                // Unexpected connection loss. Connection isn´t working anymore, so close it.
                connection.ReleaseWrite();
                connection.Close();
                connection = null;
            }
            finally
            {
                if (connection != null)
                    connection.ReleaseWrite();
            }
        }