peer_connection_client.h (4065B)
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 <cstddef> 15 #include <map> 16 #include <memory> 17 #include <string> 18 19 #include "api/async_dns_resolver.h" 20 #include "api/task_queue/pending_task_safety_flag.h" 21 #include "rtc_base/socket.h" 22 #include "rtc_base/socket_address.h" 23 #include "rtc_base/third_party/sigslot/sigslot.h" 24 25 typedef std::map<int, std::string> Peers; 26 27 struct 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 40 class PeerConnectionClient : public sigslot::has_slots<> { 41 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, 61 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 protected: 71 void DoConnect(); 72 void Close(); 73 void InitSocketSignals(); 74 bool ConnectControlSocket(); 75 void OnConnect(webrtc::Socket* socket); 76 void OnHangingGetConnect(webrtc::Socket* socket); 77 void OnMessageFromPeer(int peer_id, const std::string& message); 78 79 // Quick and dirty support for parsing HTTP header values. 80 bool GetHeaderValue(const std::string& data, 81 size_t eoh, 82 const char* header_pattern, 83 size_t* value); 84 85 bool GetHeaderValue(const std::string& data, 86 size_t eoh, 87 const char* header_pattern, 88 std::string* value); 89 90 // Returns true if the whole response has been read. 91 bool ReadIntoBuffer(webrtc::Socket* socket, 92 std::string* data, 93 size_t* content_length); 94 95 void OnRead(webrtc::Socket* socket); 96 97 void OnHangingGetRead(webrtc::Socket* socket); 98 99 // Parses a single line entry in the form "<name>,<id>,<connected>" 100 bool ParseEntry(const std::string& entry, 101 std::string* name, 102 int* id, 103 bool* connected); 104 105 int GetResponseStatus(const std::string& response); 106 107 bool ParseServerResponse(const std::string& response, 108 size_t content_length, 109 size_t* peer_id, 110 size_t* eoh); 111 112 void OnClose(webrtc::Socket* socket, int err); 113 114 void OnResolveResult(const webrtc::AsyncDnsResolverResult& result); 115 116 PeerConnectionClientObserver* callback_; 117 webrtc::SocketAddress server_address_; 118 std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver_; 119 std::unique_ptr<webrtc::Socket> control_socket_; 120 std::unique_ptr<webrtc::Socket> hanging_get_; 121 std::string onconnect_data_; 122 std::string control_data_; 123 std::string notification_data_; 124 std::string client_name_; 125 Peers peers_; 126 State state_; 127 int my_id_; 128 webrtc::ScopedTaskSafety safety_; 129 }; 130 131 #endif // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_