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.

111 lines
3.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2010 Google Inc. All Rights Reserved,
  2. //
  3. // Author: Justin Uberti (juberti@google.com)
  4. #ifndef TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
  5. #define TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
  6. #include <string>
  7. #include "talk/base/basicpacketsocketfactory.h"
  8. #include "talk/base/scoped_ptr.h"
  9. #include "talk/p2p/base/portallocator.h"
  10. #include "talk/p2p/base/udpport.h"
  11. namespace talk_base {
  12. class SocketFactory;
  13. class Thread;
  14. }
  15. namespace cricket {
  16. class FakePortAllocatorSession : public PortAllocatorSession {
  17. public:
  18. FakePortAllocatorSession(talk_base::Thread* worker_thread,
  19. talk_base::PacketSocketFactory* factory,
  20. const std::string& content_name,
  21. int component,
  22. const std::string& ice_ufrag,
  23. const std::string& ice_pwd)
  24. : PortAllocatorSession(content_name, component, ice_ufrag, ice_pwd,
  25. cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG),
  26. worker_thread_(worker_thread),
  27. factory_(factory),
  28. network_("network", "unittest",
  29. talk_base::IPAddress(INADDR_LOOPBACK), 8),
  30. port_(NULL), running_(false),
  31. port_config_count_(0) {
  32. network_.AddIP(talk_base::IPAddress(INADDR_LOOPBACK));
  33. }
  34. virtual void GetInitialPorts() {
  35. GetPortConfigurations();
  36. }
  37. virtual void GetPortConfigurations() {
  38. if (!port_) {
  39. port_.reset(cricket::UDPPort::Create(worker_thread_, factory_,
  40. &network_, network_.ip(), 0, 0,
  41. username(),
  42. password()));
  43. AddPort(port_.get());
  44. }
  45. ++port_config_count_;
  46. }
  47. virtual void StartGetAllPorts() { running_ = true; }
  48. virtual void StopGetAllPorts() { running_ = false; }
  49. virtual bool IsGettingAllPorts() { return running_; }
  50. int port_config_count() { return port_config_count_; }
  51. void AddPort(cricket::Port* port) {
  52. port->set_component(component_);
  53. port->set_generation(0);
  54. port->SignalAddressReady.connect(
  55. this, &FakePortAllocatorSession::OnAddressReady);
  56. port->PrepareAddress();
  57. SignalPortReady(this, port);
  58. }
  59. void OnAddressReady(cricket::Port* port) {
  60. SignalCandidatesReady(this, port->Candidates());
  61. SignalCandidatesAllocationDone(this);
  62. }
  63. private:
  64. talk_base::Thread* worker_thread_;
  65. talk_base::PacketSocketFactory* factory_;
  66. talk_base::Network network_;
  67. talk_base::scoped_ptr<cricket::Port> port_;
  68. bool running_;
  69. int port_config_count_;
  70. };
  71. class FakePortAllocator : public cricket::PortAllocator {
  72. public:
  73. FakePortAllocator(talk_base::Thread* worker_thread,
  74. talk_base::PacketSocketFactory* factory)
  75. : worker_thread_(worker_thread), factory_(factory) {
  76. if (factory_ == NULL) {
  77. owned_factory_.reset(new talk_base::BasicPacketSocketFactory(
  78. worker_thread_));
  79. factory_ = owned_factory_.get();
  80. }
  81. }
  82. virtual cricket::PortAllocatorSession* CreateSessionInternal(
  83. const std::string& content_name,
  84. int component,
  85. const std::string& ice_ufrag,
  86. const std::string& ice_pwd) {
  87. return new FakePortAllocatorSession(
  88. worker_thread_, factory_, content_name, component, ice_ufrag, ice_pwd);
  89. }
  90. private:
  91. talk_base::Thread* worker_thread_;
  92. talk_base::PacketSocketFactory* factory_;
  93. talk_base::scoped_ptr<talk_base::BasicPacketSocketFactory> owned_factory_;
  94. };
  95. } // namespace cricket
  96. #endif // TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_