{"id":391381,"date":"2024-06-29T09:47:38","date_gmt":"2024-06-29T09:47:38","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=391381"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=391381","title":{"rendered":"<span>\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u0435\u0440\u043d\u0430\u044f \u0438\u0433\u0440\u0430 \u043d\u0430 Rust + gRPC \u0441\u043e \u0441\u043f\u0435\u043a\u0442\u0430\u0442\u043e\u0440 \u043c\u043e\u0434\u043e\u043c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442. \u042d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0433\u0430\u0439\u0434 \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u0435\u0440\u043d\u044b\u0435 \u0438\u0433\u0440\u044b.   \u042f \u0438\u0437\u0443\u0447\u0430\u044e rust, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0432\u0435\u0440\u043d\u044b. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e \u0433\u0443\u0440\u0443 rust \u043f\u043e\u043f\u0440\u0430\u0432\u044f\u0442 \u043c\u0435\u043d\u044f \u0435\u0441\u043b\u0438 \u0443\u0432\u0438\u0434\u044f\u0442 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435. <\/p>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u0435\u0440\u043d\u044b\u0439 \u043f\u0438\u043d\u0433-\u043f\u043e\u043d\u0433. \u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d <a href=\"https:\/\/github.com\/loothood\/ping_pong_multiplayer\/tree\/v1.0\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.  <\/p>\n<h4>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/h4>\n<ul>\n<li>\n<p>Rust &#8212; \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c \u043d\u0430 \u043d\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u044f\u0437\u044b\u043a\u0430.<\/p>\n<\/li>\n<li>\n<p>gRPC &#8212; \u0424\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440. \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0447\u0442\u043e \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u0435\u043c-\u0442\u043e \u043d\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0437\u0432\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u043c\u044b. \u0412\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u0442\u043e \u0436\u0435  \u0441\u0430\u043c\u043e\u0435 &#8212; \u0432 Protocol Buffers (Protobuf) &#8212; \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0433\u043e\u0432\u043e\u0440\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u043c\u044b \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>Tetra &#8212; \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0434\u0432\u0438\u0436\u043e\u043a. \u041e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u041d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430\u043c \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 gRPC<\/h4>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"bash\">cargo new ping_pong_multiplayer<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 <code>src<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0434\u0432\u0430 \u0444\u0430\u0439\u043b\u0430: <code>client.rs<\/code> \u0438 <code>server.rs<\/code> &#8212; \u043e\u0434\u0438\u043d \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>\u0412 \u043a\u043e\u0440\u043d\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>build.rs<\/code> &#8212; \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 gRPC \u043a\u043e\u0434\u0430.<\/p>\n<p><code>main.rs<\/code> \u0443\u0434\u0430\u043b\u044f\u0435\u043c.<\/p>\n<p>\u0424\u0430\u0439\u043b <code>Cargo.toml<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code>[package]  name = \"ping_pong_multiplayer\"  version = \"0.1.0\"  edition = \"2018\"   [dependencies]  prost = \"^0.8.0\"  tonic = \"^0.5.2\"  tetra = \"^0.6.5\"  tokio = { version = \"^1.12.0\", features = [\"macros\", \"rt-multi-thread\"] } rand = \"0.8.4\"   [build-dependencies]  tonic-build = \"^0.5.2\"   #server binary  [[bin]]  name = \"server\"  path = \"src\/server.rs\"   #client binary  [[bin]]  name = \"client\"  path = \"src\/client.rs\" <\/code><\/pre>\n<p>\u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 prost \u0438 tonik \u2014 \u0434\u043b\u044f gRPC, tokio \u2014 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430, rand \u2014 \u0434\u043b\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0441\u0442\u0438 \u0432 \u0438\u0433\u0440\u0435 \u0438 tetra \u2014 \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0434\u0432\u0438\u0436\u043e\u043a. \u0412 build-dependencies \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442 tonic-build \u2014 \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437 proto-\u0444\u0430\u0439\u043b\u0430.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0432 \u043f\u0430\u043f\u043a\u0435 <code>src<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e <code>proto<\/code>, \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0435 \u0444\u0430\u0439\u043b <code>game.proto<\/code>. \u0422\u0443\u0442 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0442\u043e, \u043e \u0447\u0435\u043c \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u044b \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u0412\u043e\u043e\u0431\u0449\u0435, \u0443 gRPC \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0439 \u0438 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433 \u0438 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433. \u042f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c. \u041c\u044b \u0432\u043e\u0437\u044c\u043c\u0451\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442: \u043a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442.<\/p>\n<p>\u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b <code>game.proto<\/code> \u0438 \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c: <\/p>\n<pre><code>syntax = \"proto3\";   package game;   service GameProto {    rpc PlayRequest (PlayGameRequest) returns (PlayGameResponse);  }   message PlayGameRequest {    FloatTuple windowSize = 1;      FloatTuple player1Texture = 2;      FloatTuple player2Texture = 3;      FloatTuple ballTexture = 4;  }  message PlayGameResponse {    FloatTuple player1Position = 1;      FloatTuple player2Position = 2; uint32 playersCount = 3;      uint32 currentPlayerNumber = 4;      Ball ball = 5;  }  message Ball {    FloatTuple position = 1;      FloatTuple velocity = 2;  }  message FloatTuple {    float x = 1;      float y = 2;  }<\/code><\/pre>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0432\u0435\u0440\u0441\u0438\u044e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430. \u0414\u0430\u043b\u044c\u0448\u0435 \u0438\u0434\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u0446\u0438\u044f \u043f\u0430\u043a\u0435\u0442\u0430. \u0412 \u0441\u0442\u0440\u043e\u0447\u043a\u0435<\/p>\n<pre><code>rpc PlayRequest (PlayGameRequest) returns (PlayGameResponse);<\/code><\/pre>\n<p>\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e \u0447\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c  \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 <code>PlayRequest<\/code> \u0441 \u0442\u0438\u043f\u043e\u043c <code>PlayGameRequest<\/code> \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432 \u043e\u0442\u0432\u0435\u0442 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 <code>PlayGameResponse<\/code>.   \u0427\u0442\u043e \u043b\u0435\u0436\u0438\u0442 \u0432 \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0438\u0436\u0435:<\/p>\n<pre><code>message PlayGameRequest {   FloatTuple windowSize = 1;   FloatTuple player1Texture = 2;   FloatTuple player2Texture = 3;   FloatTuple ballTexture = 4; }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043d\u0430 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0433\u0440\u0430\u0442\u044c, \u043c\u044b \u0432\u044b\u0441\u044b\u043b\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043e\u043a\u043d\u0430, \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u0440\u0430\u043a\u0435\u0442\u043e\u043a) \u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043c\u044f\u0447\u0430. \u0420\u0430\u0437\u043c\u0435\u0440\u044b \u0438\u0433\u0440\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u044b\u0441\u044b\u043b\u0430\u0442\u044c \u0438\u0445, \u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u0431\u044b \u0434\u0432\u0430 \u043c\u0435\u0441\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0434\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0435\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0438\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u2014 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442.<\/p>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043c\u044b \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u043c:<\/p>\n<pre><code>message PlayGameResponse {   FloatTuple player1Position = 1;   FloatTuple player2Position = 2;   uint32 playersCount = 3;   uint32 currentPlayerNumber = 4;   Ball ball = 5; } <\/code><\/pre>\n<p>\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0433\u0434\u0435 \u0432 \u043e\u043a\u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u0440\u0430\u043a\u0435\u0442\u043a\u0438, \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u0437\u0430 \u0441\u0442\u043e\u043b\u043e\u043c, \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0438\u0433\u0440\u043e\u043a\u0430 \u0438 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u044f\u0447\u0430.  <\/p>\n<p>\u0422\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<pre><code>message Ball {   FloatTuple position = 1;   FloatTuple velocity = 2; } message FloatTuple {   float x = 1;   float y = 2; }<\/code><\/pre>\n<p>\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435. <\/p>\n<p>\u0412\u0441\u0435 \u043e\u043d\u0438 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u044f\u0442\u0441\u044f \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b.  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0433\u0430\u0439\u0434\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <\/p>\n<pre><code>  uint32 playersCount = 3;   uint32 currentPlayerNumber = 4; <\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0430\u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u0434\u0438\u043d uint32, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044f \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u044e\u0441\u044c \u0447\u0442\u043e \u043c\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0443\u044e \u0438\u0433\u0440\u0443, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u043b\u043e  \u0431\u044b uint16, \u0430 \u044d\u0442\u043e 65535 \u0432 \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435. \u041d\u043e \u0442\u0435\u043c\u0430 \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u044d\u0442\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c <code>main.rs<\/code>, \u0430 \u0432 <code>client.rs<\/code> \u0438 <code>server.rs<\/code> \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c: <\/p>\n<pre><code class=\"rust\"> fn main(){}<\/code><\/pre>\n<p><code>build.rs<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"rust\"> fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     tonic_build::configure()         .compile(             &amp;[\"src\/proto\/game.proto\"],             &amp;[\"src\/proto\"],         ).unwrap();     Ok(()) }<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0438\u0437 <code>proto<\/code> \u0444\u0430\u0439\u043b\u0430, \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0431\u0438\u043b\u0434:  <\/p>\n<pre><code> cargo build<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432 \u043f\u0430\u043f\u043a\u0435 <code>target\\debug\\build\\ping_pong_multiplayer-tetra_check-e8cc5eb2d2c25880\\out\\<\/code> \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0444\u0430\u0439\u043b <code>game.rs<\/code>. \u0412 \u0432\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0445\u044d\u0448-\u0447\u0430\u0441\u0442\u044c \u0438\u043c\u0435\u043d\u0438 \u043f\u0430\u043f\u043a\u0438 <code>ping_pong_multiplayer-tetra_check-e8cc5eb2d2c25880<\/code> \u0431\u0443\u0434\u0435\u0442 \u0434\u0440\u0443\u0433\u043e\u0439. \u041c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b &#8212; \u0438\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0443\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u043e\u0436\u0435\u043d \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 <code>src\\generated\\<\/code> \u0438 \u0443\u043a\u0430\u0436\u0435\u043c \u0432 <code>build.rs<\/code>: <\/p>\n<pre><code class=\"rust\">fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     tonic_build::configure()         .out_dir(\"src\/generated\")          .compile(             &amp;[\"src\/proto\/game.proto\"],             &amp;[\"src\/proto\"],          ).unwrap();     Ok(()) } <\/code><\/pre>\n<p>\u0422\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0431\u0443\u0434\u0435\u0442 \u0432 \u043f\u0430\u043f\u043a\u0435 <code>src\\generated\\<\/code>. <\/p>\n<h4>\u0421\u0435\u0440\u0432\u0435\u0440 <\/h4>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u0438\u043c\u0435\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0441 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u0444\u0430\u0439\u043b\u0443, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0435 <code>src<\/code> \u0444\u0430\u0439\u043b <code>generated_shared.rs<\/code> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre><code class=\"rust\">tonic::include_proto!(\"game\"); <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0430\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440: <\/p>\n<pre><code class=\"rust\">use tonic::transport::Server; use generated_shared::game_proto_server::{GameProto, GameProtoServer}; use generated_shared::{Ball, FloatTuple, PlayGameRequest, PlayGameResponse};  mod generated_shared;  pub struct PlayGame { }  impl PlayGame {     fn new() -> PlayGame {         PlayGame {         }     } }  #[tonic::async_trait] impl GameProto for PlayGame {     async fn play_request(         &amp;self,         request: tonic::Request&lt;PlayGameRequest>,     ) -> Result&lt;tonic::Response&lt;PlayGameResponse>, tonic::Status> {         unimplemented!()     } }  #[tokio::main] async fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     let addr = \"[::1]:50051\".parse()?;     let play_game = PlayGame::new();     println!(\"Server listening on {}\", addr);     Server::builder()         .add_service(GameProtoServer::new(play_game))         .serve(addr)         .await?;     Ok(()) } <\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u0430\u0440\u043a\u0430\u0441. \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e warning. \u041d\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435 \u043d\u0430 \u043d\u0438\u0445 \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f: <\/p>\n<pre><code class=\"bash\">% cargo run --bin server    Compiling ping_pong_multiplayer v0.1.0  warning: unused imports: `Ball`, `FloatTuple`  --> src\/server.rs:3:24   | 3 | use generated_shared::{Ball, FloatTuple, PlayGameRequest, PlayGameResponse};   |                        ^^^^  ^^^^^^^^^^   |   = note: `#[warn(unused_imports)]` on by default  warning: unused variable: `request`   --> src\/server.rs:20:9    | 20 |         request: tonic::Request&lt;PlayGameRequest>,    |         ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_request`    |    = note: `#[warn(unused_variables)]` on by default  warning: `ping_pong_multiplayer` (bin \"server\") generated 2 warnings     Finished dev [unoptimized + debuginfo] target(s) in 1.70s      Running `target\/debug\/server` Server listening on [::1]:50051 <\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c: <\/p>\n<pre><code class=\"rust\">async fn play_request(         &amp;self,         request: tonic::Request&lt;PlayGameRequest>,     ) -> Result&lt;tonic::Response&lt;PlayGameResponse>, tonic::Status> {         uninmplemented!();     } <\/code><\/pre>\n<p>\u043c\u044b \u0432\u0437\u044f\u043b\u0438 \u0438\u0437 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430 \u0432\u0445\u043e\u0434 <code>PlayGameRequest<\/code> \u0438 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0431\u0443\u0434\u0435\u043c <code>PlayGameResponse<\/code>.<\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0438 \u043f\u0440\u043e\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e \u0435\u0433\u043e:<\/p>\n<pre><code class=\"rust\">use tonic::{transport::Server, Response}; use generated_shared::game_proto_server::{GameProto, GameProtoServer}; use generated_shared::{Ball, FloatTuple, PlayGameRequest, PlayGameResponse}; use std::sync::{Mutex, Arc}; use tetra::math::Vec2; use rand::Rng;  mod generated_shared;  const BALL_SPEED: f32 = 5.0;  #[derive(Clone)] struct Entity {     texture_size: Vec2&lt;f32>,     position: Vec2&lt;f32>,     velocity: Vec2&lt;f32>, }  impl Entity {     fn new(texture_size: Vec2&lt;f32>, position: Vec2&lt;f32>) -> Entity {         Entity::with_velocity(texture_size, position, Vec2::zero())     }         fn with_velocity(texture_size: Vec2&lt;f32>, position: Vec2&lt;f32>, velocity: Vec2&lt;f32>) -> Entity {         Entity { texture_size, position, velocity }     } }  #[derive(Clone)] struct World {     player1: Entity,     player2: Entity,     ball: Entity,     world_size: Vec2&lt;f32>,     winner: u32, }  pub struct PlayGame {     world: Arc&lt;Mutex&lt;Option&lt;World>>>,     players_count: Arc&lt;Mutex&lt;u32>>, }  impl PlayGame {     fn new() -> PlayGame {         PlayGame {             world: Arc::new(Mutex::new(None)),             players_count: Arc::new(Mutex::new(0u32)),         }     }     fn init(&amp;self, window_size: FloatTuple, player1_texture: FloatTuple,             player2_texture: FloatTuple, ball_texture: FloatTuple) {         let window_width = window_size.x;         let window_height = window_size.y;         let world = Arc::clone(&amp;self.world);         let mut world = world.lock().unwrap();         let players_count = Arc::clone(&amp;self.players_count);         let players_count = players_count.lock().unwrap().clone();         let mut ball_velocity = 0f32;         if players_count >= 2 {             let num = rand::thread_rng().gen_range(0..2);             if num == 0 {                 ball_velocity = -BALL_SPEED;             } else {                 ball_velocity = BALL_SPEED;             }         }         *world =             Option::Some(World {                 player1: Entity::new(                     Vec2::new(player1_texture.x, player1_texture.y),                     Vec2::new(                         16.0,                         (window_height - player1_texture.y) \/ 2.0,                     ),                 ),                 player2: Entity::new(                     Vec2::new(player2_texture.x, player2_texture.y),                     Vec2::new(                         window_width - player2_texture.y - 16.0,                         (window_height - player2_texture.y) \/ 2.0,                     ),                 ),                 ball: Entity::with_velocity(                     Vec2::new(ball_texture.x, ball_texture.y),                     Vec2::new(                         window_width \/ 2.0 - ball_texture.x \/ 2.0,                         window_height \/ 2.0 - ball_texture.y \/ 2.0,                     ),                     Vec2::new(                         ball_velocity,                         0f32,                     ),                 ),                 world_size: Vec2::new(window_size.x, window_size.y),                 \/\/ No one win yet                 winner: 2,             });     }     fn increase_players_count(&amp;self) {         let players_count = Arc::clone(&amp;self.players_count);         let mut players_count = players_count.lock().unwrap();         *players_count += 1;     } }  #[tonic::async_trait] impl GameProto for PlayGame {     async fn play_request(         &amp;self,         request: tonic::Request&lt;PlayGameRequest>,     ) -> Result&lt;tonic::Response&lt;PlayGameResponse>, tonic::Status> {         let pgr: PlayGameRequest = request.into_inner();         let window_size = pgr.window_size.unwrap();         let player1_texture = pgr.player1_texture.unwrap();         let player2_texture = pgr.player2_texture.unwrap();         let ball_texture_height = pgr.ball_texture.unwrap();         self.increase_players_count();         self.init(window_size, player1_texture,                   player2_texture, ball_texture_height);         let world = Arc::clone(&amp;self.world).lock().unwrap().as_ref().unwrap().clone();         let current_players = Arc::clone(&amp;self.players_count);         let current_players = current_players.lock().unwrap();         let reply = PlayGameResponse {             player1_position: Option::Some(FloatTuple {                 x: world.player1.position.x,                 y: world.player1.position.y,             }),             player2_position: Option::Some(FloatTuple {                 x: world.player2.position.x,                 y: world.player2.position.y,             }),             current_player_number: current_players.clone(),             players_count: current_players.clone(),             ball: Option::Some(Ball {                 position: Option::Some(FloatTuple {                     x: world.ball.position.x,                     y: world.ball.position.y,                 }),                 velocity: Option::Some(FloatTuple {                     x: world.ball.velocity.x,                     y: world.ball.velocity.y,                 }),             }),         };         Ok(Response::new(reply))     } }  #[tokio::main] async fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     let addr = \"[::1]:50051\".parse()?;     let play_game = PlayGame::new();     println!(\"Server listening on {}\", addr);     Server::builder()         .add_service(GameProtoServer::new(play_game))         .serve(addr)         .await?;     Ok(()) } <\/code><\/pre>\n<p>\u041d\u0430\u0448\u0430 &#171;\u0433\u043b\u0430\u0432\u043d\u0430\u044f&#187; \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 &#8212; <code>PlayGame<\/code>. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0445\u0440\u0430\u043d\u0438\u043c \u0432\u0435\u0441\u044c \u043c\u0438\u0440 \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432. \u041e\u0431\u0430 \u043f\u043e\u043b\u044f \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044b \u0432 <code>Arc&lt;Mutex&lt;>><\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u044d\u0442\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u043c. \u0412\u043e\u043e\u0431\u0449\u0435, \u0432 rust \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c. \u0422\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f.   <\/p>\n<p>\u041f\u0435\u0440\u0432\u043e-\u043d\u0430\u043f\u0435\u0440\u0432\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"rust\">let pgr: PlayGameRequest = request.into_inner();  <\/code><\/pre>\n<p>\u042d\u0442\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443(<code>PlayGameRequest<\/code>) \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u0442\u0430\u043c \u043f\u043e\u043b\u044f. \u0414\u0430\u043b\u0435\u0435, \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u044b \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c:<\/p>\n<pre><code class=\"rust\">let window_size = pgr.window_size.unwrap(); let player1_texture = pgr.player1_texture.unwrap(); let player2_texture = pgr.player2_texture.unwrap();  let ball_texture_height = pgr.ball_texture.unwrap();<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043d\u043e\u0432\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0435, \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432: <\/p>\n<pre><code class=\"rust\">fn increase_players_count(&amp;self) { let players_count = Arc::clone(&amp;self.players_count); let mut players_count = players_count.lock().unwrap(); *players_count += 1; }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044b\u0445 \u0432 <code>Arc&lt;Mutex&lt;>><\/code>.  <\/p>\n<p>\u0421 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u0440. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>self.init()<\/code>. \u0412 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0438\u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0440\u043e\u043c\u0435<\/p>\n<pre><code class=\"rust\">let mut ball_velocity = 0f32; if players_count >= 2 {     let num = rand::thread_rng().gen_range(0..2);     if num == 0 {         ball_velocity = -BALL_SPEED;     } else {         ball_velocity = BALL_SPEED;     } } <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430 \u0441\u0442\u043e\u043b\u043e\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0438\u0433\u0440\u043e\u043a \u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0449\u0435 \u043d\u0435\u0442, \u0442\u043e \u043c\u044f\u0447 \u0441\u0442\u043e\u0438\u0442 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435 &#8212; \u0435\u0433\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c 0. \u0415\u0441\u043b\u0438 \u0436\u0435 \u043f\u0440\u0438\u0448\u0435\u043b \u0432\u0442\u043e\u0440\u043e\u0439 \u0438\u0433\u0440\u043e\u043a, \u0442\u043e \u0438\u0433\u0440\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438 \u043c\u044f\u0447 \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0447\u0430\u0442\u044c \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f. \u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043d\u0430\u0447\u0438\u043d\u0430\u043b \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443. \u041f\u043e\u0442\u043e\u043c\u0443 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043b\u0438\u0431\u043e 0 \u043b\u0438\u0431\u043e 1 \u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0432\u044b\u043f\u0430\u043b\u043e, \u043c\u044f\u0447 \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0432\u043b\u0435\u0432\u043e \u0438\u043b\u0438 \u0432\u043f\u0440\u0430\u0432\u043e.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043c\u0438\u0440 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0435\u0433\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432 \u043e\u0442\u0432\u0435\u0442\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 <code>PlayGameResponse<\/code> &#8212; \u0435\u0435 \u043f\u043e\u043b\u044f \u0438 &#171;\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438&#187; \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u0436\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c <code>game.rs<\/code> \u0444\u0430\u0439\u043b\u0435.    \u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<pre><code>% cargo run --bin server    Compiling ping_pong_multiplayer v0.1.0 (\/Users\/macbook\/rust\/IdeaProjects\/ping_pong_multiplayer)     Finished dev [unoptimized + debuginfo] target(s) in 5.62s      Running `target\/debug\/server` Server listening on [::1]:50051 <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0447\u0442\u043e \u0432\u0441\u0435 warning \u043f\u0440\u043e\u043f\u0430\u043b\u0438.   <\/p>\n<h4>\u041a\u043b\u0438\u0435\u043d\u0442 <\/h4>\n<p>\u041a\u0430\u043a \u044f \u0443\u0436\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u043e\u0439  \u0434\u0432\u0438\u0436\u043e\u043a <code>tetra<\/code>. \u041e\u043d \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u0441 \u043d\u0438\u043c \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u0438\u043d\u0433-\u043f\u043e\u043d\u0433 \u0431\u044b\u043b \u0432\u044b\u0431\u0440\u0430\u043d \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443 \u043d\u0438\u0445 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u0435\u0441\u0442\u044c \u0433\u0430\u0439\u0434 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0439 \u0438\u0433\u0440\u044b.<\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442, \u043d\u0430\u0434\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u044b. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0430\u043f\u043a\u0443 <code>resources<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0442\u0443\u0434\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u0440\u043a\u0430\u0441: <\/p>\n<pre><code class=\"rust\">use tetra::graphics::{self, Color, Texture}; use tetra::math::Vec2; use tetra::{TetraError}; use tetra::{Context, ContextBuilder, State};  mod generated_shared;  const WINDOW_WIDTH: f32 = 1200.0; const WINDOW_HEIGHT: f32 = 720.0;  fn main() -> Result&lt;(), TetraError> {     ContextBuilder::new(\"Pong\", WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32)         .quit_on_escape(true)         .build()?         .run(GameState::new) }  struct Entity {     texture: Texture,     position: Vec2&lt;f32>,     velocity: Vec2&lt;f32>, } impl Entity {     fn new(texture: &amp;Texture, position: Vec2&lt;f32>) -> Entity {         Entity::with_velocity(&amp;texture, position, Vec2::zero())     }     fn with_velocity(texture: &amp;Texture, position: Vec2&lt;f32>, velocity: Vec2&lt;f32>) -> Entity {         Entity { texture: texture.clone(), position, velocity }     } }  struct GameState {     player1: Entity,     player2: Entity,     ball: Entity,     player_number: u32,     players_count: u32, } impl GameState {     fn new(ctx: &amp;mut Context) -> tetra::Result&lt;GameState> {         let player1_texture = Texture::new(ctx, \".\/resources\/player1.png\")?;         let player2_texture = Texture::new(ctx, \".\/resources\/player2.png\")?;         let ball_texture = Texture::new(ctx, \".\/resources\/ball.png\")?;         Ok(GameState {             player1: Entity::new(&amp;player1_texture, Vec2::new(16., 100.)),             player2: Entity::new(&amp;player2_texture, Vec2::new(116., 100.)),             ball: Entity::with_velocity(&amp;ball_texture, Vec2::new(52., 125.), Vec2::new(0., 0.)),             player_number: 0u32,             players_count: 0u32,         })     } } impl State for GameState {     fn update(&amp;mut self, ctx: &amp;mut Context) -> tetra::Result {         Ok(())     }     fn draw(&amp;mut self, ctx: &amp;mut Context) -> tetra::Result {         graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));         self.player1.texture.draw(ctx, self.player1.position);         self.player2.texture.draw(ctx, self.player2.position);         self.ball.texture.draw(ctx, self.ball.position);         Ok(())     } } <\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0447\u0442\u043e \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0443 \u043d\u0430\u0441 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 <code>Entity<\/code> \u0438 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0435\u0451 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b &#8212; \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u043e\u043b\u044f <code>texture<\/code>. \u0412\u043e\u043e\u0431\u0449\u0435, \u0435\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0439\u0442 <code>Send<\/code> \u0434\u043b\u044f \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 <code>Texture<\/code>, \u0442\u043e \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u044d\u0442\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0432 \u043e\u0431\u0449\u0438\u0439 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0444\u0430\u0439\u043b. \u041d\u043e \u044d\u0442\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u0437\u0430 \u0440\u0430\u043c\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u0430.<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435, \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430  <\/p>\n<pre><code>impl State for GameState <\/code><\/pre>\n<p>\u0437\u0434\u0435\u0441\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>update<\/code> \u0438 <code>draw<\/code>. Tetra \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438\u0433\u0440\u044b, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u044d\u0442\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439. <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0447\u0442\u043e \u0440\u0438\u0441\u0443\u0435\u0442\u0441\u044f \u043e\u043a\u043e\u0448\u043a\u043e \u0441 \u0433\u043e\u043b\u0443\u0431\u044b\u043c \u0444\u043e\u043d\u043e\u043c, \u0440\u0438\u0441\u0443\u044e\u0442\u0441\u044f \u0440\u0430\u043a\u0435\u0442\u043a\u0438 \u0438 \u043c\u044f\u0447: <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0a9\/933\/514\/0a9933514847a3bcd9b1f66998ea4e1f.png\" width=\"2396\" height=\"1486\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0a9\/933\/514\/0a9933514847a3bcd9b1f66998ea4e1f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"rust\">async fn establish_connection() -> GameProtoClient&lt;tonic::transport::Channel> {     GameProtoClient::connect(\"http:\/\/[::1]:50051\").await.expect(\"Can't connect to the server\") } <\/code><\/pre>\n<p>\u041e\u043f\u044f\u0442\u044c \u0436\u0435, <code>GameProtoClient<\/code> \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435. \u042d\u0442\u043e\u0442 \u043a\u043e\u043d\u043d\u0435\u043a\u0442 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u044e \u043d\u0430\u0448\u0443 \u0438\u0433\u0440\u0443. \u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e future, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u043d\u0435\u043a\u0442\u0430. \u0422\u0430\u043a \u0436\u0435, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438\u0433\u0440\u044b. \u041f\u043e\u0442\u043e\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>main<\/code> \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"rust\">fn main() -> Result&lt;(), TetraError> {     let rt = tokio::runtime::Runtime::new().expect(\"Error runtime creation\");     let mut client = rt.block_on(establish_connection());     ContextBuilder::new(\"Pong\", WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32)         .quit_on_escape(true)         .build()?         .run(|ctx|GameState::new(ctx, &amp;mut client)) } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0442\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 future. \u0412\u043e\u043e\u0431\u0449\u0435, \u0432 rust \u0435\u0441\u0442\u044c \u0446\u0435\u043b\u044b\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 crate \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 future, \u043d\u043e \u043d\u0430\u043c \u043e\u043d \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u043d\u0435\u043a\u0442, \u043c\u044b \u0437\u043d\u0430\u0435\u043c \u0447\u0442\u043e \u043e\u0442 \u043d\u0430\u0441 \u0436\u0434\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0447\u0442\u043e \u043e\u043d \u043e\u0442\u0432\u0435\u0442\u0438\u0442. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u043e:<\/p>\n<pre><code class=\"rust\">use tetra::graphics::{self, Color, Texture}; use tetra::math::Vec2; use tetra::{TetraError}; use tetra::{Context, ContextBuilder, State}; use generated_shared::game_proto_client::GameProtoClient; use generated_shared::{FloatTuple, PlayGameRequest, PlayGameResponse};  mod generated_shared;  const WINDOW_WIDTH: f32 = 1200.0; const WINDOW_HEIGHT: f32 = 720.0;  async fn establish_connection() -> GameProtoClient&lt;tonic::transport::Channel> {     GameProtoClient::connect(\"http:\/\/[::1]:50051\").await.expect(\"Can't connect to the server\") }  fn main() -> Result&lt;(), TetraError> {     let rt = tokio::runtime::Runtime::new().expect(\"Error runtime creation\");     let mut client = rt.block_on(establish_connection());     ContextBuilder::new(\"Pong\", WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32)         .quit_on_escape(true)         .build()?         .run(|ctx|GameState::new(ctx, &amp;mut client)) }  struct Entity {     texture: Texture,     position: Vec2&lt;f32>,     velocity: Vec2&lt;f32>, } impl Entity {     fn new(texture: &amp;Texture, position: Vec2&lt;f32>) -> Entity {         Entity::with_velocity(&amp;texture, position, Vec2::zero())     }     fn with_velocity(texture: &amp;Texture, position: Vec2&lt;f32>, velocity: Vec2&lt;f32>) -> Entity {         Entity { texture: texture.clone(), position, velocity }     } }  struct GameState {     player1: Entity,     player2: Entity,     ball: Entity,     player_number: u32,     players_count: u32,     client: GameProtoClient&lt;tonic::transport::Channel>, } impl GameState {         fn new(ctx: &amp;mut Context, client : &amp;mut GameProtoClient&lt;tonic::transport::Channel>) -> tetra::Result&lt;GameState> {             let player1_texture = Texture::new(ctx, \".\/resources\/player1.png\")?;             let ball_texture = Texture::new(ctx, \".\/resources\/ball.png\")?;             let player2_texture = Texture::new(ctx, \".\/resources\/player2.png\")?;             let play_request = GameState::play_request(&amp;player1_texture, &amp;player2_texture, &amp;ball_texture, client);             let ball = play_request.ball.expect(\"Cannot get ball's data from server\");             let ball_position = ball.position.expect(\"Cannot get ball position from server\");             let ball_position = Vec2::new(                 ball_position.x,                 ball_position.y,             );             let ball_velocity = ball.velocity.expect(\"Cannot get ball velocity from server\");             let ball_velocity = Vec2::new(                 ball_velocity.x,                 ball_velocity.y,             );             let player1_position = &amp;play_request.player1_position                 .expect(\"Cannot get player position from server\");             let player1_position = Vec2::new(                 player1_position.x,                 player1_position.y,             );             let player2_position = &amp;play_request.player2_position                 .expect(\"Cannot get player position from server\");             let player2_position = Vec2::new(                 player2_position.x,                 player2_position.y,             );             let player_number = play_request.current_player_number;             Ok(GameState {                 player1: Entity::new(&amp;player1_texture, player1_position),                 player2: Entity::new(&amp;player2_texture, player2_position),                 ball: Entity::with_velocity(&amp;ball_texture, ball_position, ball_velocity),                 player_number,                 players_count: player_number,                 client: client.clone(),             })         }     #[tokio::main]     async fn play_request(player1_texture: &amp;Texture, player2_texture: &amp;Texture, ball_texture: &amp;Texture,                           client : &amp;mut GameProtoClient&lt;tonic::transport::Channel>) -> PlayGameResponse {         let request = tonic::Request::new(PlayGameRequest {             window_size: Some(FloatTuple { x: WINDOW_WIDTH, y: WINDOW_HEIGHT }),             player1_texture: Some(                 FloatTuple { x: player1_texture.width() as f32, y: player1_texture.height() as f32 }             ),             player2_texture: Some(                 FloatTuple { x: player2_texture.width() as f32, y: player2_texture.height() as f32 }             ),             ball_texture: Some(                 FloatTuple { x: ball_texture.width() as f32, y: ball_texture.height() as f32 }             ),         });         client.play_request(request).await.expect(\"Cannot get Play Response the server\").into_inner()     } }  impl State for GameState {     fn update(&amp;mut self, ctx: &amp;mut Context) -> tetra::Result {         Ok(())     }     fn draw(&amp;mut self, ctx: &amp;mut Context) -> tetra::Result {         graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));         self.player1.texture.draw(ctx, self.player1.position);         self.player2.texture.draw(ctx, self.player2.position);         self.ball.texture.draw(ctx, self.ball.position);         Ok(())     } } <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435\u0442 \u0447\u0435\u0433\u043e-\u0442\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0434\u043b\u044f \u043d\u0430\u0441. \u041c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0438\u0433\u0440\u0443: <code>play_request<\/code>. \u0412 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0438\u043c\u0435\u043d\u0435\u043c &#8212; \u0442\u0430\u043c \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0447\u0442\u043e \u043e\u043d\u0430 \u0436\u0434\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0438 \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440: <\/p>\n<pre><code class=\"bash\">% cargo run --bin server      Compiling ping_pong_multiplayer v0.1.0      Finished dev [unoptimized + debuginfo] target(s) in 4.45s      Running `target\/debug\/server` Server listening on [::1]:50051<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442. \u041d\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043d\u0430 warning \u2014 \u043d\u0430\u043c \u044d\u0442\u0438 \u043f\u043e\u043b\u044f \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u043f\u043e\u0437\u0436\u0435:<\/p>\n<pre><code class=\"bash\">% cargo run --bin client warning: unused variable: `ctx`    --> src\/client.rs:104:26     | 104 |     fn update(&amp;mut self, ctx: &amp;mut Context) -> tetra::Result {     |                          ^^^ help: if this is intentional, prefix it with an underscore: `_ctx`     |     = note: `#[warn(unused_variables)]` on by default  warning: field is never read: `velocity`   --> src\/client.rs:28:5    | 28 |     velocity: Vec2&lt;f32>,    |     ^^^^^^^^^^^^^^^^^^^    |    = note: `#[warn(dead_code)]` on by default  warning: field is never read: `player_number`   --> src\/client.rs:42:5    | 42 |     player_number: u32,    |     ^^^^^^^^^^^^^^^^^^  warning: field is never read: `players_count`   --> src\/client.rs:43:5    | 43 |     players_count: u32,    |     ^^^^^^^^^^^^^^^^^^  warning: field is never read: `client`   --> src\/client.rs:44:5    | 44 |     client: GameProtoClient&lt;tonic::transport::Channel>,    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  warning: `ping_pong_multiplayer` (bin \"client\") generated 5 warnings     Finished dev [unoptimized + debuginfo] target(s) in 0.44s      Running `target\/debug\/client`<\/code><\/pre>\n<p>\u0418 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0447\u0442\u043e \u0440\u0430\u043a\u0435\u0442\u043a\u0438 \u0438 \u043c\u044f\u0447 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0438\u043b\u0438\u0441\u044c \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0445  \u043c\u0435\u0441\u0442\u0430\u0445 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/049\/aa7\/4a6\/049aa74a6fb18606b8d843ee13efa6d8.png\" width=\"2392\" height=\"1484\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/049\/aa7\/4a6\/049aa74a6fb18606b8d843ee13efa6d8.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0432\u0441\u0435. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u043a\u0435\u0442\u043a\u0430\u043c\u0438 \u0438 \u0432\u044b\u0432\u043e\u0434 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u043e\u0431\u0435\u0434\u0435 \u0438\u0433\u0440\u043e\u043a\u0430.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/585958\/\"> https:\/\/habr.com\/ru\/articles\/585958\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442. \u042d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0433\u0430\u0439\u0434 \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u0435\u0440\u043d\u044b\u0435 \u0438\u0433\u0440\u044b.   \u042f \u0438\u0437\u0443\u0447\u0430\u044e rust, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0432\u0435\u0440\u043d\u044b. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e \u0433\u0443\u0440\u0443 rust \u043f\u043e\u043f\u0440\u0430\u0432\u044f\u0442 \u043c\u0435\u043d\u044f \u0435\u0441\u043b\u0438 \u0443\u0432\u0438\u0434\u044f\u0442 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435. <\/p>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u0435\u0440\u043d\u044b\u0439 \u043f\u0438\u043d\u0433-\u043f\u043e\u043d\u0433. \u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d <a href=\"https:\/\/github.com\/loothood\/ping_pong_multiplayer\/tree\/v1.0\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.  <\/p>\n<h4>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/h4>\n<ul>\n<li>\n<p>Rust &#8212; \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c \u043d\u0430 \u043d\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u044f\u0437\u044b\u043a\u0430.<\/p>\n<\/li>\n<li>\n<p>gRPC &#8212; \u0424\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440. \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0447\u0442\u043e \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u0435\u043c-\u0442\u043e \u043d\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0437\u0432\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u043c\u044b. \u0412\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u0442\u043e \u0436\u0435  \u0441\u0430\u043c\u043e\u0435 &#8212; \u0432 Protocol Buffers (Protobuf) &#8212; \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0433\u043e\u0432\u043e\u0440\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u043c\u044b \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>Tetra &#8212; \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0434\u0432\u0438\u0436\u043e\u043a. \u041e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u041d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430\u043c \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 gRPC<\/h4>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"bash\">cargo new ping_pong_multiplayer<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 <code>src<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0434\u0432\u0430 \u0444\u0430\u0439\u043b\u0430: <code>client.rs<\/code> \u0438 <code>server.rs<\/code> &#8212; \u043e\u0434\u0438\u043d \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>\u0412 \u043a\u043e\u0440\u043d\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>build.rs<\/code> &#8212; \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 gRPC \u043a\u043e\u0434\u0430.<\/p>\n<p><code>main.rs<\/code> \u0443\u0434\u0430\u043b\u044f\u0435\u043c.<\/p>\n<p>\u0424\u0430\u0439\u043b <code>Cargo.toml<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code>[package]  name = \"ping_pong_multiplayer\"  version = \"0.1.0\"  edition = \"2018\"   [dependencies]  prost = \"^0.8.0\"  tonic = \"^0.5.2\"  tetra = \"^0.6.5\"  tokio = { version = \"^1.12.0\", features = [\"macros\", \"rt-multi-thread\"] } rand = \"0.8.4\"   [build-dependencies]  tonic-build = \"^0.5.2\"   #server binary  [[bin]]  name = \"server\"  path = \"src\/server.rs\"   #client binary  [[bin]]  name = \"client\"  path = \"src\/client.rs\" <\/code><\/pre>\n<p>\u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 prost \u0438 tonik \u2014 \u0434\u043b\u044f gRPC, tokio \u2014 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430, rand \u2014 \u0434\u043b\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0441\u0442\u0438 \u0432 \u0438\u0433\u0440\u0435 \u0438 tetra \u2014 \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0434\u0432\u0438\u0436\u043e\u043a. \u0412 build-dependencies \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442 tonic-build \u2014 \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437 proto-\u0444\u0430\u0439\u043b\u0430.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0432 \u043f\u0430\u043f\u043a\u0435 <code>src<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e <code>proto<\/code>, \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0435 \u0444\u0430\u0439\u043b <code>game.proto<\/code>. \u0422\u0443\u0442 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0442\u043e, \u043e \u0447\u0435\u043c \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u044b \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u0412\u043e\u043e\u0431\u0449\u0435, \u0443 gRPC \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0439 \u0438 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433 \u0438 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433. \u042f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c. \u041c\u044b \u0432\u043e\u0437\u044c\u043c\u0451\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442: \u043a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442.<\/p>\n<p>\u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b <code>game.proto<\/code> \u0438 \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c: <\/p>\n<pre><code>syntax = \"proto3\";   package game;   service GameProto {    rpc PlayRequest (PlayGameRequest) returns (PlayGameResponse);  }   message PlayGameRequest {    FloatTuple windowSize = 1;      FloatTuple player1Texture = 2;      FloatTuple player2Texture = 3;      FloatTuple ballTexture = 4;  }  message PlayGameResponse {    FloatTuple player1Position = 1;      FloatTuple player2Position = 2; uint32 playersCount = 3;      uint32 currentPlayerNumber = 4;      Ball ball = 5;  }  message Ball {    FloatTuple position = 1;      FloatTuple velocity = 2;  }  message FloatTuple {    float x = 1;      float y = 2;  }<\/code><\/pre>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0432\u0435\u0440\u0441\u0438\u044e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430. \u0414\u0430\u043b\u044c\u0448\u0435 \u0438\u0434\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u0446\u0438\u044f \u043f\u0430\u043a\u0435\u0442\u0430. \u0412 \u0441\u0442\u0440\u043e\u0447\u043a\u0435<\/p>\n<pre><code>rpc PlayRequest (PlayGameRequest) returns (PlayGameResponse);<\/code><\/pre>\n<p>\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e \u0447\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c  \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 <code>PlayRequest<\/code> \u0441 \u0442\u0438\u043f\u043e\u043c <code>PlayGameRequest<\/code> \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432 \u043e\u0442\u0432\u0435\u0442 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 <code>PlayGameResponse<\/code>.   \u0427\u0442\u043e \u043b\u0435\u0436\u0438\u0442 \u0432 \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0438\u0436\u0435:<\/p>\n<pre><code>message PlayGameRequest {   FloatTuple windowSize = 1;   FloatTuple player1Texture = 2;   FloatTuple player2Texture = 3;   FloatTuple ballTexture = 4; }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043d\u0430 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0433\u0440\u0430\u0442\u044c, \u043c\u044b \u0432\u044b\u0441\u044b\u043b\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043e\u043a\u043d\u0430, \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u0440\u0430\u043a\u0435\u0442\u043e\u043a) \u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043c\u044f\u0447\u0430. \u0420\u0430\u0437\u043c\u0435\u0440\u044b \u0438\u0433\u0440\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u044b\u0441\u044b\u043b\u0430\u0442\u044c \u0438\u0445, \u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u0431\u044b \u0434\u0432\u0430 \u043c\u0435\u0441\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0434\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0435\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0438\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u2014 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442.<\/p>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043c\u044b \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u043c:<\/p>\n<pre><code>message PlayGameResponse {   FloatTuple player1Position = 1;   FloatTuple player2Position = 2;   uint32 playersCount = 3;   uint32 currentPlayerNumber = 4;   Ball ball = 5; } <\/code><\/pre>\n<p>\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0433\u0434\u0435 \u0432 \u043e\u043a\u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u0440\u0430\u043a\u0435\u0442\u043a\u0438, \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u0437\u0430 \u0441\u0442\u043e\u043b\u043e\u043c, \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0438\u0433\u0440\u043e\u043a\u0430 \u0438 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u044f\u0447\u0430.  <\/p>\n<p>\u0422\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<pre><code>message Ball {   FloatTuple position = 1;   FloatTuple velocity = 2; } message FloatTuple {   float x = 1;   float y = 2; }<\/code><\/pre>\n<p>\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435. <\/p>\n<p>\u0412\u0441\u0435 \u043e\u043d\u0438 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u044f\u0442\u0441\u044f \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b.  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0433\u0430\u0439\u0434\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <\/p>\n<pre><code>  uint32 playersCount = 3;   uint32 currentPlayerNumber = 4; <\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0430\u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u0434\u0438\u043d uint32, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044f \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u044e\u0441\u044c \u0447\u0442\u043e \u043c\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0443\u044e \u0438\u0433\u0440\u0443, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u043b\u043e  \u0431\u044b uint16, \u0430 \u044d\u0442\u043e 65535 \u0432 \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435. \u041d\u043e \u0442\u0435\u043c\u0430 \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u044d\u0442\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c <code>main.rs<\/code>, \u0430 \u0432 <code>client.rs<\/code> \u0438 <code>server.rs<\/code> \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c: <\/p>\n<pre><code class=\"rust\"> fn main(){}<\/code><\/pre>\n<p><code>build.rs<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"rust\"> fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     tonic_build::configure()         .compile(             &amp;[\"src\/proto\/game.proto\"],             &amp;[\"src\/proto\"],         ).unwrap();     Ok(()) }<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0438\u0437 <code>proto<\/code> \u0444\u0430\u0439\u043b\u0430, \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0431\u0438\u043b\u0434:  <\/p>\n<pre><code> cargo build<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432 \u043f\u0430\u043f\u043a\u0435 <code>target\\debug\\build\\ping_pong_multiplayer-tetra_check-e8cc5eb2d2c25880\\out\\<\/code> \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0444\u0430\u0439\u043b <code>game.rs<\/code>. \u0412 \u0432\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0445\u044d\u0448-\u0447\u0430\u0441\u0442\u044c \u0438\u043c\u0435\u043d\u0438 \u043f\u0430\u043f\u043a\u0438 <code>ping_pong_multiplayer-tetra_check-e8cc5eb2d2c25880<\/code> \u0431\u0443\u0434\u0435\u0442 \u0434\u0440\u0443\u0433\u043e\u0439. \u041c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b &#8212; \u0438\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0443\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u043e\u0436\u0435\u043d \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 <code>src\\generated\\<\/code> \u0438 \u0443\u043a\u0430\u0436\u0435\u043c \u0432 <code>build.rs<\/code>: <\/p>\n<pre><code class=\"rust\">fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     tonic_build::configure()         .out_dir(\"src\/generated\")          .compile(             &amp;[\"src\/proto\/game.proto\"],             &amp;[\"src\/proto\"],          ).unwrap();     Ok(()) } <\/code><\/pre>\n<p>\u0422\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0431\u0443\u0434\u0435\u0442 \u0432 \u043f\u0430\u043f\u043a\u0435 <code>src\\generated\\<\/code>. <\/p>\n<h4>\u0421\u0435\u0440\u0432\u0435\u0440 <\/h4>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u0438\u043c\u0435\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0441 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u0444\u0430\u0439\u043b\u0443, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0435 <code>src<\/code> \u0444\u0430\u0439\u043b <code>generated_shared.rs<\/code> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre><code class=\"rust\">tonic::include_proto!(\"game\"); <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0430\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440: <\/p>\n<pre><code class=\"rust\">use tonic::transport::Server; use generated_shared::game_proto_server::{GameProto, GameProtoServer}; use generated_shared::{Ball, FloatTuple, PlayGameRequest, PlayGameResponse};  mod generated_shared;  pub struct PlayGame { }  impl PlayGame {     fn new() -> PlayGame {         PlayGame {         }     } }  #[tonic::async_trait] impl GameProto for PlayGame {     async fn play_request(         &amp;self,         request: tonic::Request&lt;PlayGameRequest>,     ) -> Result&lt;tonic::Response&lt;PlayGameResponse>, tonic::Status> {         unimplemented!()     } }  #[tokio::main] async fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     let addr = \"[::1]:50051\".parse()?;     let play_game = PlayGame::new();     println!(\"Server listening on {}\", addr);     Server::builder()         .add_service(GameProtoServer::new(play_game))         .serve(addr)         .await?;     Ok(()) } <\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u0430\u0440\u043a\u0430\u0441. \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e warning. \u041d\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435 \u043d\u0430 \u043d\u0438\u0445 \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f: <\/p>\n<pre><code class=\"bash\">% cargo run --bin server    Compiling ping_pong_multiplayer v0.1.0  warning: unused imports: `Ball`, `FloatTuple`  --> src\/server.rs:3:24   | 3 | use generated_shared::{Ball, FloatTuple, PlayGameRequest, PlayGameResponse};   |                        ^^^^  ^^^^^^^^^^   |   = note: `#[warn(unused_imports)]` on by default  warning: unused variable: `request`   --> src\/server.rs:20:9    | 20 |         request: tonic::Request&lt;PlayGameRequest>,    |         ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_request`    |    = note: `#[warn(unused_variables)]` on by default  warning: `ping_pong_multiplayer` (bin \"server\") generated 2 warnings     Finished dev [unoptimized + debuginfo] target(s) in 1.70s      Running `target\/debug\/server` Server listening on [::1]:50051 <\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c: <\/p>\n<pre><code class=\"rust\">async fn play_request(         &amp;self,         request: tonic::Request&lt;PlayGameRequest>,     ) -> Result&lt;tonic::Response&lt;PlayGameResponse>, tonic::Status> {         uninmplemented!();     } <\/code><\/pre>\n<p>\u043c\u044b \u0432\u0437\u044f\u043b\u0438 \u0438\u0437 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430 \u0432\u0445\u043e\u0434 <code>PlayGameRequest<\/code> \u0438 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0431\u0443\u0434\u0435\u043c <code>PlayGameResponse<\/code>.<\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0438 \u043f\u0440\u043e\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e \u0435\u0433\u043e:<\/p>\n<pre><code class=\"rust\">use tonic::{transport::Server, Response}; use generated_shared::game_proto_server::{GameProto, GameProtoServer}; use generated_shared::{Ball, FloatTuple, PlayGameRequest, PlayGameResponse}; use std::sync::{Mutex, Arc}; use tetra::math::Vec2; use rand::Rng;  mod generated_shared;  const BALL_SPEED: f32 = 5.0;  #[derive(Clone)] struct Entity {     texture_size: Vec2&lt;f32>,     position: Vec2&lt;f32>,     velocity: Vec2&lt;f32>, }  impl Entity {     fn new(texture_size: Vec2&lt;f32>, position: Vec2&lt;f32>) -> Entity {         Entity::with_velocity(texture_size, position, Vec2::zero())     }         fn with_velocity(texture_size: Vec2&lt;f32>, position: Vec2&lt;f32>, velocity: Vec2&lt;f32>) -> Entity {         Entity { texture_size, position, velocity }     } }  #[derive(Clone)] struct World {     player1: Entity,     player2: Entity,     ball: Entity,     world_size: Vec2&lt;f32>,     winner: u32, }  pub struct PlayGame {     world: Arc&lt;Mutex&lt;Option&lt;World>>>,     players_count: Arc&lt;Mutex&lt;u32>>, }  impl PlayGame {     fn new() -> PlayGame {         PlayGame {             world: Arc::new(Mutex::new(None)),             players_count: Arc::new(Mutex::new(0u32)),         }     }     fn init(&amp;self, window_size: FloatTuple, player1_texture: FloatTuple,             player2_texture: FloatTuple, ball_texture: FloatTuple) {         let window_width = window_size.x;         let window_height = window_size.y;         let world = Arc::clone(&amp;self.world);         let mut world = world.lock().unwrap();         let players_count = Arc::clone(&amp;self.players_count);         let players_count = players_count.lock().unwrap().clone();         let mut ball_velocity = 0f32;         if players_count >= 2 {             let num = rand::thread_rng().gen_range(0..2);             if num == 0 {                 ball_velocity = -BALL_SPEED;             } else {                 ball_velocity = BALL_SPEED;             }         }         *world =             Option::Some(World {                 player1:<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-391381","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/391381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=391381"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/391381\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}