OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 BASE_ASYNC_SOCKET_IO_HANDLER_H_ | 5 #ifndef BASE_ASYNC_SOCKET_IO_HANDLER_H_ |
6 #define BASE_ASYNC_SOCKET_IO_HANDLER_H_ | 6 #define BASE_ASYNC_SOCKET_IO_HANDLER_H_ |
7 | 7 |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/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" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 // The message loop callback interface is different based on platforms. | |
15 #if defined(OS_WIN) | |
16 typedef base::MessageLoopForIO::IOHandler MessageLoopIOHandler; | |
17 #elif defined(OS_POSIX) | |
18 typedef base::MessageLoopForIO::Watcher MessageLoopIOHandler; | |
19 #endif | |
20 | |
21 // Extends the CancelableSyncSocket class to allow reading from a socket | 14 // Extends the CancelableSyncSocket class to allow reading from a socket |
22 // asynchronously on a TYPE_IO message loop thread. This makes it easy to share | 15 // asynchronously on a TYPE_IO message loop thread. This makes it easy to share |
23 // a thread that uses a message loop (e.g. for IPC and other things) and not | 16 // a thread that uses a message loop (e.g. for IPC and other things) and not |
24 // require a separate thread to read from the socket. | 17 // require a separate thread to read from the socket. |
25 // | 18 // |
26 // Example usage (also see the unit tests): | 19 // Example usage (also see the unit tests): |
27 // | 20 // |
28 // class SocketReader { | 21 // class SocketReader { |
29 // public: | 22 // public: |
30 // SocketReader(base::CancelableSyncSocket* socket) | 23 // SocketReader(base::CancelableSyncSocket* socket) |
(...skipping 15 matching lines...) Expand all Loading... | |
46 // } | 39 // } |
47 // } | 40 // } |
48 // | 41 // |
49 // base::AsyncSocketIoHandler io_handler; | 42 // base::AsyncSocketIoHandler io_handler; |
50 // base::CancelableSyncSocket* socket_; | 43 // base::CancelableSyncSocket* socket_; |
51 // char buffer_[kBufferSize]; | 44 // char buffer_[kBufferSize]; |
52 // }; | 45 // }; |
53 // | 46 // |
54 class BASE_EXPORT AsyncSocketIoHandler | 47 class BASE_EXPORT AsyncSocketIoHandler |
55 : public NON_EXPORTED_BASE(base::NonThreadSafe), | 48 : public NON_EXPORTED_BASE(base::NonThreadSafe), |
56 public NON_EXPORTED_BASE(MessageLoopIOHandler) { | 49 // 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 | |
57 public: | 55 public: |
58 AsyncSocketIoHandler(); | 56 AsyncSocketIoHandler(); |
59 virtual ~AsyncSocketIoHandler(); | 57 virtual ~AsyncSocketIoHandler(); |
60 | 58 |
61 // Type definition for the callback. The parameter tells how many | 59 // Type definition for the callback. The parameter tells how many |
62 // bytes were read and is 0 if an error occurred. | 60 // bytes were read and is 0 if an error occurred. |
63 typedef base::Callback<void(int)> ReadCompleteCallback; | 61 typedef base::Callback<void(int)> ReadCompleteCallback; |
64 | 62 |
65 // Initializes the AsyncSocketIoHandler by hooking it up to the current | 63 // Initializes the AsyncSocketIoHandler by hooking it up to the current |
66 // thread's message loop (must be TYPE_IO), to do async reads from the socket | 64 // thread's message loop (must be TYPE_IO), to do async reads from the socket |
67 // on the current thread. The |callback| will be invoked whenever a Read() | 65 // on the current thread. The |callback| will be invoked whenever a Read() |
68 // has completed. | 66 // has completed. |
69 bool Initialize(base::SyncSocket::Handle socket, | 67 bool Initialize(base::SyncSocket::Handle socket, |
70 const ReadCompleteCallback& callback); | 68 const ReadCompleteCallback& callback); |
71 | 69 |
72 // Attempts to read from the socket. The return value will be |false| | 70 // Attempts to read from the socket. The return value will be |false| |
73 // if an error occurred and |true| if data was read or a pending read | 71 // if an error occurred and |true| if data was read or a pending read |
74 // was issued. Regardless of async or sync operation, the | 72 // was issued. Regardless of async or sync operation, the |
75 // ReadCompleteCallback (see above) will be called when data is available. | 73 // ReadCompleteCallback (see above) will be called when data is available. |
76 bool Read(char* buffer, int buffer_len); | 74 bool Read(char* buffer, int buffer_len); |
77 | 75 |
78 private: | 76 private: |
79 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
darin (slow to review)
2013/08/29 01:37:16
Another approach would have been to create an inne
| |
80 // Implementation of IOHandler on Windows. | 78 // Implementation of IOHandler on Windows. |
81 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | 79 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, |
82 DWORD bytes_transfered, | 80 DWORD bytes_transfered, |
83 DWORD error) OVERRIDE; | 81 DWORD error) OVERRIDE; |
84 #elif defined(OS_POSIX) | 82 #elif defined(OS_POSIX) |
85 // Implementation of base::MessageLoopForIO::Watcher. | 83 // Implementation of base::MessageLoopForIO::Watcher. |
86 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {} | 84 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {} |
87 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE; | 85 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE; |
88 | 86 |
89 void EnsureWatchingSocket(); | 87 void EnsureWatchingSocket(); |
(...skipping 13 matching lines...) Expand all Loading... | |
103 bool is_watching_; | 101 bool is_watching_; |
104 #endif | 102 #endif |
105 ReadCompleteCallback read_complete_; | 103 ReadCompleteCallback read_complete_; |
106 | 104 |
107 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler); | 105 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler); |
108 }; | 106 }; |
109 | 107 |
110 } // namespace base. | 108 } // namespace base. |
111 | 109 |
112 #endif // BASE_ASYNC_SOCKET_IO_HANDLER_H_ | 110 #endif // BASE_ASYNC_SOCKET_IO_HANDLER_H_ |
OLD | NEW |