NamedPipe.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Copyright (c) 2008-2017 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 "../Container/ArrayPtr.h"
  24. #include "../Core/Object.h"
  25. #include "../IO/AbstractFile.h"
  26. #ifdef __ANDROID__
  27. // ATOMIC BEGIN
  28. #include <SDL/include/SDL_rwops.h>
  29. // ATOMIC END
  30. #endif
  31. namespace Atomic
  32. {
  33. /// Named pipe for interprocess communication.
  34. class ATOMIC_API NamedPipe : public Object, public AbstractFile
  35. {
  36. ATOMIC_OBJECT(NamedPipe, Object);
  37. public:
  38. /// Construct.
  39. NamedPipe(Context* context);
  40. /// Construct and open in either server or client mode.
  41. NamedPipe(Context* context, const String& pipeName, bool isServer);
  42. /// Destruct and close.
  43. virtual ~NamedPipe();
  44. /// Read bytes from the pipe without blocking if there is less data available. Return number of bytes actually read.
  45. virtual unsigned Read(void* dest, unsigned size);
  46. /// Set position. No-op for pipes.
  47. virtual unsigned Seek(unsigned position);
  48. /// Write bytes to the pipe. Return number of bytes actually written.
  49. virtual unsigned Write(const void* data, unsigned size);
  50. /// Return whether pipe has no data available.
  51. virtual bool IsEof() const;
  52. /// Return the pipe name.
  53. virtual const String& GetName() const { return pipeName_; }
  54. /// Open the pipe in either server or client mode. If already open, the existing pipe is closed. For a client end to open successfully the server end must already to be open. Return true if successful.
  55. bool Open(const String& pipeName, bool isServer);
  56. /// Close the pipe. Note that once a client has disconnected, the server needs to close and reopen the pipe so that another client can connect. At least on Windows this is not possible to detect automatically, so the communication protocol should include a "bye" message to handle this situation.
  57. void Close();
  58. /// Return whether is open.
  59. bool IsOpen() const;
  60. /// Return whether is in server mode.
  61. bool IsServer() const { return isServer_; }
  62. private:
  63. /// Pipe name.
  64. String pipeName_;
  65. /// Server mode flag.
  66. bool isServer_;
  67. /// Pipe handle.
  68. #ifdef _WIN32
  69. void* handle_;
  70. #else
  71. mutable int readHandle_;
  72. mutable int writeHandle_;
  73. #endif
  74. };
  75. }