blob: 56c235a82a985a4f8c02c6235eb8e16da0df8769 [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"
20#include "rtc_base/signal_thread.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
38class PeerConnectionClient : public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000039 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 public:
41 enum State {
42 NOT_CONNECTED,
43 RESOLVING,
44 SIGNING_IN,
45 CONNECTED,
46 SIGNING_OUT_WAITING,
47 SIGNING_OUT,
48 };
49
50 PeerConnectionClient();
51 ~PeerConnectionClient();
52
53 int id() const;
54 bool is_connected() const;
55 const Peers& peers() const;
56
57 void RegisterObserver(PeerConnectionClientObserver* callback);
58
Yves Gerey665174f2018-06-19 15:03:05 +020059 void Connect(const std::string& server,
60 int port,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 const std::string& client_name);
62
63 bool SendToPeer(int peer_id, const std::string& message);
64 bool SendHangUp(int peer_id);
65 bool IsSendingMessage();
66
67 bool SignOut();
68
69 // implements the MessageHandler interface
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
72 protected:
73 void DoConnect();
74 void Close();
75 void InitSocketSignals();
76 bool ConnectControlSocket();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 void OnConnect(rtc::AsyncSocket* socket);
78 void OnHangingGetConnect(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 void OnMessageFromPeer(int peer_id, const std::string& message);
80
81 // Quick and dirty support for parsing HTTP header values.
Yves Gerey665174f2018-06-19 15:03:05 +020082 bool GetHeaderValue(const std::string& data,
83 size_t eoh,
84 const char* header_pattern,
85 size_t* value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Yves Gerey665174f2018-06-19 15:03:05 +020087 bool GetHeaderValue(const std::string& data,
88 size_t eoh,
89 const char* header_pattern,
90 std::string* value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
92 // Returns true if the whole response has been read.
Yves Gerey665174f2018-06-19 15:03:05 +020093 bool ReadIntoBuffer(rtc::AsyncSocket* socket,
94 std::string* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 size_t* content_length);
96
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 void OnRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099 void OnHangingGetRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101 // Parses a single line entry in the form "<name>,<id>,<connected>"
Yves Gerey665174f2018-06-19 15:03:05 +0200102 bool ParseEntry(const std::string& entry,
103 std::string* name,
104 int* id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 bool* connected);
106
107 int GetResponseStatus(const std::string& response);
108
Yves Gerey665174f2018-06-19 15:03:05 +0200109 bool ParseServerResponse(const std::string& response,
110 size_t content_length,
111 size_t* peer_id,
112 size_t* eoh);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000114 void OnClose(rtc::AsyncSocket* socket, int err);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
118 PeerConnectionClientObserver* callback_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000119 rtc::SocketAddress server_address_;
120 rtc::AsyncResolver* resolver_;
kwibergbfefb032016-05-01 14:53:46 -0700121 std::unique_ptr<rtc::AsyncSocket> control_socket_;
122 std::unique_ptr<rtc::AsyncSocket> hanging_get_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 std::string onconnect_data_;
124 std::string control_data_;
125 std::string notification_data_;
126 std::string client_name_;
127 Peers peers_;
128 State state_;
129 int my_id_;
130};
131
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200132#endif // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_