sync_socket.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_SYNC_SOCKET_H_
  5. #define BASE_SYNC_SOCKET_H_
  6. // A socket abstraction used for sending and receiving plain
  7. // data. Because the receiving is blocking, they can be used to perform
  8. // rudimentary cross-process synchronization with low latency.
  9. #include <stddef.h>
  10. #include "base/base_export.h"
  11. #include "base/files/platform_file.h"
  12. #include "base/synchronization/waitable_event.h"
  13. #include "base/time/time.h"
  14. #include "build/build_config.h"
  15. #if BUILDFLAG(IS_WIN)
  16. #include <windows.h>
  17. #endif
  18. #include <sys/types.h>
  19. namespace base {
  20. class BASE_EXPORT SyncSocket {
  21. public:
  22. using Handle = PlatformFile;
  23. using ScopedHandle = ScopedPlatformFile;
  24. static const Handle kInvalidHandle;
  25. SyncSocket();
  26. // Creates a SyncSocket from a Handle.
  27. explicit SyncSocket(Handle handle);
  28. explicit SyncSocket(ScopedHandle handle);
  29. SyncSocket(const SyncSocket&) = delete;
  30. SyncSocket& operator=(const SyncSocket&) = delete;
  31. virtual ~SyncSocket();
  32. // Initializes and connects a pair of sockets.
  33. // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
  34. // return, the sockets will both be valid and connected.
  35. static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
  36. // Closes the SyncSocket.
  37. virtual void Close();
  38. // Sends the message to the remote peer of the SyncSocket.
  39. // Note it is not safe to send messages from the same socket handle by
  40. // multiple threads simultaneously.
  41. // buffer is a pointer to the data to send.
  42. // length is the length of the data to send (must be non-zero).
  43. // Returns the number of bytes sent, or 0 upon failure.
  44. virtual size_t Send(const void* buffer, size_t length);
  45. // Receives a message from an SyncSocket.
  46. // buffer is a pointer to the buffer to receive data.
  47. // length is the number of bytes of data to receive (must be non-zero).
  48. // Returns the number of bytes received, or 0 upon failure.
  49. virtual size_t Receive(void* buffer, size_t length);
  50. // Same as Receive() but only blocks for data until |timeout| has elapsed or
  51. // |buffer| |length| is exhausted. Currently only timeouts less than one
  52. // second are allowed. Return the amount of data read.
  53. virtual size_t ReceiveWithTimeout(void* buffer,
  54. size_t length,
  55. TimeDelta timeout);
  56. // Returns the number of bytes available. If non-zero, Receive() will not
  57. // not block when called.
  58. virtual size_t Peek();
  59. // Returns true if the Handle is valid, and false if it is not.
  60. bool IsValid() const;
  61. // Extracts the contained handle. Used for transferring between
  62. // processes.
  63. Handle handle() const;
  64. // Extracts and takes ownership of the contained handle.
  65. Handle Release();
  66. ScopedHandle Take();
  67. protected:
  68. ScopedHandle handle_;
  69. };
  70. // Derives from SyncSocket and adds support for shutting down the socket from
  71. // another thread while a blocking Receive or Send is being done from the
  72. // thread that owns the socket.
  73. class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
  74. public:
  75. CancelableSyncSocket();
  76. explicit CancelableSyncSocket(Handle handle);
  77. explicit CancelableSyncSocket(ScopedHandle handle);
  78. CancelableSyncSocket(const CancelableSyncSocket&) = delete;
  79. CancelableSyncSocket& operator=(const CancelableSyncSocket&) = delete;
  80. ~CancelableSyncSocket() override = default;
  81. // Initializes a pair of cancelable sockets. See documentation for
  82. // SyncSocket::CreatePair for more details.
  83. static bool CreatePair(CancelableSyncSocket* socket_a,
  84. CancelableSyncSocket* socket_b);
  85. // A way to shut down a socket even if another thread is currently performing
  86. // a blocking Receive or Send.
  87. bool Shutdown();
  88. #if BUILDFLAG(IS_WIN)
  89. // Since the Linux and Mac implementations actually use a socket, shutting
  90. // them down from another thread is pretty simple - we can just call
  91. // shutdown(). However, the Windows implementation relies on named pipes
  92. // and there isn't a way to cancel a blocking synchronous Read that is
  93. // supported on <Vista. So, for Windows only, we override these
  94. // SyncSocket methods in order to support shutting down the 'socket'.
  95. void Close() override;
  96. size_t Receive(void* buffer, size_t length) override;
  97. size_t ReceiveWithTimeout(void* buffer,
  98. size_t length,
  99. TimeDelta timeout) override;
  100. #endif
  101. // Send() is overridden to catch cases where the remote end is not responding
  102. // and we fill the local socket buffer. When the buffer is full, this
  103. // implementation of Send() will not block indefinitely as
  104. // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
  105. // Note that the socket will not be closed in this case.
  106. size_t Send(const void* buffer, size_t length) override;
  107. private:
  108. #if BUILDFLAG(IS_WIN)
  109. WaitableEvent shutdown_event_;
  110. WaitableEvent file_operation_;
  111. #endif
  112. };
  113. } // namespace base
  114. #endif // BASE_SYNC_SOCKET_H_