blob: 5b5787bc14ecb0ef721e7218ad4421a56ed78ba7 [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
11#ifndef PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_
12#define PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_
13#pragma once
14
15#include <map>
16#include <string>
17
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/nethelpers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/physicalsocketserver.h"
20#include "webrtc/base/scoped_ptr.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000021#include "webrtc/base/signalthread.h"
22#include "webrtc/base/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
24typedef std::map<int, std::string> Peers;
25
26struct PeerConnectionClientObserver {
27 virtual void OnSignedIn() = 0; // Called when we're logged on.
28 virtual void OnDisconnected() = 0;
29 virtual void OnPeerConnected(int id, const std::string& name) = 0;
30 virtual void OnPeerDisconnected(int peer_id) = 0;
31 virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
32 virtual void OnMessageSent(int err) = 0;
33 virtual void OnServerConnectionFailure() = 0;
34
35 protected:
36 virtual ~PeerConnectionClientObserver() {}
37};
38
39class PeerConnectionClient : public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 public:
42 enum State {
43 NOT_CONNECTED,
44 RESOLVING,
45 SIGNING_IN,
46 CONNECTED,
47 SIGNING_OUT_WAITING,
48 SIGNING_OUT,
49 };
50
51 PeerConnectionClient();
52 ~PeerConnectionClient();
53
54 int id() const;
55 bool is_connected() const;
56 const Peers& peers() const;
57
58 void RegisterObserver(PeerConnectionClientObserver* callback);
59
60 void Connect(const std::string& server, int port,
61 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.
82 bool GetHeaderValue(const std::string& data, size_t eoh,
83 const char* header_pattern, size_t* value);
84
85 bool GetHeaderValue(const std::string& data, size_t eoh,
86 const char* header_pattern, std::string* value);
87
88 // Returns true if the whole response has been read.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 size_t* content_length);
91
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092 void OnRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094 void OnHangingGetRead(rtc::AsyncSocket* socket);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
96 // Parses a single line entry in the form "<name>,<id>,<connected>"
97 bool ParseEntry(const std::string& entry, std::string* name, int* id,
98 bool* connected);
99
100 int GetResponseStatus(const std::string& response);
101
102 bool ParseServerResponse(const std::string& response, size_t content_length,
103 size_t* peer_id, size_t* eoh);
104
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000105 void OnClose(rtc::AsyncSocket* socket, int err);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108
109 PeerConnectionClientObserver* callback_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110 rtc::SocketAddress server_address_;
111 rtc::AsyncResolver* resolver_;
112 rtc::scoped_ptr<rtc::AsyncSocket> control_socket_;
113 rtc::scoped_ptr<rtc::AsyncSocket> hanging_get_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 std::string onconnect_data_;
115 std::string control_data_;
116 std::string notification_data_;
117 std::string client_name_;
118 Peers peers_;
119 State state_;
120 int my_id_;
121};
122
123#endif // PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_