unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;
type
TResultCallback =
procedure(cmd :
String; answer :
String)
of Object;
type
TServerCmdObj =
class(TObject)
private
_sock : TCustomWinSocket;
_cmd :
String;
public
procedure recieve(cmd :
String; answer :
String);
property Socket : TCustomWinSocket
read _sock
write _sock;
property Command :
String read _cmd
write _cmd;
end;
{...}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
cmdObj : TServerCmdObj;
begin
cmdObj := TServerCmdObj.Create();
// create an cmdObj
cmdObj.Socket := Socket;
// bind the socket
cmdObj.Command := Socket.ReceiveText();
// set the cmd
commit(cmdObj.Command, cmdObj.recieve);
// tell to be handled
end;
procedure tForm1.commit(cmd :
String; proc : TResultCallback);
var
res :
String;
// the glorious string to be the result
begin
res := magicFunction(cmd);
// somthing will happen to to it
if (proc <>
nil)
then // if we got a vaild callback
proc(cmd, res);
// well call it!
end;
procedure TServerCmdObj.recieve(cmd, answer:
String);
// is called if the result is calced
begin
self._sock.SendText(answer);
// if the command has been processed
FreeAndNil(Self);
// commit suicide
end;
end.