/// <summary> /// 点击登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SignalInButton_Click(object sender, EventArgs e) { this.SignalInButton.Text = "正 在 登 录..."; //配置登录信息 AutoSignInConfig(); //密码是否正确 bool AccountFitPassword = false; //云服务器请求决断 FunctionWords functionWords = this.SendNewsAndReceiveAnswer(FunctionKind.VerifyAccount, this.Account, this.Password, null); if (functionWords != null && functionWords.stringList.Count > 0) { if (functionWords.stringList[0] == "true") { AccountFitPassword = true; } } //账户密码正确 if (AccountFitPassword) { //触发登录事件 FinishSignIn(this, Account); } else { this.SignalInButton.Text = "登 录"; } }
/// <summary> /// 初始化界面,是否为记住密码,是否需要自动登录 /// </summary> public void init() { if (File.Exists(System.Environment.CurrentDirectory + "\\init")) { this.PasswordCheckBox.Checked = true; //读取历史账户、密码 FileStream file = new FileStream(System.Environment.CurrentDirectory + "\\init", FileMode.Open); StreamReader reader = new StreamReader(file); string ifAutoSignIn = reader.ReadLine(); Account = reader.ReadLine(); Password = reader.ReadLine(); reader.Close(); //记住密码时自动获取图标 //UserImage = new Bitmap(System.Environment.CurrentDirectory + "\\initImage.png"); FunctionWords functionWords = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForImage, this.Account, "", null); if (functionWords != null && functionWords.bitmapList.Count > 0) { UserImage = functionWords.bitmapList[0]; } this.SignInPicture.BackgroundImage = UserImage; if (ifAutoSignIn == "true") //自动登录 { this.AutoSignInCheckBox.Checked = true; //自动登录勾选 this.SignalInButton.PerformClick(); //执行登录操作 } } }
/// <summary> /// 账户输入完毕,请求图像 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AccountTextLine_Leave(object sender, EventArgs e) { //向服务器请求登录图片,并且置换到init.png //此处未写完!!!!!!!!!!!! FunctionWords functionWords = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForImage, this.Account, "", null); if (functionWords != null && functionWords.bitmapList.Count > 0) { UserImage = functionWords.bitmapList[0]; this.SignInPicture.BackgroundImage = UserImage; } else { UserImage = new Bitmap(System.Environment.CurrentDirectory + "\\initImage.png"); this.SignInPicture.BackgroundImage = UserImage; } }
/// <summary> /// 登录事件 /// </summary> /// <param name="o"></param> private void SignInForm_FinishSignIn(object o, string account) { //初始界面为消息界面 this.DownForm.MessageGroupBox.PerformClick(); //初始化主界面 this.UpForm.EmailLabel.Text = account; UserInformation.Account = account; //初始化昵称???云请求 FunctionWords functionWordsForUserName = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForUserName, account, "", null); if (functionWordsForUserName != null && functionWordsForUserName.stringList.Count > 0) { this.UpForm.UserNameLabel.Text = functionWordsForUserName.stringList[0]; UserInformation.UserName = functionWordsForUserName.stringList[0]; } //初始化图像???云请求 FunctionWords functionWordsForImage = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForImage, account, "", null); if (functionWordsForImage != null && functionWordsForImage.bitmapList.Count > 0) { this.UpForm.UserImagePictureBox.BackgroundImage = functionWordsForImage.bitmapList[0]; UserInformation.Image = functionWordsForImage.bitmapList[0]; } //初始化主窗体 init(); CommonEvent.AddNewFriendEvent += CommonEvent_AddNewFriendEvent; CommonEvent.AddNewGroupEvent += CommonEvent_AddNewGroupEvent; CommonEvent.AddNewMessageEvent += CommonEvent_AddNewMessageEvent; CommonEvent.OnlineChangeEvent += CommonEvent_OnlineChangeEvent; CommonEvent.AnswerApplyToAddFriendEvent += CommonEvent_AnswerApplyToAddFriendEvent; CommonEvent.AnswerApplyToAddGroupEvent += CommonEvent_AnswerApplyToAddGroupEvent; //向服务器声明自己 CallServer.AddSendNews(MessageProtocol.GetStartBytes(FunctionKind.DeclareConnect), MessageBirdCommon.UserInformation.Account.ToTxtBytes(), MessageProtocol.GetEndBytes()); //模拟好友/群聊消息接收 RunVirtualMessage(); }
/// <summary> /// 点击显示群成员 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void viewGroupMemberButton_Click(object sender, EventArgs e) { //显示群成员 //????云请求群聊好友 FunctionWords functionWords = this.SendNewsAndReceiveAnswer(FunctionKind.QueryMemberOfGroup, this.AccountOpposite, "", null); if (functionWords != null && functionWords.stringList.Count > 1 && functionWords.bitmapList.Count == functionWords.stringList.Count) { //显示群成员便签 this.viewGroupMemberButton.Visible = false; this.groupShowMemberLabel.Visible = true; for (int i = 0; i < functionWords.stringList.Count; i++) { GroupShowEasyForm.GroupShowEasyForm groupShowEasyForm = new GroupShowEasyForm.GroupShowEasyForm(functionWords.stringList[i], functionWords.bitmapList[i]); groupShowEasyForm.Dock = DockStyle.Top; this.groupMemberPanel.Controls.Add(groupShowEasyForm); } } else { MessageBox.Show("信息存在异常,请重试"); } }
/// <summary> /// 执行功能块语句 /// </summary> /// <param name="functionWords"></param> private static void RunFunctionWords(FunctionWords functionWords) { switch (functionWords.functionKind) { case FunctionKind.AnswerApplyToAddFriendEvent: int circulationTimes1 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 3 == 0) { circulationTimes1 = functionWords.stringList.Count / 3; } else { MessageBox.Show("来自服务器的消息格式错误1"); return; } for (int i = 0; i < circulationTimes1; i++) { string account = functionWords.stringList[3 * i]; string userName = functionWords.stringList[3 * i + 1]; bool yesOrno = false; if (functionWords.stringList[3 * i + 2] == "true") { yesOrno = true; } CommonEvent.EmitAnswerApplyToAddFriendEvent(account, userName, yesOrno); } break; case FunctionKind.AnswerApplyToAddGroupEvent: int circulationTimes2 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 3 == 0) { circulationTimes2 = functionWords.stringList.Count / 3; } else { MessageBox.Show("来自服务器的消息格式错误2"); return; } for (int i = 0; i < circulationTimes2; i++) { string groupAccount = functionWords.stringList[3 * i]; string groupName = functionWords.stringList[3 * i + 1]; bool yesOrno = false; if (functionWords.stringList[3 * i + 2] == "true") { yesOrno = true; } CommonEvent.EmitAnswerApplyToAddGroupEvent(groupAccount, groupName, yesOrno); } break; case FunctionKind.AddFriendMessageListToClient: int circulationTimes3 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 3 == 0) { circulationTimes3 = functionWords.stringList.Count / 3; } else { MessageBox.Show("来自服务器的消息格式错误3"); return; } for (int i = 0; i < circulationTimes3; i++) { string account = functionWords.stringList[3 * i]; string message = functionWords.stringList[3 * i + 1]; string time = functionWords.stringList[3 * i + 2]; CommonEvent.EmitAddNewFriendMessageEvent(account, message, time); } break; case FunctionKind.AddGroupMessageListToClient: int circulationTimes4 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 5 == 0 && ((functionWords.stringList.Count / 5) == functionWords.bitmapList.Count)) { circulationTimes4 = functionWords.bitmapList.Count; } else { MessageBox.Show("来自服务器的消息格式错误4"); return; } for (int i = 0; i < circulationTimes4; i++) { string userName = functionWords.stringList[5 * i]; string userAccount = functionWords.stringList[5 * i + 1]; string message = functionWords.stringList[5 * i + 2]; string time = functionWords.stringList[5 * i + 3]; string groupAccount = functionWords.stringList[5 * i + 4]; CommonEvent.EmitAddNewGroupMessageEvent(userName, userAccount, message, time, functionWords.bitmapList[i], groupAccount); } break; case FunctionKind.AddFriendsListToClient: int circulationTimes5 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 3 == 0 && ((functionWords.stringList.Count / 3) == functionWords.bitmapList.Count)) { circulationTimes5 = functionWords.bitmapList.Count; } else { MessageBox.Show("来自服务器的消息格式错误5"); return; } for (int i = 0; i < circulationTimes5; i++) { string userName = functionWords.stringList[3 * i]; string account = functionWords.stringList[3 * i + 1]; bool isOnline = false; if (functionWords.stringList[3 * i + 2] == "true") { isOnline = true; } CommonEvent.EmitAddNewFriendEvent(userName, account, functionWords.bitmapList[i], isOnline); } ReceivedListCount++; break; case FunctionKind.AddGroupsListToClient: int circulationTimes6 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 2 == 0 && ((functionWords.stringList.Count / 2) == functionWords.bitmapList.Count)) { circulationTimes6 = functionWords.bitmapList.Count; } else { MessageBox.Show("来自服务器的消息格式错误5"); return; } for (int i = 0; i < circulationTimes6; i++) { string groupName = functionWords.stringList[2 * i]; string account = functionWords.stringList[2 * i + 1]; CommonEvent.EmitAddNewGroupEvent(groupName, account, functionWords.bitmapList[i]); } ReceivedListCount++; break; case FunctionKind.SendOnlineStatusToClient: int circulationTimes7 = 0; if (functionWords.stringList.Count > 0 && functionWords.stringList.Count % 2 == 0) { circulationTimes7 = functionWords.stringList.Count % 2; } else { MessageBox.Show("来自服务器的消息格式错误5"); return; } for (int i = 0; i < circulationTimes7; i++) { string account = functionWords.stringList[2 * i]; bool isOnline = false; if (functionWords.stringList[2 * i + 2] == "true") { isOnline = true; } CommonEvent.EmitOnlineChangeEvent(account, isOnline); } break; } }
/// <summary> /// 接收消息、解码并存储在 “待执行的功能块队列” 中 /// 并发执行 /// </summary> private static void ReceiveNewsFormServer() { //开始接收返回值 Byte[] realDataReceived = new Byte[0]; FunctionWords functionWords = new FunctionWords(); try { //开始读取,遇到结束标识跳出 while (true) { //确保头部四字节数据完整 while (realDataReceived.Length < 4) { Byte[] RecvArray = new Byte[bufferSize]; //用于存放接收信息 int dataLengthInFact = clientSocket.Receive(RecvArray); //接收信息 if (dataLengthInFact <= 0) //服务器主动断开连接 { errorTimes++; if (errorTimes > 3) { MessageBox.Show("远程服务器主动退出,立即中断操作。错误:7"); return; } } else { errorTimes = 0; } realDataReceived = MessageProtocol.AddBytes(realDataReceived, RecvArray.Take(dataLengthInFact).ToArray()); } BlockInformation blockInformation = realDataReceived.ReadBlockInformation(0); //确保尾部数据完整 while (realDataReceived.Length < blockInformation.lengthOfAll) { Byte[] RecvArray = new Byte[bufferSize]; //用于存放接收信息 int dataLengthInFact = clientSocket.Receive(RecvArray); //接收信息 realDataReceived = MessageProtocol.AddBytes(realDataReceived, RecvArray.Take(dataLengthInFact).ToArray()); } switch (blockInformation.blockKind) { case BlockKind.Start: functionWords.functionKind = blockInformation.functionKind; break; case BlockKind.Txt: //读取字符串 functionWords.stringList.Add(realDataReceived.ToTxt(4, blockInformation.LengthOfValue)); break; case BlockKind.Image: functionWords.bitmapList.Add(realDataReceived.ToBitmap(4, blockInformation.LengthOfValue)); break; case BlockKind.File: //预留接口 Console.WriteLine("文件传输功能尚未开放,错误:8"); break; case BlockKind.End: //读取到块结尾,说明一个功能语句读取完毕 receivedFunctionWords.Enqueue(functionWords); functionWords = new FunctionWords(); break; } realDataReceived = realDataReceived.Skip(blockInformation.lengthOfAll).ToArray(); //去除字节数组中已经读过的部分 } } catch (Exception ex) { MessageBox.Show("连接错误,异常信息:9" + ex.Message); return; } }
/// <summary> /// 发送请求 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sendbutton_Click(object sender, EventArgs e) { if (this.sendbutton.Text == "发送请求") { switch (currentFunction) { case FunctionChoice.AddFriend: if (!this.accountTextbox.Text.EndsWith(".com") || this.accountTextbox.Text == "") { MessageBox.Show("请输入正确的好友邮箱"); return; } //云验证账户,并请求昵称??? FunctionWords functionWordsForFriendUserName = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForUserName, this.accountTextbox.Text, "", null); if (functionWordsForFriendUserName != null && functionWordsForFriendUserName.stringList.Count > 0) { this.userNameTextBox.Text = functionWordsForFriendUserName.stringList[0]; this.showBusyLabel.Text = "已返回昵称,请确认"; break; } this.showBusyLabel.Text = "账户不存在"; return; case FunctionChoice.AddToGroup: if (this.accountTextbox.Text.EndsWith(".com") || this.accountTextbox.Text == "") { MessageBox.Show("请输入正确的群号"); return; } //云验证账户,并请求昵称??? FunctionWords functionWordsForGroupUserName = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForUserName, this.accountTextbox.Text, "", null); if (functionWordsForGroupUserName != null && functionWordsForGroupUserName.stringList.Count > 0) { this.userNameTextBox.Text = functionWordsForGroupUserName.stringList[0]; this.showBusyLabel.Text = "已返回昵称,请确认"; break; } this.showBusyLabel.Text = "账户不存在"; return; case FunctionChoice.CreateGroup: if (this.userNameTextBox.Text == "") { MessageBox.Show("请输入正确的昵称"); return; } //云请求群号,返回群号??? FunctionWords functionWordsForCreateGroup = this.SendNewsAndReceiveAnswer(FunctionKind.SeekForNewGroupAccount, "", "", null); if (functionWordsForCreateGroup != null && functionWordsForCreateGroup.stringList.Count > 0) { this.accountTextbox.Text = functionWordsForCreateGroup.stringList[0]; this.showBusyLabel.Text = "已返回群号,请确认"; break; } this.showBusyLabel.Text = "服务器异常,请稍后重试"; return; default: return; } this.sendbutton.Text = "确认"; } else if (this.sendbutton.Text == "确认") { if (this.userNameTextBox.Text == "" || this.accountTextbox.Text == "") { MessageBox.Show("错误: 似乎存在信息不完整,请重试"); this.sendbutton.Text = "发送请求"; return; } switch (currentFunction) { case FunctionChoice.AddFriend: //发送请求??? CallServer.AddSendNews(MessageProtocol.GetStartBytes(FunctionKind.ApplyToAddFriend), MessageBirdCommon.UserInformation.Account.ToTxtBytes(), MessageBirdCommon.UserInformation.UserName.ToTxtBytes(), this.accountTextbox.Text.ToTxtBytes(), MessageProtocol.GetEndBytes()); break; case FunctionChoice.AddToGroup: //发送请求??? CallServer.AddSendNews(MessageProtocol.GetStartBytes(FunctionKind.ApplyToAddGroup), MessageBirdCommon.UserInformation.Account.ToTxtBytes(), MessageBirdCommon.UserInformation.UserName.ToTxtBytes(), this.accountTextbox.Text.ToTxtBytes(), MessageProtocol.GetEndBytes()); break; case FunctionChoice.CreateGroup: //发送请求??? MessageBox.Show("请选择小于30KB的png图片"); OpenFileDialog fileDialog = new OpenFileDialog(); DialogResult result = fileDialog.ShowDialog(); if (result == DialogResult.OK) { FileInfo fileInfo = new FileInfo(fileDialog.FileName); double length = Convert.ToDouble(fileInfo.Length); if (!fileInfo.FullName.ToLower().EndsWith(".png") || (fileInfo.Length > 31457280)) { MessageBox.Show("目前仅支持小于30KB的png图像"); return; } Bitmap image = new Bitmap(Image.FromFile(fileDialog.FileName)); CallServer.AddSendNews(MessageProtocol.GetStartBytes(FunctionKind.RequestToRegeditGroup), MessageBirdCommon.UserInformation.Account.ToTxtBytes(), MessageBirdCommon.UserInformation.UserName.ToTxtBytes(), this.accountTextbox.Text.ToTxtBytes(), image.ToImageBytes(), MessageProtocol.GetEndBytes()); } else { return; } break; default: return; } MessageBox.Show("申请信息已发出"); this.sendbutton.Text = "发送请求"; } }