blob: 8f9c5b6a7569cac7d17ccc9872ae69f73abd9927 [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_CLIENT_PEER_CONNECTION_CLIENT_H_
12#define EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <map>
kwibergbfefb032016-05-01 14:53:46 -070015#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
17
Danil Chapovalovb9201b02022-08-17 17:12:29 +020018#include "api/task_queue/pending_task_safety_flag.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/net_helpers.h"
20#include "rtc_base/physical_socket_server.h"
Artem Titove41c4332018-07-25 15:04:28 +020021#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23typedef std::map<int, std::string> Peers;
24
25struct 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:
35 virtual ~PeerConnectionClientObserver() {}
36};
37
Danil Chapovalovb9201b02022-08-17 17:12:29 +020038class PeerConnectionClient : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 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
Yves Gerey665174f2018-06-19 15:03:05 +020058 void Connect(const std::string& server,
59 int port,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 protected:
69 void DoConnect();
70 void Close();
71 void InitSocketSignals();
72 bool ConnectControlSocket();
Niels Möllerd0b88792021-08-12 10:32:30 +020073 void OnConnect(rtc::Socket* socket);
74 void OnHangingGetConnect(rtc::Socket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 void OnMessageFromPeer(int peer_id, const std::string& message);
76
77 // Quick and dirty support for parsing HTTP header values.
Yves Gerey665174f2018-06-19 15:03:05 +020078 bool GetHeaderValue(const std::string& data,
79 size_t eoh,
80 const char* header_pattern,
81 size_t* value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Yves Gerey665174f2018-06-19 15:03:05 +020083 bool GetHeaderValue(const std::string& data,
84 size_t eoh,
85 const char* header_pattern,
86 std::string* value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
88 // Returns true if the whole response has been read.
Niels Möllerd0b88792021-08-12 10:32:30 +020089 bool ReadIntoBuffer(rtc::Socket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +020090 std::string* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 size_t* content_length);
92
Niels Möllerd0b88792021-08-12 10:32:30 +020093 void OnRead(rtc::Socket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
Niels Möllerd0b88792021-08-12 10:32:30 +020095 void OnHangingGetRead(rtc::Socket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 // Parses a single line entry in the form "<name>,<id>,<connected>"
Yves Gerey665174f2018-06-19 15:03:05 +020098 bool ParseEntry(const std::string& entry,
99 std::string* name,
100 int* id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 bool* connected);
102
103 int GetResponseStatus(const std::string& response);
104
Yves Gerey665174f2018-06-19 15:03:05 +0200105 bool ParseServerResponse(const std::string& response,
106 size_t content_length,
107 size_t* peer_id,
108 size_t* eoh);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
Niels Möllerd0b88792021-08-12 10:32:30 +0200110 void OnClose(rtc::Socket* socket, int err);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 PeerConnectionClientObserver* callback_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 rtc::SocketAddress server_address_;
116 rtc::AsyncResolver* resolver_;
Niels Möllerd0b88792021-08-12 10:32:30 +0200117 std::unique_ptr<rtc::Socket> control_socket_;
118 std::unique_ptr<rtc::Socket> hanging_get_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 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_;
Danil Chapovalovb9201b02022-08-17 17:12:29 +0200126 webrtc::ScopedTaskSafety safety_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127};
128
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_