Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: remoting/base/buffered_socket_writer.h

Issue 1197853003: Add P2PDatagramSocket and P2PStreamSocket interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ 5 #ifndef REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_
6 #define REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ 6 #define REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_
7 7
8 #include <list> 8 #include <list>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
11 #include "base/synchronization/lock.h" 12 #include "base/synchronization/lock.h"
12 #include "base/threading/non_thread_safe.h" 13 #include "base/threading/thread_checker.h"
14 #include "net/base/completion_callback.h"
13 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
14 #include "net/socket/socket.h"
15 16
16 namespace net { 17 namespace net {
17 class Socket; 18 class Socket;
18 } // namespace net 19 } // namespace net
19 20
20 namespace remoting { 21 namespace remoting {
21 22
22 // BufferedSocketWriter and BufferedDatagramWriter implement write data queue 23 // BufferedSocketWriter implement write data queue for stream sockets.
23 // for stream and datagram sockets. BufferedSocketWriterBase is a base class 24 class BufferedSocketWriter {
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.
30
31 class BufferedSocketWriterBase : public base::NonThreadSafe {
32 public: 25 public:
26 typedef base::Callback<int(const scoped_refptr<net::IOBuffer>& buf,
27 int buf_len,
28 const net::CompletionCallback& callback)>
29 WriteCallback;
33 typedef base::Callback<void(int)> WriteFailedCallback; 30 typedef base::Callback<void(int)> WriteFailedCallback;
34 31
35 BufferedSocketWriterBase(); 32 static scoped_ptr<BufferedSocketWriter> CreateForSocket(
36 virtual ~BufferedSocketWriterBase(); 33 net::Socket* socket,
34 const WriteFailedCallback& write_failed_callback);
37 35
38 // Initializes the writer. Must be called on the thread that will be used 36 BufferedSocketWriter();
39 // to access the socket in the future. |callback| will be called after each 37 virtual ~BufferedSocketWriter();
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);
43 38
44 // Puts a new data chunk in the buffer. Returns false and doesn't enqueue 39 // Initializes the writer. |write_callback| is called to write data to the
45 // the data if called before Init(). Can be called on any thread. 40 // socket. |write_failed_callback| is called when write operation fails.
46 bool Write(scoped_refptr<net::IOBufferWithSize> buffer, 41 // Writing stops after the first failed write.
42 void Init(const WriteCallback& write_callback,
43 const WriteFailedCallback& write_failed_callback);
44
45 // Puts a new data chunk in the buffer. Returns false if writing has stopped
46 // because of an error.
47 bool Write(const scoped_refptr<net::IOBufferWithSize>& buffer,
47 const base::Closure& done_task); 48 const base::Closure& done_task);
48 49
49 // Returns true when there is data waiting to be written. 50 // Returns true when there is data waiting to be written.
50 bool has_data_pending() { return !queue_.empty(); } 51 bool has_data_pending() { return !queue_.empty(); }
51 52
52 // Stops writing and drops current buffers. Must be called on the 53 private:
53 // network thread.
54 void Close();
55
56 protected:
57 struct PendingPacket; 54 struct PendingPacket;
58 typedef std::list<PendingPacket*> DataQueue; 55 typedef std::list<PendingPacket*> DataQueue;
59 56
60 DataQueue queue_; 57 // Returns true if the writer is closed due to an error.
58 bool is_closed();
61 59
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();
66
67 // Following three methods must be implemented in child classes.
68
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;
72
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;
76
77 // This method is called whenever there is an error writing to the socket.
78 virtual void OnError(int result) = 0;
79
80 private:
81 void DoWrite(); 60 void DoWrite();
82 void HandleWriteResult(int result, bool* write_again); 61 void HandleWriteResult(int result, bool* write_again);
83 void OnWritten(int result); 62 void OnWritten(int result);
84 63
85 // This method is called when an error is encountered. 64 base::ThreadChecker thread_checker_;
86 void HandleError(int result);
87 65
88 net::Socket* socket_; 66 WriteCallback write_callback_;
89 WriteFailedCallback write_failed_callback_; 67 WriteFailedCallback write_failed_callback_;
90 68
91 bool write_pending_; 69 DataQueue queue_;
92 70
93 bool closed_; 71 bool write_pending_ = false;
94 72
95 bool* destroyed_flag_; 73 base::WeakPtrFactory<BufferedSocketWriter> weak_factory_;
Wez 2015/07/14 21:49:20 Looks like this class is missing DISALLOW_COPY_AND
Wez 2015/07/16 21:55:36 Ping.
96 };
97
98 class BufferedSocketWriter : public BufferedSocketWriterBase {
99 public:
100 BufferedSocketWriter();
101 ~BufferedSocketWriter() override;
102
103 protected:
104 void GetNextPacket(net::IOBuffer** buffer, int* size) override;
105 base::Closure AdvanceBufferPosition(int written) override;
106 void OnError(int result) override;
107
108 private:
109 scoped_refptr<net::DrainableIOBuffer> current_buf_;
110 };
111
112 class BufferedDatagramWriter : public BufferedSocketWriterBase {
113 public:
114 BufferedDatagramWriter();
115 ~BufferedDatagramWriter() override;
116
117 protected:
118 void GetNextPacket(net::IOBuffer** buffer, int* size) override;
119 base::Closure AdvanceBufferPosition(int written) override;
120 void OnError(int result) override;
121 }; 74 };
122 75
123 } // namespace remoting 76 } // namespace remoting
124 77
125 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_ 78 #endif // REMOTING_BASE_BUFFERED_SOCKET_WRITER_H_
OLDNEW
« no previous file with comments | « no previous file | remoting/base/buffered_socket_writer.cc » ('j') | remoting/base/buffered_socket_writer.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698