OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_BASE_BUFFERED_SOCKET_WRITER_H_ | 5 #ifndef REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ |
6 #define REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ | 6 #define REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
13 #include "net/base/completion_callback.h" | |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/socket/socket.h" | |
15 | |
16 namespace net { | |
17 class Socket; | |
18 } // namespace net | |
19 | 15 |
20 namespace remoting { | 16 namespace remoting { |
21 | 17 |
22 // BufferedSocketWriter and BufferedDatagramWriter implement write data queue | 18 // BufferedSocketWriter and BufferedDatagramWriter implement write data queue |
23 // for stream and datagram sockets. BufferedSocketWriterBase is a base class | 19 // for stream and datagram sockets. BufferedSocketWriterBase is a base class |
24 // that implements base functionality common for streams and datagrams. | 20 // that implements base functionality common for streams and datagrams. |
25 // These classes are particularly useful when data comes from a thread | 21 // These classes are particularly useful when data comes from a thread |
26 // that doesn't own the socket, as Write() can be called from any thread. | 22 // that doesn't own the socket, as Write() can be called from any thread. |
27 // Whenever new data is written it is just put in the queue, and then written | 23 // Whenever new data is written it is just put in the queue, and then written |
28 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() | 24 // on the thread that owns the socket. GetBufferChunks() and GetBufferSize() |
29 // can be used to throttle writes. | 25 // can be used to throttle writes. |
30 | |
31 class BufferedSocketWriterBase : public base::NonThreadSafe { | 26 class BufferedSocketWriterBase : public base::NonThreadSafe { |
32 public: | 27 public: |
28 typedef base::Callback<int(net::IOBuffer* buf, int buf_len, | |
29 const net::CompletionCallback& callback)> | |
30 WriteCallback; | |
33 typedef base::Callback<void(int)> WriteFailedCallback; | 31 typedef base::Callback<void(int)> WriteFailedCallback; |
34 | 32 |
35 BufferedSocketWriterBase(); | 33 BufferedSocketWriterBase(); |
36 virtual ~BufferedSocketWriterBase(); | 34 virtual ~BufferedSocketWriterBase(); |
37 | 35 |
38 // Initializes the writer. Must be called on the thread that will be used | 36 // Initializes the writer. Must be called on the thread that will be used |
39 // to access the socket in the future. |callback| will be called after each | 37 // to access the socket in the future. |callback| will be called after each |
40 // failed write. Caller retains ownership of |socket|. | 38 // failed write. |
41 // TODO(sergeyu): Change it so that it take ownership of |socket|. | 39 void Init(const WriteCallback& write_callback, |
42 void Init(net::Socket* socket, const WriteFailedCallback& callback); | 40 const WriteFailedCallback& write_failed_callback); |
43 | 41 |
44 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue | 42 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue |
45 // the data if called before Init(). Can be called on any thread. | 43 // the data if called before Init(). Can be called on any thread. |
46 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, | 44 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, |
47 const base::Closure& done_task); | 45 const base::Closure& done_task); |
Wez
2015/07/10 01:18:24
Not necessarily for this CL, but this class seems
Sergey Ulanov
2015/07/10 20:48:57
Simplified this code now. BufferedDatagramWriter w
| |
48 | 46 |
49 // Returns true when there is data waiting to be written. | 47 // Returns true when there is data waiting to be written. |
50 bool has_data_pending() { return !queue_.empty(); } | 48 bool has_data_pending() { return !queue_.empty(); } |
51 | 49 |
52 // Stops writing and drops current buffers. Must be called on the | 50 // Stops writing and drops current buffers. Must be called on the |
53 // network thread. | 51 // network thread. |
54 void Close(); | 52 void Close(); |
55 | 53 |
56 protected: | 54 protected: |
57 struct PendingPacket; | 55 struct PendingPacket; |
(...skipping 20 matching lines...) Expand all Loading... | |
78 virtual void OnError(int result) = 0; | 76 virtual void OnError(int result) = 0; |
79 | 77 |
80 private: | 78 private: |
81 void DoWrite(); | 79 void DoWrite(); |
82 void HandleWriteResult(int result, bool* write_again); | 80 void HandleWriteResult(int result, bool* write_again); |
83 void OnWritten(int result); | 81 void OnWritten(int result); |
84 | 82 |
85 // This method is called when an error is encountered. | 83 // This method is called when an error is encountered. |
86 void HandleError(int result); | 84 void HandleError(int result); |
87 | 85 |
88 net::Socket* socket_; | 86 WriteCallback write_callback_; |
89 WriteFailedCallback write_failed_callback_; | 87 WriteFailedCallback write_failed_callback_; |
90 | 88 |
91 bool write_pending_; | 89 bool write_pending_; |
92 | 90 |
93 bool closed_; | 91 bool closed_; |
94 | 92 |
95 bool* destroyed_flag_; | 93 bool* destroyed_flag_; |
96 }; | 94 }; |
97 | 95 |
98 class BufferedSocketWriter : public BufferedSocketWriterBase { | 96 class BufferedSocketWriter : public BufferedSocketWriterBase { |
(...skipping 17 matching lines...) Expand all Loading... | |
116 | 114 |
117 protected: | 115 protected: |
118 void GetNextPacket(net::IOBuffer** buffer, int* size) override; | 116 void GetNextPacket(net::IOBuffer** buffer, int* size) override; |
119 base::Closure AdvanceBufferPosition(int written) override; | 117 base::Closure AdvanceBufferPosition(int written) override; |
120 void OnError(int result) override; | 118 void OnError(int result) override; |
121 }; | 119 }; |
122 | 120 |
123 } // namespace remoting | 121 } // namespace remoting |
124 | 122 |
125 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ | 123 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ |
OLD | NEW |