unit LANFS_KERNAL;
interface
uses SysUtils, Classes,
IdBaseComponent,IdStreamVCL, IdComponent,
IdTCPServer,IdTCPConnection,IdTCPClient,IdContext;
type
TRECEIVEDDATA =
procedure(Sender: TObject; MemStream: TMemoryStream)
of Object;
TLANFS_KERNAL =
class
fReceivedData: TRECEIVEDDATA;
public
procedure ReceiveStream(Acontext:TidContext);
procedure Start(port:integer);
procedure Stop();
procedure SendStream(Stream: TFilestream;Host:
String);
property OnReceivedData: TRECEIVEDDATA
read fReceivedData
write fReceivedData;
end;
var LANFS_SYSTEM :TLANFS_KERNAL;
TCPClient1: TIdTCPClient;
TCPServer1: TIdTCPServer;
implementation
procedure TLANFS_KERNAL.Start(port:integer);
{
+Aktiviert TCPClient und TCPServer
+Sollte immer am Start des Programms aufgerufen werden.
+zuletzt editiert: 24.05.06
}
begin
TCPClient1:= TIdTCPClient.Create;
TCPClient1.Port:=Port;
with TCPClient1
do
begin
ReadTimeout:=5000;
ConnectTimeout:=5000;
end;
TCPServer1:= TIdTCPServer.Create;
TCPServer1.OnExecute := LANFS_SYSTEM.ReceiveStream;
with TCPServer1
do
begin
MaxConnections:=1;
DefaultPort:=Port;
Active:=True;
end;
end;
procedure TLANFS_KERNAL.Stop();
{Stopt Server und Client, gibt Speicher frei.
zuletzt editiert: 24.06.05}
begin
TCPClient1.Free;
TCPServer1.Active:=False;
TCPServer1.Free;
end;
procedure TLANFS_KERNAL.SendStream(Stream: TFilestream;Host:
String);
{
+Versendet vom TCPClient an TCPServer einen Filestream
+Dieser wird als Parameter übergeben, und muss vorher erzeugt worden sein.
+zuletzt editiert: 24.05.06
}
var
IndyStream: TidStreamVCL;
begin
TCPClient1.Host:=Host;
try
IndyStream := TidStreamVCL.Create(Stream);
try
{ Senden }
with TCPClient1
do
begin
Connect;
try
Socket.
Write(IndyStream, 0, true);
finally
Disconnect;
end;
end;
finally
FreeAndNil(IndyStream);
end;
finally
FreeAndNil(Stream);
end;
end;
procedure TLANFS_KERNAL.ReceiveStream(Acontext:TidContext);
{
+Empfängt vom TCPClient an TCPServer einen Filestream
+zuletzt editiert: 25.05.06
}
var
FSTream: TMemoryStream;
SizeOfIncomingStream: Integer;
IDS: TidStreamVCL;
begin
FStream := TMemoryStream.Create;
// FStream := TFileStream.Create('C:\test.tmp', fmCreate);
try
IDS := TidStreamVCL.Create(FStream);
try
{ Empfangen }
with AContext.connection
do
begin
try
SizeOfIncomingStream := Socket.ReadInteger;
Socket.ReadStream(IDS, SizeOfIncomingStream);
finally
Disconnect;
end;
end;
{ Damit der Inhalt von IDS in FStream übertragen wird }
IDS.VCLStream.Position := 0;
finally
FreeAndNil(IDS);
end;
finally
{Event losschicken}
if Assigned(fReceivedData)
then
fReceivedData(Self, FStream);
FreeAndNil(FStream);
end;
end;
end.