tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

peer_channel.h (3469B)


      1 /*
      2 *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #ifndef EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
     12 #define EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
     13 
     14 #include <time.h>
     15 
     16 #include <queue>
     17 #include <string>
     18 #include <vector>
     19 
     20 class DataSocket;
     21 
     22 // Represents a single peer connected to the server.
     23 class ChannelMember {
     24 public:
     25  explicit ChannelMember(DataSocket* socket);
     26  ~ChannelMember();
     27 
     28  bool connected() const { return connected_; }
     29  int id() const { return id_; }
     30  void set_disconnected() { connected_ = false; }
     31  bool is_wait_request(DataSocket* ds) const;
     32  const std::string& name() const { return name_; }
     33 
     34  bool TimedOut();
     35 
     36  std::string GetPeerIdHeader() const;
     37 
     38  bool NotifyOfOtherMember(const ChannelMember& other);
     39 
     40  // Returns a string in the form "name,id\n".
     41  std::string GetEntry() const;
     42 
     43  void ForwardRequestToPeer(DataSocket* ds, ChannelMember* peer);
     44 
     45  void OnClosing(DataSocket* ds);
     46 
     47  void QueueResponse(const std::string& status,
     48                     const std::string& content_type,
     49                     const std::string& extra_headers,
     50                     const std::string& data);
     51 
     52  void SetWaitingSocket(DataSocket* ds);
     53 
     54 protected:
     55  struct QueuedResponse {
     56    std::string status, content_type, extra_headers, data;
     57  };
     58 
     59  DataSocket* waiting_socket_;
     60  int id_;
     61  bool connected_;
     62  time_t timestamp_;
     63  std::string name_;
     64  std::queue<QueuedResponse> queue_;
     65  static int s_member_id_;
     66 };
     67 
     68 // Manages all currently connected peers.
     69 class PeerChannel {
     70 public:
     71  typedef std::vector<ChannelMember*> Members;
     72 
     73  PeerChannel() {}
     74 
     75  ~PeerChannel() { DeleteAll(); }
     76 
     77  const Members& members() const { return members_; }
     78 
     79  // Returns true if the request should be treated as a new ChannelMember
     80  // request.  Otherwise the request is not peerconnection related.
     81  static bool IsPeerConnection(const DataSocket* ds);
     82 
     83  // Finds a connected peer that's associated with the `ds` socket.
     84  ChannelMember* Lookup(DataSocket* ds) const;
     85 
     86  // Checks if the request has a "peer_id" parameter and if so, looks up the
     87  // peer for which the request is targeted at.
     88  ChannelMember* IsTargetedRequest(const DataSocket* ds) const;
     89 
     90  // Adds a new ChannelMember instance to the list of connected peers and
     91  // associates it with the socket.
     92  bool AddMember(DataSocket* ds);
     93 
     94  // Closes all connections and sends a "shutting down" message to all
     95  // connected peers.
     96  void CloseAll();
     97 
     98  // Called when a socket was determined to be closing by the peer (or if the
     99  // connection went dead).
    100  void OnClosing(DataSocket* ds);
    101 
    102  void CheckForTimeout();
    103 
    104 protected:
    105  void DeleteAll();
    106  void BroadcastChangedState(const ChannelMember& member,
    107                             Members* delivery_failures);
    108  void HandleDeliveryFailures(Members* failures);
    109 
    110  // Builds a simple list of "name,id\n" entries for each member.
    111  std::string BuildResponseForNewMember(const ChannelMember& member,
    112                                        std::string* content_type);
    113 
    114 protected:
    115  Members members_;
    116 };
    117 
    118 #endif  // EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_