{"id":275547,"date":"2016-03-05T07:14:03","date_gmt":"2016-03-05T04:14:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=275547"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=275547","title":{"rendered":"Rust \u0432 \u0434\u0435\u0442\u0430\u043b\u044f\u0445, \u0447\u0430\u0441\u0442\u044c 2"},"content":{"rendered":"<br \/>\n<h2>\u0427\u0430\u0441\u0442\u044c 2: \u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439<\/h2>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u0447\u0430\u0442\u0430 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0432\u043e \u0432\u0441\u0435\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445. \u0426\u0435\u043b\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430 <strong>Rust<\/strong> \u043d\u0430 \u0444\u043e\u043d\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 API, \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c.<\/p>\n<p>  \u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u044b\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <a href=\"http:\/\/habrahabr.ru\/post\/268609\/\">\u043f\u0435\u0440\u0432\u043e\u0439<\/a>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u043b\u0438 \u0432\u044b \u0435\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043b\u0438 (\u0438\u043b\u0438 \u0437\u0430\u0431\u044b\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442), \u0442\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0439. \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 WebSocket.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>14 \u0424\u0440\u0435\u0439\u043c\u044b<\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0433\u043e\u0442\u043e\u0432\u044b \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u043e\u0431\u043c\u0435\u043d\u0443 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438. \u041d\u043e \u043f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0430\u043a \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b WebSocket \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0438. \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"https:\/\/tools.ietf.org\/html\/rfc6455#section-5.2\">RFC<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u0441 \u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e <em>\u0444\u0440\u0435\u0439\u043c\u044b<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0438\u0437 <em>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430<\/em>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0433\u043e \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e (\u0442\u0438\u043f \u0444\u0440\u0435\u0439\u043c\u0430, \u0438 \u0442.\u043f.), \u0438 <em>\u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438<\/em> (payload), \u0442.\u0435., \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c.<\/p>\n<p>  \u041d\u0430\u0447\u0430\u043b\u043e \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 2 \u0431\u0430\u0439\u0442\u043e\u0432 (\u0438\u043b\u0438 16 \u0431\u0438\u0442\u043e\u0432), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/cda\/8ac\/441\/cda8ac441c9241889ed27f792113bb48.png\" \/><\/div>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u043e\u043c\u0431 \u043d\u0430 \u0441\u0445\u0435\u043c\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0434\u0438\u043d \u0431\u0438\u0442 (\u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0438\u043b\u0438 \u043d\u043e\u043b\u044c).<\/p>\n<ul>\n<li><code>fin<\/code> \u2014 \u043c\u0430\u0440\u043a\u0435\u0440 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e <em>\u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430<\/em> \u0432 \u0441\u0435\u0440\u0438\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438. \u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043e\u0441\u0432\u0435\u0449\u0430\u0442\u044c \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u0432 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u044f\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/li>\n<li><code>rsv1<\/code>, <code>rsv2<\/code>, \u0438 <code>rsv3<\/code> \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0434\u043b\u044f \u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u0439 WebSocket \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430. \u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u043d\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043c\u0435\u043b\u043e \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043f\u043e\u043b\u044f.<\/li>\n<li><code>opcode<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0442\u0438\u043f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0438\u043b\u0438 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u043b\u0438 \u0442\u0435\u043a\u0441\u0442. \u041f\u043e\u043c\u0438\u043c\u043e \u0442\u043e\u0433\u043e, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 <em>\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432<\/em> (control frames), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0434\u043b\u044f \u043f\u0438\u043d\u0433\u043e\u0432, \u0438\u043b\u0438 \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0442\u043e\u0440\u043e\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435. \u041c\u044b \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043f\u043e\u0437\u0436\u0435.<\/li>\n<li><code>masked<\/code> \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 1 (true) \u043a\u043e\u0433\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0431\u0438\u0442\u043e\u0432\u043e\u0439 \u043c\u0430\u0441\u043a\u0438. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0437\u0434\u043d\u0435\u0435.<\/li>\n<li><code>payload len<\/code> \u2014 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u043f\u043e\u043b\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0441\u043e\u0431\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0442.\u043a. \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u043c\u044b \u0441\u043a\u043e\u0440\u043e \u043a \u044d\u0442\u043e\u043c\u0443 \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f.<\/li>\n<\/ul>\n<p>  \u0423\u0447\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0432\u0441\u0435 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u044b \u0432\u0441\u0435\u0433\u043e \u0432 <strong>2<\/strong> <em>\u043e\u043a\u0442\u0435\u0442\u0430<\/em> (\u043e\u043a\u0442\u0435\u0442 \u044d\u0442\u043e \u0431\u0430\u0439\u0442, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 8 \u0431\u0438\u0442\u043e\u0432). \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432 JSON:<\/p>\n<pre><code>{   &quot;fin&quot;  : 1,   &quot;rsv1&quot; : 0,   &quot;rsv2&quot; : 0,   &quot;rsv3&quot; : 0,   &quot;opcode&quot; : 2,   &quot;masked&quot; : 1,   &quot;payload_len&quot;: 64 }<\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u044d\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b \u0432 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u0411\u0438\u0442\u043e\u0432\u043e\u0435_\u043f\u043e\u043b\u0435\">\u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435<\/a>, \u043a\u0430\u043a \u0433\u0440\u0443\u043f\u043f\u044b \u0431\u0438\u0442\u043e\u0432:<\/p>\n<pre><code>{   &quot;fin&quot;  : b1,   &quot;rsv1&quot; : b0,   &quot;rsv2&quot; : b0,   &quot;rsv3&quot; : b0,   &quot;opcode&quot; : b10,   &quot;masked&quot; : b1,   &quot;payload_len&quot;: b1000000 }<\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u0430\u043a \u043e\u0434\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e: <code>1.0.0.0.0010.1.1000000<\/code>, \u0438\u043b\u0438 \u0436\u0435 \u0432 \u0448\u0435\u0441\u043d\u0430\u0434\u0446\u0430\u0442\u0435\u0440\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f: <code>0x82C0<\/code>.<br \/>  \u042d\u0442\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u043e\u0433\u043e \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0438 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0441 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044f\u043c\u0438, \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0432 \u0435\u0433\u043e \u043d\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u0438.<\/p>\n<h2>15 \u0420\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432<\/h2>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u0438\u0437 \u0447\u0438\u0441\u043b\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u0443\u044e \u043d\u0430\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0442\u0435\u0445\u043d\u0438\u043a\u0443, \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0443\u044e \u043a\u0430\u043a <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u0411\u0438\u0442\u043e\u0432\u0430\u044f_\u043c\u0430\u0441\u043a\u0430\"><em>\u0431\u0438\u0442\u043e\u0432\u044b\u0435 \u043c\u0430\u0441\u043a\u0438<\/em><\/a>. \u0417\u0432\u0443\u0447\u0438\u0442 \u0441\u0442\u0440\u0430\u0448\u043d\u043e, \u043d\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0438\u0434\u0435\u044f.<\/p>\n<p>  \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e &quot;\u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c&quot; \u0431\u0438\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c &quot;\u0438\u0437\u0432\u043b\u0435\u0447\u044c&quot; \u0438\u0437 \u0447\u0438\u0441\u043b\u0430:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/31b\/bcb\/e32\/31bbcbe32ed349bc9ef8959787fbbbe9.png\" \/><\/div>\n<p>  \u0421\u0435\u0440\u044b\u0435 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u044b \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u043d\u0443\u043b\u0438, \u0430 \u0436\u0435\u043b\u0442\u044b\u0435 \u2014 \u0435\u0434\u0438\u043d\u0438\u0446\u044b.<br \/>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0441\u0445\u0435\u043c\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0431\u0438\u0442\u043e\u0432\u043e\u0439 \u043c\u0430\u0441\u043a\u0435 <abbr title=\"0x0F \u0432 \u0448\u0435\u0441\u0442\u043d\u0430\u0434\u0446\u0430\u0442\u0435\u0440\u0438\u0447\u043d\u043e\u0439, 15 \u0432 \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435\"><code>00001111b<\/code><\/abbr>.<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/ba0\/4ec\/034\/ba04ec0340a64151b3813568c18bac54.png\" \/><\/div>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044f \u0442\u043e\u0439 \u0436\u0435 \u043b\u043e\u0433\u0438\u043a\u0435, \u044d\u0442\u043e \u0431\u0438\u0442\u043e\u0432\u0430\u044f \u043c\u0430\u0441\u043a\u0430 <abbr title=\"0x20 \u0432 \u0448\u0435\u0441\u0442\u043d\u0430\u0434\u0446\u0430\u0442\u0435\u0440\u0438\u0447\u043d\u043e\u0439, 32 \u0432 \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435\"><code>00100000b<\/code><\/abbr>.<\/p>\n<p>  \u041c\u044b &quot;\u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c&quot; \u043c\u0430\u0441\u043a\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <code>&amp;<\/code> (\u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0435 <strong>\u0418<\/strong>, <code>and<\/code>) \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: <code>\u0431\u0430\u0439\u0442 &amp; \u043c\u0430\u0441\u043a\u0430<\/code>.<br \/>  \u0412\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/211\/c44\/e83\/211c44e839674294be92f87ae44abf03.png\" \/><\/div>\n<p>  \u041e\u043f\u044f\u0442\u044c \u0436\u0435, \u0441\u0435\u0440\u044b\u0435 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 <code>0<\/code>, \u0430 \u0437\u0435\u043b\u0435\u043d\u044b\u0435 \u2014 <code>1<\/code>.<br \/>  \u0418\u043c\u0435\u044f \u0434\u0432\u0435 &quot;\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435&quot; <code>A = 1b<\/code> \u0438 <code>B = 0b<\/code>, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c <code>Out = 0b<\/code>.<br \/>  \u0422\u043e \u0436\u0435 \u0432\u0435\u0440\u043d\u043e \u0434\u043b\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <code>A = 0b<\/code> \u0438 <code>B = 1b<\/code>.<br \/>  <code>Out = 1b<\/code> \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043e\u0431\u0430 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0447\u0438\u0441\u043b\u0430 \u2014 \u0435\u0434\u0438\u043d\u0438\u0446\u044b. \u0412\u043e\u0442 \u0438 \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430.<\/p>\n<p>  \u0412 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 <em>\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e<\/em> \u0431\u0438\u0442\u043e\u0432, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f <code>\u0418<\/code> (AND) \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043d\u0438\u0445. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>A = 101b<\/code> \u0438 <code>B = 011b<\/code>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f <code>Out = 001b<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u0449\u0438\u0439 \u0431\u0438\u0442 \u0443 <code>A<\/code> \u0438 <code>B<\/code> \u2014 \u044d\u0442\u043e \u0442\u0440\u0435\u0442\u044c\u044f \u0435\u0434\u0438\u043d\u0438\u0446\u0430, <code>1b<\/code>.<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/030\/01d\/add\/03001dadd0e143d49a42277b2df1d68a.png\" \/><\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u0443 \u0436\u0435 \u043b\u043e\u0433\u0438\u043a\u0443, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0430\u0441\u043a\u0438 \u0441 &quot;\u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438&quot; \u0431\u0438\u0442\u0430\u043c\u0438, \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0432\u0438\u0434\u0435\u043b\u0438 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435, \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445 \u043d\u0430\u0441 \u0447\u0430\u0441\u0442\u0435\u0439 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430.<\/p>\n<h2>16 \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432<\/h2>\n<p>  <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u041f\u043e\u0440\u044f\u0434\u043e\u043a_\u0431\u0430\u0439\u0442\u043e\u0432\">\u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432<\/a> (endianness<sup><a href=\"#n1\">[1]<\/a><\/sup>) \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0431\u0430\u0439\u0442\u0430 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435. \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0438\u0434\u0442\u0438 \u043e\u0442 <em>\u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443<\/em> (little-endian), \u043b\u0438\u0431\u043e \u043e\u0442 <em>\u0441\u0442\u0430\u0440\u0448\u0435\u0433\u043e \u043a \u043c\u043b\u0430\u0434\u0448\u0435\u043c\u0443<\/em> (big-endian).<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0432 \u0447\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435.<\/p>\n<p>  \u041e\u0434\u043d\u043e \u0441\u043b\u043e\u0432\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u0434\u043e <abbr title=\"65535 \u0432 \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435\"><code>0xFFFF<\/code><\/abbr>.<br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>0x0001<\/code> \u2014 \u0432 \u043a\u0430\u043a\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0434\u0432\u0430 \u044d\u0442\u0438\u0445 \u0431\u0430\u0439\u0442\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438?<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/117\/28d\/282\/11728d28252b420b8c33ba3efce82046.png\" \/><\/div>\n<p>  \u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0430\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u2014 \u044d\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u043b\u043e\u0432 \u043d\u0430 \u0431\u0443\u043c\u0430\u0433\u0435. \u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u0432 \u0435\u0432\u0440\u043e\u043f\u0435\u0439\u0441\u043a\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043e\u043d\u0438 \u043f\u0438\u0448\u0443\u0442\u0441\u044f \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e. \u0422\u0430\u043a\u0438\u043c \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0438 \u0430\u0440\u0430\u0431\u0441\u043a\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u2014 \u0441\u043b\u0435\u0432\u0430 \u0446\u0438\u0444\u0440\u044b \u0441\u0442\u0430\u0440\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 (\u0442\u044b\u0441\u044f\u0447\u0438, \u0441\u043e\u0442\u043d\u0438), \u0441\u043f\u0440\u0430\u0432\u0430 \u2014 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 (\u0434\u0435\u0441\u044f\u0442\u043a\u0438, \u0435\u0434\u0438\u043d\u0438\u0446\u044b). \u0412 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u044b \u0441\u0442\u0430\u0440\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u2014 \u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u0430, \u0442.\u0435., \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0447\u0438\u0441\u043b\u0430 <abbr title=\"10818 \u0432 \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435\"><code>0x2A42<\/code><\/abbr> \u0441\u0442\u0430\u0440\u0448\u0438\u043c \u0431\u0430\u0439\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>0x2A<\/code>. \u0422\u0430\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043a\u0443 <em>big-endian<\/em>.<\/p>\n<p>  \u0412 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u043e\u0441\u0442\u043e\u0447\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0438\u0432\u0440\u0438\u0442\u0435 \u0438 \u0430\u0440\u0430\u0431\u0441\u043a\u043e\u043c), \u0441\u043b\u043e\u0432\u0430 \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e. \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 <em>little-endian<\/em> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u0445\u043e\u0436\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u043c\u043b\u0430\u0434\u0448\u0438\u0435 \u0431\u0430\u0439\u0442\u044b \u0442\u0430\u043c \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043b\u0435\u0432\u0430, \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e <code>0x2A42<\/code> \u043c\u044b \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u0430\u043a <code>0x422A<\/code>.<\/p>\n<p>  \u0412\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u0438 \u0447\u0438\u0441\u043b\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/e43\/e8b\/9b8\/e43e8b9b8a8145619d5c17cbd772737c.png\" \/><\/div>\n<p>  \u0412 \u0446\u0435\u043b\u043e\u043c \u044d\u0442\u043e \u0432\u0441\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043f\u0440\u043e \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0434\u0432\u0443\u0445\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c <code>0x0001<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 little-endian \u043c\u044b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c &quot;\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c&quot; \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u2014 \u043a\u0430\u043a <code>0x0100<\/code>.<\/p>\n<p>  \u0412\u0441\u0435 \u044d\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435: \u043f\u043e \u0441\u0435\u0442\u0438 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u043d\u0430\u043c \u0432 big-endian, \u0438 \u044d\u0442\u043e \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u0442\u0430\u043a\u0436\u0435 &quot;\u0441\u0435\u0442\u0435\u0432\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0431\u0430\u0439\u0442&quot;. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u044b \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b x86 (\u0438 x86-64) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a little-endian<sup><a href=\"#n1\">[1]<\/a><\/sup>, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u0447\u0438\u0441\u0435\u043b \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439. \u0417\u0432\u0443\u0447\u0438\u0442 \u0441\u043b\u043e\u0436\u043d\u043e? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0436\u0438\u0437\u043d\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f <a href=\"http:\/\/crates.io\">Crates<\/a>.<\/p>\n<p>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0430\u044f \u043d\u0430\u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <a href=\"http:\/\/burntsushi.net\/rustdoc\/byteorder\/\"><em>byteorder<\/em><\/a>, \u0438 \u043e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u0430\u0439\u0442\u0430\u043c\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c, \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0439 \u043d\u0430\u0441 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u0434 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0438\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u0431\u0430\u0439\u0442\u043e\u0432. \u0412\u0441\u0435 \u044d\u0442\u043e \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0434\u043d\u0435\u0435.<\/p>\n<h2>17 \u0420\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f<\/h2>\n<p>  \u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043c\u044b \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0438 \u0431\u0430\u0439\u0442\u043e\u0432, \u0437\u0430\u0439\u043c\u0435\u043c\u0441\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0430\u0437\u043c\u0438\u043d\u043a\u043e\u0439 \u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0442\u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>WebSocketClient<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448 \u043a\u043e\u0434 \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u043b\u0441\u044f \u0432 \u043b\u0430\u043f\u0448\u0443. \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043c\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f, \u0438 \u043c\u0435\u0442\u043e\u0434 <code>read<\/code> \u0432 \u043d\u0435\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043a \u043d\u0430\u043c \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043c \u043a\u043e\u0434 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0432 <code>fn read<\/code> \u0432 <code>fn read_handshake<\/code>. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>read<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0443\u0436\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"rust\">fn read(&amp;mut self) {     match self.state {         ClientState::AwaitingHandshake =&gt; {             self.read_handshake();         },         _ =&gt; {}     } }<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>self.state<\/code>, \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e <code>AwaitingHandshake<\/code> (\u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0435\u0434\u043d\u0438\u0435\u043d\u0438\u044f \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 HTTP). \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d <code>_<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Rust \u0442\u0440\u0435\u0431\u0443\u0435\u0442, \u0447\u0442\u043e\u0431\u044b <code>match<\/code> \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u043b \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<p>  \u0418 \u0440\u0430\u0437 \u0443\u0436 \u043c\u044b \u0437\u0430\u0442\u0440\u043e\u043d\u0443\u043b\u0438 \u0442\u0435\u043c\u0443 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f (<code>self.state<\/code>), \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c Rust&#8217;\u0430. \u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u044b \u043f\u0430\u0440\u0441\u0438\u043b\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, <code>Parser<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 HTTP-\u043f\u0430\u0440\u0441\u0435\u0440\u0430. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u044d\u0442\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0443\u0436\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u2014 \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0435\u0449\u0435 \u043d\u0435 \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043a \u043e\u0431\u0449\u0435\u043d\u0438\u044e \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 WebSocket, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u0443\u044e \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u043c \u043d\u0430\u043c \u043f\u0430\u0440\u0441\u0435\u0440\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0442\u0440\u044e\u043a\u0430: \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 (enum) <code>ClientState<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"rust\">enum ClientState {     \/\/ \u041c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u043f\u0440\u044f\u043c\u043e \u0432 enum:     AwaitingHandshake(RefCell&lt;Parser&lt;HttpParser&gt;&gt;),     \u2026 }  struct WebSocketClient {     \/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0438\u0437 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f:     headers: Rc&lt;RefCell&lt;HashMap&lt;String, String&gt;&gt;&gt;,     \u2026     interest: EventSet,     \u2026 }  \/\/ \u0418\u0437\u043c\u0435\u043d\u0438\u043c \u043a\u043e\u0434 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b: impl WebSocketClient {     fn new(socket: TcpStream) -&gt; WebSocketClient {         \u2026         WebSocketClient {             socket: socket,             headers: headers.clone(),             interest: EventSet::readable(),             state: ClientState::AwaitingHandshake(RefCell::new(Parser::request(HttpParser {                 current_key: None,                 headers: headers.clone()             })))         }     } }<\/code><\/pre>\n<p>  \u0427\u0442\u043e \u044d\u0442\u043e \u043d\u0430\u043c \u0434\u0430\u0435\u0442, \u0438 \u043a\u0430\u043a \u0442\u0430\u043a\u043e\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e? \u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u2014 \u044d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438. \u0413\u043e\u0432\u043e\u0440\u044f \u0435\u0449\u0435 \u0442\u043e\u0447\u043d\u0435\u0435, \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 Rust \u2014 \u044d\u0442\u043e <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u0410\u043b\u0433\u0435\u0431\u0440\u0430\u0438\u0447\u0435\u0441\u043a\u0438\u0439_\u0442\u0438\u043f_\u0434\u0430\u043d\u043d\u044b\u0445\"><em>\u0430\u043b\u0433\u0435\u0431\u0440\u0430\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/em><\/a> (ADT), \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 (OCaml, Haskell, \u0438 \u0434\u0440.). ADT \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043d\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435), \u043f\u043e\u043b\u044c\u0437\u0443\u044f\u0441\u044c \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439 \u0442\u0438\u043f\u043e\u0432. \u0422\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0432 \u044f\u0437\u044b\u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 \u043d\u0430\u043c <code>Option<\/code> \u0438 <code>Result<\/code>. \u0412 \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u0445 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0447\u0435\u0440\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438, \u0438 \u0432\u044b \u043f\u0440\u0438 \u0436\u0435\u043b\u0430\u043d\u0438\u0438 \u043c\u043e\u0436\u0435\u0442\u0435 \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, Rust \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u0442\u0438\u043f\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u043e\u0434\u043d\u0430\u043a\u043e, \u043f\u043e\u043b\u044f \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>RefCell<\/code> \u0434\u043b\u044f \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u044f\u0446\u0438\u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0430\u0440\u0441\u0435\u0440\u0430.<\/p>\n<p>  \u0418\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 (\u0430 \u0442\u043e\u0447\u043d\u0435\u0435 \u2014 \u0432 \u0442\u0438\u043f <code>ClientState::AwaitingHandshake<\/code>), \u0432\u0441\u044f \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0430\u044f \u0441 \u043d\u0438\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0430 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a <code>ClientState::Connected<\/code>. \u042d\u0442\u043e\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0430 \u043c\u044b \u0438 \u0434\u043e\u0431\u0438\u0432\u0430\u043b\u0438\u0441\u044c. \u0422\u0435\u043f\u0435\u0440\u044c, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u0443\u0431\u0440\u0430\u043b\u0438 <code>http_parser<\/code> \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read<\/code> \u0438 <code>read_handshake<\/code>:<\/p>\n<pre><code class=\"rust\">fn read(&amp;mut self) {     match self.state {         ClientState::AwaitingHandshake(_) =&gt; {             self.read_handshake();         },         _ =&gt; {}     } }  fn read_handshake(&amp;mut self) {    let is_upgrade = if let ClientState::AwaitingHandshake(ref parser_state) = self.state {        let mut parser = parser_state.borrow_mut();        parser.parse(&amp;buf);        parser.is_upgrade()    } else { false };     if is_upgrade {        \/\/ \u0418\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435        self.state = ClientState::HandshakeResponse;        \u2026    } }<\/code><\/pre>\n<p>  \u0412 <code>read_handshake<\/code> \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0435\u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u0432\u0438\u0434 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f, <a href=\"http:\/\/rurust.github.io\/rust_book_ru\/src\/if-let.html\"><code>if let<\/code><\/a>:<\/p>\n<pre><code class=\"rust\">let is_upgrade = if let ClientState::AwaitingHandshake(ref parser_state) = self.state {<\/code><\/pre>\n<p>  <code>if let<\/code> \u2014 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c <code>match<\/code>. \u0421 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043e\u0431\u0440\u0430\u0437\u0446\u0443, \u0447\u0442\u043e \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0443\u0434\u043e\u0431\u043d\u0435\u0435, \u0447\u0435\u043c <code>match<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432.<\/p>\n<p>  \u0418 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043e\u0431\u0440\u0430\u0437\u0435\u0446, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0438 \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <code>ref<\/code>:<\/p>\n<pre><code class=\"rust\">ClientState::AwaitingHandshake(ref parser_state)<\/code><\/pre>\n<p>  <code>ref<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 (\u0442\u043e \u0435\u0441\u0442\u044c, \u0434\u043b\u044f \u0435\u0433\u043e \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f) \u2014 \u0431\u0435\u0437 <code>ref<\/code> \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0431\u044b \u0442\u0430\u043a, \u0431\u0443\u0434\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u043d\u043e\u0432\u043e\u043c\u0443 \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0443, \u043c\u0435\u0442\u0430\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>parser_state<\/code> (\u0438\u043b\u0438 \u0436\u0435 \u043d\u0435\u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0435\u0433\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c).<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read<\/code>, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u0435\u043b\u0430\u043b\u0438 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"rust\">match self.state {     ClientState::AwaitingHandshake(ref parser_state) =&gt; {         self.read_handshake(parser_state);     },     \u2026 }<\/code><\/pre>\n<p>  \u041d\u043e \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u0430\u0432\u0438\u043b \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0439, \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0442\u0430\u043a\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443:<\/p>\n<pre><code class=\"rust\">error: cannot borrow `*self` as mutable because `self.state.0` is also borrowed as immutable        ClientState::AwaitingHandshake(ref parser_state) =&gt; self.read_handshake(...),                                                            ^~~~<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/dc7\/50d\/c1c\/dc750dc1c2b84e2abfc20d028004b376.png\" align=\"right\"\/><br \/>  \u041f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u044d\u0442\u043e \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0430\u0440\u0441\u0435\u0440\u0430 <code>ref parser_state<\/code> \u0438\u0437 <code>self.state<\/code> \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u0443\u0435\u043c \u0438 <code>self<\/code> \u2014 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0439 \u0438\u0437 <code>self.state<\/code>.<\/p>\n<p>  \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read_handshake<\/code> \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0445\u043e\u0434\u0430 \u043c\u044b <em>\u0435\u0449\u0435 \u0440\u0430\u0437<\/em> \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u0443\u0435\u043c <code>self<\/code> \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u2014 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0434\u0432\u043e\u0439\u043d\u043e\u0433\u043e \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f <code>self<\/code> \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0447\u0442\u043e, \u043a\u0430\u043a \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u0432 Rust \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043e \u2014 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u0443\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read_handshake<\/code>.<\/p>\n<p>  \u0412\u0441\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u043c, \u043d\u043e \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c Rust \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043f\u043e\u0432\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u0441\u043b\u0435\u0434\u0443\u044f \u0442\u0430\u043a\u0438\u043c \u0441\u0442\u0440\u043e\u0433\u0438\u043c, \u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c, \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u043c \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u044b, \u0447\u0442\u043e \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e.<\/p>\n<h2>18 \u0420\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430<\/h2>\n<p>  \u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430. \u041d\u043e \u043f\u0440\u0435\u0436\u0434\u0435, \u0447\u0435\u043c \u043c\u044b \u043a \u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c, \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 \u043c\u043e\u0434\u0443\u043b\u0438 \u2014 \u0441 \u0440\u043e\u0441\u0442\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0442\u0440\u043e\u043a \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u041c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043c\u043e\u0434\u0443\u043b\u044f <code>frame.rs<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043a\u043e\u0434, \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0438\u0439\u0441\u044f \u043a \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0444\u0440\u0435\u0439\u043c\u0430\u043c\u0438.<\/p>\n<p>  \u0414\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u043c\u043e\u0434\u0443\u043b\u044f \u2014 \u043a\u043e\u0434 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043e\u0431\u044a\u0435\u043c\u043d\u044b\u0439, \u043d\u043e \u043d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c, \u043f\u043e\u0441\u043b\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0433\u043e \u0432\u043e \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u044f\u0445:<\/p>\n<pre><code class=\"rust\">use std::io; use std::io::Read; use std::error::Error;  use byteorder::{ReadBytesExt, BigEndian};  const PAYLOAD_LEN_U16: u8 = 126; const PAYLOAD_LEN_U64: u8 = 127;  #[derive(Debug, Clone, Copy, PartialEq)] #[allow(dead_code)] pub enum OpCode {     TextFrame = 1,     BinaryFrame = 2,     ConnectionClose = 8,     Ping = 9,     Pong = 0xA }  impl OpCode {     fn from(op: u8) -&gt; Option&lt;OpCode&gt; {         match op {             1 =&gt; Some(OpCode::TextFrame),             2 =&gt; Some(OpCode::BinaryFrame),             8 =&gt; Some(OpCode::ConnectionClose),             9 =&gt; Some(OpCode::Ping),             0xA =&gt; Some(OpCode::Pong),             _ =&gt; None         }     } }  pub struct WebSocketFrameHeader {     fin: bool,     rsv1: bool,     rsv2: bool,     rsv3: bool,     masked: bool,     opcode: OpCode,     payload_length: u8 }  pub struct WebSocketFrame {     header: WebSocketFrameHeader,     mask: Option&lt;[u8; 4]&gt;,     pub payload: Vec&lt;u8&gt; }  impl WebSocketFrame {     pub fn read&lt;R: Read&gt;(input: &amp;mut R) -&gt; io::Result&lt;WebSocketFrame&gt; {         let buf = try!(input.read_u16::&lt;BigEndian&gt;());         let header = Self::parse_header(buf);          let len = try!(Self::read_length(header.payload_length, input));         let mask_key = if header.masked {             let mask = try!(Self::read_mask(input));             Some(mask)         } else {             None         };         let mut payload = try!(Self::read_payload(len, input));          if let Some(mask) = mask_key {             Self::apply_mask(mask, &amp;mut payload);         }          Ok(WebSocketFrame {             header: header,             payload: payload,             mask: mask_key         })     }      pub fn get_opcode(&amp;self) -&gt; OpCode {         self.header.opcode.clone()     }      fn parse_header(buf: u16) -&gt; Result&lt;WebSocketFrameHeader, String&gt; {         let opcode_num = ((buf &gt;&gt; 8) as u8) &amp; 0x0F;         let opcode = OpCode::from(opcode_num);          if let Some(opcode) = opcode {             Ok(WebSocketFrameHeader {                 fin: (buf &gt;&gt; 8) &amp; 0x80 == 0x80,                 rsv1: (buf &gt;&gt; 8) &amp; 0x40 == 0x40,                 rsv2: (buf &gt;&gt; 8) &amp; 0x20 == 0x20,                 rsv3: (buf &gt;&gt; 8) &amp; 0x10 == 0x10,                 opcode: opcode,                  masked: buf &amp; 0x80 == 0x80,                 payload_length: (buf as u8) &amp; 0x7F,             })         } else {             Err(format!(&quot;\u041d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 opcode: {}&quot;, opcode_num))         }     }      fn apply_mask(mask: [u8; 4], bytes: &amp;mut Vec&lt;u8&gt;) {         for (idx, c) in bytes.iter_mut().enumerate() {             *c = *c ^ mask[idx % 4];         }     }      fn read_mask&lt;R: Read&gt;(input: &amp;mut R) -&gt; io::Result&lt;[u8; 4]&gt; {         let mut buf = [0; 4];         try!(input.read(&amp;mut buf));         Ok(buf)     }      fn read_payload&lt;R: Read&gt;(payload_len: usize, input: &amp;mut R) -&gt; io::Result&lt;Vec&lt;u8&gt;&gt; {         let mut payload: Vec&lt;u8&gt; = Vec::with_capacity(payload_len);         payload.extend(iter::repeat(0).take(payload_len));         try!(input.read(&amp;mut payload));         Ok(payload)     }      fn read_length&lt;R: Read&gt;(payload_len: u8, input: &amp;mut R) -&gt; io::Result&lt;usize&gt; {         return match payload_len {             PAYLOAD_LEN_U64 =&gt; input.read_u64::&lt;BigEndian&gt;().map(|v| v as usize).map_err(|e| io::Error::from(e)),             PAYLOAD_LEN_U16 =&gt; input.read_u16::&lt;BigEndian&gt;().map(|v| v as usize).map_err(|e| io::Error::from(e)),             _ =&gt; Ok(payload_len as usize) \/\/ payload_len &lt; 127         }     } }<\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0432\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0432 \u043a\u043e\u0434\u0435 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 <code>pub<\/code> \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0438 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442 \u0432 \u044f\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u041e\u043d\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043c\u043e\u0434\u0443\u043b\u044f \u2014 \u0442.\u0435., \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0434\u0440\u0443\u0433\u0438\u043c \u043c\u043e\u0434\u0443\u043b\u044f\u043c \u0434\u043b\u044f \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <code>use frame::{a, b, c};<\/code>.<\/p>\n<p>  \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0434\u0432\u0435 \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b: <code>WebSocketFrameHeader<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430, \u0430 <code>WebSocketFrame<\/code> \u043d\u0443\u0436\u043d\u0430 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0445 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 <code>WebSocketFrame<\/code> \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>read<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0440\u0435\u0439\u043c\u044b \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0441\u043e\u043a\u0435\u0442\u0430, \u043d\u043e \u0438 \u0438\u0437 \u043b\u044e\u0431\u044b\u0445 \u0434\u0440\u0443\u0433\u0438\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u0444\u0430\u0439\u043b\u043e\u0432, \u0441\u0442\u0440\u043e\u043a, \u0438 \u0442.\u0434. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043a\u0430\u043a <code>TcpStream<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u0449\u0438\u0439 \u0434\u043b\u044f \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u0442\u0438\u043f\u0430\u0436 <a href=\"http:\/\/doc.rust-lang.org\/std\/io\/trait.Read.html\"><code>Read<\/code><\/a>. \u042d\u0442\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u2014 \u0440\u0435\u0434\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u043b\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b\/\u0442\u0438\u043f\u0430\u0436\u0438 \u2014 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<h3>\u0427\u0442\u0435\u043d\u0438\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430<\/h3>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0440\u0430\u0437\u0431\u043e\u0440 \u043a\u043e\u0434\u0430 \u0441 \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430:<\/p>\n<pre><code class=\"rust\">fn parse_header(buf: [u8; 2]) -&gt; WebSocketFrameHeader {     let opcode_num = ((buf &gt;&gt; 8) as u8) &amp; 0x0F;     let opcode = OpCode::from(opcode_num);      if let Some(opcode) = opcode {         Ok(WebSocketFrameHeader {             fin: (buf &gt;&gt; 8) &amp; 0x80 == 0x80,             rsv1: (buf &gt;&gt; 8) &amp; 0x40 == 0x40,             rsv2: (buf &gt;&gt; 8) &amp; 0x20 == 0x20,             rsv3: (buf &gt;&gt; 8) &amp; 0x10 == 0x10,             opcode: opcode,              masked: buf &amp; 0x80 == 0x80,             payload_length: (buf as u8) &amp; 0x7F,         })     } else {         Err(format!(&quot;\u041d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 opcode: {}&quot;, opcode_num))     } }<\/code><\/pre>\n<p>  \u0412 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u043c\u0443 <code>&amp;<\/code> (\u0418) \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e <em>\u0441\u0434\u0432\u0438\u0433\u0430 \u0432\u043f\u0440\u0430\u0432\u043e<\/em> \u2014 <code>&gt;&gt;<\/code>.<br \/>  \u0418\u0434\u0435\u044f \u0442\u0443\u0442 \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c \u0441 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u044b\u043c <code>\u0418<\/code> \u2014 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044d\u0442\u043e \u0442\u0430\u043a:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/cda\/8ac\/441\/cda8ac441c9241889ed27f792113bb48.png\" \/><\/div>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/f10\/e98\/ff3\/f10e98ff35de42039c50814136eb0ed9.png\" \/><\/div>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0438\u0442\u043e\u0432 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u044b\u043c\u0438 \u043c\u0430\u0441\u043a\u0430\u043c\u0438.<\/p>\n<pre><code class=\"rust\">let opcode_num = ((buf &gt;&gt; 8) as u8) &amp; 0x0F;<\/code><\/pre>\n<p>  \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u043c\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c 8 \u0441\u0442\u0430\u0440\u0448\u0438\u0445 \u0431\u0438\u0442\u043e\u0432 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0443\u044e \u043d\u0430\u043c \u043c\u0430\u0441\u043a\u0443:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/31b\/bcb\/e32\/31bbcbe32ed349bc9ef8959787fbbbe9.png\" \/><\/div>\n<p>  \u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0430\u0441\u043a\u0438 \u043a \u0434\u0440\u0443\u0433\u0438\u043c \u0447\u0430\u0441\u0442\u044f\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430.<\/p>\n<p>  \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>OpCode<\/code> \u0438\u0437 \u043d\u043e\u043c\u0435\u0440\u0430 \u0442\u0438\u043f\u0430 \u0444\u0440\u0435\u0439\u043c\u0430:<\/p>\n<pre><code class=\"rust\">let opcode = OpCode::from(opcode_num);<\/code><\/pre>\n<p>  \u0412 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0442 \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435\u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0438 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442 \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0438\u043a\u0430\u043a\u043e\u043c\u0443 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043e\u043f\u043a\u043e\u0434\u0443 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u0412 <code>opcode_num<\/code> \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<br \/>  \u041a \u0442\u043e\u043c\u0443 \u0436\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0442\u0438\u043f \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0438 \u0441 \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u0447\u0438\u0441\u043b\u043e\u043c:<\/p>\n<pre><code class=\"rust\">pub enum OpCode {     TextFrame = 1,     BinaryFrame = 2,     \u2026 }<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0438\u0437 \u0447\u0438\u0441\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0442\u0438\u043f <code>OpCode<\/code>, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"rust\">impl OpCode {     fn from(op: u8) -&gt; Option&lt;OpCode&gt; {         match op {             1 =&gt; Some(OpCode::TextFrame),             2 =&gt; Some(OpCode::BinaryFrame),             \u2026             _ =&gt; None         }     } }<\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <code>Option&lt;OpCode&gt;<\/code> \u2014 \u0434\u043b\u044f \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u043e\u043c\u0435\u0440 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u043c\u0443 \u043e\u043f\u043a\u043e\u0434\u0443, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c <code>None<\/code>.<\/p>\n<h3>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445. \u0417\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0444\u0440\u0435\u0439\u043c\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0432 7 \u0431\u0438\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f <code>payload len<\/code> \u2014 127. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 \u044d\u0442\u0438 \u043f\u0440\u0435\u0434\u0435\u043b\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0434\u043b\u0438\u043d\u044b \u0432 \u0431\u0430\u0439\u0442\u0430\u0445. \u0426\u0438\u0442\u0430\u0442\u0430 \u0438\u0437 <a href=\"https:\/\/tools.ietf.org\/html\/rfc6455#section-5.2\">RFC<\/a> (\u0440\u0430\u0437\u0434\u0435\u043b 5.2):<\/p>\n<blockquote><p>\u0420\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u0431\u0430\u0439\u0442\u0430\u0445: \u0435\u0441\u043b\u0438 0-125, \u0442\u043e \u044d\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 126, \u0442\u043e<br \/>  \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 2 \u0431\u0430\u0439\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a<br \/>  16-\u0431\u0438\u0442\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0431\u0435\u0437\u0437\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u0415\u0441\u043b\u0438 127, \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435<br \/>  8 \u0431\u0430\u0439\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a 64-\u0431\u0438\u0442\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0431\u0435\u0437\u0437\u043d\u0430\u043a\u043e\u0432\u043e\u0435<br \/>  \u0447\u0438\u0441\u043b\u043e.  <\/p><\/blockquote>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c, \u0435\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 Rust, \u043c\u044b \u0447\u0438\u0442\u0430\u0435\u043c <code>u16<\/code>, \u0435\u0441\u043b\u0438 <code>payload_len<\/code> \u0440\u0430\u0432\u043d\u043e <code>126<\/code>, \u0438 <code>u64<\/code>, \u0435\u0441\u043b\u0438 <code>127<\/code>. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u0434\u0435 \u044d\u0442\u0438 &quot;\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435&quot; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043f\u0430\u0440\u0443 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u0435\u0435-\u043c\u0435\u043d\u0435\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c\u0438:<\/p>\n<pre><code class=\"rust\">const PAYLOAD_LEN_U16: u8 = 126; const PAYLOAD_LEN_U64: u8 = 127;<\/code><\/pre>\n<p>  \u0418 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"rust\">fn read_length&lt;R: Read&gt;(payload_len: u8, input: &amp;mut R) -&gt; io::Result&lt;usize&gt; {     return match payload_len {         PAYLOAD_LEN_U64 =&gt; input.read_u64::&lt;BigEndian&gt;().map(|v| v as usize).map_err(From::from),         PAYLOAD_LEN_U16 =&gt; input.read_u16::&lt;BigEndian&gt;().map(|v| v as usize).map_err(From::from),         _ =&gt; Ok(payload_len as usize) \/\/ payload_len &lt; 127     } }<\/code><\/pre>\n<p>  \u041a\u043e\u0434 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u043b\u043e\u0442\u043d\u044b\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"rust\">fn read_length&lt;R: Read&gt;(payload_len: u8, input: &amp;mut R) -&gt; io::Result&lt;usize&gt; {<\/code><\/pre>\n<p>  \u041e\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430: <code>payload_len<\/code> (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430), \u0438 <code>input<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f <code>R<\/code>. <code>&lt;R: Read&gt;<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u044d\u0442\u043e\u0442 \u0442\u0438\u043f.<\/p>\n<p>  <a href=\"http:\/\/rurust.github.io\/rust_book_ru\/src\/generics.html\"><em>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435<\/em><\/a> (\u0438\u043b\u0438 <em>\u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0435<\/em>) \u0442\u0438\u043f\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e, \u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u2014 \u044d\u0442\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 &quot;\u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0438&quot; \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<p>  \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read_length<\/code> \u043c\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043b\u044e\u0431\u043e\u0439 \u0442\u0438\u043f, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u0442\u0438\u043f\u0430\u0436 <a href=\"http:\/\/doc.rust-lang.org\/std\/io\/trait.Read.html\"><code>Read<\/code><\/a>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <code>input<\/code> \u0434\u043e\u043b\u0436\u043d\u0430 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0447\u0438\u0442\u0430\u0435\u043c \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u043d\u0435\u043a\u043e\u0435\u0433\u043e \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u044b\u043c \u0441\u043e\u043a\u0435\u0442\u043e\u043c, \u0444\u0430\u0439\u043b\u043e\u043c, \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0431\u0430\u0439\u0442\u043e\u0432 \u2014 \u0442\u0438\u043f\u0430\u0436 <code>Read<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432).<\/p>\n<p>  \u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u0438\u043f\u0430 <code>io::Result&lt;usize&gt;<\/code>. <code>io::Result<\/code> \u044d\u0442\u043e <a href=\"http:\/\/rurust.github.io\/rust_book_ru\/src\/type-aliases.html\"><em>\u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c \u0442\u0438\u043f\u0430<\/em><\/a> \u0434\u043b\u044f <code>Result<\/code>. \u0412 \u043c\u043e\u0434\u0443\u043b\u0435 <code>std::io<\/code> \u043e\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">type Result&lt;T&gt; = std::result::Result&lt;T, io::Error&gt;<\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c, \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043a\u0440\u0430\u0442\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u0442\u0438\u043f\u0430 <code>Result&lt;T, io::Error&gt;<\/code>, \u0433\u0434\u0435 <code>T<\/code> \u2014 \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0432\u044b\u0448\u0435 \u0434\u043b\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read_length<\/code>. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c\u044b \u0442\u0438\u043f\u043e\u0432 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>payload_len<\/code>:<\/p>\n<pre><code class=\"rust\">return match payload_len {     PAYLOAD_LEN_U64 =&gt; input.read_u64::&lt;BigEndian&gt;().map(|v| v as usize).map_err(From::from),     \u2026 }<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0443 \u0441\u0430\u043c\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>byteorder<\/code>, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435. \u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u043e\u0434\u0443\u043b\u044f \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0435\u0435 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"rust\">use byteorder::{ReadBytesExt, BigEndian};<\/code><\/pre>\n<p>  \u041c\u043e\u0434\u0443\u043b\u044c <code>ReadBytesExt<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0438\u043f\u0430\u0436, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u0435\u043b \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u2014 <code>u16<\/code>, <code>u32<\/code>, \u0438 <code>u64<\/code> \u2014 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0431\u0430\u0439\u0442\u043e\u0432. <code>ReadBytesExt<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u043e\u043d \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0442\u0438\u043f\u0430\u0436 <code>Read<\/code> \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a \u043b\u044e\u0431\u044b\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 <code>Read<\/code>. Rust \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0442\u0430\u043a \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0442\u0438\u043f\u0430\u0436 (\u0438\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443), \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043a \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0438 \u0431\u0443\u0434\u0443\u0449\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c<sup><a href=\"#n2\">[2]<\/a><\/sup>.<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u2014 \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0432\u043e\u0442 \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d <code>ReadBytesExt<\/code> \u0432 \u043a\u043e\u0434\u0435 <a href=\"https:\/\/github.com\/BurntSushi\/byteorder\/blob\/master\/src\/new.rs#L68-L194\">byteorder<\/a>:<\/p>\n<pre><code class=\"rust\">\/\/\/ \u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c `Read`, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u0435\u043b. (\u0414\u043b\u044f `std::io`.) pub trait ReadBytesExt: io::Read {     fn read_u16&lt;T: ByteOrder&gt;(&amp;mut self) -&gt; Result&lt;u16&gt; {         \/\/ \u2026 \u043a\u043e\u0434 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d \u2026     }      \/\/ \u2026 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 read_u32, read_u64, \u0438 \u0434\u0440. \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u044b. \u2026 }  \/\/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u0437 `ReadBytesExt` \u043a\u043e \u0432\u0441\u0435\u043c \u0442\u0438\u043f\u0430\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u043c `Read`: impl&lt;R: io::Read + ?Sized&gt; ReadBytesExt for R {}<\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443, \u0433\u0434\u0435 \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435. \u0417\u0434\u0435\u0441\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f <code>R<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u0439 \u0432 \u0441\u0435\u0431\u044f \u0432\u0441\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0435 \u0442\u0438\u043f\u0430\u0436 <code>Read<\/code>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 <code>R<\/code> (\u0430 \u0442\u043e\u0447\u043d\u0435\u0435 \u2014 \u0434\u043b\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u0438\u043f\u043e\u0432) \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u0438\u043f\u0430\u0436 <code>ReadBytesExt<\/code>. \u0422\u0435\u043b\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 (<code>impl<\/code>) \u043f\u0443\u0441\u0442\u043e\u0435, \u0442\u0430\u043a \u043a\u0430\u043a <code>ReadBytesExt<\/code> \u0443\u0436\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 (\u0447\u0442\u043e \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u0437 &quot;\u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445&quot; \u041e\u041e\u041f-\u044f\u0437\u044b\u043a\u043e\u0432).<\/p>\n<p>  \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432: \u043e\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f\u043e\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u043c \u0442\u0438\u043f\u0430\u0436 <code>ByteOrder<\/code>. \u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 <code>byteorder<\/code> \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0441 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0442 <code>BigEndian<\/code> \u0438 <code>LittleEndian<\/code>.<\/p>\n<p>  \u0412\u043e\u0442 \u043a\u0430\u043a \u043c\u044b \u0432\u0441\u0435 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432 \u043d\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435, \u0447\u0438\u0442\u0430\u044f 8 \u0431\u0430\u0439\u0442\u043e\u0432 \u0432 \u0441\u0435\u0442\u0435\u0432\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 big-endian:<\/p>\n<pre><code class=\"rust\">input.read_u64::&lt;BigEndian&gt;()     .map(|v| v as usize)     .map_err(From::from),<\/code><\/pre>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <code>Result<\/code>, <code>map<\/code> \u0438 <code>map_err<\/code>, \u043c\u044b \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <code>Result&lt;u64, byteorder::Error&gt;<\/code> \u043a \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u043d\u0430\u043c \u0442\u0438\u043f\u0443 <code>Result&lt;usize, io::Error&gt;<\/code>. <code>map<\/code> \u0437\u0434\u0435\u0441\u044c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0430 <code>map_err<\/code>, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0442\u0438\u043f \u043e\u0448\u0438\u0431\u043a\u0438.<\/p>\n<h3>\u041c\u0430\u0441\u043a\u0430<\/h3>\n<p>  \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0440\u0435\u0439\u043c\u0430, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c 4-\u0431\u0430\u0439\u0442\u043d\u0443\u044e \u043c\u0430\u0441\u043a\u0443 \u2014 \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0431\u0438\u0442 <code>mask<\/code> \u0440\u0430\u0432\u0435\u043d 1.<br \/>  \u0412\u043f\u043e\u043b\u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0438\u043f <code>Option<\/code>:<\/p>\n<pre><code class=\"rust\">let mask_key = if header.masked {     let mask = try!(Self::read_mask(input));     Some(mask) } else {     None }<\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043a\u0443\u0441\u043a\u0435 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0443\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e: \u043c\u0430\u043a\u0440\u043e\u0441 <a href=\"http:\/\/rustbyexample.com\/std\/result\/try.html\"><code>try!<\/code><\/a>. \u042d\u0442\u043e\u0442 \u043c\u0430\u043a\u0440\u043e\u0441 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0440\u0443\u0442\u0438\u043d\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u0444\u043e\u0440\u043c\u0435. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"rust\">match Self::read_mask(input) {     Ok(val) =&gt; val,     Err(err) =&gt; {         return Err(From::from(err))     } }<\/code><\/pre>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043d \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u043e\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>read_mask<\/code> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u2014 \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u043e\u0448\u0438\u0431\u043a\u0430, \u0442\u043e \u043e\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 <code>Ok(...)<\/code> \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"rust\">Ok(val) =&gt; val,<\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430, \u0442\u043e \u043e\u043d\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0442\u0438\u043f (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e <code>io::Error<\/code>), \u0438 \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043f\u0440\u0435\u0440\u044b\u0432\u0430\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0435\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435):<\/p>\n<pre><code class=\"rust\">Err(err) =&gt; {     return Err(From::from(err)) }<\/code><\/pre>\n<p>  \u041c\u0430\u043a\u0440\u043e\u0441 <code>try!<\/code> \u2014 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043d\u0435 \u0437\u0430\u0433\u0440\u044f\u0437\u043d\u044f\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0448\u0438\u0431\u043e\u043a. \u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043b\u044e\u0447\u0435, \u0441 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0447\u0435\u0440\u0435\u0437 <code>match<\/code>, \u0431\u044b\u043b\u043e \u0431\u044b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0442\u043e\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438, \u0447\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043d\u0435 \u043e\u0441\u043e\u0431\u043e-\u0442\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u043e.<\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>read_mask<\/code> (\u043a\u043e\u0442\u043e\u0440\u0430\u044f, \u0432\u043f\u0440\u043e\u0447\u0435\u043c, \u043d\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e \u2014 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 4 \u0431\u0430\u0439\u0442\u0430 \u0438\u0437 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445):<\/p>\n<pre><code class=\"rust\">fn read_mask&lt;R: Read&gt;(input: &amp;mut R) -&gt; io::Result&lt;[u8; 4]&gt; {     let mut buf = [0; 4];     try!(input.read(&amp;mut buf));     Ok(buf) }<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438 \u043c\u0430\u0441\u043a\u0443, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043a \u0447\u0442\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u0430:<\/p>\n<pre><code class=\"rust\">fn read_payload&lt;R: Read&gt;(payload_len: usize, input: &amp;mut R) -&gt; io::Result&lt;Vec&lt;u8&gt;&gt; {     let mut payload: Vec&lt;u8&gt; = Vec::with_capacity(payload_len);     payload.extend(iter::repeat(0).take(payload_len));     try!(input.read(&amp;mut payload));     Ok(payload) }<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u043d\u044b \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f. \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0443\u0444\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0440\u0435\u0439\u043c\u0430:<\/p>\n<pre><code class=\"rust\">let mut payload: Vec&lt;u8&gt; = Vec::with_capacity(payload_len);<\/code><\/pre>\n<p>  <code>Vec::with_capacity<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0441 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432\u0435\u043a\u0442\u043e\u0440, \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 <code>[0; &lt;\u0440\u0430\u0437\u043c\u0435\u0440&gt;]<\/code> \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e <em>\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u044b<\/em>, \u0438 \u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d.<\/p>\n<p>  \u0412\u0435\u043a\u0442\u043e\u0440\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043e \u0440\u0430\u0437\u043d\u0438\u0446\u0435 \u043c\u0435\u0436\u0434\u0443 <a href=\"https:\/\/doc.rust-lang.org\/std\/vec\/struct.Vec.html#capacity-and-reallocation\"><em>\u0435\u043c\u043a\u043e\u0441\u0442\u044c\u044e<\/em> \u0438 <em>\u0434\u043b\u0438\u043d\u043e\u0439<\/em><\/a>.<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/70f\/b44\/a2a\/70fb44a2a71f4faa83199133f5202bb8.png\" \/><\/div>\n<p>  \u0415\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u0435, \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0435\u043a\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0435\u0437 <em>\u043f\u0435\u0440\u0435\u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438<\/em>. \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043b\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u0441\u0435 \u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u044d\u0442\u043e \u043d\u0435\u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u043b\u0438 \u043c\u044b \u0443\u0433\u0430\u0434\u0430\u0435\u043c \u0441 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0439 \u0435\u043c\u043a\u043e\u0441\u0442\u044c\u044e, \u0442\u043e \u0432\u0435\u043a\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<p>  \u0421 \u044d\u0442\u0438\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u044c. \u041c\u044b \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 <code>Read<\/code> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">try!(input.read(&amp;mut payload));<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0445 \u0431\u0430\u0439\u0442 \u043c\u044b \u043d\u0438\u0433\u0434\u0435 \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043c\u0435\u0442\u043e\u0434 <code>read<\/code> \u0434\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430. \u0422\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u0434\u043b\u0438\u043d\u043e\u0439, \u0430 \u043d\u0435 \u0435\u043c\u043a\u043e\u0441\u0442\u044c\u044e, \u0438 \u043d\u0430\u0448 \u0432\u0435\u043a\u0442\u043e\u0440 <code>payload<\/code> \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0442\u043e, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, <code>read<\/code> \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c 0 \u0431\u0430\u0439\u0442\u043e\u0432 \u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u0435\u0440\u043d\u0435\u0442.<\/p>\n<p>  \u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u0442\u0440\u044e\u043a, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044f \u0431\u0443\u0444\u0435\u0440 \u043d\u0443\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"rust\">payload.extend(iter::repeat(0).take(payload_len));<\/code><\/pre>\n<p>  \u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c <em>\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442 \u043d\u0443\u043b\u0438, \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>payload_len<\/code>, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c <code>[0, 0, 0, ...payload_len]<\/code>.<\/p>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435 \u2014 \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0435\u043a\u0442\u043e\u0440 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0444\u0440\u0435\u0439\u043c\u0430. \u041d\u0430\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a \u043d\u0435\u043c\u0443 \u043c\u0430\u0441\u043a\u0443, \u0441\u043b\u0435\u0434\u0443\u044f <a href=\"https:\/\/tools.ietf.org\/html\/rfc6455#section-5.3\">\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0432 RFC<\/a>:<\/p>\n<blockquote><p><strong>5.3 \u041c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443<\/strong><\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043d\u0435\u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435,<br \/>   \u0438\u043b\u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c. \u0422\u043e\u0442 \u0436\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f<br \/>   \u0432\u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u043a\u0442\u0435\u0442 i \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 (\u00abtransformed-octet-i\u00bb),<br \/>   \u043a \u043e\u043a\u0442\u0435\u0442\u0443 i \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 (\u00aboriginal-octet-i\u00bb) \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0435\u0435<br \/>   \u0418\u041b\u0418 (XOR) \u0441 \u043e\u043a\u0442\u0435\u0442\u043e\u043c \u0438\u0437 \u043a\u043b\u044e\u0447\u0430 \u043c\u0430\u0441\u043a\u0438 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 i \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c 4 (\u00abmasking-key-octet-j\u00bb):<\/p>\n<pre>`j                   = i MOD 4 transformed-octet-i = original-octet-i XOR masking-key-octet-j`<\/pre>\n<p>  <\/p><\/blockquote>\n<p>  \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u043c\u044b \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0434\u0430\u043b\u0435\u0435, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f <em>\u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0435\u0435 \u0418\u041b\u0418<\/em> \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0431\u0430\u0439\u0442\u0443 \u0431\u0443\u0444\u0435\u0440\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043b\u044e\u0447\u0430 \u043c\u0430\u0441\u043a\u0438:<\/p>\n<pre><code class=\"rust\">fn apply_mask(mask: [u8; 4], bytes: &amp;mut Vec&lt;u8&gt;) {     for (idx, c) in bytes.iter_mut().enumerate() {         *c = *c ^ mask[idx % 4];     } }<\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <a href=\"https:\/\/doc.rust-lang.org\/std\/vec\/struct.Vec.html#method.iter_mut\"><code>iterate_mut()<\/code><\/a> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043f\u0440\u044f\u043c\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u0430 <a href=\"https:\/\/doc.rust-lang.org\/std\/iter\/trait.Iterator.html#method.enumerate\"><code>enumerate()<\/code><\/a> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043d\u0434\u0435\u043a\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430 \u0438\u0437 \u043a\u043b\u044e\u0447\u0430 \u043c\u0430\u0441\u043a\u0438.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/a7f\/797\/05a\/a7f79705ac38413cae784a277d313eb0.png\" align=\"right\"\/><br \/>  \u041f\u043e\u0431\u0438\u0442\u043e\u0432\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f <em>\u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0435\u0433\u043e \u0418\u041b\u0418<\/em>, <strong><code>^<\/code><\/strong>, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 <em>\u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0435 \u0418<\/em>: <code>1<\/code> \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0438\u0441\u0442\u0438\u043d\u043d\u044b (\u0442.\u0435. \u0440\u0430\u0432\u043d\u044b <code>1<\/code>) <code>A<\/code> \u0438\u043b\u0438 <code>B<\/code>, \u043d\u043e \u043d\u0435 \u043e\u0431\u0430 \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<p>  \u0422\u043e\u0433\u0434\u0430 \u043a\u0430\u043a \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0430\u0441\u043a\u0438 \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u043c \u0418 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e\u0439 \u043d\u0430\u043c \u0447\u0430\u0441\u0442\u0438 \u0431\u0438\u0442\u043e\u0432, \u043c\u0430\u0441\u043a\u0438 \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u043c \u0418\u041b\u0418 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f <em>\u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/em> \u0431\u0438\u0442\u043e\u0432 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<\/p>\n<p>  \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u044b\u0445 \u0431\u0438\u0442\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c, \u0438 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u044b\u0435 \u043c\u0430\u0441\u043a\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u0435\u0440\u044b \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432\u043f\u0440\u043e\u0447\u0435\u043c, \u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u044d\u0442\u043e \u043a\u0430\u043a &quot;\u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c&quot; \u2014 \u044d\u0442\u043e \u043d\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435). \u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u0432 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 WebSocket \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u043c\u0430\u0441\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0430\u0442\u0430\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e &quot;\u043e\u0442\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430&quot;.<sup><a href=\"#n3\">[3]<\/a><\/sup>.<\/p>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0440\u0435\u0439\u043c\u043e\u0432, \u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0441\u043b\u0438\u0442\u044c \u0438\u0445 \u0432\u043e\u0435\u0434\u0438\u043d\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>WebSocketClient<\/code>:<\/p>\n<pre><code class=\"rust\">impl WebSocketClient {     \/\/ \u2026 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u2026     fn read(&amp;mut self) {         match self.state {             ClientState::AwaitingHandshake(_) =&gt; { \u2026 },              \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043b\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f `Connected`:             ClientState::Connected =&gt; {                 let frame = WebSocketFrame::read(&amp;mut self.socket);                 match frame {                     Ok(frame) =&gt; println!(&quot;{:?}&quot;, frame),                     Err(e) =&gt; println!(&quot;\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u0444\u0440\u0435\u0439\u043c\u0430: {}&quot;, e)                 }             }         }     } }<\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435 \u2014 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>cargo run<\/code> \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0444\u0440\u0435\u0439\u043c\u044b \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u043a\u043d\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0430:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/b25\/287\/915\/b25287915bad4d3d8933e82d16969a2d.png\" \/><\/div>\n<p>  <\/p>\n<h1>19 \u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u043e\u0432<\/h1>\n<p>  \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0444\u0440\u0435\u0439\u043c\u044b \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u2014 \u044d\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0444\u0440\u0435\u0439\u043c \u0438\u0437 \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 (\u0441\u0442\u0440\u043e\u043a\u0438, \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0430\u0439\u0442\u043e\u0432, \u0438 \u0442.\u043f.) \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u043e\u043a\u0435\u0442.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432:<\/p>\n<pre><code class=\"rust\">impl WebSocketFrameHeader {     fn new_header(len: usize, opcode: OpCode) -&gt; WebSocketFrameHeader {         WebSocketFrameHeader {             fin: true,             rsv1: false, rsv2: false, rsv3: false,             masked: false,             payload_length: Self::determine_len(len),             opcode: opcode         }     }      fn determine_len(len: usize) -&gt; u8 {         if len &lt; (PAYLOAD_LEN_U16 as usize) {             len as u8         } else if len &lt; (u16::MAX as usize) {             PAYLOAD_LEN_U16         } else {             PAYLOAD_LEN_U64         }     } }<\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>new_header<\/code> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 2 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u2014 \u0434\u043b\u0438\u043d\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0431\u0430\u0439\u0442\u0430\u0445 \u0438 \u043e\u043f\u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0442\u0438\u043f \u0444\u0440\u0435\u0439\u043c\u0430. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>determine_len<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f &quot;\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e&quot; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0440\u0435\u0439\u043c\u0430 (\u0442.\u0435., \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c\u044b\u0435 \u0440\u0430\u043d\u0435\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b 126 \u0438 127).<\/p>\n<p>  \u041a\u0430\u043a \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0444\u0440\u0435\u0439\u043c\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u2014 \u0434\u043b\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u043e\u043a \u0432\u043e \u0444\u0440\u0435\u0439\u043c\u044b:<\/p>\n<pre><code class=\"rust\">impl&lt;'a&gt; From&lt;&amp;'a str&gt; for WebSocketFrame {     fn from(payload: &amp;str) -&gt; WebSocketFrame {         WebSocketFrame {             header: WebSocketFrameHeader::new_header(payload.len(), OpCode::TextFrame),             payload: Vec::from(payload),             mask: None         }     } }<\/code><\/pre>\n<p>  \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f\u0430\u0436 <code>From<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432. \u041f\u043e\u043b\u043d\u0430\u044f \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u0442\u0438\u043f\u0430\u0436\u0430 \u2014 <code>From&lt;&amp;'a str&gt;<\/code>. \u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u2014 \u043f\u043e\u0447\u0435\u043c\u0443 <code>&amp;'a str<\/code>, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e <code>&amp;str<\/code>, \u0438 \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 <code>&lt;'a&gt;<\/code> \u0432 <code>impl&lt;'a&gt;<\/code>?<\/p>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0432\u0430\u0436\u043d\u044b\u0445 \u0438\u0434\u0435\u0439 \u044f\u0437\u044b\u043a\u0430 Rust: <em>\u0432\u0440\u0435\u043c\u0435\u043d\u0430\u043c\u0438 \u0436\u0438\u0437\u043d\u0438<\/em>. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u044b \u0438\u0445 \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438, \u0438 \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b\u0438 \u043d\u0435\u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0436\u0438\u0437\u043d\u0438 \u2014 \u043d\u0435\u043e\u0442\u044a\u0435\u043c\u043b\u0435\u043c\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0439, \u043e\u0441\u043d\u043e\u0432\u044b \u044f\u0437\u044b\u043a\u0430. \u0412\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u0430\u043a \u0434\u043e\u043b\u0433\u043e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438, \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0430\u044f\u0441\u044f \u043a \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0439 \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043d\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f <em>\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0436\u0438\u0437\u043d\u0438<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043d\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043e\u0431\u0440\u0430\u0437\u043e\u043c. <strong><code>'a<\/code><\/strong> \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043a\u043e\u0434\u0430 \u0432\u044b\u0448\u0435 \u0438 \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0436\u0438\u0437\u043d\u0438.<\/p>\n<p>  \u041e\u0431\u044b\u0447\u043d\u043e, \u043a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0442\u0438\u043f\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0443\u043c\u0435\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0439 \u2014 \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0432\u044b\u0448\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u043e\u0434\u0430 \u043c\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0436\u0438\u0437\u043d\u0438 \u043d\u0438\u0433\u0434\u0435, \u043a\u0440\u043e\u043c\u0435 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u0442\u0438\u043f\u0430\u0436\u0430 <code>Form<\/code>.<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e, \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u044d\u0442\u043e \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u043d\u0430\u0442\u044c \u043e\u0431 \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u043e \u043e\u0431 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0435 <a href=\"http:\/\/rurust.github.io\/rust_book_ru\/src\/lifetimes.html\">\u043a\u043d\u0438\u0433\u0438 &quot;\u042f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Rust&quot;<\/a>.<\/p>\n<p>  \u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u043a\u0435 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0440\u0435\u0439\u043c\u043e\u0432. \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0440\u0435\u0439\u043c\u0430, \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0440\u0435\u0439\u043c \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u043e \u0441\u0435\u0442\u0438. \u041f\u0435\u0440\u0432\u043e\u0439 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e <code>parse_header<\/code> \u2014 <code>serialize_header<\/code> \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0430:<\/p>\n<pre><code class=\"rust\">impl WebSocketFrame {     \/\/ \u2026 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u2026     fn serialize_header(hdr: &amp;WebSocketFrameHeader) -&gt; u16 {         let b1 = ((hdr.fin as u8) &lt;&lt; 7)                   | ((hdr.rsv1 as u8) &lt;&lt; 6)                   | ((hdr.rsv2 as u8) &lt;&lt; 5)                   | ((hdr.rsv3 as u8) &lt;&lt; 4)                   | ((hdr.opcode as u8) &amp; 0x0F);          let b2 = ((hdr.masked as u8) &lt;&lt; 7)             | ((hdr.payload_length as u8) &amp; 0x7F);          ((b1 as u16) &lt;&lt; 8) | (b2 as u16)     } }<\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u044b \u0443\u0436\u0435 \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0438\u0441\u044c, <code>&lt;&lt;<\/code> \u2014 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0433\u043e \u0441\u0434\u0432\u0438\u0433\u0430. \u041e\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043a\u0430\u043a \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0434\u0432\u0438\u0433\u0430 <code>&gt;&gt;<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0438\u0442\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u044e\u0442\u0441\u044f \u043d\u0435 \u0432\u043f\u0440\u0430\u0432\u043e, \u0430 <em>\u0432\u043b\u0435\u0432\u043e<\/em>:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/7af\/150\/7a1\/7af1507a148044398d7b7aba5b5a8a79.png\" \/><\/div>\n<p>  \u0412\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0447\u0435\u0440\u0442\u0430, <strong><code>|<\/code><\/strong> \u2014 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0435 <code>\u0418\u041b\u0418<\/code> \u2014 \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f <em>\u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/em> \u0431\u0438\u0442\u043e\u0432. \u0422\u043e \u0435\u0441\u0442\u044c, \u0442\u0430\u043c, \u0433\u0434\u0435 \u0443 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0442\u043e\u0432 <strong><code>1<\/code><\/strong>, \u0430 \u0443 \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 <strong><code>0<\/code><\/strong>, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 <code>1<\/code>:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/0cd\/c79\/b77\/0cdc79b778be4892a0b577460b7ec95f.png\" \/><\/div>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0434\u0432\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0445 \u0431\u0430\u0439\u0442\u0430 \u0432 \u0441\u043b\u043e\u0432\u043e (<code>u16<\/code>) \u2014 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0432\u0438\u0433\u0430\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u0431\u0430\u0439\u0442 \u0432\u043b\u0435\u0432\u043e \u043d\u0430 8 \u0431\u0438\u0442, \u0437\u0430\u0442\u0435\u043c \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043e \u0432\u0442\u043e\u0440\u044b\u043c \u0431\u0430\u0439\u0442\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0418\u041b\u0418:<\/p>\n<pre><code class=\"rust\">((b1 as u16) &lt;&lt; 8) | (b2 as u16)<\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0432 \u043e\u0434\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>write<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0444\u0440\u0435\u0439\u043c \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a (\u0441\u0442\u0440\u043e\u043a\u0443, \u0444\u0430\u0439\u043b, \u0438 \u0442.\u0434.):<\/p>\n<pre><code class=\"rust\">pub fn write&lt;W: Write&gt;(&amp;self, output: &amp;mut W) -&gt; io::Result&lt;()&gt; {     let hdr = Self::serialize_header(&amp;self.header);     try!(output.write_u16::&lt;BigEndian&gt;(hdr));      match self.header.payload_length {         PAYLOAD_LEN_U16 =&gt; try!(output.write_u16::&lt;BigEndian&gt;(self.payload.len() as u16)),         PAYLOAD_LEN_U64 =&gt; try!(output.write_u64::&lt;BigEndian&gt;(self.payload.len() as u64)),         _ =&gt; {}     }      try!(output.write(&amp;self.payload));     Ok(()) }<\/code><\/pre>\n<p>  \u041c\u044b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c 2 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0445 \u0431\u0430\u0439\u0442\u0430, \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u0430 (\u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 <em>125<\/em> \u0431\u0430\u0439\u0442), \u0430 \u0437\u0430\u0442\u0435\u043c \u0438 \u0441\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e RFC \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u043f\u0440\u044f\u043c\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043a \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435 \u2014 \u043c\u044b \u043f\u043e\u0447\u0442\u0438 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 \u0441 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u043e\u0439 \u0444\u0440\u0435\u0439\u043c\u043e\u0432! \u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>WebSocketClient.write<\/code> \u2014 \u0442\u043e\u0442 \u043a\u043e\u0434, \u0447\u0442\u043e \u0442\u0430\u043c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043c\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c \u0432 \u043d\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>write_handshake<\/code>, \u0430 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>write<\/code> \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"rust\">impl WebSocketClient {     fn write(&amp;mut self) {         match self.state {             ClientState::AwaitingHandshake(_) =&gt; {                 self.write_handshake();             },             _ =&gt; {}         }     }      fn write_handshake(&amp;mut self) {         let headers = self.headers.borrow();         let response_key = gen_key(&amp;headers.get(&quot;Sec-WebSocket-Key&quot;).unwrap());         \u2026     } }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f <code>ClientState::Connected<\/code> \u2014 \u0432 \u043d\u0435\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0444\u0440\u0435\u0439\u043c\u044b, \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c. \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u2014 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043d\u043e \u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0440\u0435\u0439\u043c\u043e\u0432, \u0438 \u043c\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0446\u0438\u043a\u043b\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 <code>WebSocketClient<\/code>:<\/p>\n<pre><code class=\"rust\">struct WebSocketClient {     socket: TcpStream,     \u2026     \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432:     outgoing: Vec&lt;WebSocketFrame&gt; }<\/code><\/pre>\n<p>  \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u044f \u043f\u043e\u0434\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440:<\/p>\n<pre><code class=\"rust\">impl WebSocketClient {     \/\/ \u2026 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u2026     fn new(socket: TcpStream) -&gt; WebSocketClient {         let headers = Rc::new(RefCell::new(HashMap::new()));          WebSocketClient {             \u2026             outgoing: Vec::new()         }     } }<\/code><\/pre>\n<p>  \u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>write<\/code>:<\/p>\n<pre><code class=\"rust\">match self.state {     ClientState::HandshakeResponse =&gt; \u2026,     ClientState::Connected =&gt; {         println!(&quot;\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u043d\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443: {}&quot;, self.outgoing.len());          for frame in self.outgoing.iter() {             if let Err(e) = frame.write(&amp;mut self.socket) {                 println!(&quot;\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445: {}&quot;, e);             }         }          self.outgoing.clear();          self.interest.remove(EventSet::writable());         self.interest.insert(EventSet::readable());     },     _ =&gt; {} }<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0444\u0440\u0435\u0439\u043c \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0435\u0435, \u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443 \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0444\u0440\u0435\u0439\u043c. \u0412\u043d\u0435\u0441\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 <code>read<\/code>:<\/p>\n<pre><code class=\"rust\">fn read(&amp;mut self) {     match self.state {         ClientState::AwaitingHandshake(_) =&gt; { \u2026 },         ClientState::Connected =&gt; {             let frame = WebSocketFrame::read(&amp;mut self.socket);             match frame {                 Ok(frame) =&gt; {                     println!(&quot;{:?}&quot;, frame),                      \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u0432\u0435\u0442\u043d\u044b\u0439 \u0444\u0440\u0435\u0439\u043c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c:                     let reply_frame = WebSocketFrame::from(&quot;\u041f\u0440\u0438\u0432\u0435\u0442!&quot;);                     self.outgoing.push(reply_frame);                      \/\/ \u041f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 \u0440\u0435\u0436\u0438\u043c \u0437\u0430\u043f\u0438\u0441\u0438:                     self.interest.remove(EventSet::readable());                     self.interest.insert(EventSet::writable());                 },                 Err(e) =&gt; println!(&quot;\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u0444\u0440\u0435\u0439\u043c\u0430: {}&quot;, e)             }         }     } }<\/code><\/pre>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>cargo run<\/code>, \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u044c, \u0438 \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430:<\/p>\n<pre><code class=\"javascript\">ws = new WebSocket('ws:\/\/127.0.0.1:10000');  ws.onmessage = function (event) {     console.log('\u041e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430: ', event.data); };<\/code><\/pre>\n<p>  \u0418 \u0441\u043a\u0430\u0436\u0438\u0442\u0435 &quot;\u043f\u0440\u0438\u0432\u0435\u0442&quot; \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443:<\/p>\n<pre><code class=\"javascript\">ws.send('\u041f\u0440\u0438\u0432\u0435\u0442!');<\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/414\/6d0\/fde\/4146d0fdeaaa4377adf47f3f486c6497.png\" \/><\/div>\n<p>  <\/p>\n<h2>20 \u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0435 \u0444\u0440\u0435\u0439\u043c\u044b<\/h2>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u0437\u0430\u0434\u0430\u0447\u0430\u043c, \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442\u044c \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0443\u0442\u0438\u043d\u043e\u0439. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u044d\u0442\u0430 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430. \u041d\u0430 \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043b\u0438\u0448\u044c \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u042d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0442 \u043d\u0430\u0441 \u043e\u0441\u043e\u0431\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0445 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0431\u0435\u0436\u0438\u043c\u0441\u044f \u043f\u043e-\u0431\u044b\u0441\u0442\u0440\u043e\u043c\u0443.<\/p>\n<p>  \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0432 \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0440\u0435\u0439\u043c\u0430 \u0438\u0437 <code>WebSocketClient.read<\/code> \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"rust\">pub fn read(&amp;mut self) {     match self.state {         ClientState::AwaitingHandshake(_) =&gt; self.read_handshake();         ClientState::Connected =&gt; self.read_frame(),         _ =&gt; {}     } }  fn read_frame(&amp;mut self) {     let frame = WebSocketFrame::read(&amp;mut self.socket);     \u2026 }<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u043e\u0432, ping \u0438 pong, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e \u0442\u0438\u043f\u0443 \u0444\u0440\u0435\u0439\u043c\u0430 \u0432 <code>read_frame<\/code>:<\/p>\n<pre><code class=\"rust\">fn read_frame(&amp;mut self) {     let frame = WebSocketFrame::read(&amp;mut self.socket);      match frame {         Ok(frame) =&gt; {             match frame.get_opcode() {                 OpCode::TextFrame =&gt; {                     println!(&quot;{:?}&quot;, frame),                     let reply_frame = WebSocketFrame::from(&quot;\u041f\u0440\u0438\u0432\u0435\u0442!&quot;);                     self.outgoing.push(reply_frame);                 },                 _ =&gt; {}             }             self.interest.remove(EventSet::readable());             self.interest.insert(EventSet::writable());         }         Err(e) =&gt; println!(&quot;\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u0444\u0440\u0435\u0439\u043c\u0430: {}&quot;, e)     } }<\/code><\/pre>\n<p>  \u0421\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0434\u0435\u0442 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0440\u0430\u043d\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>get_opcode<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0438\u043f \u0444\u0440\u0435\u0439\u043c\u0430.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u0442\u0438\u043f\u0430 <code>Ping<\/code>:<\/p>\n<pre><code class=\"rust\">match frame.get_opcode() {     OpCode::TextFrame =&gt; \u2026,     OpCode::Ping =&gt; {         self.outgoing.push(WebSocketFrame::pong(&amp;frame));     }     _ =&gt; {} }<\/code><\/pre>\n<p>  \u0410 \u0442\u0430\u043a\u0436\u0435 \u043a\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0440\u0435\u0439\u043c\u0430 <code>Pong<\/code> \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e \u043f\u0438\u043d\u0433\u0430 (\u043a\u0430\u043a \u0442\u043e\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b):<\/p>\n<pre><code class=\"rust\">impl WebSocketFrame {     \/\/ \u2026 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u2026     pub fn pong(ping_frame: &amp;WebSocketFrame) -&gt; WebSocketFrame {         let payload = ping_frame.payload.clone();         WebSocketFrame {             header: WebSocketFrameHeader::new_header(payload.len(), OpCode::Pong),             payload: payload,             mask: None         }     } }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0444\u0440\u0435\u0439\u043c\u0430\u043c \u0442\u0438\u043f\u0430 <code>Close<\/code>:<\/p>\n<pre><code class=\"rust\">match frame.get_opcode() {     OpCode::TextFrame =&gt; \u2026,     OpCode::Ping =&gt; \u2026,      \/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0440\u0435\u0439\u043c\u043e\u0432 \u0441 \u043e\u043f\u043a\u043e\u0434\u043e\u043c ConnectionClose:     OpCode::ConnectionClose =&gt; {         self.outgoing.push(WebSocketFrame::close_from(&amp;frame));     },     _ =&gt; {} }<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0442 \u043d\u0430\u0441 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043e\u043f\u043a\u043e\u0434\u043e\u043c <code>ConnectionClose<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u043c \u0444\u0440\u0435\u0439\u043c\u043e\u043c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0442\u0432\u0435\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0447\u0442\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0444\u0440\u0435\u0439\u043c \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0442\u0435\u043b\u043e, \u0438 \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0442\u0435\u043b\u043e \u0438\u043c\u0435\u0435\u0442\u0441\u044f, \u0435\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043e\u0437\u0430\u0433\u043b\u0430\u0432\u043b\u0435\u043d\u043e \u0434\u0432\u0443\u0445\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u043c \u043a\u043e\u0434\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<blockquote><p>\u041f\u0435\u0440\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0431\u0430\u0439\u0442\u0430\u043c\u0438 \u0442\u0435\u043b\u0430 \u0414\u041e\u041b\u0416\u041d\u041e \u0431\u044b\u0442\u044c 2-\u0431\u0430\u0439\u0442\u043d\u043e\u0435 \u0431\u0435\u0437\u0437\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 (\u0432 \u0441\u0435\u0442\u0435\u0432\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0431\u0430\u0439\u0442\u043e\u0432),<br \/>  \u0441\u043e\u043e\u0431\u0449\u0430\u044e\u0449\u0435\u0435 \u043a\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \/code\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0432 \u0420\u0430\u0437\u0434\u0435\u043b\u0435 7.4.   <\/p><\/blockquote>\n<p>  \u0420\u0430\u0437\u0434\u0435\u043b 7.4 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u0434\u043e\u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043e\u0434\u043d\u0430\u043a\u043e \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043c \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u2014 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0438 \u0434\u0435\u0442\u0430\u043b\u0438 \u043f\u043e\u0437\u0436\u0435. \u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0444\u0440\u0435\u0439\u043c\u043e\u0432, <code>close_from<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u0442\u0432\u0435\u0442:<\/p>\n<pre><code class=\"rust\">impl WebSocketFrame {     \u2026     pub fn close_from(recv_frame: &amp;WebSocketFrame) -&gt; WebSocketFrame {         let body = if recv_frame.payload.len() &gt; 0 {             let status_code = &amp;recv_frame.payload[0..2];             let mut body = Vec::with_capacity(2);             body.write(status_code);             body         } else {             Vec::new()         };         WebSocketFrame {             header: WebSocketFrameHeader::new_header(body.len(), OpCode::ConnectionClose),             payload: body,             mask: None         }     } }<\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435. \u041c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0444\u0440\u0435\u0439\u043c\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432 <code>WebSocketClient<\/code>, \u043f\u043e\u043c\u0435\u043d\u044f\u0432 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443 \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>hup<\/code> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">impl WebSocketClient {     fn write(&amp;mut self) {         match self.state {             ClientState::HandshakeResponse =&gt; \u2026,             ClientState::Connected =&gt; {                 \/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u043b\u0430\u0433 \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0430 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438:                 let mut close_connection = false;                  for frame in self.outgoing.iter() {                     if let Err(e) = frame.write(&amp;mut self.socket) {                         println!(&quot;\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0444\u0440\u0435\u0439\u043c\u0430: {}&quot;, e);                     }                      \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0444\u0440\u0435\u0439\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435:                     if (frame.is_close()) {                         close_connection = true;                     }                 }                  self.outgoing.clear();                  self.interest.remove(EventSet::writable());                 \/\/ \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 - \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f `hup`, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 -                 \/\/ \u0442\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0435 \u0444\u0440\u0435\u0439\u043c\u044b:                 if (close_connection) {                     self.interest.insert(EventSet::hup());                 } else {                     self.interest.insert(EventSet::readable());                 }             },             _ =&gt; {}         }     } }<\/code><\/pre>\n<p>  \u0415\u0449\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u043f\u043a\u043e\u0434\u0430 \u0444\u0440\u0435\u0439\u043c\u0430 \u2014 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043b\u0438 \u043e\u043d:<\/p>\n<pre><code class=\"rust\">impl WebSocketFrame {     \/\/ \u2026 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u2026     pub fn is_close(&amp;self) -&gt; bool {         self.header.opcode == OpCode::ConnectionClose     } }<\/code><\/pre>\n<p>  \u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u2014 \u0432 <code>WebSocketServer<\/code> \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"rust\">impl Handler for WebSocketServer {     \/\/ \u2026 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u2026     fn ready(&amp;mut self, event_loop: &amp;mut EventLoop&lt;WebSocketServer&gt;, token: Token, events: EventSet) {         if events.is_readable() {             \u2026         }          if events.is_writable() {             \u2026         }          \/\/ \u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f:         if events.is_hup() {             let client = self.clients.remove(&amp;token).unwrap();              client.socket.shutdown(Shutdown::Both);             event_loop.deregister(&amp;client.socket);         }     } }<\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u043f\u043e\u043a\u0430 \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435.<\/p>\n<h2>21 \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u043b\u0438\u0441\u044c \u0432\u043f\u0435\u0440\u0435\u0434. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0436\u043d\u044b\u0445 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0439 \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430\u043c\u0438, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043b\u0438 \u0432\u043e\u043f\u043b\u043e\u0449\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e WebSocket \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043b\u0438 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 WebSocket \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435.<\/p>\n<p>  \u0412 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 \u043e\u0441\u0432\u0435\u0442\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0442\u0435\u0440\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439 \u0438\u0437 \u0432\u0438\u0434\u0430, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0430 \u043c\u0435\u043d\u044f <a href=\"https:\/\/twitter.com\/nbaksalyar\">\u0432 \u0422\u0432\u0438\u0442\u0442\u0435\u0440\u0435<\/a> \u0438\u043b\u0438 \u043d\u0430 <a href=\"http:\/\/feeds.feedburner.com\/NikitaBaksalyar\">RSS-\u043b\u0435\u043d\u0442\u0443 \u0431\u043b\u043e\u0433\u0430<\/a>.<\/p>\n<p>  \u041a\u043e\u0434, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435, \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 <a href=\"https:\/\/github.com\/nbaksalyar\/rust-chat\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 GitHub<\/a>. \u0421 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u043b\u044e\u0431\u044b\u0435 \u043f\u0443\u043b\u043b-\u0440\u0435\u043a\u0432\u0435\u0441\u0442\u044b, \u0435\u0441\u043b\u0438 \u0432\u044b \u0440\u0435\u0448\u0438\u0442\u0435 \u0447\u0442\u043e-\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u043b\u0438 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c.<\/p>\n<p>  C\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<h2>\u0417\u0430\u043c\u0435\u0442\u043a\u0438<\/h2>\n<p>  [1] \u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435, \u043a\u0441\u0442\u0430\u0442\u0438, \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437 \u043a\u043d\u0438\u0433\u0438 \u0414\u0436\u043e\u043d\u0430\u0442\u0430\u043d\u0430 \u0421\u0432\u0438\u0444\u0442\u0430 &quot;<a href=\"http:\/\/www.lib.ru\/INOOLD\/SWIFT\/gulliver.txt\">\u041f\u0443\u0442\u0435\u0448\u0435\u0441\u0442\u0432\u0438\u044f \u0413\u0443\u043b\u043b\u0438\u0432\u0435\u0440\u0430<\/a>&quot;, \u043e\u0442\u0441\u044b\u043b\u0430\u044f \u043a \u0441\u043f\u043e\u0440\u0443 \u043c\u0435\u0436\u0434\u0443 &quot;\u043e\u0441\u0442\u0440\u043e\u043a\u043e\u043d\u0435\u0447\u043d\u0438\u043a\u0430\u043c\u0438&quot; (little-endian) \u0438 &quot;\u0442\u0443\u043f\u043e\u043a\u043e\u043d\u0435\u0447\u043d\u0438\u043a\u0430\u043c\u0438&quot; (big-endian).<\/p>\n<p>  [1] \u0412\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0443\u0434\u0438\u0432\u0438\u0442\u044c \u00ab\u043d\u0435\u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439\u00bb \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432, \u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0439 \u0432 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 x86. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0437\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e: \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u043a\u0430\u043a \u0432\u044b \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u0435 \u0447\u0438\u0441\u043b\u0430, \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u044f \u0438\u0445 \u0432 \u0441\u0442\u043e\u043b\u0431\u0438\u043a \u2014 \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430. \u041f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 little-endian, \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0438\u043b\u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u0431\u0430\u0439\u0442\u0430.<\/p>\n<p>  [2] \u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0438\u0441\u0430\u043b\u0438 \u043d\u0430 C#, \u0432\u044b \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0442\u0430\u043c \u0441 \u043f\u043e\u0445\u043e\u0436\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u041c\u0435\u0442\u043e\u0434_\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\">\u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f<\/a>. \u0410 \u0444\u0430\u043d\u0430\u0442\u044b \u0425\u0430\u0441\u043a\u0435\u043b\u044f \u0438 \u0421\u043a\u0430\u043b\u044b \u0443\u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0438\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0438\u0447\u0442\u043e \u0438\u043d\u043e\u0435, \u043a\u0430\u043a \u0442\u0430\u0439\u043f\u043a\u043b\u0430\u0441\u0441\u044b.<\/p>\n<p>  [3] \u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0441 \u044d\u0442\u043e\u0439 \u0442\u0435\u0445\u043d\u0438\u043a\u043e\u0439 \u0430\u0442\u0430\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u043c <a href=\"http:\/\/security.stackexchange.com\/questions\/36930\/how-does-websocket-frame-masking-protect-against-cache-poisoning\">\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438<\/a> <em>(\u0430\u043d\u0433\u043b.)<\/em>. \u0410\u0442\u0430\u043a\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 &quot;<a href=\"http:\/\/w2spconf.com\/2011\/papers\/websocket.pdf\">Talking to Yourself for Fun and Profit<\/a>&quot;.<\/p>\n<hr\/>\n<p>  \u041a\u0430\u043a \u0438 \u0432\u0441\u0435\u0433\u0434\u0430, \u0441\u043f\u0430\u0441\u0438\u0431\u043e  <a href=\"https:\/\/habrahabr.ru\/users\/podust\/\" class=\"user_link\">podust<\/a> \u0437\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u0432 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0438 \u0432\u044b\u0447\u0438\u0442\u043a\u0435.<\/p>\n<div class=\"clear\"><\/div>\n<p> \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:\/\/habrahabr.ru\/post\/278635\/\"> https:\/\/habrahabr.ru\/post\/278635\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<br \/>\n<h2>\u0427\u0430\u0441\u0442\u044c 2: \u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439<\/h2>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u0447\u0430\u0442\u0430 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0432\u043e \u0432\u0441\u0435\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445. \u0426\u0435\u043b\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430 <strong>Rust<\/strong> \u043d\u0430 \u0444\u043e\u043d\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 API, \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c.<\/p>\n<p>  \u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u044b\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <a href=\"http:\/\/habrahabr.ru\/post\/268609\/\">\u043f\u0435\u0440\u0432\u043e\u0439<\/a>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u043b\u0438 \u0432\u044b \u0435\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043b\u0438 (\u0438\u043b\u0438 \u0437\u0430\u0431\u044b\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442), \u0442\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0439. \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 WebSocket.  <\/p>\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-275547","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/275547","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=275547"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/275547\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}