Пример #1
0
        public void SendWithSession(KCPClientSession session, byte[] data, int size)
        {
            var e = mSAEPool.GetObject();

            e.RemoteEndPoint = session.ClientEndPoint;
            Buffer.BlockCopy(data, 0, e.Buffer, 0, size);
            e.SetBuffer(0, size);
            if (!mServerSocket.SendToAsync(e))
            {
                ProcessSend(e);
            }
        }
Пример #2
0
 public virtual void Update()
 {
     ProcessRecvQueue();
     foreach (var vk in mSessions)
     {
         KCPClientSession session = vk.Value;
         session.Update();
         if (session.IsDead())
         {
             mDisposeSessionConvs.Enqueue(session.Conv);
         }
     }
     ProcessDisposeSession();
 }
Пример #3
0
 private void ProcessDisposeSession()
 {
     while (!mDisposeSessionConvs.IsEmpty)
     {
         UInt32 conv      = 0;
         bool   isSuccess = mDisposeSessionConvs.TryDequeue(out conv);
         if (!isSuccess)
         {
             continue;
         }
         KCPClientSession tempSession = null;
         mSessions.TryRemove(conv, out tempSession);
         Console.WriteLine("Dead Session:" + tempSession.ClientEndPoint.ToString());
     }
 }
Пример #4
0
        private void ProcessRecvQueue()
        {
            while (!RecvQueue.IsEmpty)
            {
                SocketAsyncEventArgs e = null;
                bool isSuccess         = RecvQueue.TryDequeue(out e);
                if (!isSuccess || e == null)
                {
                    continue;
                }
                if (e.BytesTransferred == 0)
                {
                    mSAEPool.PutObject(e);
                    continue;
                }
                //handshake with {0,0,0,0} at the first
                if (Helper.IsHandshakeDataRight(e.Buffer, e.Offset, e.BytesTransferred))
                {
                    uint conv       = Helper.iclock();
                    var  newSession = new KCPClientSession(conv);
                    newSession.ClientEndPoint  = e.RemoteEndPoint;
                    newSession.KCPOutput       = SendWithSession;
                    newSession.RecvDataHandler = RecvDataHandler;
                    mSessions.TryAdd(conv, newSession);

                    KCP.ikcp_encode32u(e.Buffer, e.BytesTransferred, conv);
                    e.SetBuffer(0, e.BytesTransferred + 4);
                    if (!mServerSocket.SendToAsync(e))
                    {
                        ProcessSend(e);
                    }
                    Console.WriteLine("Handshake from:" + e.RemoteEndPoint.ToString());
                }
                else
                {
                    uint conv = 0;
                    KCP.ikcp_decode32u(e.Buffer, e.Offset, ref conv);
                    KCPClientSession session = null;
                    mSessions.TryGetValue(conv, out session);
                    if (session != null)
                    {
                        session.processRecvQueue(e);
                    }
                    mSAEPool.PutObject(e);
                }
            }
        }
Пример #5
0
 public void OnPacket(KCPClientSession session, byte[] data)
 {
     Console.WriteLine("Receive:" + Encoding.UTF8.GetString(data));
     session.Send(data);
 }