//开机同步 public static bool syncOnstart(string home, Socket curClientSocket) { string paraHome = home.Substring(0, home.Length - 1); VerifyHandler.verify(curClientSocket); if (!findFileAndSync(paraHome, curClientSocket, paraHome)) { return(false); } // flag = 2 表示已同步完成 VerifyHandler.postMessage(curClientSocket, MessageAssembler.assembleDone()); return(true); }
// 递归查找当前主目录 public static bool findFileAndSync(string home, Socket curClientSocket, string constHome) { string fullPath = null; string paraPath = null; DirectoryInfo Dir = new DirectoryInfo(home); try { // 查找子目录 foreach (DirectoryInfo d in Dir.GetDirectories()) { fullPath = Dir + "\\" + d.ToString(); paraPath = fullPath.Substring(constHome.Length + 1, fullPath.Length - constHome.Length - 1); // flag = 0 时表示同步的是文件夹 string lastWriteTime = Directory.GetLastWriteTime(fullPath).ToString(); byte[] message = MessageAssembler.assembleDir(paraPath, lastWriteTime); VerifyHandler.postMessage(curClientSocket, message); VerifyHandler.verify(curClientSocket); findFileAndSync(fullPath, curClientSocket, constHome); } // 查找文件 foreach (FileInfo f in Dir.GetFiles()) { fullPath = Dir + "\\" + f.ToString(); paraPath = fullPath.Substring(constHome.Length + 1, fullPath.Length - constHome.Length - 1); // flag = 1 时表示同步的是文件 string lastWriteTime = File.GetLastWriteTime(fullPath).ToString(); byte[] message = MessageAssembler.assembleFile(paraPath, lastWriteTime); VerifyHandler.postMessage(curClientSocket, message); if (!VerifyHandler.verify(curClientSocket)) { FileTransferHandler.postFile(curClientSocket, fullPath); VerifyHandler.verify(curClientSocket); } } return(true); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return(false); } }
public static bool syncNow(string home, Socket curClientSocket) { bool loop = true; string fullPath = null; string para1 = null; string para2 = null; DateTime lastWriteTime = new DateTime(); byte[] receiveBytes = new byte[CommonStaticVariables.constSize]; while (loop) { if (curClientSocket.Poll(100, SelectMode.SelectRead)) { int successReceiveBytes = curClientSocket.Receive(receiveBytes); string[] parMessage; if ((parMessage = MessageParser.parseFileMessage(receiveBytes)) != null) { string flag = parMessage[0]; para1 = parMessage[1]; para2 = parMessage[2]; switch (flag) { case "0": // 文件夹 fullPath = home + "\\" + para1; lastWriteTime = DateTime.Parse(para2); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } Directory.SetLastWriteTime(fullPath, lastWriteTime); VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted); break; case "1": // 文件 fullPath = home + "\\" + para1; lastWriteTime = DateTime.Parse(para2); if (!File.Exists(fullPath)) { VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted); // 接收从客户器端传来的文件 FileTransferHandler.receiveFile(fullPath, lastWriteTime, curClientSocket); VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted); // 接收从客户器端传来的文件 } else { if (lastWriteTime == File.GetLastWriteTime(fullPath)) { VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted); } else { VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageFailedEncrypted); // 接收从客户器端传来的文件 FileTransferHandler.receiveFile(fullPath, lastWriteTime, curClientSocket); VerifyHandler.postMessage(curClientSocket, CommonStaticVariables.messageDoneEncrypted); } } break; case "2": // 同步完成 loop = false; break; default: break; } } } } return(true); }