summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/buffered_socket_writer.h
blob: 3d4709df9d9f05931f1fe9988d87219bf5b16355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_
#define REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_

#include <deque>

#include "base/lock.h"
#include "base/ref_counted.h"
#include "net/base/io_buffer.h"
#include "net/socket/socket.h"

class MessageLoop;

namespace net {
class Socket;
}  // namespace net

namespace remoting {

class BufferedSocketWriter
    : public base::RefCountedThreadSafe<BufferedSocketWriter> {
 public:
  typedef Callback1<int>::Type WriteFailedCallback;

  BufferedSocketWriter();
  virtual ~BufferedSocketWriter();

  // Initializes the writer. Must be called on the thread that will be used
  // to access the socket in the future. |callback| will be called after each
  // failed write.
  void Init(net::Socket* socket, WriteFailedCallback* callback);

  // Puts a new data chunk in the buffer. Returns false and doesn't enqueue
  // the data if called before Init(). Can be called on any thread.
  bool Write(scoped_refptr<net::IOBufferWithSize> buffer);

  // Returns current size of the buffer. Can be called on any thread.
  int GetBufferSize();

  // Returns number of chunks that are currently in the buffer waiting
  // to be written. Can be called on any thread.
  int GetBufferChunks();

  // Stops writing and drops current buffers.
  void Close();

 private:
  typedef std::deque<scoped_refptr<net::IOBufferWithSize> > DataQueue;

  void DoWrite();
  void OnWritten(int result);

  net::Socket* socket_;
  MessageLoop* message_loop_;
  scoped_ptr<WriteFailedCallback> write_failed_callback_;

  Lock lock_;

  DataQueue queue_;
  int buffer_size_;
  scoped_refptr<net::DrainableIOBuffer> current_buf_;
  bool write_pending_;

  net::CompletionCallbackImpl<BufferedSocketWriter> written_callback_;

  bool closed_;
};

}  // namespace remoting

#endif  // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_