[Android WebViewShell] Make WebViewLayoutTest runnable with test_runner.py
[chromium-blink-merge.git] / remoting / base / buffered_socket_writer.h
blobc6012e37e8bc97efe98cc09ba43a876c01596a24
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/synchronization/lock.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/io_buffer.h"
14 #include "net/socket/socket.h"
16 namespace net {
17 class Socket;
18 } // namespace net
20 namespace remoting {
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 {
32 public:
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 true when there is data waiting to be written.
50 bool has_data_pending() { return !queue_.empty(); }
52 // Stops writing and drops current buffers. Must be called on the
53 // network thread.
54 void Close();
56 protected:
57 struct PendingPacket;
58 typedef std::list<PendingPacket*> DataQueue;
60 DataQueue queue_;
62 // Removes element from the front of the queue and returns |done_task| for
63 // that element. Called from AdvanceBufferPosition() implementation, which
64 // then returns result of this function to its caller.
65 base::Closure PopQueue();
67 // Following three methods must be implemented in child classes.
69 // Returns next packet that needs to be written to the socket. Implementation
70 // must set |*buffer| to nullptr if there is nothing left in the queue.
71 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) = 0;
73 // Returns closure that must be executed or null closure if the last write
74 // didn't complete any messages.
75 virtual base::Closure AdvanceBufferPosition(int written) = 0;
77 // This method is called whenever there is an error writing to the socket.
78 virtual void OnError(int result) = 0;
80 private:
81 void DoWrite();
82 void HandleWriteResult(int result, bool* write_again);
83 void OnWritten(int result);
85 // This method is called when an error is encountered.
86 void HandleError(int result);
88 net::Socket* socket_;
89 WriteFailedCallback write_failed_callback_;
91 bool write_pending_;
93 bool closed_;
95 bool* destroyed_flag_;
98 class BufferedSocketWriter : public BufferedSocketWriterBase {
99 public:
100 BufferedSocketWriter();
101 ~BufferedSocketWriter() override;
103 protected:
104 void GetNextPacket(net::IOBuffer** buffer, int* size) override;
105 base::Closure AdvanceBufferPosition(int written) override;
106 void OnError(int result) override;
108 private:
109 scoped_refptr<net::DrainableIOBuffer> current_buf_;
112 class BufferedDatagramWriter : public BufferedSocketWriterBase {
113 public:
114 BufferedDatagramWriter();
115 ~BufferedDatagramWriter() override;
117 protected:
118 void GetNextPacket(net::IOBuffer** buffer, int* size) override;
119 base::Closure AdvanceBufferPosition(int written) override;
120 void OnError(int result) override;
123 } // namespace remoting
125 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_