public override void Execute(Protocol protocol, ClientToken client) { if (protocol.GetType() != typeof(LoginProtocol)) { return; } LoginProtocol loginProtocol = protocol as LoginProtocol; Console.WriteLine("登录服务运行,账号为:" + loginProtocol.userId + ",密码为:" + loginProtocol.userPassword + ";"); AccountDatabase accountDatabase = server.databases[typeof(AccountDatabase)] as AccountDatabase; //查询是否有用户名密码相符的用户 if (accountDatabase.Find(x => (x.Id == loginProtocol.userId && x.Password == loginProtocol.userPassword)) != null) { //登录成功 LoginSuccessProtocol loginSuccessProtocol = new LoginSuccessProtocol(); //生成令牌 loginSuccessProtocol.PassBadge = 0; Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(loginSuccessProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } else { //登录失败 LoginFailedProtocol loginFailedProtocol = new LoginFailedProtocol(); if (accountDatabase.Find(x => (x.Id == loginProtocol.userId)) != null) { loginFailedProtocol.State = LoginFailedState.WrongPassword; } else { loginFailedProtocol.State = LoginFailedState.WrongID; } Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(loginFailedProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } }
public override void Execute(Protocol protocol, ClientToken client) { if (protocol.GetType() != typeof(RegisterProtocol)) { return; } RegisterProtocol registerProtocol = protocol as RegisterProtocol; Console.WriteLine("注册服务运行,账号为:" + registerProtocol.userId + ",密码为:" + registerProtocol.userPassword + ";"); AccountDatabase accountDatabase = server.databases[typeof(AccountDatabase)] as AccountDatabase; //检查是否ID已经被注册 if (accountDatabase.Find(x => (x.Id == registerProtocol.userId)) != null) { //已被注册,返回注册失败 RegisterFailedProtocol registerFailedProtocol = new RegisterFailedProtocol(); registerFailedProtocol.state = RegisterFailedState.IDAlreadyExist; Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(registerFailedProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } else { //注册成功 RegisterSuccessProtocol registerSuccessProtocol = new RegisterSuccessProtocol(); registerSuccessProtocol.PassBadge = 0; //写入数据库 accountDatabase.Write(new Account() { Id = registerProtocol.userId, Password = registerProtocol.userPassword }); Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(registerSuccessProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } }
static void SerailizingTestCase() { LoginProtocol loginProtocol = new LoginProtocol() { userId = "abc", userPassword = "******" }; Byte[] bytes = ProtocolHelper.ConvertProtocolToBytes(loginProtocol); var result = ProtocolHelper.ConvertBytesToProtocol(bytes); if (result != null) { Debug.Log("序列化成功"); } else { Debug.Log("序列化失败"); } }
public void SendMessage(Protocol protocol) { if (socketConnection == null) { return; } try { NetworkStream stream = socketConnection.GetStream(); if (stream.CanWrite) { Byte[] serializedData = ProtocolHelper.ConvertProtocolToBytes(protocol); byte[] clientMessageAsByteArray = GameProtocol.Encoder.Encode(serializedData); stream.Write(clientMessageAsByteArray, 0, clientMessageAsByteArray.Length); Debug.Log("发送数据协议,协议类型为" + protocol.GetType().ToString()); } } catch (SocketException socketException) { Debug.Log("套接字异常:" + socketException.ToString()); } }