Beispiel #1
0
 /// <summary>
 ///     单方向信道抽象父类,提供了相关的基本操作
 /// </summary>
 /// <param name="channel">基于流的通讯信道</param>
 /// <param name="protocolStack">协议栈</param>
 protected OnewayChannel(IRawTransportChannel channel, IProtocolStack protocolStack)
 {
     if (channel == null)
     {
         throw new ArgumentNullException(nameof(channel));
     }
     if (!channel.IsConnected)
     {
         throw new ArgumentException("Current raw transport channel hsa been disconnected!");
     }
     if (protocolStack == null)
     {
         throw new ArgumentNullException(nameof(protocolStack));
     }
     if (!protocolStack.Initialize())
     {
         throw new ArgumentException("Cannot initialize current protocol stack!");
     }
     _channel = channel;
     if (_channel.Buffer == null)
     {
         _channel.Buffer = new ReceiveBuffer(102400);
     }
     _logicalAddress        = _channel.LogicalAddress;
     _address               = _channel.Address;
     _channel.Disconnected += RawChannelDisconnected;
     _channel.ReceivedData += RawChannelReceivedData;
     _protocolStack         = protocolStack;
 }
Beispiel #2
0
 //raw channel disconnected.
 void RawChannelDisconnected(object sender, System.EventArgs e)
 {
     lock (_disconnectLockObj)
     {
         if (_rawChannel != null)
         {
             _rawChannel.Buffer               = null;
             _rawChannel.ReceivedData        -= RawReceivedData;
             _rawChannel.Opened              -= Opened;
             _rawChannel.Opening             -= Opening;
             _rawChannel.Closed              -= Closed;
             _rawChannel.Closing             -= Closing;
             _rawChannel.Faulted             -= Faulted;
             _rawChannel.Disconnected        -= Disconnected;
             _rawChannel.Connected           -= Connected;
             _rawChannel.ReceivedDataSegment -= ReceivedDataSegment;
             _rawChannel = null;
         }
         if (_multiPacketManager != null)
         {
             _multiPacketManager.Dispose();
             _multiPacketManager = null;
         }
         if (_parser != null)
         {
             _parser.ParseSucceed -= SegmentParseSucceed;
             _parser.Dispose();
             _parser = null;
         }
     }
     DisconnectedHandler(null);
 }
Beispiel #3
0
 /// <summary>
 ///     关闭
 /// </summary>
 public void Close()
 {
     if (_rawChannel != null)
     {
         _rawChannel.Close();
         _rawChannel = null;
     }
     if (_multiPacketManager != null)
     {
         _multiPacketManager.Dispose();
         _multiPacketManager = null;
     }
 }
Beispiel #4
0
        /// <summary>
        ///     连接到远程终结点
        /// </summary>
        /// <param name="channel">基于流的通讯信道</param>
        /// <exception cref="NullReferenceException">远程终结点地址不能为空</exception>
        public void Connect(IRawTransportChannel channel)
        {
            try
            {
                #region Clear resource before.

                //get clear for org channel.
                if (_channel != null)
                {
                    _channel.Buffer        = null;
                    _channel.Disconnected -= RawChannelDisconnected;
                    _channel.ReceivedData -= RawChannelReceivedData;
                    if (_channel.IsConnected)
                    {
                        _channel.Disconnect();
                    }
                    _channel = null;
                }

                #endregion

                if (channel == null)
                {
                    throw new ArgumentNullException("channel");
                }
                channel.Connect();
                if (_connected = channel.IsConnected)
                {
                    _channel = channel;
                    if (_channel.Buffer == null)
                    {
                        _channel.Buffer = new ReceiveBuffer(102400);
                    }
                    _logicalAddress        = _channel.LogicalAddress;
                    _address               = _channel.Address;
                    _channel.Disconnected += RawChannelDisconnected;
                    _channel.ReceivedData += RawChannelReceivedData;
                    ChannelConnectedHandler(null);
                }
            }
            catch (System.Exception ex)
            {
                _tracing.Error(ex, null);
                throw;
            }
        }
Beispiel #5
0
 /// <summary>
 ///     消息传输信道
 /// </summary>
 /// <param name="rawChannel">数据流信道</param>
 /// <param name="protocolStack">协议栈</param>
 /// <param name="parser">解析器</param>
 /// <exception cref="ArgumentNullException">参数错误</exception>
 public MessageTransportChannel(IRawTransportChannel rawChannel, IProtocolStack protocolStack, ISegmentDataParser <T> parser)
 {
     if (rawChannel == null)
     {
         throw new ArgumentNullException("rawChannel");
     }
     if (protocolStack == null)
     {
         throw new ArgumentNullException("protocolStack");
     }
     if (rawChannel.SupportSegment && parser == null)
     {
         throw new ArgumentNullException("parser");
     }
     _rawChannel  = rawChannel;
     _channelType = _rawChannel.ChannelType;
     //initialize segment data parser, if current channel support segment data recv.
     if (_rawChannel.SupportSegment)
     {
         _parser = parser;
         _parser.ParseSucceed            += SegmentParseSucceed;
         _rawChannel.ReceivedDataSegment += ReceivedDataSegment;
     }
     else
     {
         _rawChannel.ReceivedData += RawReceivedData;
         if (_rawChannel.Buffer == null)
         {
             _rawChannel.Buffer = new ReceiveBuffer(ChannelConst.RecvBufferSize);
         }
     }
     _protocolStack            = protocolStack;
     _rawChannel.Opened       += Opened;
     _rawChannel.Opening      += Opening;
     _rawChannel.Closed       += Closed;
     _rawChannel.Closing      += Closing;
     _rawChannel.Faulted      += Faulted;
     _rawChannel.Disconnected += RawChannelDisconnected;
     _rawChannel.Connected    += Connected;
     _key       = _rawChannel.Key;
     _localIep  = _rawChannel.LocalEndPoint;
     _remoteIep = _rawChannel.RemoteEndPoint;
     //open this channel at last.
     _rawChannel.Open();
 }
 /// <summary>
 ///     输出通道元接口,提供了相关的基本操作。
 /// </summary>
 /// <param name="channel">基于流的通讯信道</param>
 /// <param name="protocolStack">协议栈</param>
 public OutputChannel(IRawTransportChannel channel, IProtocolStack protocolStack)
     : base(channel, protocolStack)
 {
 }
Beispiel #7
0
 /// <summary>
 ///     消息传输信道
 /// </summary>
 /// <param name="rawChannel">数据流信道</param>
 /// <param name="protocolStack">协议栈</param>
 /// <exception cref="ArgumentNullException">参数错误</exception>
 public MessageTransportChannel(IRawTransportChannel rawChannel, IProtocolStack protocolStack)
     : this(rawChannel, protocolStack, new SegmentDataParser <T>(protocolStack))
 {
 }