IdMappedPortUDP.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.6 7/21/04 3:14:04 PM RLebeau
  18. Removed local Buffer variable from TIdMappedPortUDP.DoUDPRead(), not needed
  19. Rev 1.5 2004.02.03 5:44:00 PM czhower
  20. Name changes
  21. Rev 1.4 2/2/2004 4:20:30 PM JPMugaas
  22. Removed warning from Delphi 8. It now should compile in DotNET.
  23. Rev 1.3 1/21/2004 3:11:34 PM JPMugaas
  24. InitComponent
  25. Rev 1.2 10/25/2003 06:52:14 AM JPMugaas
  26. Updated for new API changes and tried to restore some functionality.
  27. Rev 1.1 2003.10.24 10:38:28 AM czhower
  28. UDP Server todos
  29. Rev 1.0 11/13/2002 07:56:46 AM JPMugaas
  30. }
  31. unit IdMappedPortUDP;
  32. interface
  33. {$i IdCompilerDefines.inc}
  34. {
  35. - Syncronized with Indy standards by Gregor Ibic
  36. - Original DNS mapped port by Gregor Ibic
  37. }
  38. uses
  39. Classes,
  40. IdGlobal,
  41. IdUDPServer,
  42. IdSocketHandle,
  43. IdGlobalProtocols;
  44. type
  45. TIdMappedPortUDP = class(TIdUDPServer)
  46. protected
  47. FMappedPort: TIdPort;
  48. FMappedHost: String;
  49. FOnRequest: TNotifyEvent;
  50. //
  51. procedure DoRequestNotify; virtual;
  52. procedure InitComponent; override;
  53. procedure DoUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle); override;
  54. published
  55. property MappedHost: string read FMappedHost write FMappedHost;
  56. property MappedPort: TIdPort read FMappedPort write FMappedPort;
  57. property OnRequest: TNotifyEvent read FOnRequest write FOnRequest;
  58. end;
  59. implementation
  60. uses
  61. IdAssignedNumbers,
  62. IdUDPClient, SysUtils;
  63. procedure TIdMappedPortUDP.InitComponent;
  64. begin
  65. inherited InitComponent;
  66. DefaultPort := IdPORT_DOMAIN;
  67. end;
  68. procedure TIdMappedPortUDP.DoRequestNotify;
  69. begin
  70. if Assigned(OnRequest) then begin
  71. OnRequest(Self);
  72. end;
  73. end;
  74. procedure TIdMappedPortUDP.DoUDPRead(AThread: TIdUDPListenerThread;
  75. const AData: TIdBytes; ABinding: TIdSocketHandle);
  76. var
  77. LClient: TIdUDPClient;
  78. LData: TIdBytes;
  79. i: Integer;
  80. begin
  81. inherited DoUDPRead(AThread, AData, ABinding);
  82. DoRequestNotify;
  83. LClient := TIdUDPClient.Create(nil);
  84. try
  85. LClient.Host := FMappedHost;
  86. LClient.Port := FMappedPort;
  87. LClient.SendBuffer(AData);
  88. SetLength(LData, LClient.BufferSize);
  89. i := LClient.ReceiveBuffer(LData);
  90. if i > 0 then begin
  91. SetLength(LData, i);
  92. ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, LData, 0, i, ABinding.IPVersion);
  93. end;
  94. finally
  95. FreeAndNil(LClient);
  96. end;
  97. end;
  98. end.