Ejemplo n.º 1
0
        public static void postFile(Socket clientSocket, string sourceFilePath)
        {
            try
            {
                VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageBeginEncrypted);
                VerifyHandler.verify(clientSocket);

                FileStream fileToSend = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);  //注意与receive的filestream的区别

                BinaryReader binaryreader = new BinaryReader(fileToSend);
                byte[]       fileBytes    = new byte[CommonStaticVariables.constSize];
                int          count;
                while ((count = binaryreader.Read(fileBytes, 0, CommonStaticVariables.constSize)) != 0)                 //这个注意是将文件写成流的形式
                {
                    byte[] fileFragmentToSend = EncryptionDecryptionHandler.messageEncrypt(fileBytes);
                    clientSocket.Send(fileFragmentToSend, count, SocketFlags.None);           //发送文件流到目标机器
                }

                binaryreader.Close();
                fileToSend.Close();
                System.Threading.Thread.Sleep(200);
                VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageOverEncrypted);
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 2
0
        public static void receiveFile(string fullPath, DateTime lastWriteTime, Socket clientSocket)
        {
            try
            {
                byte[] receiveBytes = new byte[CommonStaticVariables.constSize];
                // 接收从服务器端传来的文件
                if (File.Exists(fullPath))
                {
                    File.Delete(fullPath);
                }
                FileStream   fileToReceive = new FileStream(fullPath, FileMode.Append, FileAccess.Write);
                BinaryWriter binarywrite   = new BinaryWriter(fileToReceive);

                while (true)
                {
                    if (clientSocket.Poll(100, SelectMode.SelectRead))
                    {
                        Array.Clear(receiveBytes, 0, CommonStaticVariables.constSize);
                        int count = clientSocket.Receive(receiveBytes);

                        if (VerifyHandler.verify(receiveBytes, CommonStaticVariables.messageBeginEncrypted, count))
                        {
                            VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);
                            continue;
                        }
                        else if (VerifyHandler.verify(receiveBytes, CommonStaticVariables.messageOverEncrypted, count))
                        {
                            binarywrite.Flush();
                            binarywrite.Close();
                            fileToReceive.Close();
                            break;
                        }
                        else
                        {
                            //将接收的流用写成文件
                            byte[] fileFragmentToWrite = EncryptionDecryptionHandler.messageDecrypt(receiveBytes);
                            binarywrite.Write(fileFragmentToWrite, 0, count);
                        }
                    }
                    else
                    {
                        if (clientSocket.Poll(100, SelectMode.SelectError))
                        {
                            break;
                        }
                    }
                }
                File.SetLastWriteTime(fullPath, lastWriteTime);
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 3
0
        public static bool regist(string account, string password)
        {
            connectServer();
            byte[] message = MessageAssembler.registMsgAssemble(account, password);
            VerifyHandler.postMessage(clientSocket, message);

            if (!VerifyHandler.verify(clientSocket))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 4
0
        public static bool createFile(string path, string lastWriteTime)
        {
            connectServer();
            byte[] message = MessageAssembler.createFile(path, lastWriteTime);
            VerifyHandler.postMessage(clientSocket, message);

            if (!VerifyHandler.verify(clientSocket))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 5
0
        public static bool renameFile(string sourceFilePath, string destFilePath, string lastWriteTime)
        {
            connectServer();
            byte[] message = MessageAssembler.renameFile(sourceFilePath, destFilePath, lastWriteTime);
            VerifyHandler.postMessage(clientSocket, message);

            if (!VerifyHandler.verify(clientSocket))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 6
0
        public static bool delete(string path)
        {
            connectServer();
            byte[] message = MessageAssembler.delete(path);
            VerifyHandler.postMessage(clientSocket, message);

            if (!VerifyHandler.verify(clientSocket))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 7
0
        public static void syncNow()
        {
            connectServer();
            string paraHome = CommonStaticVariables.homePath.Substring(0, CommonStaticVariables.homePath.Length - 1);

            byte[] message = MessageAssembler.syncNow();

            VerifyHandler.postMessage(clientSocket, message);
            VerifyHandler.verify(clientSocket);

            findFileAndSync(paraHome, paraHome);
            // flag = 2 表示已同步完成
            VerifyHandler.postMessage(clientSocket, MessageAssembler.assembleDone());
        }
Ejemplo n.º 8
0
        public static bool modifyFile(string sourceFilePath, string fullPath, string lastWriteTime)
        {
            connectServer();
            byte[] message = MessageAssembler.modifyFile(sourceFilePath, lastWriteTime);
            VerifyHandler.postMessage(clientSocket, message);

            VerifyHandler.verify(clientSocket);
            FileTransferHandler.postFile(clientSocket, fullPath);

            if (!VerifyHandler.verify(clientSocket))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 9
0
        public static bool logout(string account, string password)
        {
            if (!clientSocket.Connected)
            {
                return(true);
            }

            byte[] message = MessageAssembler.logoutMsgAssemble(account, password);
            VerifyHandler.postMessage(clientSocket, message);

            if (!VerifyHandler.verify(clientSocket))
            {
                return(false);
            }
            clientSocket.Shutdown(SocketShutdown.Both);
            clientSocket.Close();
            return(true);
        }
Ejemplo n.º 10
0
        // 递归查找当前主目录
        public static void findFileAndSync(string home, 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(clientSocket, message);
                    VerifyHandler.verify(clientSocket);
                    findFileAndSync(fullPath, 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(clientSocket, message);

                    if (!VerifyHandler.verify(clientSocket))
                    {
                        FileTransferHandler.postFile(clientSocket, fullPath);
                        VerifyHandler.verify(clientSocket);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
Ejemplo n.º 11
0
        public static void syncOnstart()
        {
            bool     loop          = true;
            string   fullPath      = null;
            string   para1         = null;
            string   para2         = null;
            DateTime lastWriteTime = new DateTime();

            byte[] message = MessageAssembler.syncOnstart();

            connectServer();
            VerifyHandler.postMessage(clientSocket, message);
            VerifyHandler.verify(clientSocket);
            VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);

            byte[] receiveBytes = new byte[CommonStaticVariables.constSize];

            while (loop)
            {
                if (clientSocket.Poll(100, SelectMode.SelectRead))
                {
                    int      successReceiveBytes = clientSocket.Receive(receiveBytes);
                    string[] parMessage;
                    if ((parMessage = MessageParser.parse(receiveBytes)) != null)
                    {
                        string flag = parMessage[0];
                        para1 = parMessage[1];
                        para2 = parMessage[2];
                        switch (flag)
                        {
                        case "0":
                            // 文件夹
                            fullPath      = CommonStaticVariables.homePath + para1;
                            lastWriteTime = DateTime.Parse(para2);
                            if (!Directory.Exists(fullPath))
                            {
                                Directory.CreateDirectory(fullPath);
                            }
                            Directory.SetLastWriteTime(fullPath, lastWriteTime);
                            VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);
                            break;

                        case "1":
                            // 文件
                            fullPath      = CommonStaticVariables.homePath + para1;
                            lastWriteTime = DateTime.Parse(para2);
                            if (!File.Exists(fullPath))
                            {
                                VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageFailedEncrypted);
                                // 接收从服务器端传来的文件
                                FileTransferHandler.receiveFile(fullPath, lastWriteTime, clientSocket);
                                VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);
                                // 接收从服务器端传来的文件
                            }
                            else
                            {
                                if (lastWriteTime == File.GetLastWriteTime(fullPath))
                                {
                                    VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);
                                }
                                else
                                {
                                    VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageFailedEncrypted);
                                    // 接收从服务器端传来的文件
                                    FileTransferHandler.receiveFile(fullPath, lastWriteTime, clientSocket);
                                    VerifyHandler.postMessage(clientSocket, CommonStaticVariables.messageDoneEncrypted);
                                    // 接收从服务器端传来的文件
                                }
                            }
                            break;

                        case "2":
                            // 同步完成
                            loop = false;
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }