announce_multiplayer_session.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <functional>
  6. #include <memory>
  7. #include <mutex>
  8. #include <set>
  9. #include <thread>
  10. #include "common/announce_multiplayer_room.h"
  11. #include "common/common_types.h"
  12. #include "common/thread.h"
  13. namespace Network {
  14. class Room;
  15. class RoomNetwork;
  16. } // namespace Network
  17. namespace Core {
  18. /**
  19. * Instruments AnnounceMultiplayerRoom::Backend.
  20. * Creates a thread that regularly updates the room information and submits them
  21. * An async get of room information is also possible
  22. */
  23. class AnnounceMultiplayerSession {
  24. public:
  25. using CallbackHandle = std::shared_ptr<std::function<void(const WebService::WebResult&)>>;
  26. AnnounceMultiplayerSession(Network::RoomNetwork& room_network_);
  27. ~AnnounceMultiplayerSession();
  28. /**
  29. * Allows to bind a function that will get called if the announce encounters an error
  30. * @param function The function that gets called
  31. * @return A handle that can be used the unbind the function
  32. */
  33. CallbackHandle BindErrorCallback(std::function<void(const WebService::WebResult&)> function);
  34. /**
  35. * Unbind a function from the error callbacks
  36. * @param handle The handle for the function that should get unbind
  37. */
  38. void UnbindErrorCallback(CallbackHandle handle);
  39. /**
  40. * Registers a room to web services
  41. * @return The result of the registration attempt.
  42. */
  43. WebService::WebResult Register();
  44. /**
  45. * Starts the announce of a room to web services
  46. */
  47. void Start();
  48. /**
  49. * Stops the announce to web services
  50. */
  51. void Stop();
  52. /**
  53. * Returns a list of all room information the backend got
  54. * @param func A function that gets executed when the async get finished, e.g. a signal
  55. * @return a list of rooms received from the web service
  56. */
  57. AnnounceMultiplayerRoom::RoomList GetRoomList();
  58. /**
  59. * Whether the announce session is still running
  60. */
  61. bool IsRunning() const;
  62. /**
  63. * Recreates the backend, updating the credentials.
  64. * This can only be used when the announce session is not running.
  65. */
  66. void UpdateCredentials();
  67. private:
  68. void UpdateBackendData(std::shared_ptr<Network::Room> room);
  69. void AnnounceMultiplayerLoop();
  70. Common::Event shutdown_event;
  71. std::mutex callback_mutex;
  72. std::set<CallbackHandle> error_callbacks;
  73. std::unique_ptr<std::thread> announce_multiplayer_thread;
  74. /// Backend interface that logs fields
  75. std::unique_ptr<AnnounceMultiplayerRoom::Backend> backend;
  76. std::atomic_bool registered = false; ///< Whether the room has been registered
  77. Network::RoomNetwork& room_network;
  78. };
  79. } // namespace Core