Example #1
0
        private void MessageHandle(InternalChannelHandlerContext <TKey, TProtocol> context)
        {
            try
            {
                if (!_messageHandle.ContainsKey(context.Code))
                {
                    return;
                }

                var messageHandle = _messageHandle[context.Code];

                var types = ((System.Reflection.TypeInfo)messageHandle.GetType()).ImplementedInterfaces.Single(i => i.GenericTypeArguments.Length == 3).GenericTypeArguments;

                var channelHandlerContextType = typeof(ChannelHandlerContext <, ,>).MakeGenericType(typeof(TKey), typeof(TProtocol), types[2]);

                var ctor = channelHandlerContextType.GetConstructor(new[] { context.Channel.GetType(), context.Message.GetType() });

                var channelHandlerContext = ctor.Invoke(new[] { context.Channel, context.Message });

                var method = messageHandle.GetType().GetMethod("Handle", new Type[] { channelHandlerContextType });

                method.Invoke(messageHandle, new[] { channelHandlerContext });
            }
            catch (Exception ex)
            {
                // todo log
                Debug.WriteLine($"消息处理失败!{context.Channel.Socket.LocalEndPoint} {context.Channel.Socket.RemoteEndPoint} 退出连接!{ex}");
            }
        }
Example #2
0
        private void FailHandle(InternalChannelHandlerContext <TKey, TProtocol> context)
        {
            if (context.Channel.Socket.Connected)
            {
                Debug.WriteLine($"消息接收失败!{context.Channel.Socket.LocalEndPoint} {context.Channel.Socket.RemoteEndPoint} 退出连接! {context.Exception}");

                context.Channel.Socket.Shutdown(SocketShutdown.Both);
                context.Channel.Socket.Close();
            }
        }
Example #3
0
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private InternalChannelHandlerContext <TKey, TProtocol> Deserialize(InternalChannelHandlerContext <TKey, TProtocol> context)
        {
            try
            {
                context.Message = _messageSerializer.Deserialize(context.ProcessedBytes, _messageMapping[context.Code]);

                return(context);
            }
            catch (Exception ex)
            {
                context.Exception  = ex;
                context.PipeStatus = PipeStatus.DeserializeError;

                return(context);
            }
        }
Example #4
0
        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private InternalChannelHandlerContext <TKey, TProtocol> Decoder(InternalChannelHandlerContext <TKey, TProtocol> context)
        {
            try
            {
                TProtocol protocol = _protocolCoder.Decoder(context.UnProcessedBytes);
                context.Code           = protocol.Code;
                context.ProcessedBytes = protocol.Message;

                return(context);
            }
            catch (Exception ex)
            {
                context.Exception  = ex;
                context.PipeStatus = PipeStatus.DecoderError;

                return(context);
            }
        }
Example #5
0
        /// <summary>
        /// 拆包
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private IEnumerable <InternalChannelHandlerContext <TKey, TProtocol> > UnPack(InternalChannelHandlerContext <TKey, TProtocol> context)
        {
            try
            {
                var message = context.Channel.UnProcessed.Concat(context.UnProcessedBytes).ToArray();
                IEnumerable <byte[]> processed = _unpacker.Unpack(message, out var unProcessed);
                context.Channel.UnProcessed = unProcessed;

                return(processed.Select(m => new InternalChannelHandlerContext <TKey, TProtocol> {
                    Channel = context.Channel, UnProcessedBytes = m
                }));
            }
            catch (Exception ex)
            {
                context.PipeStatus = PipeStatus.UnPackError;
                context.Exception  = ex;
                return(new InternalChannelHandlerContext <TKey, TProtocol>[] { context });
            }
        }