版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, ComCtrls, ExtCtrls, StdCtrls, IdBaseComponent, IdComponent,IdTCPConnection, IdTCPClient,Unit2;constpacksize=65000;/ 包大小typeTRevBuffer=packed recordmasmID:Word;/ 未用ID:Word;/ 標(biāo)記F1:DWord;F2:DWord;
2、F3:DWord;F4:DWord;Len:DWord;MD:Int64;end;TSendBuffer=packed recordmasmID:Word;/ 未用ID:Word;/ 標(biāo)記F1:DWord;F2:DWord;F3:DWord;F4:DWord;Len:DWord;/ 數(shù)據(jù)區(qū)的大小MD:Int64;/ 摘要buffer:Array0.packsize-1 of byte;end;/性能監(jiān)測(cè)線程typeTMonitorThread = class(TThread)privateSecondTimer:DWord;protectedprocedure Execute; overrid
3、e;publicconstructor Create(); reintroduce;end;typeTFeiler = class;TSocketThread = class(TThread)privateRev:TRevBuffer;Snd:TSendBuffer;PRev,PSnd,TP,UP:PByte;TempPointer:PByte;RemainBytes:Word;ReceivedCounter:Integer;HeaderFinished,BodyFinished,PushTransporting:Boolean;FeilerThread : TFeiler;MyIndex:I
4、nteger;FClient:TIdTCPClient;SendBytes:DWord;/ 該線程要傳輸?shù)淖止?jié)數(shù)MapOffset:DWord;/ 文件映像指針偏移FileMapPointer:PByte;/ 內(nèi)存映像文件指針SendBlock:DWord;/ 該線程要傳輸?shù)膲K數(shù)Thread_H,Thread_T:DWord;procedure Process;procedure Branch;procedure RequestTransfer;procedure ServerReady;procedure transporting;protectedprocedure Execute; ov
5、erride;publicconstructor Create(F: TFeiler;Index:Integer;Offset:DWord;Count:DWord;FileMap:PByte); reintroduce;end;TFeiler = class(TForm)Edit1: TEdit;ComboBox1: TComboBox;Button1: TButton;Button2: TButton;Edit2: TEdit;OpenDialog1: TOpenDialog;Label1: TLabel;Label2: TLabel;Label3: TLabel;Button3: TBut
6、ton;Panel1: TPanel;StatusBar1: TStatusBar;procedure Button2Click(Sender: TObject);procedure Button1Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction); private Private declarations publicFileSize:DWord;/ 要傳輸?shù)奈募笮ocketThread:A
7、rray of TSocketThread;Event:Array of THandle;Instructor:Array of TProgressBar;MonitorThread:TMonitorThread;MP:PByte; Public declarations end;varFeiler: TFeiler;ShowProgress:Boolean;HeightInc:Integer;implementation$R *.dfmTSocketThread.Create(F:constructorTFeiler;Index:Integer;Offset:DWord;Count:DWor
8、d;FileMap:PByte); varIswork:Boolean;beginFeilerThread := F;MyIndex:=Index;MapOffset:=Offset;SendBytes:=Count;FileMapPointer:=FileMap;INC(FileMapPointer,Offset);SentBytesMyIndex:=0;IF Index=0 thenIswork:=FalseelseIswork:=True;ReceivedCounter:=32;PRev:=Rev;PSnd:=Snd;TempPointer:=PRev;inherited Create(
9、IsWork);end;procedure TSocketThread.Execute;varH,I,J:Integer;Thread_L:Integer;CurPointerOffset:DWord;File_Name:String250;beginFClient:=TIdTCPClient.Create(nil);FClient.Host:=FeilerThread.Edit1.Text;FClient.Port:=60606;tryFClient.Connect(10000);except/錯(cuò)誤處理Application.MessageBox(' 無法連接服務(wù)器 ',
10、39;ERROR',MB_ICONERROR);FClient.Free;exit;end;/* 接收服務(wù)器信息 */Thread_H:=(SendBytes-1) div packsize +1;/ 需要傳輸 Thread_H 個(gè)數(shù)據(jù)塊Thread_T:=SendBytes-(Thread_H-1)*packsize;/ 尾巴 FeilerThread.InstructorMyIndex.Max:=Thread_H;Snd.F2:=Thread_H;IF MyIndex=0 then/0 號(hào)線程負(fù)責(zé)初始化服務(wù)器beginSnd.ID:=0;/ 通知服務(wù)器建立文件TP:=PSnd;In
11、c(TP,32);File_name:=ExtractFileName(FeilerThread.Edit2.Text);Snd.Len:=Length(File_name)+1;Snd.F1:=FeilerThread.FileSize;UP:=File_name;move(UPYTPA,S nd.Le n);FClient.Socket.Send(PSndSnd.Len+32); 通知服務(wù)器repeatprocess;until Rev.ID=3;/Snd.F1=Snd.F2;SentBytesMyIndex:=SentBytesMyIndex+Snd.Len;/ 最后一個(gè)尾巴H:=Len
12、gth(FeilerThread.Event);IF H>1 then/2 個(gè)線程以上的話WaitForMultipleObjects(H-1,FeilerThread.Event1,True,INFINITE);Feiler.MonitorThread.Terminate;Snd.ID:=3;/ 告知服務(wù)器關(guān)閉內(nèi)存影像Snd.Len:=0;FClie nt.Socket.Se nd(PS nd"32);UnMapViewOfFile(FeilerThread.MP);/ 關(guān)閉內(nèi)存映像For I:=0 to H-1 doCloseHandle(FeilerThread.Even
13、tI);SetLength(FeilerThread.Event,0);FeilerThread.Button1.Enabled:=True;endelsebeginRequestTransfer;repeatprocess;until Rev.ID=3;/Snd.F1=Snd.F2;SentBytesMyIndex:=SentBytesMyIndex+Snd.Len;SetEvent(FeilerThread.EventMyIndex);end;FClient.Disconnect;end;procedure TFeiler.Button2Click(Sender: TObject);var
14、F:Tsearchrec;beginIF OpenDialog1.Execute thenEdit2.Text:=OpenDialog1.FileName;IF FindFirst(Edit2.Text,FaAnyfile,F)=0 thenFileSize:=F.Size;end;procedure TFeiler.Button1Click(Sender: TObject);varH,I,J,K,U:Integer;FH,MH:THandle;begin/建立文件內(nèi)存映像Button1.Enabled:=False;FH:=FileOpen(Edit2.Text,fmOpenRead);MH
15、:=CreateFileMapping(FH,nil,Page_ReadOnly,0,FileSize,nil);CloseHandle(FH);MP:=MapViewOfFile(MH,File_Map_Read,0,0,FileSize);CloseHandle(MH);J:=strtoint(ComboBox1.Text);I:=(FileSize-1) div (1024*1024) + 1;/ 最多線程數(shù)IF J>I thenJ:=I;/一共有 J 個(gè)線程/建立進(jìn)度指示ThreadCount:=J;For K:=0 to Length(Instructor)-1 doInstr
16、uctorK.Free;SetLength(Instructor,J);SetLength(SentBytes,J);For K:=0 to J-1 dobeginInstructorK:=TProgressBar.Create(self);InstructorK.Left:=0;InstructorK.Top:=K*16;InstructorK.Width:=390;InstructorK.Step:=1;InstructorK.Parent:=Panel1;end;I:=FileSize div J;/ 每個(gè)線程的傳輸字節(jié)K:=FileSize-I*J;/ 最后一個(gè)線程要多傳輸?shù)淖止?jié),尾巴
17、U:=I;SetLength(SocketThread,J);SetLength(Event,J);For H:=0 to J-1 dobeginIF H=J-1 thenI:=I+K;/ 最后的數(shù)據(jù)塊SocketThreadH:=TSocketThread.Create(self,H,H*U,I,MP);EventH:=CreateEvent(nil,False,False,nil);end;/循環(huán)結(jié)束end;procedure TSocketThread.Branch;begincase Rev.ID of1:ServerReady;2:transporting;end;end;proce
18、dure TSocketThread.RequestTransfer;beginSnd.ID:=1;Snd.F1:=0;Snd.Len:=0;FClie nt.Socket.Se nd(PS ndY32);end;procedure TSocketThread.Process;varBytes:Integer;beginIF RemainBytes>0 then/ 上次還有字節(jié)沒收取beginReceivedCounter:=ReceivedCounter-RemainBytes;IF ReceivedCounter>0 then/ 接收剩余字節(jié)后數(shù)據(jù)報(bào)尚未完成接收beginByt
19、es:=RemainBytes;/ 接收剩余RemainBytes:=0;endelsebeginIF ReceivedCounter=0 then/ 剩余字節(jié)剛好接收完成beginBytes:=RemainBytes;RemainBytes:=0;IF HeaderFinished thenBodyFinished:=TrueelsebeginHeaderFinished:=True;/ 如果頭完成就是體完成,否則只是頭完成 PushTransporting:=True;end;endelsebegin/剩余字節(jié)超過本數(shù)據(jù)報(bào)大小,產(chǎn)生數(shù)據(jù)粘連Bytes:=ReceivedCounter+Re
20、mainBytes;RemainBytes:=RemainBytes-Bytes;/ 完成本數(shù)據(jù)報(bào)后剩余字節(jié)IF HeaderFinished thenBodyFinished:=Trueelsebegin否則只是頭完成HeaderFinished:=True;/ 如果頭完成就是體完成,PushTransporting:=True;end;end;end;FClie nt.ReadBufer(TempPoi nterBytes);Inc(TempPointer,Bytes);IF (Rev.Len=0) and HeaderFinished then/ 只要頭beginHeaderFinish
21、ed:=False;PushTransporting:=False;TempPointer:=PRev;ReceivedCounter:=32;/ 準(zhǔn)備下一個(gè)頭Bran ch;/分支處理endelsebeginIF PushTransporting then / 正在傳輸 bodybeginPushTransporting:=False;ReceivedCounter:=Rev.Len;endelseIF BodyFinished thenbeginTempPointer:=PRev;HeaderFinished:=False;BodyFinished:=False;ReceivedCou n
22、ter:=32; 準(zhǔn)備下一個(gè)頭Bran ch;/分支處理endend;endelsebegin/RemainBytes=0TryRemainBytes:=FClient.ReadFromStack(True,15000,True);Exceptexit; end;end;end;procedure TSocketThread.ServerReady;varH:Integer;beginFor H:=2 to strtoint(Feiler.ComboBox1.Text) do Feiler.SocketThreadH-1.Resume;RequestTransfer;Feiler.Monito
23、rThread:=TMonitorThread.Create; end;procedure TSocketThread.transporting;varFTT,FUU:PByte;begin已發(fā)送字節(jié)SentBytesMyIndex:=SentBytesMyIndex+Snd.Len;/ Snd.ID:=2;Snd.F1:=Rev.F1;IF Snd.F1=Snd.F2 thenSnd.Len:=Thread_TelseSnd.Len:=packsize; Snd.F3:=MapOffset+(Snd.F1-1)*packsize;FTT:=FileMapPointer;INC(FTT,(Sn
24、d.F1-1)*packsize);FUU:=PSnd;Inc(FUU,32);move(FTT"FUUA,S nd丄en);FClie nt.Socket.Se nd(PS ndS nd.Le n+32);FeilerThread.InstructorMyIndex.StepIt;end;procedure TFeiler.Button3Click(Sender: TObject); beginIF Not(ShowProgress) thenButton3.Caption:='<< 進(jìn)度顯示 'elseButton3.Caption:=' 進(jìn)度
25、顯示 >>' ShowProgress:=Not(ShowProgress); IF ShowProgress then begin HeightInc:=strtoint(ComboBox1.Text)*20; Feiler.Height:=Feiler.Height+HeightInc;endelse begin Feiler.Height:=Feiler.Height-HeightInc;end;end; TMonitorThread constructor TMonitorThread.Create;beginSecondTimer:=0;inherited Cre
26、ate(false);end;procedure TMonitorThread.Execute;varH,I,V:DWord;beginWhile Not(Terminated) dobeginSleep(1000);SecondTimer:=SecondTimer+1;H:=0;For I:=0 to ThreadCount-1 doH:=H+SentBytesI;V:=H div SecondTimer;V:=V div 1024;Feiler.StatusBar1.Panels2.Text:=' 平均速率 :'+Inttostr(V)+'KB/S' end
27、;end;procedure TFeiler.FormClose(Sender: TObject; var Action: TCloseAction); varK:Integer;beginFor K:=0 to Length(Instructor)-1 doInstructorK.Free;SetLength(Instructor,0);SetLength(SentBytes,0);SetLength(SocketThread,0);SetLength(Event,0);end;end.unit Unit1;interfaceusesWindows, Messages, SysUtils,
28、Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,SocketProcess, IdBaseComponent, IdComponent, IdTCPServer;typeTForm1 = class(TForm)Button1: TButton;IdTCPServer1: TIdTCPServer;Memo1: TMemo;Button2: TButton;procedure IdTCPServer1Connect(AThread: TIdPeerThread); procedure IdTCPServer1Disconnect(A Thread: TIdPeerThread);procedure IdTCPServer1Execute(AThread: TIdPeerThread); procedure Button1Click(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Bu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 萬達(dá)商業(yè)廣場(chǎng)2024年物業(yè)綜合管理協(xié)議版B版
- 論文答辯精要解析
- 2025年度拆遷安置住房租賃及物業(yè)管理合同4篇
- 二零二五年度建筑工程項(xiàng)目建造師勞動(dòng)合同范本9篇
- 2025年度產(chǎn)教融合校企產(chǎn)學(xué)研合作項(xiàng)目執(zhí)行框架協(xié)議4篇
- 二零二五年度餐廳經(jīng)理勞動(dòng)合同范本:服務(wù)質(zhì)量提升3篇
- 二零二四年事業(yè)單位委托第三方社保代繳與員工績(jī)效獎(jiǎng)勵(lì)協(xié)議3篇
- 二零二五年度大米產(chǎn)品綠色包裝與環(huán)保材料應(yīng)用合同2篇
- 2024飼料行業(yè)客戶數(shù)據(jù)共享協(xié)議
- 2025年度商業(yè)地產(chǎn)項(xiàng)目場(chǎng)地租賃及物業(yè)管理合同12篇
- 國(guó)家自然科學(xué)基金項(xiàng)目申請(qǐng)書
- 電力電纜故障分析報(bào)告
- 中國(guó)電信網(wǎng)絡(luò)資源管理系統(tǒng)介紹
- 2024年浙江首考高考選考技術(shù)試卷試題真題(答案詳解)
- 《品牌形象設(shè)計(jì)》課件
- 倉庫管理基礎(chǔ)知識(shí)培訓(xùn)課件1
- 藥品的收貨與驗(yàn)收培訓(xùn)課件
- GH-T 1388-2022 脫水大蒜標(biāo)準(zhǔn)規(guī)范
- 高中英語人教版必修第一二冊(cè)語境記單詞清單
- 政府機(jī)關(guān)保潔服務(wù)投標(biāo)方案(技術(shù)方案)
- HIV感染者合并慢性腎病的治療指南
評(píng)論
0/150
提交評(píng)論