xref: /aosp_15_r20/external/webrtc/examples/peerconnection/client/peer_connection_client.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_CLIENT_PEER_CONNECTION_CLIENT_H_
12 #define EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 
18 #include "api/task_queue/pending_task_safety_flag.h"
19 #include "rtc_base/net_helpers.h"
20 #include "rtc_base/physical_socket_server.h"
21 #include "rtc_base/third_party/sigslot/sigslot.h"
22 
23 typedef std::map<int, std::string> Peers;
24 
25 struct PeerConnectionClientObserver {
26   virtual void OnSignedIn() = 0;  // Called when we're logged on.
27   virtual void OnDisconnected() = 0;
28   virtual void OnPeerConnected(int id, const std::string& name) = 0;
29   virtual void OnPeerDisconnected(int peer_id) = 0;
30   virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
31   virtual void OnMessageSent(int err) = 0;
32   virtual void OnServerConnectionFailure() = 0;
33 
34  protected:
~PeerConnectionClientObserverPeerConnectionClientObserver35   virtual ~PeerConnectionClientObserver() {}
36 };
37 
38 class PeerConnectionClient : public sigslot::has_slots<> {
39  public:
40   enum State {
41     NOT_CONNECTED,
42     RESOLVING,
43     SIGNING_IN,
44     CONNECTED,
45     SIGNING_OUT_WAITING,
46     SIGNING_OUT,
47   };
48 
49   PeerConnectionClient();
50   ~PeerConnectionClient();
51 
52   int id() const;
53   bool is_connected() const;
54   const Peers& peers() const;
55 
56   void RegisterObserver(PeerConnectionClientObserver* callback);
57 
58   void Connect(const std::string& server,
59                int port,
60                const std::string& client_name);
61 
62   bool SendToPeer(int peer_id, const std::string& message);
63   bool SendHangUp(int peer_id);
64   bool IsSendingMessage();
65 
66   bool SignOut();
67 
68  protected:
69   void DoConnect();
70   void Close();
71   void InitSocketSignals();
72   bool ConnectControlSocket();
73   void OnConnect(rtc::Socket* socket);
74   void OnHangingGetConnect(rtc::Socket* socket);
75   void OnMessageFromPeer(int peer_id, const std::string& message);
76 
77   // Quick and dirty support for parsing HTTP header values.
78   bool GetHeaderValue(const std::string& data,
79                       size_t eoh,
80                       const char* header_pattern,
81                       size_t* value);
82 
83   bool GetHeaderValue(const std::string& data,
84                       size_t eoh,
85                       const char* header_pattern,
86                       std::string* value);
87 
88   // Returns true if the whole response has been read.
89   bool ReadIntoBuffer(rtc::Socket* socket,
90                       std::string* data,
91                       size_t* content_length);
92 
93   void OnRead(rtc::Socket* socket);
94 
95   void OnHangingGetRead(rtc::Socket* socket);
96 
97   // Parses a single line entry in the form "<name>,<id>,<connected>"
98   bool ParseEntry(const std::string& entry,
99                   std::string* name,
100                   int* id,
101                   bool* connected);
102 
103   int GetResponseStatus(const std::string& response);
104 
105   bool ParseServerResponse(const std::string& response,
106                            size_t content_length,
107                            size_t* peer_id,
108                            size_t* eoh);
109 
110   void OnClose(rtc::Socket* socket, int err);
111 
112   void OnResolveResult(rtc::AsyncResolverInterface* resolver);
113 
114   PeerConnectionClientObserver* callback_;
115   rtc::SocketAddress server_address_;
116   rtc::AsyncResolver* resolver_;
117   std::unique_ptr<rtc::Socket> control_socket_;
118   std::unique_ptr<rtc::Socket> hanging_get_;
119   std::string onconnect_data_;
120   std::string control_data_;
121   std::string notification_data_;
122   std::string client_name_;
123   Peers peers_;
124   State state_;
125   int my_id_;
126   webrtc::ScopedTaskSafety safety_;
127 };
128 
129 #endif  // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
130