| 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 TOOLS_ANDROID_FORWARDER2_FORWARDER_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_FORWARDER_H_ |
| 6 #define TOOLS_ANDROID_FORWARDER2_FORWARDER_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_FORWARDER_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include <sys/select.h> |
| 9 |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/threading/thread.h" | 11 #include "base/threading/thread_checker.h" |
| 11 #include "tools/android/forwarder2/self_deleter_helper.h" | |
| 12 | 12 |
| 13 namespace forwarder2 { | 13 namespace forwarder2 { |
| 14 | 14 |
| 15 class PipeNotifier; | |
| 16 class Socket; | 15 class Socket; |
| 17 | 16 |
| 18 // Internal class that wraps a helper thread to forward traffic between | 17 // Internal class that forwards traffic between |socket1| and |socket2|. Note |
| 19 // |socket1| and |socket2|. After creating a new instance, call its Start() | 18 // that this class is not thread-safe. |
| 20 // method to launch operations. Thread stops automatically if one of the socket | |
| 21 // disconnects, but ensures that all buffered writes to the other, still alive, | |
| 22 // socket, are written first. When this happens, the instance will delete itself | |
| 23 // automatically. | |
| 24 // Note that the instance will always be destroyed on the same thread that | |
| 25 // created it. | |
| 26 class Forwarder { | 19 class Forwarder { |
| 27 public: | 20 public: |
| 28 // Callback used on error invoked by the Forwarder to self-delete. | 21 Forwarder(scoped_ptr<Socket> socket1, scoped_ptr<Socket> socket2); |
| 29 typedef base::Callback<void (scoped_ptr<Forwarder>)> ErrorCallback; | |
| 30 | |
| 31 Forwarder(scoped_ptr<Socket> socket1, | |
| 32 scoped_ptr<Socket> socket2, | |
| 33 PipeNotifier* deletion_notifier, | |
| 34 const ErrorCallback& error_callback); | |
| 35 | 22 |
| 36 ~Forwarder(); | 23 ~Forwarder(); |
| 37 | 24 |
| 38 void Start(); | 25 void RegisterFDs(fd_set* read_fds, fd_set* write_fds, int* max_fd); |
| 26 |
| 27 void ProcessEvents(const fd_set& read_fds, const fd_set& write_fds); |
| 28 |
| 29 bool IsClosed() const; |
| 30 |
| 31 void Shutdown(); |
| 39 | 32 |
| 40 private: | 33 private: |
| 41 void ThreadHandler(); | 34 class BufferedCopier; |
| 42 | 35 |
| 43 void OnThreadHandlerCompletion(const ErrorCallback& error_callback); | 36 base::ThreadChecker thread_checker_; |
| 44 | 37 const scoped_ptr<Socket> socket1_; |
| 45 SelfDeleterHelper<Forwarder> self_deleter_helper_; | 38 const scoped_ptr<Socket> socket2_; |
| 46 PipeNotifier* const deletion_notifier_; | 39 // Copies data from socket1 to socket2. |
| 47 scoped_ptr<Socket> socket1_; | 40 const scoped_ptr<BufferedCopier> buffer1_; |
| 48 scoped_ptr<Socket> socket2_; | 41 // Copies data from socket2 to socket1. |
| 49 base::Thread thread_; | 42 const scoped_ptr<BufferedCopier> buffer2_; |
| 50 }; | 43 }; |
| 51 | 44 |
| 52 } // namespace forwarder2 | 45 } // namespace forwarder2 |
| 53 | 46 |
| 54 #endif // TOOLS_ANDROID_FORWARDER2_FORWARDER_H_ | 47 #endif // TOOLS_ANDROID_FORWARDER2_FORWARDER_H_ |
| OLD | NEW |