blob: 00d2192681a0c7b8eb03683777cdaac01b2daab9 [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
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/net_helpers.h"
19#include "rtc_base/physical_socket_server.h"
Artem Titove41c4332018-07-25 15:04:28 +020020#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22typedef std::map<int, std::string> Peers;
23
24struct PeerConnectionClientObserver {
25 virtual void OnSignedIn() = 0; // Called when we're logged on.
26 virtual void OnDisconnected() = 0;
27 virtual void OnPeerConnected(int id, const std::string& name) = 0;
28 virtual void OnPeerDisconnected(int peer_id) = 0;
29 virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
30 virtual void OnMessageSent(int err) = 0;
31 virtual void OnServerConnectionFailure() = 0;
32
33 protected:
34 virtual ~PeerConnectionClientObserver() {}
35};
36
37class PeerConnectionClient : public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000038 public rtc::MessageHandler {
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
68 // implements the MessageHandler interface
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069 void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71 protected:
72 void DoConnect();
73 void Close();
74 void InitSocketSignals();
75 bool ConnectControlSocket();
Niels Möllerd0b88792021-08-12 10:32:30 +020076 void OnConnect(rtc::Socket* socket);
77 void OnHangingGetConnect(rtc::Socket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 void OnMessageFromPeer(int peer_id, const std::string& message);
79
80 // Quick and dirty support for parsing HTTP header values.
Yves Gerey665174f2018-06-19 15:03:05 +020081 bool GetHeaderValue(const std::string& data,
82 size_t eoh,
83 const char* header_pattern,
84 size_t* value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
Yves Gerey665174f2018-06-19 15:03:05 +020086 bool GetHeaderValue(const std::string& data,
87 size_t eoh,
88 const char* header_pattern,
89 std::string* value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
91 // Returns true if the whole response has been read.
Niels Möllerd0b88792021-08-12 10:32:30 +020092 bool ReadIntoBuffer(rtc::Socket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +020093 std::string* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 size_t* content_length);
95
Niels Möllerd0b88792021-08-12 10:32:30 +020096 void OnRead(rtc::Socket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
Niels Möllerd0b88792021-08-12 10:32:30 +020098 void OnHangingGetRead(rtc::Socket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100 // Parses a single line entry in the form "<name>,<id>,<connected>"
Yves Gerey665174f2018-06-19 15:03:05 +0200101 bool ParseEntry(const std::string& entry,
102 std::string* name,
103 int* id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 bool* connected);
105
106 int GetResponseStatus(const std::string& response);
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108 bool ParseServerResponse(const std::string& response,
109 size_t content_length,
110 size_t* peer_id,
111 size_t* eoh);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
Niels Möllerd0b88792021-08-12 10:32:30 +0200113 void OnClose(rtc::Socket* socket, int err);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
117 PeerConnectionClientObserver* callback_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000118 rtc::SocketAddress server_address_;
119 rtc::AsyncResolver* resolver_;
Niels Möllerd0b88792021-08-12 10:32:30 +0200120 std::unique_ptr<rtc::Socket> control_socket_;
121 std::unique_ptr<rtc::Socket> hanging_get_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 std::string onconnect_data_;
123 std::string control_data_;
124 std::string notification_data_;
125 std::string client_name_;
126 Peers peers_;
127 State state_;
128 int my_id_;
129};
130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_