예제 #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);
            }
        }
예제 #2
0
파일: KcpSvr.cs 프로젝트: 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);
            }
        }
예제 #3
0
파일: KcpSession.cs 프로젝트: sgf/mkcp
 public static void KCPInput(KcpSession session, Span <byte> data)
 {
     if (!session.Closed)
     {
         session.LastRevicedTime = DateTimeOffset.Now;
         session.kcp.Input(data);
     }
 }
예제 #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);
     }
 }
예제 #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);
        }
예제 #6
0
파일: KcpClient.cs 프로젝트: sgf/mkcp
 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;
 }
예제 #7
0
파일: KcpSvr.cs 프로젝트: sgf/mkcp
 private void SessionMgr_OnNew(KcpSession session)
 {
     //新的连接
 }
예제 #8
0
파일: KcpSvr.cs 프로젝트: sgf/mkcp
 private void SessionMgr_OnKick(KcpSession session)
 {
     throw new NotImplementedException();
 }