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 rtc::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& local_address,
     78                        const net::IPEndPoint& remote_address);
     79   void OnIncomingTcpConnection(const net::IPEndPoint& address);
     80   void OnSendComplete(int packet_id);
     81   void OnSendComplete();
     82   void OnError();
     83   void OnDataReceived(const net::IPEndPoint& address,
     84                       const std::vector<char>& data,
     85                       const base::TimeTicks& timestamp);
     86 
     87   // Proxy methods that deliver messages to the delegate thread.
     88   void DeliverOnSocketCreated(const net::IPEndPoint& local_address,
     89                               const net::IPEndPoint& remote_address);
     90   void DeliverOnIncomingTcpConnection(
     91       const net::IPEndPoint& address,
     92       scoped_refptr<P2PSocketClient> new_client);
     93   void DeliverOnSendComplete();
     94   void DeliverOnError();
     95   void DeliverOnDataReceived(const net::IPEndPoint& address,
     96                              const std::vector<char>& data,
     97                              const base::TimeTicks& timestamp);
     98 
     99   // Scheduled on the IPC thread to finish initialization.
    100   void DoInit(P2PSocketType type,
    101               const net::IPEndPoint& local_address,
    102               const P2PHostAndIPEndPoint& remote_address);
    103 
    104   // Scheduled on the IPC thread to finish closing the connection.
    105   void DoClose();
    106 
    107   // Called by the dispatcher when it is destroyed.
    108   void Detach();
    109 
    110   P2PSocketDispatcher* dispatcher_;
    111   scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
    112   scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
    113   int socket_id_;
    114   P2PSocketClientDelegate* delegate_;
    115   State state_;
    116 
    117   // These two fields are used to identify packets for tracing.
    118   uint32 random_socket_id_;
    119   uint32 next_packet_id_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl);
    122 };
    123 
    124 }  // namespace content
    125 
    126 #endif  // CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
    127