blob: 2f4b13d74a1c4a851910b6f9a3216b6d59cf2152 [file] [log] [blame]
[email protected]39cb64f2013-08-22 12:39:331// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]a0e3f0f2012-06-07 12:45:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]39cb64f2013-08-22 12:39:335#ifndef BASE_ASYNC_SOCKET_IO_HANDLER_H_
6#define BASE_ASYNC_SOCKET_IO_HANDLER_H_
[email protected]a0e3f0f2012-06-07 12:45:517
[email protected]5821fa0e2013-07-18 04:32:358#include "base/message_loop/message_loop.h"
[email protected]a0e3f0f2012-06-07 12:45:519#include "base/sync_socket.h"
10#include "base/threading/non_thread_safe.h"
[email protected]a0e3f0f2012-06-07 12:45:5111
[email protected]f1b96af32013-08-23 15:56:3312namespace base {
[email protected]a0e3f0f2012-06-07 12:45:5113
[email protected]a0e3f0f2012-06-07 12:45:5114// Extends the CancelableSyncSocket class to allow reading from a socket
15// asynchronously on a TYPE_IO message loop thread. This makes it easy to share
16// a thread that uses a message loop (e.g. for IPC and other things) and not
17// require a separate thread to read from the socket.
18//
19// Example usage (also see the unit tests):
20//
21// class SocketReader {
22// public:
23// SocketReader(base::CancelableSyncSocket* socket)
24// : socket_(socket), buffer_() {
[email protected]26195ea2012-07-04 13:34:2125// io_handler.Initialize(socket_->handle(),
26// base::Bind(&SocketReader::OnDataAvailable,
27// base::Unretained(this));
[email protected]a0e3f0f2012-06-07 12:45:5128// }
29//
30// void AsyncRead() {
[email protected]26195ea2012-07-04 13:34:2131// CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_)));
[email protected]a0e3f0f2012-06-07 12:45:5132// }
33//
34// private:
35// void OnDataAvailable(int bytes_read) {
[email protected]26195ea2012-07-04 13:34:2136// if (ProcessData(&buffer_[0], bytes_read)) {
37// // Issue another read.
38// CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_)));
39// }
[email protected]a0e3f0f2012-06-07 12:45:5140// }
41//
[email protected]f1b96af32013-08-23 15:56:3342// base::AsyncSocketIoHandler io_handler;
[email protected]a0e3f0f2012-06-07 12:45:5143// base::CancelableSyncSocket* socket_;
44// char buffer_[kBufferSize];
45// };
46//
[email protected]39cb64f2013-08-22 12:39:3347class BASE_EXPORT AsyncSocketIoHandler
[email protected]00994922012-06-07 12:59:5048 : public NON_EXPORTED_BASE(base::NonThreadSafe),
[email protected]b9b6b8df2013-08-28 20:13:3649// The message loop callback interface is different based on platforms.
50#if defined(OS_WIN)
51 public NON_EXPORTED_BASE(base::MessageLoopForIO::IOHandler) {
52#else
53 public NON_EXPORTED_BASE(base::MessageLoopForIO::Watcher) {
54#endif
[email protected]a0e3f0f2012-06-07 12:45:5155 public:
56 AsyncSocketIoHandler();
57 virtual ~AsyncSocketIoHandler();
58
[email protected]a0e3f0f2012-06-07 12:45:5159 // Type definition for the callback. The parameter tells how many
60 // bytes were read and is 0 if an error occurred.
61 typedef base::Callback<void(int)> ReadCompleteCallback;
62
[email protected]26195ea2012-07-04 13:34:2163 // Initializes the AsyncSocketIoHandler by hooking it up to the current
64 // thread's message loop (must be TYPE_IO), to do async reads from the socket
65 // on the current thread. The |callback| will be invoked whenever a Read()
66 // has completed.
67 bool Initialize(base::SyncSocket::Handle socket,
68 const ReadCompleteCallback& callback);
69
[email protected]a0e3f0f2012-06-07 12:45:5170 // Attempts to read from the socket. The return value will be |false|
71 // if an error occurred and |true| if data was read or a pending read
[email protected]26195ea2012-07-04 13:34:2172 // was issued. Regardless of async or sync operation, the
73 // ReadCompleteCallback (see above) will be called when data is available.
74 bool Read(char* buffer, int buffer_len);
[email protected]a0e3f0f2012-06-07 12:45:5175
76 private:
77#if defined(OS_WIN)
78 // Implementation of IOHandler on Windows.
[email protected]fb5af232013-04-22 22:40:0379 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
[email protected]a0e3f0f2012-06-07 12:45:5180 DWORD bytes_transfered,
81 DWORD error) OVERRIDE;
82#elif defined(OS_POSIX)
[email protected]fb5af232013-04-22 22:40:0383 // Implementation of base::MessageLoopForIO::Watcher.
[email protected]a0e3f0f2012-06-07 12:45:5184 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {}
85 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE;
86
87 void EnsureWatchingSocket();
88#endif
89
90 base::SyncSocket::Handle socket_;
91#if defined(OS_WIN)
[email protected]fb5af232013-04-22 22:40:0392 base::MessageLoopForIO::IOContext* context_;
[email protected]26195ea2012-07-04 13:34:2193 bool is_pending_;
[email protected]a0e3f0f2012-06-07 12:45:5194#elif defined(OS_POSIX)
[email protected]fb5af232013-04-22 22:40:0395 base::MessageLoopForIO::FileDescriptorWatcher socket_watcher_;
[email protected]a0e3f0f2012-06-07 12:45:5196 // |pending_buffer_| and |pending_buffer_len_| are valid only between
97 // Read() and OnFileCanReadWithoutBlocking().
98 char* pending_buffer_;
99 int pending_buffer_len_;
100 // |true| iff the message loop is watching the socket for IO events.
101 bool is_watching_;
102#endif
103 ReadCompleteCallback read_complete_;
104
105 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler);
106};
107
[email protected]f1b96af32013-08-23 15:56:33108} // namespace base.
[email protected]a0e3f0f2012-06-07 12:45:51109
[email protected]39cb64f2013-08-22 12:39:33110#endif // BASE_ASYNC_SOCKET_IO_HANDLER_H_