static private void waitUploading(Dictionary <string, object> callerInfo) { ClientMainWindow caller = (ClientMainWindow)callerInfo["caller"]; string path = (string)callerInfo["fileName"]; FileInfo info = new FileInfo(path); string filename = info.Name; //wait for confirmation of upload. Byte[] buffer = new byte[64]; int rec; try { rec = client.Receive(buffer); //thread waits here! } catch { caller.disconnected(); return; } if (rec <= 0) { caller.disconnected(); return; } string serverResponse = Encoding.Default.GetString(buffer); Dictionary <string, string> responseStatus = JsonConvert.DeserializeObject <Dictionary <string, string> >(serverResponse); if (responseStatus["UploadStatus"] == "OK") { caller.Invoke((MethodInvoker)(() => caller.writeLogEvent(filename + " is uploaded!"))); MessageBox.Show("Upload completed!", "Upload Progress", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
static private void sendFile(object info) { Dictionary <string, object> callerInfo = (Dictionary <string, object>)info; //wait for server to acknowledge file with its properties ClientMainWindow caller = (ClientMainWindow)callerInfo["caller"]; Byte[] buffer = new byte[64]; int rec; try { rec = client.Receive(buffer); //thread waits here! } catch { caller.disconnected(); return; } if (rec <= 0) { caller.disconnected(); return; } string serverResponse = Encoding.Default.GetString(buffer); if (serverResponse.Contains("$SENDFILE#")) { string fileName = (string)callerInfo["fileName"]; //start to send the actual file client.SendFile(fileName); waitUploading(callerInfo); } }
public RenameWindow(Socket connectedSocket, string fileName, ClientMainWindow main) { InitializeComponent(); client = connectedSocket; oldFileName = fileName; newNameTextField.Text = fileName; caller = main; }
public void OnStartUp(object sender, StartupEventArgs e) { ClientMainWindow view = new ClientMainWindow(); Models.ClientModel clientModel = new Models.ClientModel(); view.DataContext = new ClientViewModel(clientModel); view.Show(); }
public static void downloadTheFile(Stream toWrite, File fileInfo, Socket n, ClientMainWindow caller) { //same approach with uploading a file but now the client is the recipient. Dictionary <string, string> request = new Dictionary <string, string>(); request.Add("fileProcess", "DOWNLOAD"); request.Add("fileName", fileInfo.fileName); SendMessage(n, JsonConvert.SerializeObject(request)); Byte[] buffer = new byte[32768]; int rec = n.Receive(buffer); //thread waits here! if (rec <= 0) { caller.disconnected(); return; } string newmessage = Encoding.Default.GetString(buffer); newmessage = newmessage.Substring(0, newmessage.IndexOf("\0")); File comingFile; if (newmessage.Contains("\"fileSize\":")) { comingFile = JsonConvert.DeserializeObject <File>(newmessage); Console.Write("started to get file:\r\n"); Byte[] fileBuffer = new byte[comingFile.fileSize]; SendMessage(n, "$SENDFILE#"); int readBytes = 0; int minBlockSize = fileBlockSize < comingFile.fileSize ? fileBlockSize : (int)comingFile.fileSize; readBytes += n.Receive(fileBuffer, 0, minBlockSize, SocketFlags.None); while (readBytes != comingFile.fileSize) { if ((comingFile.fileSize - readBytes) < minBlockSize) { int diff = (int)comingFile.fileSize - readBytes; readBytes += n.Receive(fileBuffer, readBytes, diff, SocketFlags.None); } else { readBytes += n.Receive(fileBuffer, readBytes, minBlockSize, SocketFlags.None); } } toWrite.Write(fileBuffer, 0, (int)comingFile.fileSize); toWrite.Close(); caller.Invoke((MethodInvoker)(() => caller.writeLogEvent(comingFile.fileName + " is downloaded!"))); MessageBox.Show("Download is completed!", "Download Progress", MessageBoxButtons.OK, MessageBoxIcon.Information); } }