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 class P2PSocketDispatcher; |
| 14 class Task; |
| 15 |
| 16 // P2P socket that rountes all calls over IPC. It can be created and |
| 17 // used from any thread. |
| 18 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> { |
| 19 public: |
| 20 // Delegate is called on the IPC thread. |
| 21 class Delegate { |
| 22 public: |
| 23 virtual ~Delegate() { } |
| 24 |
| 25 virtual void OnOpen(P2PSocketAddress address) = 0; |
| 26 virtual void OnError() = 0; |
| 27 virtual void OnDataReceived(P2PSocketAddress address, |
| 28 const std::vector<char>& data) = 0; |
| 29 }; |
| 30 |
| 31 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher); |
| 32 |
| 33 // Initialize socket of the specified |type| and connected to the |
| 34 // specified |address|. |address| matters only when |type| is set to |
| 35 // P2P_SOCKET_TCP_CLIENT. |
| 36 void Init(P2PSocketType type, P2PSocketAddress address, Delegate* delegate); |
| 37 |
| 38 // Send the |data| to the |address|. |
| 39 void Send(P2PSocketAddress address, const std::vector<char>& data); |
| 40 |
| 41 // Must be called before the socket is destroyed. The delegate may |
| 42 // not be called after |closed_task| is executed. |
| 43 void Close(Task* closed_task); |
| 44 |
| 45 int socket_id() const { return socket_id_; } |
| 46 |
| 47 private: |
| 48 enum State { |
| 49 STATE_UNINITIALIZED, |
| 50 STATE_OPENING, |
| 51 STATE_OPEN, |
| 52 STATE_CLOSED, |
| 53 STATE_ERROR, |
| 54 }; |
| 55 |
| 56 friend class P2PSocketDispatcher; |
| 57 |
| 58 // Calls destructor. |
| 59 friend class base::RefCountedThreadSafe<P2PSocketClient>; |
| 60 |
| 61 virtual ~P2PSocketClient(); |
| 62 |
| 63 void OnSocketCreated(P2PSocketAddress address); |
| 64 void OnError(); |
| 65 void OnDataReceived(P2PSocketAddress address, |
| 66 const std::vector<char>& data); |
| 67 |
| 68 P2PSocketDispatcher* dispatcher_; |
| 69 int socket_id_; |
| 70 Delegate* delegate_; |
| 71 State state_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient); |
| 74 }; |
| 75 |
| 76 #endif // CHROME_RENDERER_P2P_SOCKET_CLIENT_H_ |
OLD | NEW |