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/callback.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 // Whenever new data is written it is just put in the queue, and then written | 32 // Whenever new data is written it is just put in the queue, and then written |
33 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() | 33 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() |
34 // can be used to throttle writes. | 34 // can be used to throttle writes. |
35 | 35 |
36 class BufferedSocketWriterBase | 36 class BufferedSocketWriterBase |
37 : public base::RefCountedThreadSafe<BufferedSocketWriterBase> { | 37 : public base::RefCountedThreadSafe<BufferedSocketWriterBase> { |
38 public: | 38 public: |
39 typedef base::Callback<void(int)> WriteFailedCallback; | 39 typedef base::Callback<void(int)> WriteFailedCallback; |
40 | 40 |
41 explicit BufferedSocketWriterBase(base::MessageLoopProxy* message_loop); | 41 explicit BufferedSocketWriterBase(base::MessageLoopProxy* message_loop); |
42 virtual ~BufferedSocketWriterBase(); | |
43 | 42 |
44 // Initializes the writer. Must be called on the thread that will be used | 43 // Initializes the writer. Must be called on the thread that will be used |
45 // to access the socket in the future. |callback| will be called after each | 44 // to access the socket in the future. |callback| will be called after each |
46 // failed write. Caller retains ownership of |socket|. | 45 // failed write. Caller retains ownership of |socket|. |
47 // TODO(sergeyu): Change it so that it take ownership of |socket|. | 46 // TODO(sergeyu): Change it so that it take ownership of |socket|. |
48 void Init(net::Socket* socket, const WriteFailedCallback& callback); | 47 void Init(net::Socket* socket, const WriteFailedCallback& callback); |
49 | 48 |
50 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue | 49 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue |
51 // the data if called before Init(). Can be called on any thread. | 50 // the data if called before Init(). Can be called on any thread. |
52 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, | 51 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, |
53 const base::Closure& done_task); | 52 const base::Closure& done_task); |
54 | 53 |
55 // Returns current size of the buffer. Can be called on any thread. | 54 // Returns current size of the buffer. Can be called on any thread. |
56 int GetBufferSize(); | 55 int GetBufferSize(); |
57 | 56 |
58 // Returns number of chunks that are currently in the buffer waiting | 57 // Returns number of chunks that are currently in the buffer waiting |
59 // to be written. Can be called on any thread. | 58 // to be written. Can be called on any thread. |
60 int GetBufferChunks(); | 59 int GetBufferChunks(); |
61 | 60 |
62 // Stops writing and drops current buffers. Must be called on the | 61 // Stops writing and drops current buffers. Must be called on the |
63 // network thread. | 62 // network thread. |
64 void Close(); | 63 void Close(); |
65 | 64 |
66 protected: | 65 protected: |
| 66 friend class base::RefCountedThreadSafe<BufferedSocketWriterBase>; |
| 67 |
67 class PendingPacket; | 68 class PendingPacket; |
68 typedef std::list<PendingPacket*> DataQueue; | 69 typedef std::list<PendingPacket*> DataQueue; |
69 | 70 |
| 71 virtual ~BufferedSocketWriterBase(); |
| 72 |
70 DataQueue queue_; | 73 DataQueue queue_; |
71 int buffer_size_; | 74 int buffer_size_; |
72 | 75 |
73 // Removes element from the front of the queue and calls |done_task| | 76 // Removes element from the front of the queue and calls |done_task| |
74 // for that element. | 77 // for that element. |
75 void PopQueue(); | 78 void PopQueue(); |
76 | 79 |
77 // Following three methods must be implemented in child classes. | 80 // Following three methods must be implemented in child classes. |
78 // GetNextPacket() returns next packet that needs to be written to the | 81 // GetNextPacket() returns next packet that needs to be written to the |
79 // socket. |buffer| must be set to NULL if there is nothing left in the queue. | 82 // socket. |buffer| must be set to NULL if there is nothing left in the queue. |
(...skipping 18 matching lines...) Expand all Loading... |
98 WriteFailedCallback write_failed_callback_; | 101 WriteFailedCallback write_failed_callback_; |
99 | 102 |
100 bool write_pending_; | 103 bool write_pending_; |
101 | 104 |
102 bool closed_; | 105 bool closed_; |
103 }; | 106 }; |
104 | 107 |
105 class BufferedSocketWriter : public BufferedSocketWriterBase { | 108 class BufferedSocketWriter : public BufferedSocketWriterBase { |
106 public: | 109 public: |
107 explicit BufferedSocketWriter(base::MessageLoopProxy* message_loop); | 110 explicit BufferedSocketWriter(base::MessageLoopProxy* message_loop); |
108 virtual ~BufferedSocketWriter(); | |
109 | 111 |
110 protected: | 112 protected: |
111 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size) OVERRIDE; | 113 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size) OVERRIDE; |
112 virtual void AdvanceBufferPosition_Locked(int written) OVERRIDE; | 114 virtual void AdvanceBufferPosition_Locked(int written) OVERRIDE; |
113 virtual void OnError_Locked(int result) OVERRIDE; | 115 virtual void OnError_Locked(int result) OVERRIDE; |
114 | 116 |
115 private: | 117 private: |
| 118 virtual ~BufferedSocketWriter(); |
| 119 |
116 scoped_refptr<net::DrainableIOBuffer> current_buf_; | 120 scoped_refptr<net::DrainableIOBuffer> current_buf_; |
117 }; | 121 }; |
118 | 122 |
119 class BufferedDatagramWriter : public BufferedSocketWriterBase { | 123 class BufferedDatagramWriter : public BufferedSocketWriterBase { |
120 public: | 124 public: |
121 explicit BufferedDatagramWriter(base::MessageLoopProxy* message_loop); | 125 explicit BufferedDatagramWriter(base::MessageLoopProxy* message_loop); |
122 virtual ~BufferedDatagramWriter(); | |
123 | 126 |
124 protected: | 127 protected: |
125 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size) OVERRIDE; | 128 virtual void GetNextPacket_Locked(net::IOBuffer** buffer, int* size) OVERRIDE; |
126 virtual void AdvanceBufferPosition_Locked(int written) OVERRIDE; | 129 virtual void AdvanceBufferPosition_Locked(int written) OVERRIDE; |
127 virtual void OnError_Locked(int result) OVERRIDE; | 130 virtual void OnError_Locked(int result) OVERRIDE; |
| 131 |
| 132 private: |
| 133 virtual ~BufferedDatagramWriter(); |
128 }; | 134 }; |
129 | 135 |
130 } // namespace protocol | 136 } // namespace protocol |
131 } // namespace remoting | 137 } // namespace remoting |
132 | 138 |
133 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 139 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
OLD | NEW |