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_
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"
23 // BufferedSocketWriter implement write data queue for stream sockets.
24 class BufferedSocketWriter
{
26 typedef base::Callback
<int(const scoped_refptr
<net::IOBuffer
>& buf
,
28 const net::CompletionCallback
& callback
)>
30 typedef base::Callback
<void(int)> WriteFailedCallback
;
32 static scoped_ptr
<BufferedSocketWriter
> CreateForSocket(
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(); }
54 typedef std::list
<PendingPacket
*> DataQueue
;
56 // Returns true if the writer is closed due to an error.
60 void HandleWriteResult(int result
);
61 void OnWritten(int result
);
63 base::ThreadChecker thread_checker_
;
65 WriteCallback write_callback_
;
66 WriteFailedCallback write_failed_callback_
;
70 bool write_pending_
= false;
72 base::WeakPtrFactory
<BufferedSocketWriter
> weak_factory_
;
75 } // namespace remoting
77 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_