blob: bc8b8cc9c9b53c1bc8a0a3c5d745e1d0c05a9df4 [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#pragma once
14
15#include <map>
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <string>
18
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/nethelpers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000020#include "webrtc/base/physicalsocketserver.h"
21#include "webrtc/base/scoped_ptr.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000022#include "webrtc/base/signalthread.h"
23#include "webrtc/base/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25typedef std::map<int, std::string> Peers;
26
27struct PeerConnectionClientObserver {
28 virtual void OnSignedIn() = 0; // Called when we're logged on.
29 virtual void OnDisconnected() = 0;
30 virtual void OnPeerConnected(int id, const std::string& name) = 0;
31 virtual void OnPeerDisconnected(int peer_id) = 0;
32 virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
33 virtual void OnMessageSent(int err) = 0;
34 virtual void OnServerConnectionFailure() = 0;
35
36 protected:
37 virtual ~PeerConnectionClientObserver() {}
38};
39
40class PeerConnectionClient : public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000041 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042 public:
43 enum State {
44 NOT_CONNECTED,
45 RESOLVING,
46 SIGNING_IN,
47 CONNECTED,
48 SIGNING_OUT_WAITING,
49 SIGNING_OUT,
50 };
51
52 PeerConnectionClient();
53 ~PeerConnectionClient();
54
55 int id() const;
56 bool is_connected() const;
57 const Peers& peers() const;
58
59 void RegisterObserver(PeerConnectionClientObserver* callback);
60
61 void Connect(const std::string& server, int port,
62 const std::string& client_name);
63
64 bool SendToPeer(int peer_id, const std::string& message);
65 bool SendHangUp(int peer_id);
66 bool IsSendingMessage();
67
68 bool SignOut();
69
70 // implements the MessageHandler interface
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 protected:
74 void DoConnect();
75 void Close();
76 void InitSocketSignals();
77 bool ConnectControlSocket();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 void OnConnect(rtc::AsyncSocket* socket);
79 void OnHangingGetConnect(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 void OnMessageFromPeer(int peer_id, const std::string& message);
81
82 // Quick and dirty support for parsing HTTP header values.
83 bool GetHeaderValue(const std::string& data, size_t eoh,
84 const char* header_pattern, size_t* value);
85
86 bool GetHeaderValue(const std::string& data, size_t eoh,
87 const char* header_pattern, std::string* value);
88
89 // Returns true if the whole response has been read.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090 bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 size_t* content_length);
92
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 void OnRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 void OnHangingGetRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 // Parses a single line entry in the form "<name>,<id>,<connected>"
98 bool ParseEntry(const std::string& entry, std::string* name, int* id,
99 bool* connected);
100
101 int GetResponseStatus(const std::string& response);
102
103 bool ParseServerResponse(const std::string& response, size_t content_length,
104 size_t* peer_id, size_t* eoh);
105
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000106 void OnClose(rtc::AsyncSocket* socket, int err);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 PeerConnectionClientObserver* callback_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000111 rtc::SocketAddress server_address_;
112 rtc::AsyncResolver* resolver_;
kwibergbfefb032016-05-01 14:53:46 -0700113 std::unique_ptr<rtc::AsyncSocket> control_socket_;
114 std::unique_ptr<rtc::AsyncSocket> hanging_get_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 std::string onconnect_data_;
116 std::string control_data_;
117 std::string notification_data_;
118 std::string client_name_;
119 Peers peers_;
120 State state_;
121 int my_id_;
122};
123
jbauch70625e52015-12-09 14:18:14 -0800124#endif // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_