/// <summary> /// 执行数据接收 /// </summary> /// <param name="socket"></param> public void DoReceive(Socket socket) { //read check result byte[] pullResult = new byte[8]; AdbSocketHelper.Read(socket, pullResult); this.CheckPullFileResult(pullResult); byte[] data = new byte[this.BufferSize]; FileInfo f = new FileInfo(this._Local); FileStream fos = fos = new FileStream(f.FullName, System.IO.FileMode.Create, FileAccess.Write); using (fos) { while (true) { if (AdbSocketHelper.CheckResult(pullResult, Encoding.UTF8.GetBytes("DONE"))) { break; } if (AdbSocketHelper.CheckResult(pullResult, Encoding.UTF8.GetBytes("DATA")) == false) { throw new ADBConnectionException(); } //get an check buffer length int length = Swap32bitFromArray(pullResult, 4); if (length > 64 * 1024) { throw new ApplicationException("Receiving too much data."); } //read data AdbSocketHelper.Read(socket, data, length); AdbSocketHelper.Read(socket, pullResult); //write file try { fos.Write(data, 0, length); } catch (IOException e) { throw new ApplicationException("Writing local file failed!", e); } //receive event if (this.OnReceiveData != null) { this.OnReceiveData(data, 0, length); } } //flush try { fos.Flush(); //设置文件的原始创建时间 //File.SetCreationTime(f.FullName,); } catch (IOException e) { throw new ApplicationException("Writing local file failed!", e); } } }
/****************** private methods ******************/ private void CheckPullFileResult(byte[] result) { if (AdbSocketHelper.CheckResult(result, Encoding.UTF8.GetBytes("DATA")) == false && AdbSocketHelper.CheckResult(result, Encoding.UTF8.GetBytes("DONE")) == false) { throw new ADBConnectionException(); } }