blob: c3bb1ad10b5867527d7e40c182f3d70363e2bd80 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
12#define EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <time.h>
15
16#include <queue>
17#include <string>
18#include <vector>
19
20class DataSocket;
21
22// Represents a single peer connected to the server.
23class 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
Yves Gerey665174f2018-06-19 15:03:05 +020047 void QueueResponse(const std::string& status,
48 const std::string& content_type,
49 const std::string& extra_headers,
50 const std::string& data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
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.
69class PeerChannel {
70 public:
71 typedef std::vector<ChannelMember*> Members;
72
Yves Gerey665174f2018-06-19 15:03:05 +020073 PeerChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
Yves Gerey665174f2018-06-19 15:03:05 +020075 ~PeerChannel() { DeleteAll(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200118#endif // EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_