wrote a server for his game, which receives the coordinates and direction of the character of each player and sends them to the other players so that they can see each other. My character moves smoothly, the game takes 20-25% of the CPU and 300 MB of RAM and does not lag at all. At the same time, all the jerks remain, even if you start the server and client locally ("look at yourself from the second window of the game"). Can you tell me how to solve the problem? Server / client asynchronous TCP, serialized data through json parser. If there are two players on the server, then messages with a maximum of 70-80 characters.
What happens in the game client (code):
try { if (argc == 4) { ip::tcp::resolver resolver(service); auto endpoints = resolver.resolve(argv[1], argv[2]); //Π²Π²ΠΎΠΆΡ IP ΠΈ ΠΏΠΎΡΡ Client::ptr client = Client::start(endpoints, argv[3]); //ΠΊΠΎΠ½Π½Π΅ΠΊΡ ΠΊ ΡΠ΅ΡΠ²Π΅ΡΡ, Π° Π·Π°ΡΠ΅ΠΌ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΎΡΠΏΡΠ°Π²ΠΊΠΈ/ΡΡΠ΅Π½ΠΈΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ std::thread thr(netStart); //netStart Π·Π°ΠΏΡΡΠΊΠ°Π΅Ρ service.run() ΠΈ Π²ΡΡ thr.detach(); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; return -1; } //ΠΊΠΎΠ³Π΄Π° ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΡΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΠΈΡΡΡ ΠΊ ΡΠ΅ΡΠ²Π΅ΡΡ, Π½Π°ΡΠΈΠ½Π°Π΅ΡΡΡ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΈΠ³ΡΡ ΠΈ ΠΎΡΠ½ΠΎΠ²Π½ΠΎΠΉ ΡΠΈΠΊΠ» GameInit(); GameCycle(); Network part of the game client:
boost::shared_ptr<Client> Client::start(const ip::tcp::resolver::results_type& endpoints, const std::string & username) { ptr new_(new Client(username)); new_->start(const ip::tcp::resolver::results_type& endpoints); return new_; } Client::start(const ip::tcp::resolver::results_type& endpoints){ async_connect(sock_, endpoints, boost::bind(&Client::on_connect, shared_from_this(), _1)); } void Client::on_connect(const boost::system::error_code & err) { if (!err) { std::cout << "Connected to: " << sock_.remote_endpoint().address().to_string() << ":" << sock_.remote_endpoint().port() << std::endl; start(); } else { std::cout << "Failed to connect: " << err.message() << std::endl; stop(); } } void Client::start() { send_data(); //ΡΠ΅ΡΠΈΠ°Π»ΠΈΠ·ΡΠ΅Ρ Π΄Π°Π½Π½ΡΠ΅ ΠΈ Π²ΡΠ·ΡΠ²Π°Π΅Ρ async_write_some } In the on_accept function, I call start (), if there are no errors and looping async_write_some and async_read_some in it. Ie, the async_write_some function is called in start (), if it went successfully, then at the end of the function, async_read_some is called (the game client reads the data of other players that it sends server), then again async_write_some. On the server, the process is the same, but for each client.
For parsing json using property tree. Then I print ptree to std :: streamstring, make a header for it, containing the number of characters of the message (the package?), And load it like this:
std::string outbound_data_; std::string outbound_header_; std::vector<boost::asio::const_buffer> buffers; buffers.push_back(boost::asio::buffer(outbound_header_)); buffers.push_back(boost::asio::buffer(outbound_data_)); boost::asio::async_write(sock_, buffers, boost::bind(&Client::on_send_data, shared_from_this(), _1, _2));