コード例 #1
0
        private void ReceiveMessages()
        {
            online = true;
            try
            {
                while (online)
                {
                    IPEndPoint remoteIp = null;
                    byte[]     data     = udpClient.Receive(ref remoteIp);
                    Message    message  = (Message)ByteParser.ByteArrayToObj(data);
                    switch (message.Type)
                    {
                    case messageType.Confirm:
                        Dispatcher.Invoke(new Action(delegate
                        {
                            chatTextBlock.Inlines.Add($"\n*message #{message.Id} delivered*");
                        }));
                        break;

                    case messageType.Message:
                        Dispatcher.Invoke(new Action(delegate
                        {
                            if (message.SenderPort == localPort)
                            {
                                chatTextBlock.Inlines.Add($"\n[#{message.Id}]({DateTime.Now.ToShortTimeString()}) {message.Content}");
                            }
                            else
                            {
                                chatTextBlock.Inlines.Add($"\n({DateTime.Now.ToShortTimeString()}) {message.Content}");
                            }
                        }));
                        Message responseMessage = new Message()
                        {
                            Type = messageType.Confirm, Id = message.Id, SenderPort = localPort
                        };
                        byte[] newData = ByteParser.ObjToByteArray(responseMessage);
                        udpClient.Send(newData, newData.Length, host, remotePort);
                        break;

                    case messageType.Join:
                        Dispatcher.Invoke(new Action(delegate
                        {
                            if (chatTextBlock.Text.Length > 0)
                            {
                                chatTextBlock.Inlines.Add("\n");
                            }
                            chatTextBlock.Inlines.Add($"({DateTime.Now.ToShortTimeString()}) {message.Content}");
                        }));
                        break;

                    case messageType.Leave:
                        Dispatcher.Invoke(new Action(delegate
                        {
                            chatTextBlock.Inlines.Add($"\n({DateTime.Now.ToShortTimeString()}) {message.Content}");
                        }));
                        break;

                    case messageType.ServerShutDown:
                        Dispatcher.Invoke(new Action(delegate
                        {
                            ExitChat();
                            MessageBox.Show("Server shut down");
                            chatTextBlock.Inlines.Clear();
                        }));
                        break;
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                if (!online)
                {
                    return;
                }
                throw;
            }
            catch (SocketException ex) when(ex.ErrorCode == 10004)
            {
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }