Пример #1
0
        public void ListenTcp()
        {
            byte[] data = new byte[1024];
            int    bytesCounts;

            while (true)
            {
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        do
                        {
                            bytesCounts = clientTcpSocket.Receive(data);
                            memoryStream.Write(data, 0, bytesCounts);
                        } while (clientTcpSocket.Available > 0);

                        ChatHelpingLibrary.Message message = serializer.MakeDeserialize(memoryStream.ToArray());
                        ReceiveMessageHandler(message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "74");
                    Disconnect();
                    return;
                }
            }
        }
Пример #2
0
        public void ListenTcp()
        {
            byte[] data = new byte[1024];
            int    bytesCounts;

            do
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    try
                    {
                        do
                        {
                            bytesCounts = client.Receive(data);
                            memoryStream.Write(data, 0, bytesCounts);
                        } while (client.Available > 0);
                        ChatHelpingLibrary.Message TcpMessage = serializer.MakeDeserialize(memoryStream.ToArray());
                        ReceiveMessageHandler(TcpMessage);
                    }
                    catch
                    {
                        Close();
                        break;
                    }
                }
            } while (true);
        }
Пример #3
0
 public bool CheckUdpMessage(ChatHelpingLibrary.Message message)
 {
     if (message is FindServerRecieveMessage)
     {
         FindServerRecieveMessage ServerInfoMessage = (FindServerRecieveMessage)message;
         ServerPort = ServerInfoMessage.SenderPort;
         ServerIp   = ServerInfoMessage.SenderIp;
         return(true);
     }
     return(false);
 }
Пример #4
0
 public void HandleReceivedMessages(ChatHelpingLibrary.Message message)
 {
     if (message is ChatMessage)
     {
         ChatMessage chatMessage = (ChatMessage)message;
         if (chatMessage.ReceiverId == 0)
         {
             if (selectedDialogId != 0)
             {
                 Action action = delegate
                 {
                     NewMessagelabel.Text = "Вам пришло новое сообщение в Общий чат";
                     NewMessageChatId     = 0;
                 };
                 if (InvokeRequired)
                 {
                     Invoke(action);
                 }
                 else
                 {
                     action();
                 }
             }
             ClientInfo findClientInfoList = clientsInfoList.Find(f => f.Id == chatMessage.ReceiverId);
             int        index = clientsInfoList.IndexOf(findClientInfoList);
             clientsInfoList[index].MessageHistory.Add(chatMessage);
             if (chatMessage.ReceiverId == selectedDialogId)
             {
                 RedrawOther(chatMessage.SenderName, chatMessage.Content, chatMessage.DateTime);
                 if (chatMessage.IsAnyFiles)
                 {
                     foreach (FileInMessage fileInMessage in chatMessage.FilesInMessageList)
                     {
                         RedrawFileInDialogPanel(chatMessage.SenderName + "отправил файл", fileInMessage.fileName, chatMessage.DateTime, fileInMessage.fileID);
                     }
                 }
             }
         }
         else
         {
             if (chatMessage.SenderId != selectedDialogId)
             {
                 Action action = delegate
                 {
                     NewMessagelabel.Text = "Вам пришло новое сообщение от " + chatMessage.SenderName;
                     NewMessageChatId     = chatMessage.SenderId;
                 };
                 if (InvokeRequired)
                 {
                     Invoke(action);
                 }
                 else
                 {
                     action();
                 }
             }
             ClientInfo findClientInfoList = clientsInfoList.Find(f => f.Id == chatMessage.SenderId);
             int        index = clientsInfoList.IndexOf(findClientInfoList);
             clientsInfoList[index].MessageHistory.Add(chatMessage);
             if (chatMessage.SenderId == selectedDialogId)
             {
                 RedrawOther(chatMessage.SenderName, chatMessage.Content, chatMessage.DateTime);
                 if (chatMessage.IsAnyFiles)
                 {
                     foreach (FileInMessage fileInMessage in chatMessage.FilesInMessageList)
                     {
                         RedrawFileInDialogPanel(chatMessage.SenderName + "отправил файл", fileInMessage.fileName, chatMessage.DateTime, fileInMessage.fileID);
                     }
                 }
             }
         }
     }
     if (message is ClientsListMessage)
     {
         ClientsListMessage clientsListMessage = (ClientsListMessage)message;
         clientsInfoList    = clientsListMessage.clientInfoList;
         currentClient.MyId = clientsListMessage.YourId;
         RefreshOnlineListBox();
     }
     if (message is ActionWithClientMessage)
     {
         ActionWithClientMessage actionWithClientMessage = (ActionWithClientMessage)message;
         if (actionWithClientMessage.ActionType == 1)
         {
             ClientInfo clientInfo = new ClientInfo();
             clientInfo.Id   = actionWithClientMessage.ClientId;
             clientInfo.Name = actionWithClientMessage.ClientName;
             clientsInfoList.Add(clientInfo);
         }
         else if (actionWithClientMessage.ActionType == 0)
         {
             var removeValue = clientsInfoList.First(x => x.Id == actionWithClientMessage.ClientId);
             clientsInfoList.Remove(removeValue);
         }
         RefreshOnlineListBox();
     }
 }
Пример #5
0
 public void HandleReceivedMessages(ChatHelpingLibrary.Message message)
 {
     if (message is ChatMessage)
     {
         ChatMessage chatMessage = (ChatMessage)message;
         if (chatMessage.ReceiverId == 0)
         {
             if (selectedDialogId != 0)
             {
                 NewMessagelabel.Text = "Вам пришло новое сообщение в Общий чат";
                 NewMessageChatId     = 0;
             }
             ClientInfo findClientInfoList = clientsInfoList.Find(f => f.Id == chatMessage.ReceiverId);
             int        index = clientsInfoList.IndexOf(findClientInfoList);
             clientsInfoList[index].MessageHistory.Add(chatMessage);
             if (chatMessage.ReceiverId == selectedDialogId)
             {
                 RedrawOther(chatMessage.senderName, chatMessage.Content, chatMessage.dateTime);
             }
         }
         else
         {
             if (chatMessage.SenderId != selectedDialogId)
             {
                 NewMessagelabel.Text = "Вам пришло новое сообщение от " + chatMessage.senderName;
                 NewMessageChatId     = chatMessage.SenderId;
             }
             ClientInfo findClientInfoList = clientsInfoList.Find(f => f.Id == chatMessage.SenderId);
             int        index = clientsInfoList.IndexOf(findClientInfoList);
             clientsInfoList[index].MessageHistory.Add(chatMessage);
             if (chatMessage.SenderId == selectedDialogId)
             {
                 RedrawOther(chatMessage.senderName, chatMessage.Content, chatMessage.dateTime);
             }
         }
     }
     if (message is ClientsListMessage)
     {
         ClientsListMessage clientsListMessage = (ClientsListMessage)message;
         clientsInfoList    = clientsListMessage.clientInfoList;
         currentClient.MyId = clientsListMessage.YourId;
         RefreshOnlineListBox();
     }
     if (message is ActionWithClientMessage)
     {
         ActionWithClientMessage actionWithClientMessage = (ActionWithClientMessage)message;
         if (actionWithClientMessage.ActionType == 1)
         {
             ClientInfo clientInfo = new ClientInfo();
             clientInfo.Id   = actionWithClientMessage.ClientId;
             clientInfo.Name = actionWithClientMessage.ClientName;
             clientsInfoList.Add(clientInfo);
         }
         else if (actionWithClientMessage.ActionType == 0)
         {
             var removeValue = clientsInfoList.First(x => x.Id == actionWithClientMessage.ClientId);
             clientsInfoList.Remove(removeValue);
         }
         RefreshOnlineListBox();
     }
 }