peer_connection_client.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
  11. #define EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include "rtc_base/net_helpers.h"
  16. #include "rtc_base/physical_socket_server.h"
  17. #include "rtc_base/signal_thread.h"
  18. #include "rtc_base/third_party/sigslot/sigslot.h"
  19. typedef std::map<int, std::string> Peers;
  20. struct PeerConnectionClientObserver {
  21. virtual void OnSignedIn() = 0; // Called when we're logged on.
  22. virtual void OnDisconnected() = 0;
  23. virtual void OnPeerConnected(int id, const std::string& name) = 0;
  24. virtual void OnPeerDisconnected(int peer_id) = 0;
  25. virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
  26. virtual void OnMessageSent(int err) = 0;
  27. virtual void OnServerConnectionFailure() = 0;
  28. protected:
  29. virtual ~PeerConnectionClientObserver() {}
  30. };
  31. class PeerConnectionClient : public sigslot::has_slots<>,
  32. public rtc::MessageHandler {
  33. public:
  34. enum State {
  35. NOT_CONNECTED,
  36. RESOLVING,
  37. SIGNING_IN,
  38. CONNECTED,
  39. SIGNING_OUT_WAITING,
  40. SIGNING_OUT,
  41. };
  42. PeerConnectionClient();
  43. ~PeerConnectionClient();
  44. int id() const;
  45. bool is_connected() const;
  46. const Peers& peers() const;
  47. void RegisterObserver(PeerConnectionClientObserver* callback);
  48. void Connect(const std::string& server,
  49. int port,
  50. const std::string& client_name);
  51. bool SendToPeer(int peer_id, const std::string& message);
  52. bool SendHangUp(int peer_id);
  53. bool IsSendingMessage();
  54. bool SignOut();
  55. // implements the MessageHandler interface
  56. void OnMessage(rtc::Message* msg);
  57. protected:
  58. void DoConnect();
  59. void Close();
  60. void InitSocketSignals();
  61. bool ConnectControlSocket();
  62. void OnConnect(rtc::AsyncSocket* socket);
  63. void OnHangingGetConnect(rtc::AsyncSocket* socket);
  64. void OnMessageFromPeer(int peer_id, const std::string& message);
  65. // Quick and dirty support for parsing HTTP header values.
  66. bool GetHeaderValue(const std::string& data,
  67. size_t eoh,
  68. const char* header_pattern,
  69. size_t* value);
  70. bool GetHeaderValue(const std::string& data,
  71. size_t eoh,
  72. const char* header_pattern,
  73. std::string* value);
  74. // Returns true if the whole response has been read.
  75. bool ReadIntoBuffer(rtc::AsyncSocket* socket,
  76. std::string* data,
  77. size_t* content_length);
  78. void OnRead(rtc::AsyncSocket* socket);
  79. void OnHangingGetRead(rtc::AsyncSocket* socket);
  80. // Parses a single line entry in the form "<name>,<id>,<connected>"
  81. bool ParseEntry(const std::string& entry,
  82. std::string* name,
  83. int* id,
  84. bool* connected);
  85. int GetResponseStatus(const std::string& response);
  86. bool ParseServerResponse(const std::string& response,
  87. size_t content_length,
  88. size_t* peer_id,
  89. size_t* eoh);
  90. void OnClose(rtc::AsyncSocket* socket, int err);
  91. void OnResolveResult(rtc::AsyncResolverInterface* resolver);
  92. PeerConnectionClientObserver* callback_;
  93. rtc::SocketAddress server_address_;
  94. rtc::AsyncResolver* resolver_;
  95. std::unique_ptr<rtc::AsyncSocket> control_socket_;
  96. std::unique_ptr<rtc::AsyncSocket> hanging_get_;
  97. std::string onconnect_data_;
  98. std::string control_data_;
  99. std::string notification_data_;
  100. std::string client_name_;
  101. Peers peers_;
  102. State state_;
  103. int my_id_;
  104. };
  105. #endif // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_