SocketManager.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_SOCKETMANAGER_HPP
  28. #define ZT_SOCKETMANAGER_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <map>
  32. #include <stdexcept>
  33. #include "Constants.hpp"
  34. #include "InetAddress.hpp"
  35. #include "Buffer.hpp"
  36. #include "NonCopyable.hpp"
  37. #include "Socket.hpp"
  38. namespace ZeroTier {
  39. /**
  40. * Socket I/O implementation
  41. */
  42. class SocketManager : NonCopyable
  43. {
  44. public:
  45. SocketManager() {}
  46. virtual ~SocketManager() {}
  47. /**
  48. * Send a message to a remote peer
  49. *
  50. * @param to Destination address
  51. * @param tcp Use TCP?
  52. * @param autoConnectTcp If true, automatically initiate TCP connection if there is none
  53. * @param msg Message to send
  54. * @param msglen Length of message
  55. */
  56. virtual bool send(
  57. const InetAddress &to,
  58. bool tcp,
  59. bool autoConnectTcp,
  60. const void *msg,
  61. unsigned int msglen) = 0;
  62. /**
  63. * Send a message to a remote peer via UDP (shortcut for setting both TCP params to false in send)
  64. *
  65. * @param to Destination address
  66. * @param msg Message to send
  67. * @param msglen Length of message
  68. */
  69. inline bool sendUdp(
  70. const InetAddress &to,
  71. const void *msg,
  72. unsigned int msglen) { return send(to,false,false,msg,msglen); }
  73. /**
  74. * Perform I/O polling operation (e.g. select())
  75. *
  76. * If called concurrently, one will block until the other completes.
  77. *
  78. * @param timeout Timeout in milliseconds, may return sooner if whack() is called
  79. * @param handler Packet data handler
  80. * @param arg Void argument to packet data handler
  81. */
  82. virtual void poll(
  83. unsigned long timeout,
  84. void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
  85. void *arg);
  86. /**
  87. * Cause current or next blocking poll() operation to timeout immediately
  88. */
  89. virtual void whack() = 0;
  90. /**
  91. * Close TCP sockets
  92. */
  93. virtual void closeTcpSockets() = 0;
  94. };
  95. } // namespace ZeroTier
  96. #endif