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" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // } | 39 // } |
40 // } | 40 // } |
41 // | 41 // |
42 // base::AsyncSocketIoHandler io_handler; | 42 // base::AsyncSocketIoHandler io_handler; |
43 // base::CancelableSyncSocket* socket_; | 43 // base::CancelableSyncSocket* socket_; |
44 // char buffer_[kBufferSize]; | 44 // char buffer_[kBufferSize]; |
45 // }; | 45 // }; |
46 // | 46 // |
47 class BASE_EXPORT AsyncSocketIoHandler | 47 class BASE_EXPORT AsyncSocketIoHandler |
48 : public NON_EXPORTED_BASE(base::NonThreadSafe), | 48 : public NON_EXPORTED_BASE(base::NonThreadSafe), |
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) { | 49 public NON_EXPORTED_BASE(base::MessageLoopForIO::Watcher) { |
54 #endif | |
55 public: | 50 public: |
56 AsyncSocketIoHandler(); | 51 AsyncSocketIoHandler(); |
57 ~AsyncSocketIoHandler() override; | 52 ~AsyncSocketIoHandler() override; |
58 | 53 |
59 // Type definition for the callback. The parameter tells how many | 54 // Type definition for the callback. The parameter tells how many |
60 // bytes were read and is 0 if an error occurred. | 55 // bytes were read and is 0 if an error occurred. |
61 typedef base::Callback<void(int)> ReadCompleteCallback; | 56 typedef base::Callback<void(int)> ReadCompleteCallback; |
62 | 57 |
63 // Initializes the AsyncSocketIoHandler by hooking it up to the current | 58 // 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 | 59 // 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() | 60 // on the current thread. The |callback| will be invoked whenever a Read() |
66 // has completed. | 61 // has completed. |
67 bool Initialize(base::SyncSocket::Handle socket, | 62 bool Initialize(base::SyncSocket::Handle socket, |
68 const ReadCompleteCallback& callback); | 63 const ReadCompleteCallback& callback); |
69 | 64 |
70 // Attempts to read from the socket. The return value will be |false| | 65 // 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 | 66 // if an error occurred and |true| if data was read or a pending read |
72 // was issued. Regardless of async or sync operation, the | 67 // was issued. Regardless of async or sync operation, the |
73 // ReadCompleteCallback (see above) will be called when data is available. | 68 // ReadCompleteCallback (see above) will be called when data is available. |
74 bool Read(char* buffer, int buffer_len); | 69 bool Read(char* buffer, int buffer_len); |
75 | 70 |
76 private: | 71 private: |
77 #if defined(OS_WIN) | |
78 // Implementation of IOHandler on Windows. | |
79 void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | |
80 DWORD bytes_transfered, | |
81 DWORD error) override; | |
82 #elif defined(OS_POSIX) | |
83 // Implementation of base::MessageLoopForIO::Watcher. | 72 // Implementation of base::MessageLoopForIO::Watcher. |
| 73 #if defined(OS_POSIX) |
84 void OnFileCanWriteWithoutBlocking(int socket) override {} | 74 void OnFileCanWriteWithoutBlocking(int socket) override {} |
85 void OnFileCanReadWithoutBlocking(int socket) override; | 75 void OnFileCanReadWithoutBlocking(int socket) override; |
86 | 76 |
87 void EnsureWatchingSocket(); | 77 void EnsureWatchingSocket(); |
88 #endif | 78 #endif |
89 | 79 |
90 base::SyncSocket::Handle socket_; | 80 base::SyncSocket::Handle socket_; |
91 #if defined(OS_WIN) | 81 #if defined(OS_POSIX) |
92 base::MessageLoopForIO::IOContext* context_; | |
93 bool is_pending_; | |
94 #elif defined(OS_POSIX) | |
95 base::MessageLoopForIO::FileDescriptorWatcher socket_watcher_; | 82 base::MessageLoopForIO::FileDescriptorWatcher socket_watcher_; |
96 // |pending_buffer_| and |pending_buffer_len_| are valid only between | 83 // |pending_buffer_| and |pending_buffer_len_| are valid only between |
97 // Read() and OnFileCanReadWithoutBlocking(). | 84 // Read() and OnFileCanReadWithoutBlocking(). |
98 char* pending_buffer_; | 85 char* pending_buffer_; |
99 int pending_buffer_len_; | 86 int pending_buffer_len_; |
100 // |true| iff the message loop is watching the socket for IO events. | 87 // |true| iff the message loop is watching the socket for IO events. |
101 bool is_watching_; | 88 bool is_watching_; |
102 #endif | 89 #endif |
103 ReadCompleteCallback read_complete_; | 90 ReadCompleteCallback read_complete_; |
104 | 91 |
105 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler); | 92 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler); |
106 }; | 93 }; |
107 | 94 |
108 } // namespace base. | 95 } // namespace base. |
109 | 96 |
110 #endif // BASE_ASYNC_SOCKET_IO_HANDLER_H_ | 97 #endif // BASE_ASYNC_SOCKET_IO_HANDLER_H_ |
OLD | NEW |