|
21 | 21 | #include <thread>
|
22 | 22 | #include <vector>
|
23 | 23 |
|
| 24 | +// ----------------------- |
| 25 | +#include <nlohmann/json.hpp> |
| 26 | +using json = nlohmann::json; |
| 27 | +// ----------------------- |
24 | 28 |
|
25 | 29 | // Report a failure
|
26 | 30 | void fail(boost::system::error_code ec, char const* what) {
|
@@ -637,11 +641,56 @@ class session : public boost::asio::coroutine,
|
637 | 641 |
|
638 | 642 | std::string reply_echo = get_data_echo();
|
639 | 643 | unlock_data_echo();
|
| 644 | + |
| 645 | + std::string reply_echo_copy = reply_echo; |
| 646 | + |
| 647 | + reply_echo = reply_echo + std::string(" "); |
| 648 | + |
| 649 | + |
| 650 | + std::size_t n = reply_echo.length(); |
| 651 | + std::string escaped; |
| 652 | + escaped.reserve(n * 2); // pessimistic preallocation |
| 653 | + |
| 654 | + for (std::size_t i = 0; i < n; ++i) { |
| 655 | + if (reply_echo[i] == '\\' && reply_echo[i + 1] == '\"') { |
| 656 | + |
| 657 | + } |
| 658 | + else { |
| 659 | + escaped += reply_echo[i]; |
| 660 | + } |
| 661 | + } |
| 662 | + |
| 663 | + reply_echo = escaped; |
| 664 | + |
| 665 | + |
| 666 | + |
| 667 | + size_t endpos = reply_echo.find_last_not_of(" \t"); |
| 668 | + size_t startpos = reply_echo.find_first_not_of(" \t"); |
| 669 | + if (std::string::npos != endpos) |
| 670 | + { |
| 671 | + reply_echo = reply_echo.substr(0, endpos + 1); |
| 672 | + reply_echo = reply_echo.substr(startpos); |
| 673 | + } |
| 674 | + else { |
| 675 | + reply_echo.erase(std::remove(std::begin(reply_echo), std::end(reply_echo), ' '), std::end(reply_echo)); |
| 676 | + } |
| 677 | + |
| 678 | + |
| 679 | + |
| 680 | + |
| 681 | + |
| 682 | + |
| 683 | + if (reply_echo.length() > 0 && json::accept(reply_echo)) { |
| 684 | + //If valid JSON we use the modified string we made, otherwise use the original string |
| 685 | + }else{ |
| 686 | + reply_echo = std::string("\"") + reply_echo_copy + std::string("\""); |
| 687 | + } |
| 688 | + |
640 | 689 |
|
641 | 690 | std::string local_ip_str = get_ip_echo();
|
642 | 691 | unlock_ip_echo();
|
643 | 692 |
|
644 |
| - std::string reply_message = "{\"vr_trackers\": [" + reply_data + "], \"echo\": \"" + reply_echo + "\", \"ip\": \"" + local_ip_str + "\"" + "}"; |
| 693 | + std::string reply_message = "{\"vr_trackers\": [" + reply_data + "], \"echo\": " + reply_echo + ", \"ip\": [" + local_ip_str + "]" + "}"; |
645 | 694 |
|
646 | 695 |
|
647 | 696 | // std::string reply_message = "{\"vr_trackers\": [" + reply_data + "], \"echo\": \"" + reply_echo + "\"" + "}";
|
@@ -780,7 +829,7 @@ int SaveLocalIP()
|
780 | 829 |
|
781 | 830 |
|
782 | 831 | std::string current_ip(inet_ntoa(addr));
|
783 |
| - output_string = output_string + current_ip; |
| 832 | + output_string = output_string + std::string("\"") + current_ip + std::string("\""); |
784 | 833 | }
|
785 | 834 |
|
786 | 835 | set_ip_echo(output_string);
|
@@ -823,10 +872,6 @@ std::string GetExePath()
|
823 | 872 | #include <cassert>
|
824 | 873 |
|
825 | 874 | int multithreadServer_ws() {
|
826 |
| - // set_ip_echo(getLocalIP()); |
827 |
| - // unlock_ip_echo(); |
828 |
| - SaveLocalIP(); |
829 |
| - |
830 | 875 | // Code for the websocket server
|
831 | 876 | boost::asio::io_context ioc{ 1 };
|
832 | 877 | auto const address = boost::asio::ip::make_address("0.0.0.0");
|
@@ -868,14 +913,15 @@ void startServer() {
|
868 | 913 | static bool running = FALSE;
|
869 | 914 | if (!running) {
|
870 | 915 | running = TRUE;
|
| 916 | + |
| 917 | + // set_ip_echo(getLocalIP()); |
| 918 | + // unlock_ip_echo(); |
| 919 | + SaveLocalIP(); |
| 920 | + |
871 | 921 | boost::thread* thread = new boost::thread(&multithreadServer_ws);
|
872 | 922 | boost::thread* thread_web = new boost::thread(&multithreadServer_web);
|
873 | 923 | }
|
874 | 924 | }
|
875 |
| -// ----------------------- |
876 |
| -#include <nlohmann/json.hpp> |
877 |
| -using json = nlohmann::json; |
878 |
| -// ----------------------- |
879 | 925 |
|
880 | 926 |
|
881 | 927 |
|
|
0 commit comments