private void btSendMessage_Click(object sender, EventArgs e) { if ((tbMessageContent.Text.Length > 0)) { string content = tbMessageContent.Text; if (fileSharingClient.filesToSend.Count > 0) { content += " [Вложения]"; } if (CurrentDialog == CommonDialogID) { client.SendMessage(new LANMessage(MessageType.CommonMess, content, client.ClientId, ((IPEndPoint)client.TCPsocket.LocalEndPoint).Address.ToString(), GetAttachedFilesId(fileSharingClient.filesToSend))); } else { client.SendMessage(new LANMessage(MessageType.PrivateMess, client.ClientId, MatchingDialogs[CurrentDialog], ((IPEndPoint)client.TCPsocket.LocalEndPoint).Address.ToString(), content, GetAttachedFilesId(fileSharingClient.filesToSend))); } DialogInfoMethods.AddMessage(communityData.Dialogs[MatchingDialogs[CurrentDialog]].MessagesHistory, ref communityData.Dialogs[MatchingDialogs[CurrentDialog]].UnreadMessCount, new ChatMessage(client.ClientId, "me", content, GetAttachedFilesId(fileSharingClient.filesToSend), DateTime.Now)); UpdateContent(); communityData.Dialogs[MatchingDialogs[CurrentDialog]].UnreadMessCount--; tbMessageContent.Clear(); lbMessageFiles.Items.Clear(); lbAttachedFiles.Items.Clear(); } }
private void btSendMessage_Click(object sender, EventArgs e) { if ((tbMessageContent.Text.Length > 0) || (AttachedFiles.Count > 0)) { LANMessage message = null; if (CurrentDialog == CommonDialogID) { message = new LANMessage(MessageType.CommonMess, tbMessageContent.Text, client.ClientId, ((IPEndPoint)client.TCPsocket.LocalEndPoint).Address.ToString()); } else { message = new LANMessage(MessageType.PrivateMess, client.ClientId, MatchingDialogs[CurrentDialog].Id, ((IPEndPoint)client.TCPsocket.LocalEndPoint).Address.ToString(), tbMessageContent.Text); } if (AttachedFiles.Count > 0) { communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].LoadedFiles.AddRange(AttachedFiles); message.AttachedFiles = new List <int>(); message.AttachedFiles.AddRange(AttachedFiles); message.content += "\n attached files:\n"; foreach (int fileID in message.AttachedFiles) { message.content += " " + fileID.ToString() + "\n"; } } client.SendMessage(message); var newMessage = new ChatMessage(client.ClientId, "me", message.content, DateTime.Now); DialogInfoMethods.AddMessage(communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].MessagesHistory, ref communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].UnreadMessCount, newMessage); UpdateContent(); communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].UnreadMessCount--; tbMessageContent.Clear(); } }
private void btSendMessage_Click(object sender, EventArgs e) { if ((tbMessageContent.Text.Length > 0)) { if (CurrentDialog == CommonDialogID) { client.SendMessage(new LANMessage(MessageType.CommonMess, tbMessageContent.Text, client.ClientId, ((IPEndPoint)client.TCPsocket.LocalEndPoint).Address.ToString())); } else { client.SendMessage(new LANMessage(MessageType.PrivateMess, client.ClientId, MatchingDialogs[CurrentDialog].Id, ((IPEndPoint)client.TCPsocket.LocalEndPoint).Address.ToString(), tbMessageContent.Text)); } DialogInfoMethods.AddMessage(communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].MessagesHistory, ref communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].UnreadMessCount, new ChatMessage(client.ClientId, "me", tbMessageContent.Text, DateTime.Now)); UpdateContent(); communityData.Dialogs[MatchingDialogs[CurrentDialog].Id].UnreadMessCount--; tbMessageContent.Clear(); } }
public void HandleMessage(LANMessage message, int connectionID) { switch (message.messageType) { case MessageType.Identification: if (IsNewClient(message.SenderID)) { Clients[message.SenderID] = new Client(message.SenderName, connectionID); //sqliteManager.AddClient(message.SenderID, message.SenderName); } else { Clients[message.SenderID].IsConnected = true; Clients[message.SenderID].ConnectionID = connectionID; Clients[message.SenderID].UpdateDialogsState(); } Connections[connectionID].Id = message.SenderID; Console.WriteLine(message.SenderName + " joined"); DialogInfoMethods.AddMessage(CommonDialog.MessagesHistory, ref CommonDialog.UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, " joined", DateTime.Now)); SendMessage(new LANMessage(MessageType.ClientHistory, DialogInfoMethods.DictionaryIntoList(Clients[message.SenderID].Dialogs)), Connections[connectionID].socket); NotifyClients(message.SenderID); break; case MessageType.CommonMess: message.SenderName = Clients[message.SenderID].Name; SendToAll(message); //sqliteManager.AddMessage("common", message.SenderID, 0, DateTime.Now, message.content); DialogInfoMethods.AddMessage(CommonDialog.MessagesHistory, ref CommonDialog.UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, /*message.IP + " " +*/ message.content, message.AttachedFiles, DateTime.Now)); break; case MessageType.UDPRequest: Console.WriteLine("request"); UDPhandler.SendResponse(IPAddress.Parse(message.IP), message.Port, serializer.Serialize(new LANMessage(MessageType.UDPResponse, nodeInformation.NodeIP.ToString(), PortNumber))); break; case MessageType.ClientExit: Console.WriteLine(Clients[message.SenderID].Name + " exit"); Connections.Remove(connectionID); Clients[message.SenderID].IsConnected = false; DialogInfoMethods.AddMessage(CommonDialog.MessagesHistory, ref CommonDialog.UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, "exit", DateTime.Now)); UpdateDialogsState(); SendToAll(message); break; case MessageType.PrivateMess: if (Connections.ContainsKey(Clients[message.Port].ConnectionID)) { message.SenderName = Clients[message.SenderID].Name; int receiverId = message.Port; message.SenderName = Clients[message.SenderID].Name; SendMessage(message, Connections[Clients[receiverId].ConnectionID].socket); //sqliteManager.AddMessage("private", message.SenderID, receiverId, DateTime.Now, message.content); DialogInfoMethods.AddMessage(Clients[message.SenderID].Dialogs[receiverId].MessagesHistory, ref Clients[message.SenderID].Dialogs[receiverId].UnreadMessCount, new ChatMessage(message.SenderID, "me", message.content, message.AttachedFiles, DateTime.Now)); DialogInfoMethods.AddMessage(Clients[receiverId].Dialogs[message.SenderID].MessagesHistory, ref Clients[receiverId].Dialogs[message.SenderID].UnreadMessCount, new ChatMessage(message.SenderID, message.SenderName, message.content, message.AttachedFiles, DateTime.Now)); } break; } }
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; } }