MockReflector.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // libtgvoip is free and unencumbered public domain software.
  3. // For more information, see http://unlicense.org or the UNLICENSE file
  4. // you should have received with this source code distribution.
  5. //
  6. #ifndef TGVOIP_MOCK_REFLECTOR
  7. #define TGVOIP_MOCK_REFLECTOR
  8. #include <string>
  9. #include <unordered_map>
  10. #include <array>
  11. #include <stdint.h>
  12. #include <pthread.h>
  13. #include <sys/socket.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include <netdb.h>
  17. #include <net/if.h>
  18. #include <sys/ioctl.h>
  19. #include <unistd.h>
  20. #include <netinet/tcp.h>
  21. namespace tgvoip{
  22. namespace test{
  23. class MockReflector{
  24. public:
  25. MockReflector(std::string bindAddress, uint16_t bindPort);
  26. ~MockReflector();
  27. void Start();
  28. void Stop();
  29. void SetDropAllPackets(bool drop);
  30. static std::array<std::array<uint8_t, 16>, 2> GeneratePeerTags();
  31. private:
  32. void RunThread();
  33. struct ClientPair{
  34. sockaddr_in addr0={0};
  35. sockaddr_in addr1={0};
  36. };
  37. std::unordered_map<uint64_t, ClientPair> clients; // clients are identified by the first half of their peer_tag
  38. int sfd;
  39. pthread_t thread;
  40. bool running=false;
  41. bool dropAllPackets=false;
  42. };
  43. }
  44. }
  45. #endif //TGVOIP_MOCK_REFLECTOR