private void SaveFile(Entities.FileMessage msg) { SaveFileDialog dlg = new SaveFileDialog(); if (msg.FileMessageType == FileMessageType.Responese) { if (msg.FileStatus == FileStatus.NotStarted) { if (dlg.ShowDialog() == DialogResult.OK) { FileNameToSave = dlg.FileName + ".pdf"; FileManager mgr = new FileManager(FileNameToSave, msg); mgr.Save(); //msg.Index++; msg.IpAddress = com.GetMyIPAddress(); msg.FileStatus = FileStatus.Started; msg.FileMessageType = FileMessageType.Request; msg.IPLength = msg.IpAddress.Length; com.Send(msg, new System.Net.IPEndPoint(IPAddress.Parse(msg.IpAddress), 8000)); } } else { FileManager mgr = new FileManager(FileNameToSave, msg); mgr.Save(); msg.Index++; msg.IpAddress = com.GetMyIPAddress(); msg.FileMessageType = FileMessageType.Request; msg.IPLength = msg.IpAddress.Length; if (msg.FileStatus == FileStatus.Started) { com.Send(msg, new System.Net.IPEndPoint(IPAddress.Parse(msg.IpAddress), 8000)); } } } else if (msg.FileMessageType == FileMessageType.Request) { FileManager mgr = new FileManager(msg); //byte[] data = mgr.GetBytes(msg.Index); //msg.Data = new byte[data.Length]; //Array.Copy(data, 0, msg.Data, 0, data.Length); //msg.IpAddress = com.GetMyIPAddress(); //msg.IPLength = msg.IpAddress.Length; FileMessage fileMessage = mgr.GetMessage(); fileMessage.FileMessageType = FileMessageType.Responese; fileMessage.IpAddress = com.GetMyIPAddress(); fileMessage.DataLength = fileMessage.Data.Length; fileMessage.IPLength = fileMessage.IpAddress.Length; fileMessage.FileName = msg.FileName; fileMessage.FileNameLength = msg.FileNameLength; fileMessage.FileStatus = msg.FileStatus; fileMessage.Index = msg.Index; fileMessage.Preamble = msg.Preamble; com.Send(fileMessage, new System.Net.IPEndPoint(IPAddress.Parse(msg.IpAddress), 8000)); } }
public static byte[] EncodeMessage(FileMessage msg) { byte[] preamble = Encoding.Default.GetBytes("BB"); byte[] index = BitConverter.GetBytes(msg.Index); byte[] ipLength = BitConverter.GetBytes(msg.IPLength); byte[] fileNameLength = BitConverter.GetBytes(msg.FileNameLength); byte[] ipAddress = Encoding.Default.GetBytes(msg.IpAddress); byte[] fileName = Encoding.Default.GetBytes(msg.FileName); List <byte> frame = new List <byte>(); foreach (byte b in preamble) { frame.Add(b); } foreach (byte b in index) { frame.Add(b); } if (msg.FileMessageType == FileMessageType.Request) { frame.Add(0); } else { frame.Add(1); } frame.Add((byte)msg.FileStatus); foreach (byte b in ipLength) { frame.Add(b); } foreach (byte b in fileNameLength) { frame.Add(b); } foreach (byte b in ipAddress) { frame.Add(b); } foreach (byte b in fileName) { frame.Add(b); } foreach (byte b in msg.Data) { frame.Add(b); } int length = msg.Data.Length; byte[] frameLength = BitConverter.GetBytes(length); for (int i = 0; i < frameLength.Length; i++) { frame.Insert(i + 2, frameLength[i]); } return(frame.ToArray()); }
public FileManager(string _fileName, FileMessage _msg) { msg = _msg; fileName = _fileName; }
public FileManager(FileMessage _msg) { msg = _msg; fileName = _msg.FileName; }