Пример #1
0
        internal override void OnInitialization()
        {
            base.OnInitialization();

            _helper = Helper as INetworkHelper;
            _helper.LoadProtocolChannels(ChannelTypes);
            _helper.BeginConnectServerEvent += (cha) =>
            {
                BeginConnectServerEvent?.Invoke(cha);
            };
            _helper.ConnectServerSuccessEvent += (cha) =>
            {
                ConnectServerSuccessEvent?.Invoke(cha);
            };
            _helper.ConnectServerFailEvent += (cha) =>
            {
                ConnectServerFailEvent?.Invoke(cha);
            };
            _helper.DisconnectServerEvent += (cha) =>
            {
                DisconnectServerEvent?.Invoke(cha);
            };
            _helper.SendMessageEvent += (cha) =>
            {
                SendMessageEvent?.Invoke(cha);
            };
            _helper.ReceiveMessageEvent += (cha, mes) =>
            {
                ReceiveMessageEvent?.Invoke(cha, mes);
            };
        }
Пример #2
0
        /// <summary>
        /// 与服务器断开连接
        /// </summary>
        public void DisconnectServer()
        {
            if (Client != null)
            {
                if (IsNeedConnect && IsConnect)
                {
                    Client.Shutdown(SocketShutdown.Both);
                    Client.Disconnect(false);

                    DisconnectServerEvent?.Invoke(this);
                }
                Client.Close();
                Client.Dispose();
                Client = null;
            }
        }
Пример #3
0
        internal override void OnInitialization()
        {
            base.OnInitialization();

            //加载通信协议通道
            for (int i = 0; i < ChannelTypes.Count; i++)
            {
                Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(ChannelTypes[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(ProtocolChannelBase)))
                    {
                        if (!_protocolChannels.ContainsKey(type))
                        {
                            _protocolChannels.Add(type, Activator.CreateInstance(type) as ProtocolChannelBase);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:通信协议通道类 " + ChannelTypes[i] + " 必须实现接口:IProtocolChannel!");
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:丢失通信协议通道类 " + ChannelTypes[i] + "!");
                }
            }

            //初始化通道
            foreach (var channel in _protocolChannels)
            {
                channel.Value.OnInitialization();
                channel.Value.SendMessageEvent += (cha) =>
                {
                    SendMessageEvent?.Invoke(cha);
                };
                channel.Value.ReceiveMessageEvent += (cha, message) =>
                {
                    ReceiveMessageEvent?.Invoke(cha, message);
                };
                channel.Value.DisconnectServerEvent += (cha) =>
                {
                    DisconnectServerEvent?.Invoke(cha);
                };
            }
        }
        /// <summary>
        /// 加载通信管道
        /// </summary>
        /// <param name="channelTypes">启用的通信协议通道类型</param>
        public void LoadProtocolChannels(List <string> channelTypes)
        {
            for (int i = 0; i < channelTypes.Count; i++)
            {
                Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(channelTypes[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(ProtocolChannelBase)))
                    {
                        if (!ProtocolChannels.ContainsKey(type))
                        {
                            ProtocolChannels.Add(type, Activator.CreateInstance(type) as ProtocolChannelBase);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:通信协议通道类 " + channelTypes[i] + " 必须继承至基类:ProtocolChannelBase!");
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:丢失通信协议通道类 " + channelTypes[i] + "!");
                }
            }

            foreach (var channel in ProtocolChannels)
            {
                channel.Value.OnInitialization();
                channel.Value.SendMessageEvent += (cha) =>
                {
                    SendMessageEvent?.Invoke(cha);
                };
                channel.Value.ReceiveMessageEvent += (cha, message) =>
                {
                    ReceiveMessageEvent?.Invoke(cha, message);
                };
                channel.Value.DisconnectServerEvent += (cha) =>
                {
                    DisconnectServerEvent?.Invoke(cha);
                };
            }
        }