juce_InterprocessConnectionServer.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. InterprocessConnectionServer::InterprocessConnectionServer() : Thread ("JUCE IPC server")
  20. {
  21. }
  22. InterprocessConnectionServer::~InterprocessConnectionServer()
  23. {
  24. stop();
  25. }
  26. //==============================================================================
  27. bool InterprocessConnectionServer::beginWaitingForSocket (const int portNumber, const String& bindAddress)
  28. {
  29. stop();
  30. socket.reset (new StreamingSocket());
  31. if (socket->createListener (portNumber, bindAddress))
  32. {
  33. startThread();
  34. return true;
  35. }
  36. socket.reset();
  37. return false;
  38. }
  39. void InterprocessConnectionServer::stop()
  40. {
  41. signalThreadShouldExit();
  42. if (socket != nullptr)
  43. socket->close();
  44. stopThread (4000);
  45. socket.reset();
  46. }
  47. int InterprocessConnectionServer::getBoundPort() const noexcept
  48. {
  49. return (socket == nullptr) ? -1 : socket->getBoundPort();
  50. }
  51. void InterprocessConnectionServer::run()
  52. {
  53. while ((! threadShouldExit()) && socket != nullptr)
  54. {
  55. std::unique_ptr<StreamingSocket> clientSocket (socket->waitForNextConnection());
  56. if (clientSocket != nullptr)
  57. if (auto* newConnection = createConnectionObject())
  58. newConnection->initialiseWithSocket (clientSocket.release());
  59. }
  60. }
  61. } // namespace juce