private void OnLogin(object sender, RoutedEventArgs e) { if (_protocol.IsConnected == false) { _protocol.Connect(); } if (_protocol.IsConnected == false) { return; } UIUser user = null; string error = null; var msg = new MessageLoginRequest() { CallID = DateTime.Now.GetHashCode(), UserID = Account.GetHashCode(), User = this._protocol.UserList.First().Value, Account = Account, Password = Password }; using (var signal = new AutoResetEvent(false)) { EventHandler <Message> waitBack = (object sd, Message respondsMsg) => { if (msg.CallID != respondsMsg.CallID) { return; } if (respondsMsg.MessageID == MessageID.MI_LOGIN_FAILED) { error = (respondsMsg as MessageLoginFailed).Error; signal.Set(); } else if (respondsMsg.MessageID == MessageID.MI_LOGIN_RESPONSE) { var realMsg = respondsMsg as MessageLoginResponds; user = new UIUser(realMsg.UserID, realMsg.Account, "", realMsg.Name); user.Introduce = realMsg.Introduce; user.Client = realMsg.User.Client; _protocol.UserList[user.Client] = user; signal.Set(); } }; _protocol.MessageEvent += waitBack; _protocol.SendMsg(msg); if (signal.WaitOne(2000) == false) { error = "登录超时,请重试!"; } _protocol.MessageEvent -= waitBack; } if (user != null) { User = user; LoginResult = LoginResult.LoginResultLoginSuccesful; MessageWindow.Show(this.Owner, "登录成功!"); this.OnClose(); } else { MessageWindow.Show(this.Owner, error); } }
private void OnApplyRegister(object sender, RoutedEventArgs e) { try { //检查注册信息 if (userHead.Source == null) { throw new Exception("请上传头像!"); } var validateResult = new AccountValidationRule().Validate(RegisteredAccount, null); if (validateResult.IsValid == false) { throw new Exception(validateResult.ErrorContent.ToString()); } if (repeatedRegisteredPasswordBox.Password != registeredPasswordBox.Password) { throw new Exception(PasswordValidattion.GetErrorMessage(repeatedRegisteredPasswordBox)); } validateResult = new PasswordValidationRule().Validate(RegisteredPassword, null); if (validateResult.IsValid == false) { throw new Exception(validateResult.ErrorContent.ToString()); } validateResult = new NameValidationRule().Validate(RegisteredName, null); if (validateResult.IsValid == false) { throw new Exception(validateResult.ErrorContent.ToString()); } //连接网络 if (_protocol.IsConnected == false) { _protocol.Connect(); } if (_protocol.IsConnected == false) { return; } //填写消息 var msg = new MessageRegisterRequest() { CallID = DateTime.Now.GetHashCode(), UserID = Account.GetHashCode(), Account = RegisteredAccount, Password = RegisteredPassword, Name = RegisteredName, Introduce = RegisteredIntroduce, User = _protocol.UserList.First().Value }; var encoder = new BmpBitmapEncoder(); var bitmapSource = this.userHead.Source as BitmapSource; encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); using (var stream = new MemoryStream()) { encoder.Save(stream); msg.HeadsculptStream = stream.GetBuffer(); } //发送 string error = null; bool result = false; using (var signal = new ManualResetEventSlim()) { EventHandler <Message> waitBack = (object sd, Message respondsMsg) => { if (msg.CallID != respondsMsg.CallID) { return; } if (respondsMsg.MessageID == MessageID.MI_REGISTER_FAILED) { error = (respondsMsg as MessageRegisterFailed).Error; signal.Set(); } else if (respondsMsg.MessageID == MessageID.MI_REGISTER_SUCCESSFULLY) { var realMsg = (respondsMsg as MessageRegisterSuccessfully); result = true; signal.Set(); } }; this._protocol.MessageEvent += waitBack; this._protocol.SendBigMsg(msg); if (signal.Wait(2000) == false) { error = "服务器没有响应,请重试!"; } this._protocol.MessageEvent -= waitBack; } if (result == false) { throw new Exception(error); } MessageWindow.Show(this, "注册成功,赶紧登录去吧!"); this.OnLoginState(); } catch (Exception ex) { MessageWindow.Show(this, ex.Message); } _protocol.Disconnect(); }