Exemplo n.º 1
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            ISession session   = null;
            uint     sessionid = SessionIDGenerator.GetNextSessionID();

            session = new TCPSession(sessionid, sessionListener, ClientClose, ProcessSend);
            ((TCPSession)session).receiveSAEA.Completed += IOComleted;
            ((TCPSession)session).sendSAEA.Completed    += IOComleted;
            sessions.Add(session.id, session);
            session.SetSocket(e.AcceptSocket);
            sessionListener.OnClientAccept(session, (IPEndPoint)e.AcceptSocket.RemoteEndPoint);
            StartReceive(session);
            StartAccept(e);
        }
Exemplo n.º 2
0
        private void DoReceiveInThread()
        {
            EndPoint remotePoint = IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
            int      cnt         = currentSocket.ReceiveFrom(byteBuffer, byteBuffer.Length, SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                recvBufferTempReader.Attach(byteBuffer, cnt);
                byte[] convBytes = new byte[4];
                recvBufferTempReader.ReadBytes(convBytes, 0, 4);
                uint sessionid = BitConverter.ToUInt32(convBytes, 0);

                lock (sessions)
                {
                    ISession session = null;
                    if (sessionid == 0)
                    {
                        sessionid = SessionIDGenerator.GetNextSessionID();
                        session   = new KCPSession(sessionid, HandleSessionSend, sessionListener);
                        sessions.Add(session.id, session);
                    }
                    else
                    {
                        if (sessions.ContainsKey(sessionid))
                        {
                            session = sessions[sessionid];
                        }
                    }

                    if (session != null)
                    {
                        session.Active(remotePoint as IPEndPoint);
                        session.DoReceiveInGateway(byteBuffer, cnt);
                    }
                    else
                    {
                        Debuger.LogWarning("无效的包! sessionid:{0}", sessionid);
                    }
                }
            }
        }