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/synchronization/lock.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/io_buffer.h"
14 #include "net/socket/socket.h"
22 // BufferedSocketWriter and BufferedDatagramWriter implement write data queue
23 // for stream and datagram sockets. BufferedSocketWriterBase is a base class
24 // that implements base functionality common for streams and datagrams.
25 // 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.
27 // 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()
29 // can be used to throttle writes.
31 class BufferedSocketWriterBase
: public base::NonThreadSafe
{
33 typedef base::Callback
<void(int)> WriteFailedCallback
;
35 BufferedSocketWriterBase();
36 virtual ~BufferedSocketWriterBase();
38 // 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
40 // failed write. Caller retains ownership of |socket|.
41 // TODO(sergeyu): Change it so that it take ownership of |socket|.
42 void Init(net::Socket
* socket
, const WriteFailedCallback
& callback
);
44 // 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.
46 bool Write(scoped_refptr
<net::IOBufferWithSize
> buffer
,
47 const base::Closure
& done_task
);
49 // Returns current size of the buffer. Can be called on any thread.
52 // Returns number of chunks that are currently in the buffer waiting
53 // to be written. Can be called on any thread.
54 int GetBufferChunks();
56 // Stops writing and drops current buffers. Must be called on the
62 typedef std::list
<PendingPacket
*> DataQueue
;
67 // Removes element from the front of the queue and returns |done_task| for
68 // that element. Called from AdvanceBufferPosition() implementation, which
69 // then returns result of this function to its caller.
70 base::Closure
PopQueue();
72 // Following three methods must be implemented in child classes.
74 // Returns next packet that needs to be written to the socket. Implementation
75 // must set |*buffer| to nullptr if there is nothing left in the queue.
76 virtual void GetNextPacket(net::IOBuffer
** buffer
, int* size
) = 0;
78 // Returns closure that must be executed or null closure if the last write
79 // didn't complete any messages.
80 virtual base::Closure
AdvanceBufferPosition(int written
) = 0;
82 // This method is called whenever there is an error writing to the socket.
83 virtual void OnError(int result
) = 0;
87 void HandleWriteResult(int result
, bool* write_again
);
88 void OnWritten(int result
);
90 // This method is called when an error is encountered.
91 void HandleError(int result
);
94 WriteFailedCallback write_failed_callback_
;
100 bool* destroyed_flag_
;
103 class BufferedSocketWriter
: public BufferedSocketWriterBase
{
105 BufferedSocketWriter();
106 ~BufferedSocketWriter() override
;
109 void GetNextPacket(net::IOBuffer
** buffer
, int* size
) override
;
110 base::Closure
AdvanceBufferPosition(int written
) override
;
111 void OnError(int result
) override
;
114 scoped_refptr
<net::DrainableIOBuffer
> current_buf_
;
117 class BufferedDatagramWriter
: public BufferedSocketWriterBase
{
119 BufferedDatagramWriter();
120 ~BufferedDatagramWriter() override
;
123 void GetNextPacket(net::IOBuffer
** buffer
, int* size
) override
;
124 base::Closure
AdvanceBufferPosition(int written
) override
;
125 void OnError(int result
) override
;
128 } // namespace remoting
130 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_