コード例 #1
0
 /// <summary>
 /// 添加一个MessageHandler到消息处理类池中
 /// </summary>
 /// <param name="channel"></param>
 protected void AddHandler(ANetChannel channel)
 {
     if (!Handlers.ContainsKey(channel.Id))
     {
         var handlers = MessageHandlerFactory.CreateHandlers(channel, this);
         Handlers[channel.Id] = handlers;
     }
 }
コード例 #2
0
        /// <summary>
        /// 通知消息
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="packet"></param>
        public void Notice(ANetChannel channel, Packet packet)
        {
            if (!channel.Connected)
            {
                return;
            }

            this.netService.Enqueue(new SendTask
            {
                Channel = channel,
                Packet  = packet,
            });
        }
コード例 #3
0
 /// <summary>
 /// 连接服务器
 /// </summary>
 /// <param name="endPoint"></param>
 /// <returns></returns>
 public ANetChannel Connect()
 {
     if (this.protocalType == ProtocalType.Tcp)
     {
         this.netService = new TcpService(this.endPoint, this, NetServiceType.Client);
     }
     else if (this.protocalType == ProtocalType.Kcp)
     {
         this.netService = new KcpService(this.endPoint, this, NetServiceType.Client);
     }
     clientChannel = this.netService.Connect();
     return(clientChannel);
 }
コード例 #4
0
        /// <summary>
        /// 创建消息处理类
        /// </summary>
        /// <param name="channel">通讯管道对象</param>
        /// <param name="netService">网络服务对象</param>
        /// <returns></returns>
        public static IEnumerable <IMessageHandler> CreateHandlers(ANetChannel channel, ANetService netService)
        {
            var handlers = new List <IMessageHandler>();

            foreach (var type in types)
            {
                var handler = (IMessageHandler)Activator.CreateInstance(type);
                handler.Channel    = channel;
                handler.NetService = netService;
                channel.OnReceive += handler.DoReceive;
                handlers.Add(handler);
            }
            return(handlers);
        }
コード例 #5
0
        /// <summary>
        /// 处理连接断开(客户端)
        /// </summary>
        /// <param name="channel"></param>
        protected void HandleDisConnectOnClient(ANetChannel channel)
        {
            try
            {
                if (Channels.TryRemove(channel.Id, out ANetChannel value))
                {
#if DEBUG
                    LogRecord.Log(LogLevel.Info, "HandleDisConnectOnClient", $"与服务端{channel.RemoteEndPoint}连接断开.");
#endif
                }
            }
            catch (Exception e)
            {
#if DEBUG
                LogRecord.Log(LogLevel.Warn, "HandleDisConnectOnClient", e);
#endif
            }
        }
コード例 #6
0
        /// <summary>
        /// 处理连接成功回调
        /// </summary>
        /// <param name="channel"></param>
        private void HandleConnect(ANetChannel channel)
        {
            try
            {
                channel.OnDisConnect = HandleDisConnectOnClient;
                channel.Connected    = true;
                AddChannel(channel);
                AddHandler(channel);
#if DEBUG
                LogRecord.Log(LogLevel.Info, "HandleConnect", $"连接服务端:{channel.RemoteEndPoint}成功.");
#endif
            }
            catch (Exception e)
            {
#if DEBUG
                LogRecord.Log(LogLevel.Warn, "HandleConnect", e);
#endif
            }
        }
コード例 #7
0
 /// <summary>
 /// 添加一个通讯管道到连接对象池中
 /// </summary>
 /// <param name="channel"></param>
 protected void AddChannel(ANetChannel channel)
 {
     Channels.TryAdd(channel.Id, channel);
 }