intercepor 里的datain 和 dataout 如何调用?要详细! (100分)

  • 主题发起人 主题发起人 alan5188
  • 开始时间 开始时间
A

alan5188

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是intercpt.dll动态连接的源代码.(方法datain和dataout功能是拦截数据
对数据进行加密和压缩 ).生成动态连接库以后.不知道如和调用atain和dataout;
还有 DataOut(const Data: IDataBlock);参数data如何设置?
谢谢!!!!!!!!!11
{*******************************************************}
{ }
{ Midas Socket Server Intercepor Demo }
{ }
{*******************************************************}
unit Intrcptu;
{
NOTE: This demo requires the ZLib units found in the extras directory on the
CD.
The Socket Server has the ability to install an interception COM object that
can be called whenever it receives or sends data. Using this feature, you
can encrypt or compress data using any method you wish. This demo uses the
ZLib compression units that ship on the CD to compress/uncompress all data
going over the wire.
To use this demo;
1) Make sure you have copied the ZLib units from the CD to a directory and
have added that directory to this projects search path.
2) Compile Intrcpt.dpr.
3) Register Intrcpt.DLL using REGSVR32 or TREGSVR on both the client and the
server.
4) On the Server: Bring up the properties for the Socket Server (right click
on the icon in the task bar and select properties) and put the GUID for
Intrcpt.DLL in the Interceptor GUID edit control. The GUID is defined
below as Class_DataCompressor.
5) On the Client: Set the TSocketConnection.InterceptorGUID property to the
Class_DataCompressor GUID and recompile your client.
}
interface
uses
Windows, ActiveX, ComObj, SConnect;
type
{
The interception object needs to implement IDataIntercept defined in
SConnect.pas. This interface has 2 procedures DataIn and DataOut described
below.
}
TDataCompressor = class(TComObject, IDataIntercept)
protected
procedure DataIn(const Data: IDataBlock);
stdcall;
procedure DataOut(const Data: IDataBlock);
stdcall;
end;

const
Class_DataCompressor: TGUID = '{B249776C-E429-11D1-AAA4-00C04FA35CFA}';
implementation
uses ComServ, SysUtils, ZLib, Classes;
{
DataIn is called whenever data is coming into the client or server. Use this
procedure to uncompress or decrypt data.
}
procedure TDataCompressor.DataIn(const Data: IDataBlock);
var
Size: Integer;
InStream, OutStream: TMemoryStream;
ZStream: TDecompressionStream;
p: Pointer;
begin
InStream := TMemoryStream.Create;
try
{ Skip BytesReserved bytes of data }
p := Pointer(Integer(Data.Memory) + Data.BytesReserved);
Size := PInteger(p)^;
p := Pointer(Integer(p) + SizeOf(Size));
InStream.Write(p^, Data.Size - SizeOf(Size));
OutStream := TMemoryStream.Create;
try
InStream.Position := 0;
ZStream := TDecompressionStream.Create(InStream);
try
OutStream.CopyFrom(ZStream, Size);
finally
ZStream.Free;
end;
{ Clear the datablock, then
write the uncompressed data back into the
datablock }
Data.Clear;
Data.Write(OutStream.Memory^, OutStream.Size);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;

{
DataOut is called whenever data is leaving the client or server. Use this
procedure to compress or encrypt data.
}
procedure TDataCompressor.DataOut(const Data: IDataBlock);
var
InStream, OutStream: TMemoryStream;
ZStream: TCompressionStream;
Size: Integer;
begin
InStream := TMemoryStream.Create;
try
{ Skip BytesReserved bytes of data }
InStream.Write(Pointer(Integer(Data.Memory) + Data.BytesReserved)^, Data.Size);
Size := InStream.Size;
OutStream := TMemoryStream.Create;
try
ZStream := TCompressionStream.Create(clFastest, OutStream);
try
ZStream.CopyFrom(InStream, 0);
finally
ZStream.Free;
end;
{ Clear the datablock, then
write the compressed data back into the
datablock }
Data.Clear;
Data.Write(Size, SizeOf(Integer));
Data.Write(OutStream.Memory^, OutStream.Size);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;

initialization
TComObjectFactory.Create(ComServer, TDataCompressor, Class_DataCompressor,
'DataCompressor', 'SampleInterceptor', ciMultiInstance, tmApartment);
end.
 
1: regsrv32 /path/intercpt.dll
2: 在scktsrvr.exe中的各个端口中,那个Intercept GUID:GUID加上intercpt.dll的GUID
就是上面程序中的:
const
Class_DataCompressor: TGUID = '{B249776C-E429-11D1-AAA4-00C04FA35CFA}';
3:在你的客户端中的SocketConnection的属性中intercpt的值也是这个GUID.
这就行了
scktsrvr中的TServerSocket已经对这个压缩和解压已经封装,只需要对intercpt.dll进行
注册即可。
注意第一个步骤,在客户端中也在进行注册。
 
不知在dcom模型是否也是这样!
 
DCOM中没有这个IntercptGUID,SocketConnection才有,而且它们之间的Parent类也不同
可能最上面的类是一样的。这个IntercptGUID是给TSocketTransport类用的,它根据这个
GUID来创建(CreateComObject(GUID))数据压缩和解压的对象,默认为空,也就是说不进行
压缩和解压,客户端也是如此。所以你在客户和服务端必须同时注册,不然之间传输的数据
会出错的。
 
嗯,在客户端,服务器端设置好就可以了。
 
接受答案了.
 
要加密的话怎么办呢?
还是详细点好吗?
 
加密和解密我就不太懂了:)
不过具体的应该是和压缩和解压差不多,将IDataBlock内容读到流中,
然后对流进行加密和解密,大体差不多。
 
需要单独调用吗/
 
不用,不过加密或压缩只能选其一,或者你将压缩和加密写到一个DLL中(自己写),像上面如是写
procedure TDataCompressor.DataIn(const Data: IDataBlock);
begin
对Data解压
对Data解密
end;

procedure TDataCompressor.DataOut(const Data: IDataBlock);
begin
对Data压缩
对Data加密
end;

在scktsrvr.exe中的扩展TServerSocket中,它是调用TSocketTransport中的FInterceptor: IDataIntercept
而这个FInterceptor是根据用户注册的IDataIntercept接口来创建的。
Datain, Dataout是集成在IDataIntercept接口中的,所以不用单独调用。
而我们所要做的是对IDataintercept接口进行接口的实现(也就是写个类来实现接口的函数
和过程),在scktsrvr中它是自动根据这个GUID调用的。
ServerSocket.InterceptGUID --> SocketTransport.InterceptGUID.
SocketTransport:
if FInterceptGUID <> '' then
Guid := StringToGuid(FInterceptGUID);
//这里将创建一个你注册了的COM对象,也就是你写的对IDataIntercept的实现类
FInterceptor := CreateComObject(Guid) as IDataIntercept;
在数据传输时,
if Assigned(FInterceptor) then
//这里就调用你写的类中的方法
FInterceptor.Datain(Data) //or FInterceptor.DataOut(Data)
老兄,够详细的了吧。:)
 

Similar threads

后退
顶部