henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame^] | 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame^] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 18 | #include "webrtc/base/nethelpers.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 19 | #include "webrtc/base/physicalsocketserver.h" |
| 20 | #include "webrtc/base/scoped_ptr.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 21 | #include "webrtc/base/signalthread.h" |
| 22 | #include "webrtc/base/sigslot.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | |
| 24 | typedef std::map<int, std::string> Peers; |
| 25 | |
| 26 | struct 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 | |
| 39 | class PeerConnectionClient : public sigslot::has_slots<>, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 40 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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, 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 70 | void OnMessage(rtc::Message* msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | |
| 72 | protected: |
| 73 | void DoConnect(); |
| 74 | void Close(); |
| 75 | void InitSocketSignals(); |
| 76 | bool ConnectControlSocket(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 77 | void OnConnect(rtc::AsyncSocket* socket); |
| 78 | void OnHangingGetConnect(rtc::AsyncSocket* socket); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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, 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 89 | bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | size_t* content_length); |
| 91 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 92 | void OnRead(rtc::AsyncSocket* socket); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 94 | void OnHangingGetRead(rtc::AsyncSocket* socket); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
| 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 105 | void OnClose(rtc::AsyncSocket* socket, int err); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 107 | void OnResolveResult(rtc::AsyncResolverInterface* resolver); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | |
| 109 | PeerConnectionClientObserver* callback_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 110 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | 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_ |