fake_network_dispatcher.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
  5. #define REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
  6. #include <map>
  7. #include "base/compiler_specific.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/synchronization/lock.h"
  10. #include "third_party/webrtc/rtc_base/ip_address.h"
  11. #include "third_party/webrtc/rtc_base/socket_address.h"
  12. namespace base {
  13. class SingleThreadTaskRunner;
  14. } // namespace base
  15. namespace net {
  16. class IOBuffer;
  17. } // namespace net
  18. namespace remoting {
  19. class FakeNetworkDispatcher
  20. : public base::RefCountedThreadSafe<FakeNetworkDispatcher> {
  21. public:
  22. class Node {
  23. public:
  24. virtual ~Node() {}
  25. // Return thread on which ReceivePacket() should be called.
  26. virtual const scoped_refptr<base::SingleThreadTaskRunner>& GetThread()
  27. const = 0;
  28. virtual const rtc::IPAddress& GetAddress() const = 0;
  29. // Deliver a packet sent by a different node.
  30. virtual void ReceivePacket(const rtc::SocketAddress& from,
  31. const rtc::SocketAddress& to,
  32. const scoped_refptr<net::IOBuffer>& data,
  33. int data_size) = 0;
  34. };
  35. FakeNetworkDispatcher();
  36. FakeNetworkDispatcher(const FakeNetworkDispatcher&) = delete;
  37. FakeNetworkDispatcher& operator=(const FakeNetworkDispatcher&) = delete;
  38. rtc::IPAddress AllocateAddress();
  39. // Must be called on the thread that the |node| works on.
  40. void AddNode(Node* node);
  41. void RemoveNode(Node* node);
  42. void DeliverPacket(const rtc::SocketAddress& from,
  43. const rtc::SocketAddress& to,
  44. const scoped_refptr<net::IOBuffer>& data,
  45. int data_size);
  46. private:
  47. typedef std::map<rtc::IPAddress, Node*> NodesMap;
  48. friend class base::RefCountedThreadSafe<FakeNetworkDispatcher>;
  49. virtual ~FakeNetworkDispatcher();
  50. NodesMap nodes_;
  51. base::Lock nodes_lock_;
  52. // A counter used to allocate unique addresses in AllocateAddress().
  53. int allocated_address_;
  54. };
  55. } // namespace remoting
  56. #endif // REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_