NATPunchtrough.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Sample.h"
  24. namespace Urho3D
  25. {
  26. class Button;
  27. class LineEdit;
  28. class Text;
  29. class UIElement;
  30. }
  31. const int SERVER_PORT = 54654;
  32. /// Chat example
  33. /// This sample demonstrates:
  34. /// - Starting up a network server or connecting to it
  35. /// - Implementing simple chat functionality with network messages
  36. class NATPunchtrough : public Sample
  37. {
  38. URHO3D_OBJECT(NATPunchtrough, Sample);
  39. public:
  40. /// Construct.
  41. explicit NATPunchtrough(Context* context);
  42. /// Setup after engine initialization and before running the main loop.
  43. void Start() override;
  44. protected:
  45. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  46. String GetScreenJoystickPatchString() const override { return
  47. "<patch>"
  48. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">"
  49. " <attribute name=\"Is Visible\" value=\"false\" />"
  50. " </add>"
  51. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  52. " <attribute name=\"Is Visible\" value=\"false\" />"
  53. " </add>"
  54. "</patch>";
  55. }
  56. private:
  57. /// Create the UI.
  58. void CreateUI();
  59. /// Subscribe to log message, UI and network events.
  60. void SubscribeToEvents();
  61. /// Create a button to the button container.
  62. Button* CreateButton(const String& text, int width, IntVector2 position);
  63. /// Create label
  64. void CreateLabel(const String& text, IntVector2 pos);
  65. /// Create input field
  66. LineEdit* CreateLineEdit(const String& placeholder, int width, IntVector2 pos);
  67. /// Print log message.
  68. void ShowLogMessage(const String& row);
  69. /// Save NAT server config
  70. void HandleSaveNatSettings(StringHash eventType, VariantMap& eventData);
  71. /// Handle server connection message
  72. void HandleServerConnected(StringHash eventType, VariantMap& eventData);
  73. /// Handle server disconnect message
  74. void HandleServerDisconnected(StringHash eventType, VariantMap& eventData);
  75. /// Handle failed connection
  76. void HandleConnectFailed(StringHash eventType, VariantMap& eventData);
  77. /// Start server
  78. void HandleStartServer(StringHash eventType, VariantMap& eventData);
  79. /// Attempt connecting using NAT punchtrough
  80. void HandleConnect(StringHash eventType, VariantMap& eventData);
  81. /// Handle NAT master server failed connection
  82. void HandleNatConnectionFailed(StringHash eventType, VariantMap& eventData);
  83. /// Handle NAT master server disconnected
  84. void HandleNatDisconnected(StringHash eventType, VariantMap& eventData);
  85. /// Handle NAT master server succesfull connection
  86. void HandleNatConnectionSucceeded(StringHash eventType, VariantMap& eventData);
  87. /// Handle NAT punchtrough success message
  88. void HandleNatPunchtroughSucceeded(StringHash eventType, VariantMap& eventData);
  89. /// Handle failed NAT punchtrough message
  90. void HandleNatPunchtroughFailed(StringHash eventType, VariantMap& eventData);
  91. /// Handle client connecting
  92. void HandleClientConnected(StringHash eventType, VariantMap& eventData);
  93. /// Handle client disconnecting
  94. void HandleClientDisconnected(StringHash eventType, VariantMap& eventData);
  95. /// NAT master server address
  96. SharedPtr<LineEdit> natServerAddress_;
  97. /// NAT master server port
  98. SharedPtr<LineEdit> natServerPort_;
  99. /// Save NAT settings button
  100. SharedPtr<Button> saveNatSettingsButton_;
  101. /// Start server button
  102. SharedPtr<Button> startServerButton_;
  103. /// Remote server GUID input field
  104. SharedPtr<LineEdit> serverGuid_;
  105. /// Connect button
  106. SharedPtr<Button> connectButton_;
  107. /// Log history text element
  108. SharedPtr<Text> logHistoryText_;
  109. /// Log messages
  110. Vector<String> logHistory_;
  111. /// Created server GUID field
  112. SharedPtr<LineEdit> guid_;
  113. };