RLPXSocket.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file RLPXSocket.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2015
  17. */
  18. #pragma once
  19. #include "Common.h"
  20. namespace dev
  21. {
  22. namespace p2p
  23. {
  24. /**
  25. * @brief Shared pointer wrapper for ASIO TCP socket.
  26. *
  27. * Thread Safety
  28. * Distinct Objects: Safe.
  29. * Shared objects: Unsafe.
  30. * * an instance method must not be called concurrently
  31. */
  32. class RLPXSocket: public std::enable_shared_from_this<RLPXSocket>
  33. {
  34. public:
  35. RLPXSocket(ba::io_service& _ioService): m_socket(_ioService) {}
  36. ~RLPXSocket() { close(); }
  37. bool isConnected() const { return m_socket.is_open(); }
  38. void close() { try { boost::system::error_code ec; m_socket.shutdown(bi::tcp::socket::shutdown_both, ec); if (m_socket.is_open()) m_socket.close(); } catch (...){} }
  39. bi::tcp::endpoint remoteEndpoint() { boost::system::error_code ec; return m_socket.remote_endpoint(ec); }
  40. bi::tcp::socket& ref() { return m_socket; }
  41. protected:
  42. bi::tcp::socket m_socket;
  43. };
  44. }
  45. }