server_port.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "core/hle/kernel/object.h"
  11. #include "core/hle/kernel/wait_object.h"
  12. #include "core/hle/result.h"
  13. namespace Kernel {
  14. class ClientPort;
  15. class KernelCore;
  16. class ServerSession;
  17. class SessionRequestHandler;
  18. class ServerPort final : public WaitObject {
  19. public:
  20. using HLEHandler = std::shared_ptr<SessionRequestHandler>;
  21. using PortPair = std::pair<SharedPtr<ServerPort>, SharedPtr<ClientPort>>;
  22. /**
  23. * Creates a pair of ServerPort and an associated ClientPort.
  24. *
  25. * @param kernel The kernel instance to create the port pair under.
  26. * @param max_sessions Maximum number of sessions to the port
  27. * @param name Optional name of the ports
  28. * @return The created port tuple
  29. */
  30. static PortPair CreatePortPair(KernelCore& kernel, u32 max_sessions,
  31. std::string name = "UnknownPort");
  32. std::string GetTypeName() const override {
  33. return "ServerPort";
  34. }
  35. std::string GetName() const override {
  36. return name;
  37. }
  38. static const HandleType HANDLE_TYPE = HandleType::ServerPort;
  39. HandleType GetHandleType() const override {
  40. return HANDLE_TYPE;
  41. }
  42. /**
  43. * Accepts a pending incoming connection on this port. If there are no pending sessions, will
  44. * return ERR_NO_PENDING_SESSIONS.
  45. */
  46. ResultVal<SharedPtr<ServerSession>> Accept();
  47. /// Whether or not this server port has an HLE handler available.
  48. bool HasHLEHandler() const {
  49. return hle_handler != nullptr;
  50. }
  51. /// Gets the HLE handler for this port.
  52. HLEHandler GetHLEHandler() const {
  53. return hle_handler;
  54. }
  55. /**
  56. * Sets the HLE handler template for the port. ServerSessions crated by connecting to this port
  57. * will inherit a reference to this handler.
  58. */
  59. void SetHleHandler(HLEHandler hle_handler_) {
  60. hle_handler = std::move(hle_handler_);
  61. }
  62. /// Appends a ServerSession to the collection of ServerSessions
  63. /// waiting to be accepted by this port.
  64. void AppendPendingSession(SharedPtr<ServerSession> pending_session);
  65. bool ShouldWait(const Thread* thread) const override;
  66. void Acquire(Thread* thread) override;
  67. private:
  68. explicit ServerPort(KernelCore& kernel);
  69. ~ServerPort() override;
  70. /// ServerSessions waiting to be accepted by the port
  71. std::vector<SharedPtr<ServerSession>> pending_sessions;
  72. /// This session's HLE request handler template (optional)
  73. /// ServerSessions created from this port inherit a reference to this handler.
  74. HLEHandler hle_handler;
  75. /// Name of the port (optional)
  76. std::string name;
  77. };
  78. } // namespace Kernel