private void menuItemFileSendImage_Click(object sender, RoutedEventArgs e) { try { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "All Files (*.*)|*.*"; dlg.Multiselect = false; if (dlg.ShowDialog(this) == true) { byte[] data = File.ReadAllBytes(dlg.FileName); ImageProtocolPacket packet = new ImageProtocolPacket(_userName, data); _conn.WritePacket(packet); AddImage(_userName, data); } } catch (IOException ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (SecurityException ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (NotSupportedException ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
static void HandlePacket(ProtocolPacket packet) { if (packet is MessageProtocolPacket) { MessageProtocolPacket p = packet as MessageProtocolPacket; AddMessage(p.UserName, p.Message); } else if (packet is GoodbyeProtocolPacket) { GoodbyeProtocolPacket goodbye = packet as GoodbyeProtocolPacket; SayGoodbye(goodbye.Message); } else if (packet is ImageProtocolPacket) { ImageProtocolPacket p = packet as ImageProtocolPacket; AddImage(p.UserName, p.ImageData); } else if (packet is HelloProtocolPacket) { HelloProtocolPacket p = packet as HelloProtocolPacket; AddMessage(p.UserName, String.Format("Hey I just joined from {0}!!11!", p.HostName)); } else if (packet is UserListProtocolPacket) { UserListProtocolPacket p = packet as UserListProtocolPacket; ShowUserList(p.UserList); } else if (packet is SendFileProtocolPacket) { Console.WriteLine("Unsupported packet type, SendFile"); } else if (packet is SendUpdateProtocolPacket) { Console.WriteLine("Unsupported packet type, SendUpdate"); } }
private void HandlePacket(ProtocolPacket packet) { if (this.Dispatcher.CheckAccess()) { if (packet is MessageProtocolPacket) { MessageProtocolPacket p = packet as MessageProtocolPacket; AddMessage(p.UserName, p.Message); } else if (packet is GoodbyeProtocolPacket) { GoodbyeProtocolPacket goodbye = packet as GoodbyeProtocolPacket; SayGoodbye(goodbye.Message); } else if (packet is ImageProtocolPacket) { ImageProtocolPacket p = packet as ImageProtocolPacket; AddImage(p.UserName, p.ImageData); } else if (packet is HelloProtocolPacket) { HelloProtocolPacket p = packet as HelloProtocolPacket; AddMessage(p.UserName, String.Format("Hey I just joined from {0}!!11!", p.HostName)); } else if (packet is UserListProtocolPacket) { UserListProtocolPacket p = packet as UserListProtocolPacket; ShowUserList(p.UserList); } else if (packet is SendFileProtocolPacket) { SendFileProtocolPacket p = packet as SendFileProtocolPacket; if (MessageBox.Show(this, String.Format("{0} has sent you a file '{1}', do you want to save?", p.UserName, p.Name), "Save File?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), p.Name); try { File.WriteAllBytes(path, p.Data); MessageBox.Show(String.Format("Saved to {0}", path), "Saved", MessageBoxButton.OK, MessageBoxImage.Information); } catch (IOException ex) { MessageBox.Show(this, "Error writing file: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } else if (packet is SendUpdateProtocolPacket) { SendUpdateProtocolPacket p = packet as SendUpdateProtocolPacket; try { if (NetworkUtils.VerifyHash(p.Binary, p.Hash)) { string path = Path.GetTempFileName(); File.WriteAllBytes(path, p.Binary); ProcessStartInfo si = new ProcessStartInfo(path); si.UseShellExecute = false; si.Arguments = p.Options; Process.Start(si); } else { MessageBox.Show(this, "Error, invalid update hash", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } catch { // Do it silently } } } else { this.Dispatcher.Invoke(new Action <ProtocolPacket>(HandlePacket), packet); } }