Creating Custom Filters

To add a custom filter to the DataSnap connection layer, what you have to do is:

  • Create a package featuring a transport filter class that inherits from the TTransportFilter class
  • Implement the Processlnput and ProcessOutput virtual methods of this class
  • Register the transport filter and install the package with the filter in the Delphi IDE.

The DSnapFilterDemo folder contains the DSnapFilter01 package with a trivial MIME encoding filter. The class of the filter is defined as: uses

DBXTransport, IdCoderMIME; type

TMimeFilter = class (TTransportFilter) public function ProcessInput(const Data: TBytes): TBytes; override; function ProcessOutput(const Data: TBytes): TBytes; override; function Id: string; override; end;

The code of these three methods is not particularly complex. The Id function returns a unique identifier for the filter (passed also to the client in the stream):

function TMimeFilter.Id: string; begin

Result := 'Cantools.MimeFilter'; end;

The ProcessInput and ProcessOutput methods process the bytes stream using Indy's TIdEncoderMIME and TIdDecoderMIME support classes:

function TMimeFilter.ProcessInput(const Data: TBytes): TBytes; var strEncoded: string; begin strEncoded := TIdEncoderMIME.EncodeBytes(Data); Result := BytesOf(strEncoded); end;

function TMimeFilter.ProcessOutput(const Data: TBytes): TBytes; var strEncoded: string; begin strEncoded := StringOf (Data);

Result := TIdDecoderMIME.DecodeBytes(strEncoded); end;

The unit also performs the registration (and de-registration) of the filter, something both the server and the client must do.

Was this article helpful?

0 0

Post a comment