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 "talk/base/sigslot.h"
36
#include "talk/base/physicalsocketserver.h"
37
#include "talk/base/scoped_ptr.h"
39
typedef std::map<int, std::string> Peers;
41
struct PeerConnectionClientObserver {
42
virtual void OnSignedIn() = 0; // Called when we're logged on.
43
virtual void OnDisconnected() = 0;
44
virtual void OnPeerConnected(int id, const std::string& name) = 0;
45
virtual void OnPeerDisconnected(int peer_id) = 0;
46
virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
47
virtual void OnMessageSent(int err) = 0;
50
virtual ~PeerConnectionClientObserver() {}
53
class PeerConnectionClient : public sigslot::has_slots<> {
63
PeerConnectionClient();
64
~PeerConnectionClient();
67
bool is_connected() const;
68
const Peers& peers() const;
70
void RegisterObserver(PeerConnectionClientObserver* callback);
72
bool Connect(const std::string& server, int port,
73
const std::string& client_name);
75
bool SendToPeer(int peer_id, const std::string& message);
76
bool SendHangUp(int peer_id);
77
bool IsSendingMessage();
83
bool ConnectControlSocket();
84
void OnConnect(talk_base::AsyncSocket* socket);
85
void OnHangingGetConnect(talk_base::AsyncSocket* socket);
86
void OnMessageFromPeer(int peer_id, const std::string& message);
88
// Quick and dirty support for parsing HTTP header values.
89
bool GetHeaderValue(const std::string& data, size_t eoh,
90
const char* header_pattern, size_t* value);
92
bool GetHeaderValue(const std::string& data, size_t eoh,
93
const char* header_pattern, std::string* value);
95
// Returns true if the whole response has been read.
96
bool ReadIntoBuffer(talk_base::AsyncSocket* socket, std::string* data,
97
size_t* content_length);
99
void OnRead(talk_base::AsyncSocket* socket);
101
void OnHangingGetRead(talk_base::AsyncSocket* socket);
103
// Parses a single line entry in the form "<name>,<id>,<connected>"
104
bool ParseEntry(const std::string& entry, std::string* name, int* id,
107
int GetResponseStatus(const std::string& response);
109
bool ParseServerResponse(const std::string& response, size_t content_length,
110
size_t* peer_id, size_t* eoh);
112
void OnClose(talk_base::AsyncSocket* socket, int err);
114
PeerConnectionClientObserver* callback_;
115
talk_base::SocketAddress server_address_;
116
talk_base::scoped_ptr<talk_base::AsyncSocket> control_socket_;
117
talk_base::scoped_ptr<talk_base::AsyncSocket> hanging_get_;
118
std::string onconnect_data_;
119
std::string control_data_;
120
std::string notification_data_;
126
#endif // PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_