OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
| 6 #define CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/ref_counted.h" |
| 11 #include "content/common/p2p_sockets.h" |
| 12 |
| 13 namespace base { |
| 14 class MessageLoopProxy; |
| 15 } // namespace base |
| 16 |
| 17 class P2PSocketDispatcher; |
| 18 class Task; |
| 19 |
| 20 // P2P socket that rountes all calls over IPC. It can be created and |
| 21 // used from any thread. |
| 22 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> { |
| 23 public: |
| 24 // Delegate is called on the IPC thread. |
| 25 class Delegate { |
| 26 public: |
| 27 virtual ~Delegate() { } |
| 28 |
| 29 virtual void OnOpen(P2PSocketAddress address) = 0; |
| 30 virtual void OnError() = 0; |
| 31 virtual void OnDataReceived(P2PSocketAddress address, |
| 32 const std::vector<char>& data) = 0; |
| 33 }; |
| 34 |
| 35 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher); |
| 36 |
| 37 // Initialize socket of the specified |type| and connected to the |
| 38 // specified |address|. |address| matters only when |type| is set to |
| 39 // P2P_SOCKET_TCP_CLIENT. |
| 40 void Init(P2PSocketType type, P2PSocketAddress address, Delegate* delegate); |
| 41 |
| 42 // Send the |data| to the |address|. |
| 43 void Send(P2PSocketAddress address, const std::vector<char>& data); |
| 44 |
| 45 // Must be called before the socket is destroyed. The delegate may |
| 46 // not be called after |closed_task| is executed. |
| 47 void Close(Task* closed_task); |
| 48 |
| 49 int socket_id() const { return socket_id_; } |
| 50 |
| 51 private: |
| 52 enum State { |
| 53 STATE_UNINITIALIZED, |
| 54 STATE_OPENING, |
| 55 STATE_OPEN, |
| 56 STATE_CLOSED, |
| 57 STATE_ERROR, |
| 58 }; |
| 59 |
| 60 friend class P2PSocketDispatcher; |
| 61 |
| 62 // Calls destructor. |
| 63 friend class base::RefCountedThreadSafe<P2PSocketClient>; |
| 64 |
| 65 virtual ~P2PSocketClient(); |
| 66 |
| 67 void OnSocketCreated(P2PSocketAddress address); |
| 68 void OnError(); |
| 69 void OnDataReceived(P2PSocketAddress address, |
| 70 const std::vector<char>& data); |
| 71 |
| 72 // Called by the dispatcher when it is destroyed. |
| 73 void Detach(); |
| 74 |
| 75 P2PSocketDispatcher* dispatcher_; |
| 76 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 77 int socket_id_; |
| 78 Delegate* delegate_; |
| 79 State state_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient); |
| 82 }; |
| 83 |
| 84 #endif // CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
OLD | NEW |