LANDiscovery.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 LANDiscovery : public Sample
  37. {
  38. URHO3D_OBJECT(LANDiscovery, Sample);
  39. public:
  40. /// Construct.
  41. explicit LANDiscovery(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. Text* CreateLabel(const String& text, IntVector2 pos);
  65. /// Handle found LAN server
  66. void HandleNetworkHostDiscovered(StringHash eventType, VariantMap& eventData);
  67. /// Start server
  68. void HandleStartServer(StringHash eventType, VariantMap& eventData);
  69. /// Stop server
  70. void HandleStopServer(StringHash eventType, VariantMap& eventData);
  71. /// Start network discovery
  72. void HandleDoNetworkDiscovery(StringHash eventType, VariantMap& eventData);
  73. /// Start server
  74. SharedPtr<Button> startServer_;
  75. /// Stop server
  76. SharedPtr<Button> stopServer_;
  77. /// Redo LAN discovery
  78. SharedPtr<Button> refreshServerList_;
  79. /// Found server list
  80. SharedPtr<Text> serverList_;
  81. };