Home | History | Annotate | Download | only in base
      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 
      5 #ifndef BASE_SYNC_SOCKET_H_
      6 #define BASE_SYNC_SOCKET_H_
      7 
      8 // A socket abstraction used for sending and receiving plain
      9 // data.  Because the receiving is blocking, they can be used to perform
     10 // rudimentary cross-process synchronization with low latency.
     11 
     12 #include "base/basictypes.h"
     13 #if defined(OS_WIN)
     14 #include <windows.h>
     15 #endif
     16 #include <sys/types.h>
     17 
     18 #include "base/base_export.h"
     19 #include "base/compiler_specific.h"
     20 #include "base/synchronization/waitable_event.h"
     21 
     22 namespace base {
     23 
     24 class BASE_EXPORT SyncSocket {
     25  public:
     26 #if defined(OS_WIN)
     27   typedef HANDLE Handle;
     28 #else
     29   typedef int Handle;
     30 #endif
     31   static const Handle kInvalidHandle;
     32 
     33   SyncSocket();
     34 
     35   // Creates a SyncSocket from a Handle.  Used in transport.
     36   explicit SyncSocket(Handle handle) : handle_(handle)  {}
     37   virtual ~SyncSocket();
     38 
     39   // Initializes and connects a pair of sockets.
     40   // |socket_a| and |socket_b| must not hold a valid handle.  Upon successful
     41   // return, the sockets will both be valid and connected.
     42   static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
     43 
     44   // Closes the SyncSocket.  Returns true on success, false on failure.
     45   virtual bool Close();
     46 
     47   // Sends the message to the remote peer of the SyncSocket.
     48   // Note it is not safe to send messages from the same socket handle by
     49   // multiple threads simultaneously.
     50   // buffer is a pointer to the data to send.
     51   // length is the length of the data to send (must be non-zero).
     52   // Returns the number of bytes sent, or 0 upon failure.
     53   virtual size_t Send(const void* buffer, size_t length);
     54 
     55   // Receives a message from an SyncSocket.
     56   // buffer is a pointer to the buffer to receive data.
     57   // length is the number of bytes of data to receive (must be non-zero).
     58   // Returns the number of bytes received, or 0 upon failure.
     59   virtual size_t Receive(void* buffer, size_t length);
     60 
     61   // Returns the number of bytes available. If non-zero, Receive() will not
     62   // not block when called. NOTE: Some implementations cannot reliably
     63   // determine the number of bytes available so avoid using the returned
     64   // size as a promise and simply test against zero.
     65   size_t Peek();
     66 
     67   // Extracts the contained handle.  Used for transferring between
     68   // processes.
     69   Handle handle() const { return handle_; }
     70 
     71  protected:
     72   Handle handle_;
     73 
     74  private:
     75   DISALLOW_COPY_AND_ASSIGN(SyncSocket);
     76 };
     77 
     78 // Derives from SyncSocket and adds support for shutting down the socket from
     79 // another thread while a blocking Receive or Send is being done from the
     80 // thread that owns the socket.
     81 class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
     82  public:
     83   CancelableSyncSocket();
     84   explicit CancelableSyncSocket(Handle handle);
     85   virtual ~CancelableSyncSocket() {}
     86 
     87   // Initializes a pair of cancelable sockets.  See documentation for
     88   // SyncSocket::CreatePair for more details.
     89   static bool CreatePair(CancelableSyncSocket* socket_a,
     90                          CancelableSyncSocket* socket_b);
     91 
     92   // A way to shut down a socket even if another thread is currently performing
     93   // a blocking Receive or Send.
     94   bool Shutdown();
     95 
     96 #if defined(OS_WIN)
     97   // Since the Linux and Mac implementations actually use a socket, shutting
     98   // them down from another thread is pretty simple - we can just call
     99   // shutdown().  However, the Windows implementation relies on named pipes
    100   // and there isn't a way to cancel a blocking synchronous Read that is
    101   // supported on <Vista. So, for Windows only, we override these
    102   // SyncSocket methods in order to support shutting down the 'socket'.
    103   virtual bool Close() OVERRIDE;
    104   virtual size_t Receive(void* buffer, size_t length) OVERRIDE;
    105 #endif
    106 
    107   // Send() is overridden to catch cases where the remote end is not responding
    108   // and we fill the local socket buffer. When the buffer is full, this
    109   // implementation of Send() will not block indefinitely as
    110   // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
    111   // Note that the socket will not be closed in this case.
    112   virtual size_t Send(const void* buffer, size_t length) OVERRIDE;
    113 
    114  private:
    115 #if defined(OS_WIN)
    116   WaitableEvent shutdown_event_;
    117   WaitableEvent file_operation_;
    118 #endif
    119   DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
    120 };
    121 
    122 #if defined(OS_WIN) && !defined(COMPONENT_BUILD)
    123 // TODO(cpu): remove this once chrome is split in two dlls.
    124 __declspec(selectany)
    125     const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
    126 #endif
    127 
    128 }  // namespace base
    129 
    130 #endif  // BASE_SYNC_SOCKET_H_
    131