clientapp.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. program clientapp;
  2. {$IFDEF WINDOWS}
  3. {$APPTYPE CONSOLE}
  4. {$ENDIF}
  5. {$mode objfpc}{$H+}
  6. uses
  7. custapp, Classes, SysUtils, enet, uenetclass;
  8. type
  9. { TClientApplication }
  10. TClientApplication = class(TCustomApplication)
  11. Private
  12. myClient: TENetClass;
  13. procedure DoConnect;
  14. procedure OnConnect(const Event:ENetEvent);
  15. procedure OnReceive(const Event:ENetEvent; var BroadcastMsg : Boolean; var BroadcastChannel : Byte);
  16. procedure ProcessLoop;
  17. procedure SendMessage(AMessage: String);
  18. procedure WriteHelp;
  19. Protected
  20. Procedure DoRun; override;
  21. public
  22. Constructor Create(AOWner : TComponent); override;
  23. Destructor Destroy; override;
  24. end;
  25. Var
  26. Application : TClientApplication;
  27. constructor TClientApplication.Create(AOWner: TComponent);
  28. begin
  29. INherited;
  30. StopOnException:=True;
  31. myClient := TENetClass.Create(30000,False);
  32. myClient.MessageTimeout:=100;
  33. myClient.OnReceive:=@OnReceive;
  34. myClient.OnConnect:=@OnConnect;
  35. myClient.OnDisconnect:=@OnConnect;
  36. end;
  37. destructor TClientApplication.Destroy;
  38. begin
  39. myClient.Free;
  40. Inherited;
  41. end;
  42. procedure TClientApplication.SendMessage(AMessage : String);
  43. begin
  44. myClient.BroadcastMsg(1,PChar(AMessage),Length(AMessage)+1,[ENetPacketReliable]);
  45. end;
  46. procedure TClientApplication.ProcessLoop;
  47. begin
  48. myClient.ProcessMsg;
  49. end;
  50. procedure TClientApplication.DoConnect;
  51. Var
  52. H : String;
  53. P : Integer;
  54. begin
  55. H:=GetOptionValue('H','host');
  56. if (H='') then
  57. H:='localhost';
  58. P:=StrToIntDef(GetOptionValue('p','port'),0);
  59. if P=0 then
  60. P:=30000;
  61. myClient.Connect(H,P);
  62. end;
  63. procedure TClientApplication.OnConnect(const Event: ENetEvent);
  64. begin
  65. if Event.kind=ENET_EVENT_TYPE_CONNECT then
  66. Writeln('Connected')
  67. else
  68. Writeln('Disconnected');
  69. end;
  70. procedure TClientApplication.OnReceive(const Event: ENetEvent; var BroadcastMsg: Boolean;
  71. var BroadcastChannel: Byte);
  72. var
  73. msg : string;
  74. begin
  75. msg := StrPas(PChar(Event.packet^.data));
  76. Writeln('Received : "',Msg,'"');
  77. end;
  78. procedure TClientApplication.WriteHelp;
  79. begin
  80. Writeln('Usage ',ExtractFileName(Self.ExeName),' [options]');
  81. Writeln('Where options is one or more of');
  82. Writeln('-h --help This message');
  83. Writeln('-H --host=hostname Hostname to connect to (default : localhost)');
  84. Writeln('-p --port=portno Port to connect to (default 30000)');
  85. Writeln('-p --port=portno Port to connect to (default 30000)');
  86. Writeln('-m --message=msg Message to send to server (none means no message is sent)');
  87. Writeln('-c --messagecount=N Number of times the message should be sent');
  88. Writeln('-p --pingcout=N Number of times the server should be pinged. (default 0)');
  89. Writeln('-c --messagecount=N Number of times the message should be sent (default 1)');
  90. end;
  91. procedure TClientApplication.DoRun;
  92. Var
  93. I,PingCount,MessageCount : Integer;
  94. Msg : String;
  95. begin
  96. if HasOption('h','help') then
  97. begin
  98. WriteHelp;
  99. Terminate;
  100. exit;
  101. end;
  102. DoConnect;
  103. PingCount:=StrToIntDef(GetOptionValue('p','pingcount'),0);
  104. if PingCount>0 then
  105. begin
  106. Writeln('Pinging server');
  107. For I:=1 to PingCount do
  108. begin
  109. myClient.Ping;
  110. ProcessLoop;
  111. end;
  112. end;
  113. Msg:=GetOptionValue('m','message');
  114. if (Msg<>'') then
  115. begin
  116. MessageCount:=StrToIntDef(GetOptionValue('c','messagecount'),1);
  117. Writeln('Sending message to server (count:',MessageCount,')');
  118. For I:=1 to MessageCount do
  119. begin
  120. SendMessage(Msg+' (iteration : '+IntToStr(i)+')');
  121. ProcessLoop;
  122. end;
  123. end;
  124. Terminate;
  125. end;
  126. begin
  127. Application:=TClientApplication.Create(Nil);
  128. try
  129. Application.Initialize;
  130. Application.Run;
  131. finally
  132. Application.Free;
  133. end;
  134. end.