Example #1
0
        /// ファイルを受信する
        public static void receiveFiles(Network network, String strDirectory, FolderLANSync.SyncFolderMaster parent)
        {
            if ( strDirectory.Length > 3 && !Directory.Exists(strDirectory))
                Directory.CreateDirectory(strDirectory);

            while (true)
            {
                byte nFlag = (byte)network.receiveByte();
                if (nFlag == 0)
                    break;

                String strFilePath = network.receiveString();

                parent.addLog("「" + strFilePath + "」を受信開始");

                byte[] data = network.receiveBinary();  // 先に受信してしまう

                strFilePath = Path.Combine(strDirectory, strFilePath);
                parent.beginReceiveFile(strFilePath);   //フォルダを変更する前に呼び出す.監視をオフにする。

            #if true
                // スレッド同期とってないし封印
                {
                    ReceiveThread receiveThread = new ReceiveThread(data, parent, strFilePath);
                    Thread thread = new Thread(new ThreadStart(receiveThread.receiveFileThread));
                    thread.Start();
                }
            #else
                Directory.CreateDirectory(Path.GetDirectoryName(strFilePath));

                System.IO.FileStream wout = null;
                for (int it = 0; it < 3; ++it)
                {
                    try
                    {
                        wout = new System.IO.FileStream(strFilePath, FileMode.Create);
                    }
                    catch (Exception ex)
                    {
                        parent.addLog("「" + strFilePath + "」が上書きできませんでした");
                        System.Threading.Thread.Sleep(500);
                    }
                    if (wout != null)
                    {
                        if (it > 0)
                        {
                            parent.addLog("「" + strFilePath + "」に書き込めました");
                        }
                    }
                }

                if (data != null)
                {
                    System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(new MemoryStream(data), System.IO.Compression.CompressionMode.Decompress);
                    byte[] buffer = new byte[1024 * 1024 * 50];
                    while (true)
                    {
                        int nReadSize = gzip.Read(buffer, 0, buffer.Length);
                        if (nReadSize == 0)
                            break;
                        if ( wout != null )
                            wout.Write(buffer, 0, nReadSize);
                    }
                    gzip.Close();
                }
                parent.addLog("「" + strFilePath + "」を受信終了");
                if (wout != null)
                   wout.Close();

                parent.endReceiveFile(strFilePath);   //フォルダを変更する前に呼び出す.監視をオフにする。
            #endif
            }
        }