private void ClientForm_FormClosing(object sender, FormClosingEventArgs e) { client?.SendMessage(new LANMessage(MessageType.ClientExit, client.ClientId, tbName.Text)); client?.Disconnect(); client = null; communityData = null; }
private void btDisconnect_Click(object sender, EventArgs e) { client?.SendMessage(new LANMessage(MessageType.ClientExit, client.ClientId, tbName.Text)); client?.Disconnect(); client = null; btDisconnect.Enabled = false; btSendMessage.Enabled = false; foreach (DialogInfo dialog in communityData.Dialogs.Values) { dialog.IsActive = false; } UpdateParticipants(); communityData = null; btConnect.Enabled = true; cbIsConnected.Checked = false; tbName.ReadOnly = false; tbPassword.ReadOnly = false; }
public void HandleMess(LANMessage message) { switch (message.messageType) { case MessageType.ClientHistory: { Action action = delegate { communityData = new CommunityData(DialogInfoMethods.ListIntoDictionary(message.Dialogs)); CurrentDialog = CommonDialogID; MatchingDialogs = new Dictionary <int, ClientDialog>(); UpdateParticipants(); if (CommonDialogID == CurrentDialog) { UpdateContent(); } lbCurrentDialog.Text = communityData.Dialogs[CommonDialogID].Name; }; if (InvokeRequired) { Invoke(action); } else { action(); } } break; case MessageType.CommonMess: if (message.AttachedFiles != null) { communityData.Dialogs[CommonDialogID].LoadedFiles.AddRange(message.AttachedFiles); } DialogInfoMethods.AddMessage(communityData.Dialogs[CommonDialogID].MessagesHistory, ref communityData.Dialogs[CommonDialogID].UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, message.IP + " " + message.content, DateTime.Now)); if (CommonDialogID == CurrentDialog) { UpdateContent(); communityData.Dialogs[CommonDialogID].UnreadMessCount--; } else { UpdateParticipants(); } break; case MessageType.ClientJoin: { Action action = delegate { DialogInfoMethods.AddMessage(communityData.Dialogs[CommonDialogID].MessagesHistory, ref communityData.Dialogs[CommonDialogID].UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, " joined", DateTime.Now)); if (DialogExists(message.SenderID)) { communityData.Dialogs[message.SenderID].IsActive = true; } else { communityData.Dialogs[message.SenderID] = new DialogInfo(message.SenderName, message.SenderID); } if (CommonDialogID == CurrentDialog) { UpdateContent(); communityData.Dialogs[CommonDialogID].UnreadMessCount--; } UpdateParticipants(); }; if (InvokeRequired) { Invoke(action); } else { action(); } } break; case MessageType.ClientExit: communityData.Dialogs[message.SenderID].IsActive = false; DialogInfoMethods.AddMessage(communityData.Dialogs[CommonDialogID].MessagesHistory, ref communityData.Dialogs[CommonDialogID].UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, " exit", DateTime.Now)); if (CommonDialogID == CurrentDialog) { UpdateContent(); communityData.Dialogs[CommonDialogID].UnreadMessCount--; } UpdateParticipants(); break; case MessageType.PrivateMess: if (message.AttachedFiles != null) { communityData.Dialogs[message.SenderID].LoadedFiles.AddRange(message.AttachedFiles); } DialogInfoMethods.AddMessage(communityData.Dialogs[message.SenderID].MessagesHistory, ref communityData.Dialogs[message.SenderID].UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, message.IP + " " + message.content, DateTime.Now)); if (message.SenderID == CurrentDialog) { UpdateContent(); communityData.Dialogs[message.SenderID].UnreadMessCount--; } else { UpdateParticipants(); } break; case MessageType.DialogData: DialogInfo receivedDialog = message.Dialogs.Last(); communityData.Dialogs[receivedDialog.Id] = receivedDialog; UpdateSelectedDialog(); break; } }