Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
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.

net_client.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. MMO Client/Server Framework using ASIO
  3. "Happy Birthday Mrs Javidx9!" - javidx9
  4. Videos:
  5. Part #1: https://youtu.be/2hNdkYInj4g
  6. Part #2: https://youtu.be/UbjxGvrDrbw
  7. License (OLC-3)
  8. ~~~~~~~~~~~~~~~
  9. Copyright 2018 - 2020 OneLoneCoder.com
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. 1. Redistributions or derivations of source code must retain the above
  14. copyright notice, this list of conditions and the following disclaimer.
  15. 2. Redistributions or derivative works in binary form must reproduce
  16. the above copyright notice. This list of conditions and the following
  17. disclaimer must be reproduced in the documentation and/or other
  18. materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its
  20. contributors may be used to endorse or promote products derived
  21. from this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. Links
  34. ~~~~~
  35. YouTube: https://www.youtube.com/javidx9
  36. Discord: https://discord.gg/WhwHUMV
  37. Twitter: https://www.twitter.com/javidx9
  38. Twitch: https://www.twitch.tv/javidx9
  39. GitHub: https://www.github.com/onelonecoder
  40. Homepage: https://www.onelonecoder.com
  41. Author
  42. ~~~~~~
  43. David Barr, aka javidx9, ©OneLoneCoder 2019, 2020
  44. */
  45. #pragma once
  46. #include "net_common.h"
  47. #include "net_connection.h"
  48. namespace net
  49. {
  50. template <typename T>
  51. class ClientInterface
  52. {
  53. public:
  54. ClientInterface()
  55. {}
  56. virtual ~ClientInterface()
  57. {
  58. // If the client is destroyed, always try and disconnect from server
  59. Disconnect();
  60. }
  61. public:
  62. // Connect to server with hostname/ip-address and port
  63. bool Connect(const std::string& host, const uint16_t port)
  64. {
  65. try
  66. {
  67. // Resolve hostname/ip-address into tangiable physical address
  68. asio::ip::tcp::resolver resolver(m_context);
  69. asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host, std::to_string(port));
  70. // Create connection
  71. m_connection = std::make_unique<Connection<T>>(Connection<T>::Owner::CLIENT, m_context, asio::ip::tcp::socket(m_context), m_qMessagesIn);
  72. // Tell the connection object to connect to server
  73. m_connection->ConnectToServer(endpoints);
  74. // Start Context Thread
  75. thrContext = std::thread([this]() { m_context.run(); });
  76. }
  77. catch (std::exception& e)
  78. {
  79. std::cerr << "Client Exception: " << e.what() << "\n";
  80. return false;
  81. }
  82. return true;
  83. }
  84. // Disconnect from server
  85. void Disconnect()
  86. {
  87. // If connection exists, and it's connected then...
  88. if(IsConnected())
  89. {
  90. // ...disconnect from server gracefully
  91. m_connection->Disconnect();
  92. }
  93. // Either way, we're also done with the asio context...
  94. m_context.stop();
  95. // ...and its thread
  96. if (thrContext.joinable())
  97. thrContext.join();
  98. // Destroy the connection object
  99. m_connection.release();
  100. }
  101. // Check if client is actually connected to a server
  102. bool IsConnected()
  103. {
  104. if (m_connection)
  105. return m_connection->IsConnected();
  106. else
  107. return false;
  108. }
  109. public:
  110. // Send message to server
  111. void Send(const Message<T>& msg)
  112. {
  113. if (IsConnected())
  114. m_connection->Send(msg);
  115. }
  116. // Retrieve queue of messages from server
  117. net::ts_dequeue<OwnedMessage<T>>& Incoming()
  118. {
  119. return m_qMessagesIn;
  120. }
  121. protected:
  122. // asio context handles the data transfer...
  123. asio::io_context m_context;
  124. // ...but needs a thread of its own to execute its work commands
  125. std::thread thrContext;
  126. // The client has a single instance of a "connection" object, which handles data transfer
  127. std::unique_ptr<Connection<T>> m_connection;
  128. private:
  129. // This is the thread safe queue of incoming messages from server
  130. net::ts_dequeue<OwnedMessage<T>> m_qMessagesIn;
  131. };
  132. }