| OLD | NEW |
| 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_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 5 #ifndef REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
| 6 #define REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 6 #define REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 | 52 |
| 53 // Returns number of chunks that are currently in the buffer waiting | 53 // Returns number of chunks that are currently in the buffer waiting |
| 54 // to be written. Can be called on any thread. | 54 // to be written. Can be called on any thread. |
| 55 int GetBufferChunks(); | 55 int GetBufferChunks(); |
| 56 | 56 |
| 57 // Stops writing and drops current buffers. Must be called on the | 57 // Stops writing and drops current buffers. Must be called on the |
| 58 // network thread. | 58 // network thread. |
| 59 void Close(); | 59 void Close(); |
| 60 | 60 |
| 61 protected: | 61 protected: |
| 62 class PendingPacket; | 62 struct PendingPacket; |
| 63 typedef std::list<PendingPacket*> DataQueue; | 63 typedef std::list<PendingPacket*> DataQueue; |
| 64 | 64 |
| 65 DataQueue queue_; | 65 DataQueue queue_; |
| 66 int buffer_size_; | 66 int buffer_size_; |
| 67 | 67 |
| 68 // Removes element from the front of the queue and calls |done_task| | 68 // Removes element from the front of the queue and returns |done_task| for |
| 69 // for that element. | 69 // that element. Called from AdvanceBufferPosition() implementation, which |
| 70 void PopQueue(); | 70 // then returns result of this function to its caller. |
| 71 base::Closure PopQueue(); |
| 71 | 72 |
| 72 // Following three methods must be implemented in child classes. | 73 // Following three methods must be implemented in child classes. |
| 73 // GetNextPacket() returns next packet that needs to be written to the | 74 |
| 74 // socket. |buffer| must be set to NULL if there is nothing left in the queue. | 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. |
| 75 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) = 0; | 77 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) = 0; |
| 76 virtual void AdvanceBufferPosition(int written) = 0; | 78 |
| 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; |
| 77 | 82 |
| 78 // This method is called whenever there is an error writing to the socket. | 83 // This method is called whenever there is an error writing to the socket. |
| 79 virtual void OnError(int result) = 0; | 84 virtual void OnError(int result) = 0; |
| 80 | 85 |
| 81 private: | 86 private: |
| 82 void DoWrite(); | 87 void DoWrite(); |
| 88 void HandleWriteResult(int result, bool* write_again); |
| 83 void OnWritten(int result); | 89 void OnWritten(int result); |
| 84 | 90 |
| 85 // This method is called when an error is encountered. | 91 // This method is called when an error is encountered. |
| 86 void HandleError(int result); | 92 void HandleError(int result); |
| 87 | 93 |
| 88 net::Socket* socket_; | 94 net::Socket* socket_; |
| 89 WriteFailedCallback write_failed_callback_; | 95 WriteFailedCallback write_failed_callback_; |
| 90 | 96 |
| 91 bool write_pending_; | 97 bool write_pending_; |
| 92 | 98 |
| 93 bool closed_; | 99 bool closed_; |
| 100 |
| 101 bool* destroyed_flag_; |
| 94 }; | 102 }; |
| 95 | 103 |
| 96 class BufferedSocketWriter : public BufferedSocketWriterBase { | 104 class BufferedSocketWriter : public BufferedSocketWriterBase { |
| 97 public: | 105 public: |
| 98 BufferedSocketWriter(); | 106 BufferedSocketWriter(); |
| 99 virtual ~BufferedSocketWriter(); | 107 virtual ~BufferedSocketWriter(); |
| 100 | 108 |
| 101 protected: | 109 protected: |
| 102 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) OVERRIDE; | 110 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) OVERRIDE; |
| 103 virtual void AdvanceBufferPosition(int written) OVERRIDE; | 111 virtual base::Closure AdvanceBufferPosition(int written) OVERRIDE; |
| 104 virtual void OnError(int result) OVERRIDE; | 112 virtual void OnError(int result) OVERRIDE; |
| 105 | 113 |
| 106 private: | 114 private: |
| 107 scoped_refptr<net::DrainableIOBuffer> current_buf_; | 115 scoped_refptr<net::DrainableIOBuffer> current_buf_; |
| 108 }; | 116 }; |
| 109 | 117 |
| 110 class BufferedDatagramWriter : public BufferedSocketWriterBase { | 118 class BufferedDatagramWriter : public BufferedSocketWriterBase { |
| 111 public: | 119 public: |
| 112 BufferedDatagramWriter(); | 120 BufferedDatagramWriter(); |
| 113 virtual ~BufferedDatagramWriter(); | 121 virtual ~BufferedDatagramWriter(); |
| 114 | 122 |
| 115 protected: | 123 protected: |
| 116 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) OVERRIDE; | 124 virtual void GetNextPacket(net::IOBuffer** buffer, int* size) OVERRIDE; |
| 117 virtual void AdvanceBufferPosition(int written) OVERRIDE; | 125 virtual base::Closure AdvanceBufferPosition(int written) OVERRIDE; |
| 118 virtual void OnError(int result) OVERRIDE; | 126 virtual void OnError(int result) OVERRIDE; |
| 119 }; | 127 }; |
| 120 | 128 |
| 121 } // namespace protocol | 129 } // namespace protocol |
| 122 } // namespace remoting | 130 } // namespace remoting |
| 123 | 131 |
| 124 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ | 132 #endif // REMOTING_PROTOCOL_BUFFERED_SOCKET_WRITER_H_ |
| OLD | NEW |