NamedPipe.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Core/Object.h"
  6. #include "../IO/AbstractFile.h"
  7. #ifdef __ANDROID__
  8. #include <SDL/SDL_rwops.h>
  9. #endif
  10. namespace Urho3D
  11. {
  12. /// Named pipe for interprocess communication.
  13. class URHO3D_API NamedPipe : public Object, public AbstractFile
  14. {
  15. URHO3D_OBJECT(NamedPipe, Object);
  16. public:
  17. /// Construct.
  18. explicit NamedPipe(Context* context);
  19. /// Construct and open in either server or client mode.
  20. NamedPipe(Context* context, const String& name, bool isServer);
  21. /// Destruct and close.
  22. ~NamedPipe() override;
  23. /// Read bytes from the pipe without blocking if there is less data available. Return number of bytes actually read.
  24. i32 Read(void* dest, i32 size) override;
  25. /// Set position. No-op for pipes.
  26. i64 Seek(i64 position) override;
  27. /// Write bytes to the pipe. Return number of bytes actually written.
  28. i32 Write(const void* data, i32 size) override;
  29. /// Return whether pipe has no data available.
  30. bool IsEof() const override;
  31. /// Not supported.
  32. void SetName(const String& name) override;
  33. /// 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.
  34. bool Open(const String& name, bool isServer);
  35. /// 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.
  36. void Close();
  37. /// Return whether is open.
  38. /// @property
  39. bool IsOpen() const;
  40. /// Return whether is in server mode.
  41. /// @property
  42. bool IsServer() const { return isServer_; }
  43. private:
  44. /// Server mode flag.
  45. bool isServer_;
  46. /// Pipe handle.
  47. #ifdef _WIN32
  48. void* handle_;
  49. #else
  50. mutable int readHandle_;
  51. mutable int writeHandle_;
  52. #endif
  53. };
  54. }