| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_ | 5 #ifndef MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_ |
| 6 #define MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_ | 6 #define MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_ |
| 7 | 7 |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/sync_socket.h" | 9 #include "base/sync_socket.h" |
| 10 #include "base/threading/non_thread_safe.h" | 10 #include "base/threading/non_thread_safe.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // asynchronously on a TYPE_IO message loop thread. This makes it easy to share | 23 // asynchronously on a TYPE_IO message loop thread. This makes it easy to share |
| 24 // a thread that uses a message loop (e.g. for IPC and other things) and not | 24 // a thread that uses a message loop (e.g. for IPC and other things) and not |
| 25 // require a separate thread to read from the socket. | 25 // require a separate thread to read from the socket. |
| 26 // | 26 // |
| 27 // Example usage (also see the unit tests): | 27 // Example usage (also see the unit tests): |
| 28 // | 28 // |
| 29 // class SocketReader { | 29 // class SocketReader { |
| 30 // public: | 30 // public: |
| 31 // SocketReader(base::CancelableSyncSocket* socket) | 31 // SocketReader(base::CancelableSyncSocket* socket) |
| 32 // : socket_(socket), buffer_() { | 32 // : socket_(socket), buffer_() { |
| 33 // io_handler.Initialize(socket_->handle()); | 33 // io_handler.Initialize(socket_->handle(), |
| 34 // base::Bind(&SocketReader::OnDataAvailable, |
| 35 // base::Unretained(this)); |
| 34 // } | 36 // } |
| 35 // | 37 // |
| 36 // void AsyncRead() { | 38 // void AsyncRead() { |
| 37 // CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_), | 39 // CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_))); |
| 38 // base::Bind(&SocketReader::OnDataAvailable, | |
| 39 // base::Unretained(this))); | |
| 40 // } | 40 // } |
| 41 // | 41 // |
| 42 // private: | 42 // private: |
| 43 // void OnDataAvailable(int bytes_read) { | 43 // void OnDataAvailable(int bytes_read) { |
| 44 // ProcessData(&buffer_[0], bytes_read); | 44 // if (ProcessData(&buffer_[0], bytes_read)) { |
| 45 // // Issue another read. |
| 46 // CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_))); |
| 47 // } |
| 45 // } | 48 // } |
| 46 // | 49 // |
| 47 // media::AsyncSocketIoHandler io_handler; | 50 // media::AsyncSocketIoHandler io_handler; |
| 48 // base::CancelableSyncSocket* socket_; | 51 // base::CancelableSyncSocket* socket_; |
| 49 // char buffer_[kBufferSize]; | 52 // char buffer_[kBufferSize]; |
| 50 // }; | 53 // }; |
| 51 // | 54 // |
| 52 class MEDIA_EXPORT AsyncSocketIoHandler | 55 class MEDIA_EXPORT AsyncSocketIoHandler |
| 53 : public NON_EXPORTED_BASE(base::NonThreadSafe), | 56 : public NON_EXPORTED_BASE(base::NonThreadSafe), |
| 54 public NON_EXPORTED_BASE(MessageLoopIOHandler) { | 57 public NON_EXPORTED_BASE(MessageLoopIOHandler) { |
| 55 public: | 58 public: |
| 56 AsyncSocketIoHandler(); | 59 AsyncSocketIoHandler(); |
| 57 virtual ~AsyncSocketIoHandler(); | 60 virtual ~AsyncSocketIoHandler(); |
| 58 | 61 |
| 59 // Initializes the AsyncSocketIoHandler by hooking it up to the current | |
| 60 // thread's message loop (must be TYPE_IO), to do async reads from the socket | |
| 61 // on the current thread. | |
| 62 bool Initialize(base::SyncSocket::Handle socket); | |
| 63 | |
| 64 // Type definition for the callback. The parameter tells how many | 62 // Type definition for the callback. The parameter tells how many |
| 65 // bytes were read and is 0 if an error occurred. | 63 // bytes were read and is 0 if an error occurred. |
| 66 typedef base::Callback<void(int)> ReadCompleteCallback; | 64 typedef base::Callback<void(int)> ReadCompleteCallback; |
| 67 | 65 |
| 66 // Initializes the AsyncSocketIoHandler by hooking it up to the current |
| 67 // thread's message loop (must be TYPE_IO), to do async reads from the socket |
| 68 // on the current thread. The |callback| will be invoked whenever a Read() |
| 69 // has completed. |
| 70 bool Initialize(base::SyncSocket::Handle socket, |
| 71 const ReadCompleteCallback& callback); |
| 72 |
| 68 // Attempts to read from the socket. The return value will be |false| | 73 // Attempts to read from the socket. The return value will be |false| |
| 69 // if an error occurred and |true| if data was read or a pending read | 74 // if an error occurred and |true| if data was read or a pending read |
| 70 // was issued. Regardless of async or sync operation, the callback will | 75 // was issued. Regardless of async or sync operation, the |
| 71 // be called when data is available. | 76 // ReadCompleteCallback (see above) will be called when data is available. |
| 72 bool Read(char* buffer, int buffer_len, | 77 bool Read(char* buffer, int buffer_len); |
| 73 const ReadCompleteCallback& callback); | |
| 74 | 78 |
| 75 private: | 79 private: |
| 76 #if defined(OS_WIN) | 80 #if defined(OS_WIN) |
| 77 // Implementation of IOHandler on Windows. | 81 // Implementation of IOHandler on Windows. |
| 78 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 82 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 79 DWORD bytes_transfered, | 83 DWORD bytes_transfered, |
| 80 DWORD error) OVERRIDE; | 84 DWORD error) OVERRIDE; |
| 81 #elif defined(OS_POSIX) | 85 #elif defined(OS_POSIX) |
| 82 // Implementation of MessageLoopForIO::Watcher. | 86 // Implementation of MessageLoopForIO::Watcher. |
| 83 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {} | 87 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {} |
| 84 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE; | 88 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE; |
| 85 | 89 |
| 86 void EnsureWatchingSocket(); | 90 void EnsureWatchingSocket(); |
| 87 #endif | 91 #endif |
| 88 | 92 |
| 89 base::SyncSocket::Handle socket_; | 93 base::SyncSocket::Handle socket_; |
| 90 #if defined(OS_WIN) | 94 #if defined(OS_WIN) |
| 91 MessageLoopForIO::IOContext* context_; | 95 MessageLoopForIO::IOContext* context_; |
| 96 bool is_pending_; |
| 92 #elif defined(OS_POSIX) | 97 #elif defined(OS_POSIX) |
| 93 MessageLoopForIO::FileDescriptorWatcher socket_watcher_; | 98 MessageLoopForIO::FileDescriptorWatcher socket_watcher_; |
| 94 // |pending_buffer_| and |pending_buffer_len_| are valid only between | 99 // |pending_buffer_| and |pending_buffer_len_| are valid only between |
| 95 // Read() and OnFileCanReadWithoutBlocking(). | 100 // Read() and OnFileCanReadWithoutBlocking(). |
| 96 char* pending_buffer_; | 101 char* pending_buffer_; |
| 97 int pending_buffer_len_; | 102 int pending_buffer_len_; |
| 98 // |true| iff the message loop is watching the socket for IO events. | 103 // |true| iff the message loop is watching the socket for IO events. |
| 99 bool is_watching_; | 104 bool is_watching_; |
| 100 #endif | 105 #endif |
| 101 ReadCompleteCallback read_complete_; | 106 ReadCompleteCallback read_complete_; |
| 102 | 107 |
| 103 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler); | 108 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler); |
| 104 }; | 109 }; |
| 105 | 110 |
| 106 } // namespace media. | 111 } // namespace media. |
| 107 | 112 |
| 108 #endif // MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_ | 113 #endif // MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_ |
| OLD | NEW |