1 /*
2  *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
3  *
4  *  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.
9  */
10 
11 #ifndef EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
12 #define EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 
18 #include "rtc_base/net_helpers.h"
19 #include "rtc_base/physical_socket_server.h"
20 #include "rtc_base/signal_thread.h"
21 #include "rtc_base/third_party/sigslot/sigslot.h"
22 
23 typedef std::map<int, std::string> Peers;
24 
25 struct 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:
~PeerConnectionClientObserverPeerConnectionClientObserver35   virtual ~PeerConnectionClientObserver() {}
36 };
37 
38 class PeerConnectionClient : public sigslot::has_slots<>,
39                              public rtc::MessageHandler {
40  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,
60                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
70   void OnMessage(rtc::Message* msg);
71 
72  protected:
73   void DoConnect();
74   void Close();
75   void InitSocketSignals();
76   bool ConnectControlSocket();
77   void OnConnect(rtc::AsyncSocket* socket);
78   void OnHangingGetConnect(rtc::AsyncSocket* socket);
79   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,
83                       size_t eoh,
84                       const char* header_pattern,
85                       size_t* value);
86 
87   bool GetHeaderValue(const std::string& data,
88                       size_t eoh,
89                       const char* header_pattern,
90                       std::string* value);
91 
92   // Returns true if the whole response has been read.
93   bool ReadIntoBuffer(rtc::AsyncSocket* socket,
94                       std::string* data,
95                       size_t* content_length);
96 
97   void OnRead(rtc::AsyncSocket* socket);
98 
99   void OnHangingGetRead(rtc::AsyncSocket* socket);
100 
101   // Parses a single line entry in the form "<name>,<id>,<connected>"
102   bool ParseEntry(const std::string& entry,
103                   std::string* name,
104                   int* id,
105                   bool* connected);
106 
107   int GetResponseStatus(const std::string& response);
108 
109   bool ParseServerResponse(const std::string& response,
110                            size_t content_length,
111                            size_t* peer_id,
112                            size_t* eoh);
113 
114   void OnClose(rtc::AsyncSocket* socket, int err);
115 
116   void OnResolveResult(rtc::AsyncResolverInterface* resolver);
117 
118   PeerConnectionClientObserver* callback_;
119   rtc::SocketAddress server_address_;
120   rtc::AsyncResolver* resolver_;
121   std::unique_ptr<rtc::AsyncSocket> control_socket_;
122   std::unique_ptr<rtc::AsyncSocket> hanging_get_;
123   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 
132 #endif  // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
133