Example #1
0
        public KcpSession DetermineIsBadOrNewConnection(uint conv, IPEndPoint endPoint)
        {
            KcpSession session = null;

            //2 包合法IP端口存在(老连接的数据) 这里还需要KCP底层继续校验 例如:SN号是否合法还应该考虑客户端如果IP没变但是端口变了(这个可能需要兼容)
            //(这里暂时认为IP和端口号必须一致就OK,其他情况暂时认为都不合法)
            if (conv == 0)
            {
                return(CheckingNewOrOld(endPoint));
            }
            else if (Sessions.TryGetValue(conv, out session)) //因为conv是uint 因此这里默认隐含 conv>0
            {
                if (session.Peer == endPoint)                 //暂时要求端口也必须一致
                {
                    return(session);
                }
                else
                {
                    //logger?.LogInformation($"Client:{kcpSession.IP} duplicate,cant Add to Session list!");
                    AddBad(endPoint);
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Example #2
0
File: KcpSvr.cs Project: sgf/mkcp
        private void OnRawReceive(Span <byte> data, IPEndPoint endPoint)
        {
            //检查黑名单
            if (SessionMgr.InBad(endPoint))
            {
                return;
            }
            //检查格式
            var(isBad, conv) = Kcp.IsBadHeadFormat(data);
            if (isBad)
            {
                SessionMgr.AddBad(endPoint); return;
            }

            var session = SessionMgr.DetermineIsBadOrNewConnection(conv, endPoint);

            if (session == null)
            {
                return;
            }
            KcpSession.KCPInput(session, data);
            using var mem = _sock.GetMemory(OS._8kb);
            var buff = mem.Memory.ToArray();
            var rcnt = session.kcp.Recv(buff, 0, buff.Length);

            if (rcnt > 0)
            {
                OnKcpReceive?.Invoke(session, buff.AsSpan().Slice(0, rcnt), endPoint);
            }
        }
Example #3
0
 public static void KCPInput(KcpSession session, Span <byte> data)
 {
     if (!session.Closed)
     {
         session.LastRevicedTime = DateTimeOffset.Now;
         session.kcp.Input(data);
     }
 }
Example #4
0
 /// <summary>
 /// 移除Session
 /// </summary>
 /// <param name="session2remove"></param>
 public void Remove(KcpSession session)
 {
     if (Sessions.ContainsKey(session.SID))
     {
         ConList.Remove(session.Peer);
         Sessions.Remove(session.SID);
         SIDPool.Enqueue(session.SID);
     }
 }
Example #5
0
        private KcpSession AddNewSession(IPEndPoint endPoint)
        {
            var sid  = SIDPool.Dequeue();
            var news = KcpSession.CreateSvrSeesion(sid, endPoint, this);

            Sessions.Add(sid, news);
            ConList.Add(endPoint, news);
            OnNew?.Invoke(news);
            return(news);
        }
Example #6
0
 private KcpClient(IPEndPoint svrIpPort)
 {
     KcpSession = KcpSession.CreateClientSession(svrIpPort);
     _sock      = KcpSocket.CreateClient(svrIpPort, OnRawReceive);
     KcpSession.kcp.SetOutput((data, user) => {
         if (!KcpSession.Closed)
         {
             _ = _sock.UdpSendToAsync(new Memory <byte>(data, 0, size), KcpSession.Peer);
         }
     });
     _sock.OnUpdate += _sock_OnUpdate;
 }
Example #7
0
File: KcpSvr.cs Project: sgf/mkcp
 private void SessionMgr_OnNew(KcpSession session)
 {
     //新的连接
 }
Example #8
0
File: KcpSvr.cs Project: sgf/mkcp
 private void SessionMgr_OnKick(KcpSession session)
 {
     throw new NotImplementedException();
 }