Nidrax

UdpClient.cpp

Oct 4th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include "pch.h"
  2. #include "UdpClient.h"
  3. #include <boost/throw_exception.hpp>
  4. #include <boost/array.hpp>
  5. #include <boost/bind.hpp>
  6. #include <boost/make_shared.hpp>
  7.  
  8. static int PortIn = 32677;
  9. static int PortOut = 32676;
  10. static std::string Loopback = "127.0.0.1";
  11.  
  12. // ReSharper disable once CppPossiblyUninitializedMember
  13. UdpClient::UdpClient()  // NOLINT
  14. {
  15.     try
  16.     {
  17.         _endpointIn = std::make_shared<udp::endpoint>(address::from_string("0.0.0.0"), PortIn);
  18.     }
  19.     catch (std::exception const& e)
  20.     {
  21.         var wut = e.what();
  22.         std::cout << wut << std::endl;
  23.     }
  24.  
  25.     _endpointOut = std::make_shared<udp::endpoint>(address::from_string(Loopback), PortOut);
  26.  
  27.  
  28.     try
  29.     {
  30.         _socketIn = std::make_shared<udp::socket>(_ioServiceIn, *_endpointIn.get());
  31.     }
  32.     catch (std::exception const& e)
  33.     {
  34.         var wut = e.what();
  35.         std::cout << wut << std::endl;
  36.     }
  37.  
  38.     _socketOut = std::make_shared<udp::socket>(_ioServiceOut, *_endpointOut.get());
  39. }
  40.  
  41. void UdpClient::Init(const bool isInput)
  42. {
  43.     if (!isInput)
  44.     {
  45.         _ioServiceOut.run();
  46.         return;
  47.     }
  48.  
  49.     StartReceive();
  50.     _ioServiceIn.run();
  51. }
  52.  
  53. UdpClient::~UdpClient()  // NOLINT
  54. {
  55.     _socketIn->close();
  56.     _socketOut->close();
  57. }
  58.  
  59. void UdpClient::Send(std::string message)
  60. {
  61.     _socketOut->send_to(
  62.         boost::asio::buffer(message), *(_endpointOut.get())
  63.     );
  64. }
  65.  
  66. void UdpClient::Send(int i)
  67. {
  68.     MFiflak fif;
  69.     fif.I = i;
  70.  
  71.     boost::array<char, 4> buff = { fif.C[0], fif.C[1], fif.C[2], fif.C[3] };
  72.  
  73.     std::cout << "Sending data to: " << _endpointOut->address().to_string() << " at port " << _endpointOut->port() << std::endl;
  74.  
  75.     _socketOut->send_to(
  76.         boost::asio::buffer(buff), *(_endpointOut.get())
  77.     );
  78. }
  79.  
  80. boost::signals2::signal<void(double *, int)>* UdpClient::GetIncomer()
  81. {
  82.     return &_incoming;
  83. }
  84.  
  85. boost::signals2::signal<void(std::string)>* UdpClient::GetStringIncomer()
  86. {
  87.     return &_stringIncoming;
  88. }
  89.  
  90. void UdpClient::StartReceive()
  91. {
  92.     _socketIn->async_receive_from(
  93.         boost::asio::buffer(_bufferIn), *_endpointIn.get(),
  94.         boost::bind(
  95.             &UdpClient::Receive, this,
  96.             boost::asio::placeholders::error,
  97.             boost::asio::placeholders::bytes_transferred
  98.         )
  99.     );
  100. }
  101.  
  102. void UdpClient::Receive(const boost::system::error_code& error, std::size_t bytesTransferred)
  103. {
  104.     if (!error)
  105.     {
  106.         const var mSize = sizeof(double) * COMM_COUNT;
  107.  
  108.         if (bytesTransferred != mSize)
  109.         {
  110.             const std::string message(_bufferIn.data() + sizeof(double), _bufferIn.data() + bytesTransferred);
  111.             //Incoming String
  112.         }
  113.         else {
  114.             for (var i = 0; i < _bufferIn.size(); i += mSize)
  115.             {
  116.                 Command comm;
  117.  
  118.                 for (var j = 0; j < mSize; j++)
  119.                     comm.C[j] = _bufferIn[i + j];
  120.  
  121.                 if (comm.D[0] == 5.0)
  122.                 {
  123.                     const std::string message(_bufferIn.data() + sizeof(double), _bufferIn.data() + bytesTransferred);
  124.                     //Incoming String
  125.                 }
  126.                 else
  127.                 {
  128.                     _incoming(comm.D, COMM_COUNT);
  129.                 }
  130.             }
  131.         }
  132.     }
  133.  
  134.     StartReceive();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment