Remove stale dependency in "ozone_demo" GN target.
[chromium-blink-merge.git] / remoting / base / buffered_socket_writer.h
blobdffed45f4e277de902def779adb84bbb9e46f214
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
3 // found in the LICENSE file.
5 #ifndef REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_
6 #define REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_
8 #include <list>
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/thread_checker.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/io_buffer.h"
17 namespace net {
18 class Socket;
19 } // namespace net
21 namespace remoting {
23 // BufferedSocketWriter implement write data queue for stream sockets.
24 class BufferedSocketWriter {
25 public:
26 typedef base::Callback<int(const scoped_refptr<net::IOBuffer>& buf,
27 int buf_len,
28 const net::CompletionCallback& callback)>
29 WriteCallback;
30 typedef base::Callback<void(int)> WriteFailedCallback;
32 static scoped_ptr<BufferedSocketWriter> CreateForSocket(
33 net::Socket* socket,
34 const WriteFailedCallback& write_failed_callback);
36 BufferedSocketWriter();
37 virtual ~BufferedSocketWriter();
39 // Initializes the writer. |write_callback| is called to write data to the
40 // socket. |write_failed_callback| is called when write operation fails.
41 // Writing stops after the first failed write.
42 void Init(const WriteCallback& write_callback,
43 const WriteFailedCallback& write_failed_callback);
45 // Puts a new data chunk in the buffer.
46 void Write(const scoped_refptr<net::IOBufferWithSize>& buffer,
47 const base::Closure& done_task);
49 // Returns true when there is data waiting to be written.
50 bool has_data_pending() { return !queue_.empty(); }
52 private:
53 struct PendingPacket;
54 typedef std::list<PendingPacket*> DataQueue;
56 // Returns true if the writer is closed due to an error.
57 bool is_closed();
59 void DoWrite();
60 void HandleWriteResult(int result);
61 void OnWritten(int result);
63 base::ThreadChecker thread_checker_;
65 WriteCallback write_callback_;
66 WriteFailedCallback write_failed_callback_;
68 DataQueue queue_;
70 bool write_pending_ = false;
72 base::WeakPtrFactory<BufferedSocketWriter> weak_factory_;
75 } // namespace remoting
77 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_