OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 5 #ifndef REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
6 #define REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 6 #define REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
| 10 #include "base/callback.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
12 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
13 #include "net/socket/socket.h" | 14 #include "net/socket/socket.h" |
14 | 15 |
15 class Task; | 16 class Task; |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 class MessageLoopProxy; | 19 class MessageLoopProxy; |
19 } // namespace base | 20 } // namespace base |
(...skipping 10 matching lines...) Expand all Loading... |
30 // that implements base functionality common for streams and datagrams. | 31 // that implements base functionality common for streams and datagrams. |
31 // These classes are particularly useful when data comes from a thread | 32 // These classes are particularly useful when data comes from a thread |
32 // that doesn't own the socket, as Write() can be called from any thread. | 33 // that doesn't own the socket, as Write() can be called from any thread. |
33 // Whenever new data is written it is just put in the queue, and then written | 34 // Whenever new data is written it is just put in the queue, and then written |
34 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() | 35 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() |
35 // can be used to throttle writes. | 36 // can be used to throttle writes. |
36 | 37 |
37 class BufferedSocketWriterBase | 38 class BufferedSocketWriterBase |
38 : public base::RefCountedThreadSafe<BufferedSocketWriterBase> { | 39 : public base::RefCountedThreadSafe<BufferedSocketWriterBase> { |
39 public: | 40 public: |
40 typedef Callback1<int>::Type WriteFailedCallback; | 41 typedef base::Callback<void(int)> WriteFailedCallback; |
41 | 42 |
42 explicit BufferedSocketWriterBase(base::MessageLoopProxy* message_loop); | 43 explicit BufferedSocketWriterBase(base::MessageLoopProxy* message_loop); |
43 virtual ~BufferedSocketWriterBase(); | 44 virtual ~BufferedSocketWriterBase(); |
44 | 45 |
45 // Initializes the writer. Must be called on the thread that will be used | 46 // Initializes the writer. Must be called on the thread that will be used |
46 // to access the socket in the future. |callback| will be called after each | 47 // to access the socket in the future. |callback| will be called after each |
47 // failed write. Caller retains ownership of |socket|. | 48 // failed write. Caller retains ownership of |socket|. |
48 // TODO(sergeyu): Change it so that it take ownership of |socket|. | 49 // TODO(sergeyu): Change it so that it take ownership of |socket|. |
49 void Init(net::Socket* socket, WriteFailedCallback* callback); | 50 void Init(net::Socket* socket, const WriteFailedCallback& callback); |
50 | 51 |
51 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue | 52 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue |
52 // the data if called before Init(). Can be called on any thread. | 53 // the data if called before Init(). Can be called on any thread. |
53 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, Task* done_task); | 54 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, |
| 55 const base::Closure& done_task); |
54 | 56 |
55 // Returns current size of the buffer. Can be called on any thread. | 57 // Returns current size of the buffer. Can be called on any thread. |
56 int GetBufferSize(); | 58 int GetBufferSize(); |
57 | 59 |
58 // Returns number of chunks that are currently in the buffer waiting | 60 // Returns number of chunks that are currently in the buffer waiting |
59 // to be written. Can be called on any thread. | 61 // to be written. Can be called on any thread. |
60 int GetBufferChunks(); | 62 int GetBufferChunks(); |
61 | 63 |
62 // Stops writing and drops current buffers. Must be called on the | 64 // Stops writing and drops current buffers. Must be called on the |
63 // network thread. | 65 // network thread. |
(...skipping 24 matching lines...) Expand all Loading... |
88 void OnWritten(int result); | 90 void OnWritten(int result); |
89 | 91 |
90 // This method is called when an error is encountered. | 92 // This method is called when an error is encountered. |
91 void HandleError(int result); | 93 void HandleError(int result); |
92 | 94 |
93 // Must be locked when accessing |socket_|, |queue_| and |buffer_size_|; | 95 // Must be locked when accessing |socket_|, |queue_| and |buffer_size_|; |
94 base::Lock lock_; | 96 base::Lock lock_; |
95 | 97 |
96 net::Socket* socket_; | 98 net::Socket* socket_; |
97 scoped_refptr<base::MessageLoopProxy> message_loop_; | 99 scoped_refptr<base::MessageLoopProxy> message_loop_; |
98 scoped_ptr<WriteFailedCallback> write_failed_callback_; | 100 WriteFailedCallback write_failed_callback_; |
99 | 101 |
100 bool write_pending_; | 102 bool write_pending_; |
101 | 103 |
102 net::OldCompletionCallbackImpl<BufferedSocketWriterBase> written_callback_; | 104 net::OldCompletionCallbackImpl<BufferedSocketWriterBase> written_callback_; |
103 | 105 |
104 bool closed_; | 106 bool closed_; |
105 }; | 107 }; |
106 | 108 |
107 class BufferedSocketWriter : public BufferedSocketWriterBase { | 109 class BufferedSocketWriter : public BufferedSocketWriterBase { |
108 public: | 110 public: |
(...skipping 17 matching lines...) Expand all Loading... |
126 protected: | 128 protected: |
127 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size); | 129 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size); |
128 virtual void AdvanceBufferPosition_Locked(int written); | 130 virtual void AdvanceBufferPosition_Locked(int written); |
129 virtual void OnError_Locked(int result); | 131 virtual void OnError_Locked(int result); |
130 }; | 132 }; |
131 | 133 |
132 } // namespace protocol | 134 } // namespace protocol |
133 } // namespace remoting | 135 } // namespace remoting |
134 | 136 |
135 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 137 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
OLD | NEW |