Donald E Curtis | 6f6a712 | 2015-08-05 22:48:13 | [diff] [blame] | 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 | |
jbauch | fbcf291 | 2015-12-09 22:18:14 | [diff] [blame] | 11 | #ifndef WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ |
| 12 | #define WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ |
Donald E Curtis | 6f6a712 | 2015-08-05 22:48:13 | [diff] [blame] | 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, const std::string& content_type, |
| 48 | const std::string& extra_headers, const std::string& data); |
| 49 | |
| 50 | void SetWaitingSocket(DataSocket* ds); |
| 51 | |
| 52 | protected: |
| 53 | struct QueuedResponse { |
| 54 | std::string status, content_type, extra_headers, data; |
| 55 | }; |
| 56 | |
| 57 | DataSocket* waiting_socket_; |
| 58 | int id_; |
| 59 | bool connected_; |
| 60 | time_t timestamp_; |
| 61 | std::string name_; |
| 62 | std::queue<QueuedResponse> queue_; |
| 63 | static int s_member_id_; |
| 64 | }; |
| 65 | |
| 66 | // Manages all currently connected peers. |
| 67 | class PeerChannel { |
| 68 | public: |
| 69 | typedef std::vector<ChannelMember*> Members; |
| 70 | |
| 71 | PeerChannel() { |
| 72 | } |
| 73 | |
| 74 | ~PeerChannel() { |
| 75 | DeleteAll(); |
| 76 | } |
| 77 | |
| 78 | const Members& members() const { return members_; } |
| 79 | |
| 80 | // Returns true if the request should be treated as a new ChannelMember |
| 81 | // request. Otherwise the request is not peerconnection related. |
| 82 | static bool IsPeerConnection(const DataSocket* ds); |
| 83 | |
| 84 | // Finds a connected peer that's associated with the |ds| socket. |
| 85 | ChannelMember* Lookup(DataSocket* ds) const; |
| 86 | |
| 87 | // Checks if the request has a "peer_id" parameter and if so, looks up the |
| 88 | // peer for which the request is targeted at. |
| 89 | ChannelMember* IsTargetedRequest(const DataSocket* ds) const; |
| 90 | |
| 91 | // Adds a new ChannelMember instance to the list of connected peers and |
| 92 | // associates it with the socket. |
| 93 | bool AddMember(DataSocket* ds); |
| 94 | |
| 95 | // Closes all connections and sends a "shutting down" message to all |
| 96 | // connected peers. |
| 97 | void CloseAll(); |
| 98 | |
| 99 | // Called when a socket was determined to be closing by the peer (or if the |
| 100 | // connection went dead). |
| 101 | void OnClosing(DataSocket* ds); |
| 102 | |
| 103 | void CheckForTimeout(); |
| 104 | |
| 105 | protected: |
| 106 | void DeleteAll(); |
| 107 | void BroadcastChangedState(const ChannelMember& member, |
| 108 | Members* delivery_failures); |
| 109 | void HandleDeliveryFailures(Members* failures); |
| 110 | |
| 111 | // Builds a simple list of "name,id\n" entries for each member. |
| 112 | std::string BuildResponseForNewMember(const ChannelMember& member, |
| 113 | std::string* content_type); |
| 114 | |
| 115 | protected: |
| 116 | Members members_; |
| 117 | }; |
| 118 | |
jbauch | fbcf291 | 2015-12-09 22:18:14 | [diff] [blame] | 119 | #endif // WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ |