juce_InterprocessConnectionServer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. An object that waits for client sockets to connect to a port on this host, and
  22. creates InterprocessConnection objects for each one.
  23. To use this, create a class derived from it which implements the createConnectionObject()
  24. method, so that it creates suitable connection objects for each client that tries
  25. to connect.
  26. @see InterprocessConnection
  27. @tags{Events}
  28. */
  29. class JUCE_API InterprocessConnectionServer : private Thread
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an uninitialised server object.
  34. */
  35. InterprocessConnectionServer();
  36. /** Destructor. */
  37. ~InterprocessConnectionServer() override;
  38. //==============================================================================
  39. /** Starts an internal thread which listens on the given port number.
  40. While this is running, if another process tries to connect with the
  41. InterprocessConnection::connectToSocket() method, this object will call
  42. createConnectionObject() to create a connection to that client.
  43. Use stop() to stop the thread running.
  44. @param portNumber The port on which the server will receive
  45. connections
  46. @param bindAddress The address on which the server will listen
  47. for connections. An empty string indicates
  48. that it should listen on all addresses
  49. assigned to this machine.
  50. @see createConnectionObject, stop
  51. */
  52. bool beginWaitingForSocket (int portNumber, const String& bindAddress = String());
  53. /** Terminates the listener thread, if it's active.
  54. @see beginWaitingForSocket
  55. */
  56. void stop();
  57. /** Returns the local port number to which this server is currently bound.
  58. This is useful if you need to know to which port the OS has actually bound your
  59. socket when calling beginWaitingForSocket with a port number of zero.
  60. Returns -1 if the function fails.
  61. */
  62. int getBoundPort() const noexcept;
  63. protected:
  64. /** Creates a suitable connection object for a client process that wants to
  65. connect to this one.
  66. This will be called by the listener thread when a client process tries
  67. to connect, and must return a new InterprocessConnection object that will
  68. then run as this end of the connection.
  69. @see InterprocessConnection
  70. */
  71. virtual InterprocessConnection* createConnectionObject() = 0;
  72. private:
  73. //==============================================================================
  74. std::unique_ptr<StreamingSocket> socket;
  75. void run() override;
  76. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InterprocessConnectionServer)
  77. };
  78. } // namespace juce