blob: ab8ba0d6a049838d7e88efcd15f9b4a86814bb49 [file] [log] [blame]
[email protected]256d2732012-04-24 23:26:371// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c3af26f332010-10-06 22:46:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_
6#define REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_
7
[email protected]5bc71832010-12-09 01:34:088#include <list>
[email protected]c3af26f332010-10-06 22:46:009
[email protected]9e2a3132011-10-07 05:07:4010#include "base/callback.h"
[email protected]20305ec2011-01-21 04:55:5211#include "base/synchronization/lock.h"
[email protected]a3464dca2012-05-24 01:27:0912#include "base/threading/non_thread_safe.h"
[email protected]c3af26f332010-10-06 22:46:0013#include "net/base/io_buffer.h"
14#include "net/socket/socket.h"
15
[email protected]c3af26f332010-10-06 22:46:0016namespace net {
17class Socket;
18} // namespace net
19
20namespace remoting {
[email protected]d87c4042010-11-04 00:46:0121namespace protocol {
[email protected]c3af26f332010-10-06 22:46:0022
[email protected]aacbaac2010-10-20 23:53:4923// BufferedSocketWriter and BufferedDatagramWriter implement write data queue
24// for stream and datagram sockets. BufferedSocketWriterBase is a base class
25// that implements base functionality common for streams and datagrams.
26// These classes are particularly useful when data comes from a thread
27// that doesn't own the socket, as Write() can be called from any thread.
28// Whenever new data is written it is just put in the queue, and then written
29// on the thread that owns the socket. GetBufferChunks() and GetBufferSize()
30// can be used to throttle writes.
31
[email protected]a3464dca2012-05-24 01:27:0932class BufferedSocketWriterBase : public base::NonThreadSafe {
[email protected]c3af26f332010-10-06 22:46:0033 public:
[email protected]9e2a3132011-10-07 05:07:4034 typedef base::Callback<void(int)> WriteFailedCallback;
[email protected]c3af26f332010-10-06 22:46:0035
[email protected]a3464dca2012-05-24 01:27:0936 BufferedSocketWriterBase();
37 virtual ~BufferedSocketWriterBase();
[email protected]c3af26f332010-10-06 22:46:0038
39 // Initializes the writer. Must be called on the thread that will be used
40 // to access the socket in the future. |callback| will be called after each
[email protected]182ec8f22011-08-11 02:14:3541 // failed write. Caller retains ownership of |socket|.
42 // TODO(sergeyu): Change it so that it take ownership of |socket|.
[email protected]9e2a3132011-10-07 05:07:4043 void Init(net::Socket* socket, const WriteFailedCallback& callback);
[email protected]c3af26f332010-10-06 22:46:0044
45 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue
46 // the data if called before Init(). Can be called on any thread.
[email protected]9e2a3132011-10-07 05:07:4047 bool Write(scoped_refptr<net::IOBufferWithSize> buffer,
48 const base::Closure& done_task);
[email protected]c3af26f332010-10-06 22:46:0049
50 // Returns current size of the buffer. Can be called on any thread.
51 int GetBufferSize();
52
53 // Returns number of chunks that are currently in the buffer waiting
54 // to be written. Can be called on any thread.
55 int GetBufferChunks();
56
[email protected]86dbc722011-06-30 23:23:3057 // Stops writing and drops current buffers. Must be called on the
58 // network thread.
[email protected]c3af26f332010-10-06 22:46:0059 void Close();
60
[email protected]aacbaac2010-10-20 23:53:4961 protected:
[email protected]27626e62012-08-01 01:48:0262 struct PendingPacket;
[email protected]5bc71832010-12-09 01:34:0863 typedef std::list<PendingPacket*> DataQueue;
[email protected]c3af26f332010-10-06 22:46:0064
[email protected]aacbaac2010-10-20 23:53:4965 DataQueue queue_;
66 int buffer_size_;
67
[email protected]27626e62012-08-01 01:48:0268 // Removes element from the front of the queue and returns |done_task| for
69 // that element. Called from AdvanceBufferPosition() implementation, which
70 // then returns result of this function to its caller.
71 base::Closure PopQueue();
[email protected]5bc71832010-12-09 01:34:0872
[email protected]e221eeb2010-11-11 21:52:5773 // Following three methods must be implemented in child classes.
[email protected]27626e62012-08-01 01:48:0274
75 // Returns next packet that needs to be written to the socket. Implementation
76 // must set |*buffer| to NULL if there is nothing left in the queue.
[email protected]a3464dca2012-05-24 01:27:0977 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) = 0;
[email protected]27626e62012-08-01 01:48:0278
79 // Returns closure that must be executed or null closure if the last write
80 // didn't complete any messages.
81 virtual base::Closure AdvanceBufferPosition(int written) = 0;
[email protected]aacbaac2010-10-20 23:53:4982
[email protected]e221eeb2010-11-11 21:52:5783 // This method is called whenever there is an error writing to the socket.
[email protected]a3464dca2012-05-24 01:27:0984 virtual void OnError(int result) = 0;
[email protected]e221eeb2010-11-11 21:52:5785
[email protected]aacbaac2010-10-20 23:53:4986 private:
[email protected]c3af26f332010-10-06 22:46:0087 void DoWrite();
[email protected]27626e62012-08-01 01:48:0288 void HandleWriteResult(int result, bool* write_again);
[email protected]c3af26f332010-10-06 22:46:0089 void OnWritten(int result);
90
[email protected]e221eeb2010-11-11 21:52:5791 // This method is called when an error is encountered.
92 void HandleError(int result);
93
[email protected]c3af26f332010-10-06 22:46:0094 net::Socket* socket_;
[email protected]9e2a3132011-10-07 05:07:4095 WriteFailedCallback write_failed_callback_;
[email protected]c3af26f332010-10-06 22:46:0096
[email protected]c3af26f332010-10-06 22:46:0097 bool write_pending_;
98
[email protected]c3af26f332010-10-06 22:46:0099 bool closed_;
[email protected]27626e62012-08-01 01:48:02100
101 bool* destroyed_flag_;
[email protected]c3af26f332010-10-06 22:46:00102};
103
[email protected]aacbaac2010-10-20 23:53:49104class BufferedSocketWriter : public BufferedSocketWriterBase {
105 public:
[email protected]a3464dca2012-05-24 01:27:09106 BufferedSocketWriter();
dcheng562aba52014-10-21 12:30:14107 ~BufferedSocketWriter() override;
[email protected]256d2732012-04-24 23:26:37108
[email protected]a3464dca2012-05-24 01:27:09109 protected:
dcheng562aba52014-10-21 12:30:14110 void GetNextPacket(net::IOBuffer** buffer, int* size) override;
111 base::Closure AdvanceBufferPosition(int written) override;
112 void OnError(int result) override;
[email protected]a3464dca2012-05-24 01:27:09113
114 private:
[email protected]aacbaac2010-10-20 23:53:49115 scoped_refptr<net::DrainableIOBuffer> current_buf_;
116};
117
118class BufferedDatagramWriter : public BufferedSocketWriterBase {
119 public:
[email protected]a3464dca2012-05-24 01:27:09120 BufferedDatagramWriter();
dcheng562aba52014-10-21 12:30:14121 ~BufferedDatagramWriter() override;
[email protected]aacbaac2010-10-20 23:53:49122
123 protected:
dcheng562aba52014-10-21 12:30:14124 void GetNextPacket(net::IOBuffer** buffer, int* size) override;
125 base::Closure AdvanceBufferPosition(int written) override;
126 void OnError(int result) override;
[email protected]aacbaac2010-10-20 23:53:49127};
128
[email protected]d87c4042010-11-04 00:46:01129} // namespace protocol
[email protected]c3af26f332010-10-06 22:46:00130} // namespace remoting
131
132#endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_