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 | |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_ |
| 12 | #define WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 18 | #include "webrtc/rtc_base/nethelpers.h" |
| 19 | #include "webrtc/rtc_base/physicalsocketserver.h" |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame^] | 20 | #include "webrtc/rtc_base/signalthread.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/sigslot.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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: |
| 35 | virtual ~PeerConnectionClientObserver() {} |
| 36 | }; |
| 37 | |
| 38 | class PeerConnectionClient : public sigslot::has_slots<>, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 39 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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, int port, |
| 60 | const std::string& client_name); |
| 61 | |
| 62 | bool SendToPeer(int peer_id, const std::string& message); |
| 63 | bool SendHangUp(int peer_id); |
| 64 | bool IsSendingMessage(); |
| 65 | |
| 66 | bool SignOut(); |
| 67 | |
| 68 | // implements the MessageHandler interface |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 69 | void OnMessage(rtc::Message* msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | void DoConnect(); |
| 73 | void Close(); |
| 74 | void InitSocketSignals(); |
| 75 | bool ConnectControlSocket(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | void OnConnect(rtc::AsyncSocket* socket); |
| 77 | void OnHangingGetConnect(rtc::AsyncSocket* socket); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | void OnMessageFromPeer(int peer_id, const std::string& message); |
| 79 | |
| 80 | // Quick and dirty support for parsing HTTP header values. |
| 81 | bool GetHeaderValue(const std::string& data, size_t eoh, |
| 82 | const char* header_pattern, size_t* value); |
| 83 | |
| 84 | bool GetHeaderValue(const std::string& data, size_t eoh, |
| 85 | const char* header_pattern, std::string* value); |
| 86 | |
| 87 | // Returns true if the whole response has been read. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | size_t* content_length); |
| 90 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 91 | void OnRead(rtc::AsyncSocket* socket); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 93 | void OnHangingGetRead(rtc::AsyncSocket* socket); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
| 95 | // Parses a single line entry in the form "<name>,<id>,<connected>" |
| 96 | bool ParseEntry(const std::string& entry, std::string* name, int* id, |
| 97 | bool* connected); |
| 98 | |
| 99 | int GetResponseStatus(const std::string& response); |
| 100 | |
| 101 | bool ParseServerResponse(const std::string& response, size_t content_length, |
| 102 | size_t* peer_id, size_t* eoh); |
| 103 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 104 | void OnClose(rtc::AsyncSocket* socket, int err); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 106 | void OnResolveResult(rtc::AsyncResolverInterface* resolver); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | |
| 108 | PeerConnectionClientObserver* callback_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 109 | rtc::SocketAddress server_address_; |
| 110 | rtc::AsyncResolver* resolver_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 111 | std::unique_ptr<rtc::AsyncSocket> control_socket_; |
| 112 | std::unique_ptr<rtc::AsyncSocket> hanging_get_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | std::string onconnect_data_; |
| 114 | std::string control_data_; |
| 115 | std::string notification_data_; |
| 116 | std::string client_name_; |
| 117 | Peers peers_; |
| 118 | State state_; |
| 119 | int my_id_; |
| 120 | }; |
| 121 | |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 122 | #endif // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_ |