3 * Copyright 2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_
29 #define PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_
35 #include "webrtc/base/nethelpers.h"
36 #include "webrtc/base/physicalsocketserver.h"
37 #include "webrtc/base/scoped_ptr.h"
38 #include "webrtc/base/signalthread.h"
39 #include "webrtc/base/sigslot.h"
41 typedef std::map<int, std::string> Peers;
43 struct PeerConnectionClientObserver {
44 virtual void OnSignedIn() = 0; // Called when we're logged on.
45 virtual void OnDisconnected() = 0;
46 virtual void OnPeerConnected(int id, const std::string& name) = 0;
47 virtual void OnPeerDisconnected(int peer_id) = 0;
48 virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
49 virtual void OnMessageSent(int err) = 0;
50 virtual void OnServerConnectionFailure() = 0;
53 virtual ~PeerConnectionClientObserver() {}
56 class PeerConnectionClient : public sigslot::has_slots<>,
57 public rtc::MessageHandler {
68 PeerConnectionClient();
69 ~PeerConnectionClient();
72 bool is_connected() const;
73 const Peers& peers() const;
75 void RegisterObserver(PeerConnectionClientObserver* callback);
77 void Connect(const std::string& server, int port,
78 const std::string& client_name);
80 bool SendToPeer(int peer_id, const std::string& message);
81 bool SendHangUp(int peer_id);
82 bool IsSendingMessage();
86 // implements the MessageHandler interface
87 void OnMessage(rtc::Message* msg);
92 void InitSocketSignals();
93 bool ConnectControlSocket();
94 void OnConnect(rtc::AsyncSocket* socket);
95 void OnHangingGetConnect(rtc::AsyncSocket* socket);
96 void OnMessageFromPeer(int peer_id, const std::string& message);
98 // Quick and dirty support for parsing HTTP header values.
99 bool GetHeaderValue(const std::string& data, size_t eoh,
100 const char* header_pattern, size_t* value);
102 bool GetHeaderValue(const std::string& data, size_t eoh,
103 const char* header_pattern, std::string* value);
105 // Returns true if the whole response has been read.
106 bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data,
107 size_t* content_length);
109 void OnRead(rtc::AsyncSocket* socket);
111 void OnHangingGetRead(rtc::AsyncSocket* socket);
113 // Parses a single line entry in the form "<name>,<id>,<connected>"
114 bool ParseEntry(const std::string& entry, std::string* name, int* id,
117 int GetResponseStatus(const std::string& response);
119 bool ParseServerResponse(const std::string& response, size_t content_length,
120 size_t* peer_id, size_t* eoh);
122 void OnClose(rtc::AsyncSocket* socket, int err);
124 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
126 PeerConnectionClientObserver* callback_;
127 rtc::SocketAddress server_address_;
128 rtc::AsyncResolver* resolver_;
129 rtc::scoped_ptr<rtc::AsyncSocket> control_socket_;
130 rtc::scoped_ptr<rtc::AsyncSocket> hanging_get_;
131 std::string onconnect_data_;
132 std::string control_data_;
133 std::string notification_data_;
134 std::string client_name_;
140 #endif // PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_