sync_socket.h 5.1 KB

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