peer_channel.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_SERVER_PEER_CHANNEL_H_
  11. #define EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_
  12. #include <time.h>
  13. #include <queue>
  14. #include <string>
  15. #include <vector>
  16. class DataSocket;
  17. // Represents a single peer connected to the server.
  18. class ChannelMember {
  19. public:
  20. explicit ChannelMember(DataSocket* socket);
  21. ~ChannelMember();
  22. bool connected() const { return connected_; }
  23. int id() const { return id_; }
  24. void set_disconnected() { connected_ = false; }
  25. bool is_wait_request(DataSocket* ds) const;
  26. const std::string& name() const { return name_; }
  27. bool TimedOut();
  28. std::string GetPeerIdHeader() const;
  29. bool NotifyOfOtherMember(const ChannelMember& other);
  30. // Returns a string in the form "name,id\n".
  31. std::string GetEntry() const;
  32. void ForwardRequestToPeer(DataSocket* ds, ChannelMember* peer);
  33. void OnClosing(DataSocket* ds);
  34. void QueueResponse(const std::string& status,
  35. const std::string& content_type,
  36. const std::string& extra_headers,
  37. const std::string& data);
  38. void SetWaitingSocket(DataSocket* ds);
  39. protected:
  40. struct QueuedResponse {
  41. std::string status, content_type, extra_headers, data;
  42. };
  43. DataSocket* waiting_socket_;
  44. int id_;
  45. bool connected_;
  46. time_t timestamp_;
  47. std::string name_;
  48. std::queue<QueuedResponse> queue_;
  49. static int s_member_id_;
  50. };
  51. // Manages all currently connected peers.
  52. class PeerChannel {
  53. public:
  54. typedef std::vector<ChannelMember*> Members;
  55. PeerChannel() {}
  56. ~PeerChannel() { DeleteAll(); }
  57. const Members& members() const { return members_; }
  58. // Returns true if the request should be treated as a new ChannelMember
  59. // request. Otherwise the request is not peerconnection related.
  60. static bool IsPeerConnection(const DataSocket* ds);
  61. // Finds a connected peer that's associated with the |ds| socket.
  62. ChannelMember* Lookup(DataSocket* ds) const;
  63. // Checks if the request has a "peer_id" parameter and if so, looks up the
  64. // peer for which the request is targeted at.
  65. ChannelMember* IsTargetedRequest(const DataSocket* ds) const;
  66. // Adds a new ChannelMember instance to the list of connected peers and
  67. // associates it with the socket.
  68. bool AddMember(DataSocket* ds);
  69. // Closes all connections and sends a "shutting down" message to all
  70. // connected peers.
  71. void CloseAll();
  72. // Called when a socket was determined to be closing by the peer (or if the
  73. // connection went dead).
  74. void OnClosing(DataSocket* ds);
  75. void CheckForTimeout();
  76. protected:
  77. void DeleteAll();
  78. void BroadcastChangedState(const ChannelMember& member,
  79. Members* delivery_failures);
  80. void HandleDeliveryFailures(Members* failures);
  81. // Builds a simple list of "name,id\n" entries for each member.
  82. std::string BuildResponseForNewMember(const ChannelMember& member,
  83. std::string* content_type);
  84. protected:
  85. Members members_;
  86. };
  87. #endif // EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_