Home | History | Annotate | Download | only in p2p
      1 // Copyright 2013 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 CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
      6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "content/common/p2p_socket_type.h"
     12 #include "content/renderer/p2p/socket_client.h"
     13 #include "net/base/ip_endpoint.h"
     14 
     15 namespace base {
     16 class MessageLoopProxy;
     17 class TimeTicks;
     18 }  // namespace base
     19 
     20 namespace content {
     21 
     22 class P2PSocketDispatcher;
     23 
     24 // P2P socket that routes all calls over IPC.
     25 //
     26 // The object runs on two threads: IPC thread and delegate thread. The
     27 // IPC thread is used to interact with P2PSocketDispatcher. All
     28 // callbacks to the user of this class are called on the delegate
     29 // thread which is specified in Init().
     30 class P2PSocketClientImpl : public P2PSocketClient {
     31  public:
     32   explicit P2PSocketClientImpl(P2PSocketDispatcher* dispatcher);
     33 
     34   // Initialize socket of the specified |type| and connected to the
     35   // specified |address|. |address| matters only when |type| is set to
     36   // P2P_SOCKET_TCP_CLIENT.
     37   virtual void Init(P2PSocketType type,
     38                     const net::IPEndPoint& local_address,
     39                     const P2PHostAndIPEndPoint& remote_address,
     40                     P2PSocketClientDelegate* delegate);
     41 
     42   // Send the |data| to the |address|.
     43   virtual void Send(const net::IPEndPoint& address,
     44                     const std::vector<char>& data) OVERRIDE;
     45 
     46   // Send the |data| to the |address| using Differentiated Services Code Point
     47   // |dscp|.
     48   virtual void SendWithDscp(const net::IPEndPoint& address,
     49                             const std::vector<char>& data,
     50                             const talk_base::PacketOptions& options) OVERRIDE;
     51 
     52   // Setting socket options.
     53   virtual void SetOption(P2PSocketOption option, int value) OVERRIDE;
     54 
     55   // Must be called before the socket is destroyed. The delegate may
     56   // not be called after |closed_task| is executed.
     57   virtual void Close() OVERRIDE;
     58 
     59   virtual int GetSocketID() const OVERRIDE;
     60 
     61   virtual void SetDelegate(P2PSocketClientDelegate* delegate) OVERRIDE;
     62 
     63  private:
     64   enum State {
     65     STATE_UNINITIALIZED,
     66     STATE_OPENING,
     67     STATE_OPEN,
     68     STATE_CLOSED,
     69     STATE_ERROR,
     70   };
     71 
     72   friend class P2PSocketDispatcher;
     73 
     74   virtual ~P2PSocketClientImpl();
     75 
     76   // Message handlers that run on IPC thread.
     77   void OnSocketCreated(const net::IPEndPoint& address);
     78   void OnIncomingTcpConnection(const net::IPEndPoint& address);
     79   void OnSendComplete(int packet_id);
     80   void OnSendComplete();
     81   void OnError();
     82   void OnDataReceived(const net::IPEndPoint& address,
     83                       const std::vector<char>& data,
     84                       const base::TimeTicks& timestamp);
     85 
     86   // Proxy methods that deliver messages to the delegate thread.
     87   void DeliverOnSocketCreated(const net::IPEndPoint& address);
     88   void DeliverOnIncomingTcpConnection(
     89       const net::IPEndPoint& address,
     90       scoped_refptr<P2PSocketClient> new_client);
     91   void DeliverOnSendComplete();
     92   void DeliverOnError();
     93   void DeliverOnDataReceived(const net::IPEndPoint& address,
     94                              const std::vector<char>& data,
     95                              const base::TimeTicks& timestamp);
     96 
     97   // Scheduled on the IPC thread to finish initialization.
     98   void DoInit(P2PSocketType type,
     99               const net::IPEndPoint& local_address,
    100               const P2PHostAndIPEndPoint& remote_address);
    101 
    102   // Scheduled on the IPC thread to finish closing the connection.
    103   void DoClose();
    104 
    105   // Called by the dispatcher when it is destroyed.
    106   void Detach();
    107 
    108   P2PSocketDispatcher* dispatcher_;
    109   scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
    110   scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
    111   int socket_id_;
    112   P2PSocketClientDelegate* delegate_;
    113   State state_;
    114 
    115   // These two fields are used to identify packets for tracing.
    116   uint32 random_socket_id_;
    117   uint32 next_packet_id_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl);
    120 };
    121 
    122 }  // namespace content
    123 
    124 #endif  // CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
    125