I am working on a socket server function and I want to keep the data interpretation out of this object. It should only handle client connections and telegram reception and dispatching.
So I figured I could do something like this:
In the class file:
{$mode Delphi}
.....
type
TProcessPacket = procedure (Sender: TObject; Data: string; var Response: string);
TRemoteServer = class(TObject)
private
FServer: TIdTCPServer;
FClientPort: word;
FProcessData: TProcessPacket;
....
public
constructor Create;
destructor Destroy; override;
property ProcessData: TProcessPacket read FProcessData write FProcessData;
....
end;
procedure TRemoteServer.ServerExecute(AContext: TIdContext);
var
msg, reply, cmd: string;
i, n: integer;
begin
...
msg := AContext.Connection.IOHandler.ReadLn(ETX);
if Pos(STX, msg) = 1 then //Remove leading STX
Delete(msg, 1, 1);
if FProcessData <> nil then // <== ERROR HERE!!!
FProcessData(AContext, msg, reply)
else
reply := 'ERROR';
AContext.Connection.IOHandler.Write(reply);
end;
In the main project file:
{$mode Delphi}
var
RemoteSrv: TRemoteServer;
ch: char;
procedure Respond(Sender: TObject; Data: string; var Response: string);
var
reply: string;
n: integer;
begin
if Pos('LOGIN', Data) = 1 then
reply := 'LOGIN' + TAB + 'OK '
else
begin
n := Pos(TAB, Data);
if n <1 then
reply := Data
else
reply := Copy(Data, 1, n-1) + TAB + 'ERR';
end;
Response := reply;
end;
begin
RemoteSrv := TRemoteServer.Create; //Uses default port 23500
RemoteSrv.ProcessData := Respond; //Assign the processing function so the object can call it
try
....
But it errors out on the line indicated above with the following message:
remoteserver.pas(160,19) Error: Wrong number of parameters specified for call to "<Procedure Variable>"
I am assigning the procedure to the object property so it can be used in the code and I need to do an error check so that the function is not accidentally executed if it was not assigned....
Obviously I am doing something wrong when I check if the FProcessData field is assigned (not NIL).
How am I supposed to do this in Lazarus/FPC?