Beispiel #1
0
        public void LoginToServer(SessionToken session)
        {
            SetProtocol(SubProtocol.Handshake);
            SendPacket(new HandshakePacket(ProtocolVersion, host, (short)port, 2));
            SetProtocol(SubProtocol.Login);
            SendPacket(new LoginStartPacket(session.selectedProfile.name));

            while (socketWrapper.IsConnected())
            {
                Packet packet = ReadPacket();
                if (packet != null)
                {
                    if (packet.GetType() == typeof(LoginDisconnectPacket))
                    {
                        Debug.Log(((LoginDisconnectPacket)packet).SourceText);
                        handler.OnConnectionLost(DisconnectReason.LoginRejected, ((LoginDisconnectPacket)packet).RichText);
                        return;
                    }
                    else if (packet.GetType() == typeof(LoginSuccessPacket))
                    {
                        handler.OnGameJoined();
                        SetProtocol(SubProtocol.Game);
                        break;
                    }
                    else if (packet.GetType() == typeof(LoginSetCompressionPacket))
                    {
                        SetCompress(((LoginSetCompressionPacket)packet).Threshold);
                    }
                }
                else
                {
                    handler.OnConnectionLost(DisconnectReason.ConnectionLost, ColorUtility.Set(ColorUtility.Red, "与服务器断开连接"));
                    return;
                }
            }

            netRead = new System.Threading.Thread(() =>
            {
                while (socketWrapper.IsConnected())
                {
                    Packet packet = ReadPacket();
                    if (packet != null)
                    {
                        if (packet.GetType() == typeof(ServerKeepAlivePacket))
                        {
                            SendPacket(new ClientKeepAlivePacket(((ServerKeepAlivePacket)packet).PingID));
                        }
                        else if (packet.GetType() == typeof(ServerDisconnectPacket))
                        {
                            handler.OnConnectionLost(DisconnectReason.InGameKick, ((ServerDisconnectPacket)packet).RichText);
                            return;
                        }
                        else
                        {
                            incomingQueue.Add(packet);
                        }
                    }
                }
                Debug.Log(netRead.Name + " stoped.");
            })
            {
                Name = "PacketHandler", IsBackground = true
            };
            netSend = new System.Threading.Thread(() =>
            {
                while (socketWrapper.IsConnected())
                {
                    Packet packet = GetOutgoingQueue().Take();
                    if (packet != null)
                    {
                        SendPacket(packet);
                    }
                }
                Debug.Log(netSend.Name + " stoped.");
            })
            {
                Name = "PacketSender", IsBackground = true
            };

            netRead.Start();
            netSend.Start();
        }