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/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
13 #include "net/socket/socket.h" | 13 #include "net/socket/socket.h" |
14 | 14 |
15 class MessageLoop; | |
16 class Task; | 15 class Task; |
17 | 16 |
| 17 namespace base { |
| 18 class MessageLoopProxy; |
| 19 } // namespace base |
| 20 |
18 namespace net { | 21 namespace net { |
19 class Socket; | 22 class Socket; |
20 } // namespace net | 23 } // namespace net |
21 | 24 |
22 namespace remoting { | 25 namespace remoting { |
23 namespace protocol { | 26 namespace protocol { |
24 | 27 |
25 // BufferedSocketWriter and BufferedDatagramWriter implement write data queue | 28 // BufferedSocketWriter and BufferedDatagramWriter implement write data queue |
26 // for stream and datagram sockets. BufferedSocketWriterBase is a base class | 29 // for stream and datagram sockets. BufferedSocketWriterBase is a base class |
27 // that implements base functionality common for streams and datagrams. | 30 // that implements base functionality common for streams and datagrams. |
28 // These classes are particularly useful when data comes from a thread | 31 // These classes are particularly useful when data comes from a thread |
29 // that doesn't own the socket, as Write() can be called from any thread. | 32 // that doesn't own the socket, as Write() can be called from any thread. |
30 // Whenever new data is written it is just put in the queue, and then written | 33 // Whenever new data is written it is just put in the queue, and then written |
31 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() | 34 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() |
32 // can be used to throttle writes. | 35 // can be used to throttle writes. |
33 | 36 |
34 class BufferedSocketWriterBase | 37 class BufferedSocketWriterBase |
35 : public base::RefCountedThreadSafe<BufferedSocketWriterBase> { | 38 : public base::RefCountedThreadSafe<BufferedSocketWriterBase> { |
36 public: | 39 public: |
37 typedef Callback1<int>::Type WriteFailedCallback; | 40 typedef Callback1<int>::Type WriteFailedCallback; |
38 | 41 |
39 explicit BufferedSocketWriterBase(); | 42 explicit BufferedSocketWriterBase(base::MessageLoopProxy* message_loop); |
40 virtual ~BufferedSocketWriterBase(); | 43 virtual ~BufferedSocketWriterBase(); |
41 | 44 |
42 // Initializes the writer. Must be called on the thread that will be used | 45 // Initializes the writer. Must be called on the thread that will be used |
43 // to access the socket in the future. |callback| will be called after each | 46 // to access the socket in the future. |callback| will be called after each |
44 // failed write. Caller retains ownership of |socket|. | 47 // failed write. Caller retains ownership of |socket|. |
45 // TODO(sergeyu): Change it so that it take ownership of |socket|. | 48 // TODO(sergeyu): Change it so that it take ownership of |socket|. |
46 void Init(net::Socket* socket, WriteFailedCallback* callback); | 49 void Init(net::Socket* socket, WriteFailedCallback* callback); |
47 | 50 |
48 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue | 51 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue |
49 // the data if called before Init(). Can be called on any thread. | 52 // the data if called before Init(). Can be called on any thread. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 void DoWrite(); | 87 void DoWrite(); |
85 void OnWritten(int result); | 88 void OnWritten(int result); |
86 | 89 |
87 // This method is called when an error is encountered. | 90 // This method is called when an error is encountered. |
88 void HandleError(int result); | 91 void HandleError(int result); |
89 | 92 |
90 // Must be locked when accessing |socket_|, |queue_| and |buffer_size_|; | 93 // Must be locked when accessing |socket_|, |queue_| and |buffer_size_|; |
91 base::Lock lock_; | 94 base::Lock lock_; |
92 | 95 |
93 net::Socket* socket_; | 96 net::Socket* socket_; |
94 MessageLoop* message_loop_; | 97 scoped_refptr<base::MessageLoopProxy> message_loop_; |
95 scoped_ptr<WriteFailedCallback> write_failed_callback_; | 98 scoped_ptr<WriteFailedCallback> write_failed_callback_; |
96 | 99 |
97 bool write_pending_; | 100 bool write_pending_; |
98 | 101 |
99 net::CompletionCallbackImpl<BufferedSocketWriterBase> written_callback_; | 102 net::CompletionCallbackImpl<BufferedSocketWriterBase> written_callback_; |
100 | 103 |
101 bool closed_; | 104 bool closed_; |
102 }; | 105 }; |
103 | 106 |
104 class BufferedSocketWriter : public BufferedSocketWriterBase { | 107 class BufferedSocketWriter : public BufferedSocketWriterBase { |
105 public: | 108 public: |
106 BufferedSocketWriter(); | 109 BufferedSocketWriter(base::MessageLoopProxy* message_loop); |
107 virtual ~BufferedSocketWriter(); | 110 virtual ~BufferedSocketWriter(); |
108 | 111 |
109 protected: | 112 protected: |
110 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size); | 113 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size); |
111 virtual void AdvanceBufferPosition_Locked(int written); | 114 virtual void AdvanceBufferPosition_Locked(int written); |
112 virtual void OnError_Locked(int result); | 115 virtual void OnError_Locked(int result); |
113 | 116 |
114 private: | 117 private: |
115 scoped_refptr<net::DrainableIOBuffer> current_buf_; | 118 scoped_refptr<net::DrainableIOBuffer> current_buf_; |
116 }; | 119 }; |
117 | 120 |
118 class BufferedDatagramWriter : public BufferedSocketWriterBase { | 121 class BufferedDatagramWriter : public BufferedSocketWriterBase { |
119 public: | 122 public: |
120 BufferedDatagramWriter(); | 123 BufferedDatagramWriter(base::MessageLoopProxy* message_loop); |
121 virtual ~BufferedDatagramWriter(); | 124 virtual ~BufferedDatagramWriter(); |
122 | 125 |
123 protected: | 126 protected: |
124 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size); | 127 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size); |
125 virtual void AdvanceBufferPosition_Locked(int written); | 128 virtual void AdvanceBufferPosition_Locked(int written); |
126 virtual void OnError_Locked(int result); | 129 virtual void OnError_Locked(int result); |
127 }; | 130 }; |
128 | 131 |
129 } // namespace protocol | 132 } // namespace protocol |
130 } // namespace remoting | 133 } // namespace remoting |
131 | 134 |
132 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 135 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
OLD | NEW |