rpcapi.pp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. unit rpcapi;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fprpcrtti, myapi;
  6. Type
  7. { TIntfImpl }
  8. TIntfImpl = class(TInterfacedObject, IMyInterface)
  9. public
  10. procedure SayHello;
  11. Function Echo(args : Array of string) : String;
  12. function DoSum(a,b : Integer) : integer;
  13. function Split(aLine,aSep : string) : TStringArray;
  14. function DoVarTest(var aArg: String): Boolean;
  15. end;
  16. { TIntf2Impl }
  17. TIntf2Impl = class(TInterfacedObject, IMyOtherInterface)
  18. public
  19. function Echo(args: TStringArray): String;
  20. function SayHello: string;
  21. end;
  22. Implementation
  23. { TIntf2Impl }
  24. function TIntf2Impl.Echo(args: TStringArray): String;
  25. var
  26. S : String;
  27. begin
  28. Result:='';
  29. For S in Args do
  30. begin
  31. if Result<>'' then
  32. Result:=Result+' ';
  33. Result:=Result+S;
  34. end
  35. end;
  36. function TIntf2Impl.SayHello: string;
  37. begin
  38. Result:='Hello, World!';
  39. end;
  40. procedure TIntfImpl.SayHello;
  41. begin
  42. Writeln('Hello, World!');
  43. end;
  44. function TIntfImpl.Echo(args: array of string): String;
  45. var
  46. S : String;
  47. begin
  48. Result:='';
  49. For S in Args do
  50. begin
  51. if Result<>'' then
  52. Result:=Result+' ';
  53. Result:=Result+S;
  54. end
  55. end;
  56. function TIntfImpl.DoSum(a,b : Integer) : integer;
  57. begin
  58. Result := a + b;
  59. end;
  60. function TIntfImpl.Split(aLine,aSep : string) : TStringArray;
  61. begin
  62. Result := aLine.Split(aSep);
  63. end;
  64. function TIntfImpl.DoVarTest(var aArg: String): Boolean;
  65. begin
  66. if aArg = 'Test' then begin
  67. aArg := 'Foo';
  68. Result := True;
  69. end else
  70. Result := False;
  71. end;
  72. Function GetMyInterface(Const aName : string) : IInterface;
  73. begin
  74. Result:=TIntfImpl.Create as IInterface;
  75. end;
  76. Function GetMyOtherInterface(Const aName : string) : IInterface;
  77. begin
  78. Result:=TIntf2Impl.Create as IInterface;
  79. end;
  80. initialization
  81. RTTIJSONRPCRegistry.Add(TypeInfo(IMyInterface),@GetMyInterface);
  82. RTTIJSONRPCRegistry.Add(TypeInfo(IMyOtherInterface),@GetMyOtherInterface,'Service2');
  83. end.