Example #1
0
        public override void channelRead(HandlerContext ctx, object msg)
        {
            try
            {
                var ip  = (ctx.Socket.RemoteEndPoint as IPEndPoint);
                var key = ip.Address.ToString() + ip.Port;

                byte[] buf = (byte[])msg;
                var    str = Encoding.ASCII.GetString(buf);
                LogMsg(string.Format("recv:{0}", str));
                //取出上次未处理完的数据
                var lastData = "";
                if (cache.TryRemove(key, out lastData))
                {
                    str = lastData + str;
                }

                if (str.StartsWith(Packet.HEADER))
                {
                    //多个粘包
                    if (str.Contains(Packet.TAIL + Packet.HEADER))
                    {
                        Log.Info("数据粘包");
                        str = str.Replace(Packet.TAIL + Packet.HEADER, Packet.TAIL + "<##>" + Packet.HEADER);
                        string[] array = Regex.Split(str, "<##>");
                        foreach (var s in array)
                        {
                            if (s.EndsWith(Packet.TAIL))
                            {
                                Log.Info("组包:" + s);
                                Packet p = Packet.FromData(s);
                                Pipe(ctx, p);
                            }
                            else
                            {
                                cache.TryAdd(key, s);
                            }
                        }
                    }
                    //一个包或半个包
                    else
                    {
                        //一个包
                        if (str.EndsWith(Packet.TAIL))
                        {
                            Log.Info("组包:" + str);
                            Packet p = Packet.FromData(str);
                            Pipe(ctx, p);
                        }
                        else//半个包
                        {
                            cache.TryAdd(key, str);
                        }
                    }
                }
                else
                {
                }
            }catch (Exception e)
            {
                if (Log != null)
                {
                    Log.Error("PacketDecoder Error:", e);
                }
            }
        }
Example #2
0
 private void DataSegmentHandler_MsgEvent(string mn, HandlerContext context)
 {
     clientManager.UpdateClient(mn, context);
 }
Example #3
0
 public override void OnConnected(HandlerContext ctx)
 {
 }
Example #4
0
 private void DataSegmentHandler_ACKEvent(string mn, HandlerContext context, Segment segment)
 {
     responseManager.OnPacket(segment);
 }
Example #5
0
 /// <summary>
 /// 推送到下一个处理器进行处理
 /// </summary>
 /// <param name="ctx"></param>
 /// <param name="msg"></param>
 protected void Pipe(HandlerContext ctx, object msg)
 {
     Pipeline.Pipe(ctx, msg);
 }
Example #6
0
 /// <summary>
 /// Socket已连接
 /// </summary>
 /// <param name="ctx"></param>
 public abstract void OnConnected(HandlerContext ctx);
Example #7
0
 /// <summary>
 /// 处理流水线中的数据包
 /// </summary>
 /// <param name="ctx">上下文</param>
 /// <param name="msg">数据包</param>
 public abstract void channelRead(HandlerContext ctx, object msg);
Example #8
0
 /// <summary>
 /// 通信中断
 /// </summary>
 /// <param name="handlerContext"></param>
 public void Connect(HandlerContext handlerContext)
 {
     workline.ForEach(handler => { handler.OnConnected(handlerContext); });
 }