blob: a15924bc054212a9d9d962ca39920b6e17082324 [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
jbauch70625e52015-12-09 14:18:14 -080011#ifndef WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
12#define WEBRTC_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
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020018#include "webrtc/base/messagehandler.h"
19#include "webrtc/base/nethelpers.h"
20#include "webrtc/base/physicalsocketserver.h"
21#include "webrtc/base/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
59 void Connect(const std::string& server, 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 // 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();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 void OnConnect(rtc::AsyncSocket* socket);
77 void OnHangingGetConnect(rtc::AsyncSocket* 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.
81 bool GetHeaderValue(const std::string& data, size_t eoh,
82 const char* header_pattern, size_t* value);
83
84 bool GetHeaderValue(const std::string& data, size_t eoh,
85 const char* header_pattern, std::string* value);
86
87 // Returns true if the whole response has been read.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 size_t* content_length);
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 void OnRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 void OnHangingGetRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
95 // Parses a single line entry in the form "<name>,<id>,<connected>"
96 bool ParseEntry(const std::string& entry, std::string* name, int* id,
97 bool* connected);
98
99 int GetResponseStatus(const std::string& response);
100
101 bool ParseServerResponse(const std::string& response, size_t content_length,
102 size_t* peer_id, size_t* eoh);
103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 void OnClose(rtc::AsyncSocket* socket, int err);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000106 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 PeerConnectionClientObserver* callback_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 rtc::SocketAddress server_address_;
110 rtc::AsyncResolver* resolver_;
kwibergbfefb032016-05-01 14:53:46 -0700111 std::unique_ptr<rtc::AsyncSocket> control_socket_;
112 std::unique_ptr<rtc::AsyncSocket> hanging_get_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 std::string onconnect_data_;
114 std::string control_data_;
115 std::string notification_data_;
116 std::string client_name_;
117 Peers peers_;
118 State state_;
119 int my_id_;
120};
121
jbauch70625e52015-12-09 14:18:14 -0800122#endif // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_