You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #ifndef TALK_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
  28. #define TALK_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
  29. #pragma once
  30. #include <time.h>
  31. #include <queue>
  32. #include <string>
  33. #include <vector>
  34. class DataSocket;
  35. // Represents a single peer connected to the server.
  36. class ChannelMember {
  37. public:
  38. explicit ChannelMember(DataSocket* socket);
  39. ~ChannelMember();
  40. bool connected() const { return connected_; }
  41. int id() const { return id_; }
  42. void set_disconnected() { connected_ = false; }
  43. bool is_wait_request(DataSocket* ds) const;
  44. const std::string& name() const { return name_; }
  45. bool TimedOut();
  46. std::string GetPeerIdHeader() const;
  47. bool NotifyOfOtherMember(const ChannelMember& other);
  48. // Returns a string in the form "name,id\n".
  49. std::string GetEntry() const;
  50. void ForwardRequestToPeer(DataSocket* ds, ChannelMember* peer);
  51. void OnClosing(DataSocket* ds);
  52. void QueueResponse(const std::string& status, const std::string& content_type,
  53. const std::string& extra_headers, const std::string& data);
  54. void SetWaitingSocket(DataSocket* ds);
  55. protected:
  56. struct QueuedResponse {
  57. std::string status, content_type, extra_headers, data;
  58. };
  59. DataSocket* waiting_socket_;
  60. int id_;
  61. bool connected_;
  62. time_t timestamp_;
  63. std::string name_;
  64. std::queue<QueuedResponse> queue_;
  65. static int s_member_id_;
  66. };
  67. // Manages all currently connected peers.
  68. class PeerChannel {
  69. public:
  70. typedef std::vector<ChannelMember*> Members;
  71. PeerChannel() {
  72. }
  73. ~PeerChannel() {
  74. DeleteAll();
  75. }
  76. const Members& members() const { return members_; }
  77. // Returns true if the request should be treated as a new ChannelMember
  78. // request. Otherwise the request is not peerconnection related.
  79. static bool IsPeerConnection(const DataSocket* ds);
  80. // Finds a connected peer that's associated with the |ds| socket.
  81. ChannelMember* Lookup(DataSocket* ds) const;
  82. // Checks if the request has a "peer_id" parameter and if so, looks up the
  83. // peer for which the request is targeted at.
  84. ChannelMember* IsTargetedRequest(const DataSocket* ds) const;
  85. // Adds a new ChannelMember instance to the list of connected peers and
  86. // associates it with the socket.
  87. bool AddMember(DataSocket* ds);
  88. // Closes all connections and sends a "shutting down" message to all
  89. // connected peers.
  90. void CloseAll();
  91. // Called when a socket was determined to be closing by the peer (or if the
  92. // connection went dead).
  93. void OnClosing(DataSocket* ds);
  94. void CheckForTimeout();
  95. protected:
  96. void DeleteAll();
  97. void BroadcastChangedState(const ChannelMember& member,
  98. Members* delivery_failures);
  99. void HandleDeliveryFailures(Members* failures);
  100. // Builds a simple list of "name,id\n" entries for each member.
  101. std::string BuildResponseForNewMember(const ChannelMember& member,
  102. std::string* content_type);
  103. protected:
  104. Members members_;
  105. };
  106. #endif // TALK_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_