forked from terrylao/Artemis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIfileTrans.pas
197 lines (188 loc) · 4.49 KB
/
UIfileTrans.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
unit UIfileTrans;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,header,
winsock,socketfunc, Grids, ComCtrls;
type
TfrmUIFileTrans = class(TForm)
sg1: TStringGrid;
pb1: TProgressBar;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FCCmd:TCTLCmd;
filesizes:integer;
transfering,hasclose,hasBreaking:boolean;
{ Private declarations }
procedure HandleFILEMSG(var Msg: TMessage) ; message WM_FileTransfer;
procedure FILEACCEPT(var Msg: TMessage) ;
procedure FILERESUME(var Msg: TMessage) ;
procedure FILENEXT(var Msg: TMessage) ;
procedure FILEERROR(var Msg: TMessage) ;
procedure FILEFINISH(var Msg: TMessage) ;
public
curFile,socketid:integer;
{ Public declarations }
end;
var
frmUIFileTrans: TfrmUIFileTrans;
implementation
{$R *.lfm}
procedure TfrmUIFileTrans.HandleFILEMSG(var Msg: TMessage);
begin
case msg.WParam of
FILETRANS_ERROR:
FILEERROR(msg);
FILETRANS_REJECT:
FILEERROR(msg);
FILETRANS_ACCEPT:
FILEACCEPT(msg);
FILETRANS_NEXT:
FILENEXT(msg);
FILETRANS_RESUME:
FILERESUME(msg);
FILETRANS_FINISH:
FILEFINISH(msg);
end;
end;
procedure TfrmUIFileTrans.FILEFINISH(var Msg: TMessage);
begin
transfering:=false;
caption:=inttostr(msg.LParam);
close;
end;
procedure TfrmUIFileTrans.FILEERROR(var Msg: TMessage);
begin
transfering:=false;
caption:=inttostr(msg.LParam);
end;
procedure TfrmUIFileTrans.FILENEXT(var Msg: TMessage);
begin
inc(curFile);
Caption:='Next :'+inttostr(curFile)+' and '+inttostr(sg1.RowCount);
if curFile=sg1.RowCount then
begin
FCCmd.Cmd := FILETRANS_FINISH;
send(socketid,FCCmd, SizeOf(TCtlCmd),0);
Caption:='ALL file done!';
close;
exit;
end;
FILEACCEPT(msg);
end;
procedure TfrmUIFileTrans.FILERESUME(var Msg: TMessage);
var
fileid,i,j,k,l:integer;
pBuf: array[0..8191] of Byte;
Filedone:boolean;
begin
fileid:=fileopen(sg1.Cells[0,curFile],fmOpenRead);
FileSeek(fileid,0,0);
if Msg.LParam>0 then
begin
j:=fileseek(fileid,Msg.WParam,0);
if (j<>Msg.LParam) then
begin
Filedone:=true;
end;
end;
if Filedone then
begin
FCCmd.Cmd := FILETRANS_ERROR;
FCCmd.X := FILESIZEMISMATCH;
send(socketid,FCCmd, SizeOf(TCtlCmd),0);
exit;
end;
transfering:=true;
try
repeat
i:=fileread(fileid,pbuf[0],4096);
FCCmd.Cmd := FILETRANS_DATA;
FCCmd.X := i;
send(socketid,FCCmd, SizeOf(TCtlCmd),0);
j:=send(socketid,pbuf[0],i,0);
if j=-1 then
begin
hasClose:=true;
break;
end;
while i>j do
begin
sleep(100);
k:=i-j;
l:=send(socketid,pbuf[j],k,0);
if l=-1 then
begin
hasClose:=true;
break;
end;
j:=j+l
end;
pb1.Position:=pb1.Position+i;
application.ProcessMessages;
until (not transfering) or (i<4096);
except
on e:Exception do
begin
if i=-1 then
begin
FCCmd.Cmd := FILETRANS_ERROR;
FCCmd.X :=e.HelpContext;
send(socketid,FCCmd, SizeOf(TCtlCmd),0);
exit;
end;
end;
end;
FileClose(fileid);
sg1.Cells[1,curFile]:='Done';
if hasBreaking then
begin
FCCmd.Cmd := FILETRANS_BREAK;
send(socketid,FCCmd, SizeOf(TCtlCmd),0);
end;
if hasClose then
begin
close;
end;
end;
procedure TfrmUIFileTrans.FILEACCEPT(var Msg: TMessage);
var
s:string;
test: Trect;
begin
s :=extractfilename(sg1.cells[0,curFile]);
filesizes:=WideFileSize(sg1.cells[0,curFile]);
if filesizes=-1 then
begin
exit;
end;
pb1.Max:=filesizes;
pb1.Position:=0;
test:=sg1.CellRect(1,curFile);
pb1.SetBounds(test.Left+1,test.Top+1,test.Right - test.Left -1 ,test.Bottom - test.Top -1);
pb1.Visible:=true;
caption:='Accept Transfer';
FCCmd.Cmd := FILETRANS_REQUEST;
FCCmd.X := length(s);
FCCmd.Y := filesizes;
send(socketid,FCCmd, SizeOf(TCtlCmd),0);
send(socketid,s[1], FCCmd.X,0);
end;
procedure TfrmUIFileTrans.FormShow(Sender: TObject);
begin
curFile:=1;
pb1.Visible:=false;
hasClose:=false;
transfering:=false;
hasBreaking:=false;
end;
procedure TfrmUIFileTrans.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
transfering:=false;
hasBreaking:=true;
hasClose:=true;
end;
end.