{"id":190548,"date":"2013-08-19T12:59:03","date_gmt":"2013-08-19T08:59:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=190548"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=190548","title":{"rendered":"<span class=\"post_title\">\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439\/\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0435\u0440<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e TCP \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0437\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430\u043c \u0438 \u0441\u0436\u0430\u0442\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0421 \u0447\u0435\u0433\u043e \u0432\u0441\u0435 \u043d\u0430\u0447\u0438\u043d\u0430\u043b\u043e\u0441\u044c.<br \/>  \u0412\u0441\u0435 \u043d\u0430\u0447\u0430\u043b\u043e\u0441\u044c \u0441 \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0441 \u0447\u0435\u0433\u043e \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0445 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c-\u0431\u044b WCF \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0440\u0435\u0448\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043d\u043e, \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043e\u043d \u043d\u0435 \u043b\u0438\u0448\u0435\u043d \u043c\u0438\u043d\u0443\u0441\u043e\u0432 \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0430 \u0435\u0433\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0438\u043b\u044c\u043d\u043e \u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438 \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u043d\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432.  <\/p>\n<pre><code class=\"cs\">public interface ICommon {     string[] GetAvailableUsers();     void ChangePrivileges(string Login, string password); }  public interface IDog {     bool TryFindObject(out object obj);     int Bark(int nTimes); }  public interface ICat {     void CutTheText(ref string Text); } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u043f\u0438\u0441\u0430\u043b\u0438 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440 \u0434\u043b\u044f \u0442\u0440\u0435\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, Common \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0438\u043f\u043e\u0432 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432. Dog \u0438 Cat \u0437\u0434\u0435\u0441\u044c, \u044d\u0442\u043e \u043d\u0430\u0448\u0438 \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043c \u0441\u0430\u043c\u0438\u043c.<\/p>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c\u0438.  <\/p>\n<pre><code class=\"cs\">[Serializable] public class Message {     public Message(string Command, object[] Parameters)     {         this.Command = Command;         if (Parameters != null) this.prms = Parameters;     }      public bool IsSync;     public bool IsEmpty = true;     public readonly string Command;     public object ReturnValue;     public object[] prms;     public Exception Exception; } <\/code><\/pre>\n<p>  <\/p>\n<h5>\u041a\u043b\u0438\u0435\u043d\u0442<\/h5>\n<p>  \u041a\u043b\u0438\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u044e\u0449\u0443\u044e \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u043a\u0441\u0438:  <\/p>\n<pre><code class=\"cs\">private class Proxy&lt;T&gt; : RealProxy where T : class {     UniservClient client;      public Proxy(UniservClient client): base(typeof(T))     {         this.client = client;     }      public override IMessage Invoke(IMessage msg)     {         IMethodCallMessage call = (IMethodCallMessage)msg;         object[] parameters = call.Args;         int OutArgsCount = call.MethodBase.GetParameters().Where(x =&gt; x.IsOut).Count();          Message result = client.Execute(call.MethodName, parameters);         parameters = parameters.Select((x, index) =&gt; result.prms[index] ?? x).ToArray();         return new ReturnMessage(result.ReturnValue, parameters, OutArgsCount, call.LogicalCallContext, call);     } } <\/code><\/pre>\n<p>  \u0418 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043c\u0435\u0442\u043e\u0434\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432:  <\/p>\n<pre><code class=\"cs\">public ICommon Common { get; private set; } public IDog Dog { get; private set; } public ICat Cat { get; private set; } <\/code><\/pre>\n<p>  \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u043e\u043a\u0441\u0438 \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430:  <\/p>\n<pre><code class=\"cs\">CommonProxy = new Proxy&lt;ICommon&gt;(this); DogProxy = new Proxy&lt;IDog&gt;(this); CatProxy = new Proxy&lt;ICat&gt;(this);  Common = (ICommon)CommonProxy.GetTransparentProxy(); Dog = (IDog)DogProxy.GetTransparentProxy(); Cat = (ICat)CatProxy.GetTransparentProxy(); <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:  <\/p>\n<pre><code class=\"cs\">private void Listener() {     while (true)     {         try         {             if (ListenerToken.IsCancellationRequested) return;              if (!IsConnected) _Connect();              while (true)             {                 if (ListenerToken.IsCancellationRequested) return;                  Message msg = ReceiveData&lt;Message&gt;();                 if (msg.Command == &quot;OnPing&quot;)                 {                     \/\/ \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u043c \u043f\u0438\u043d\u0433                     SendData(msg);                     if (Events.OnPing != null) Events.OnPing.BeginInvoke(null, null);                     continue;                 }                  if (msg.IsSync)                 {  \/\/ \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b                     SyncResult(msg);                 }                 else                 {                     \/\/ \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0441\u043e\u0431\u044b\u0442\u0438\u044f                     try                     {                         \/\/ \u0438\u0449\u0435\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 Action                         var pi = typeof(IEvents).GetProperty(msg.Command, BindingFlags.Instance | BindingFlags.Public);                         if (pi == null) throw new Exception(string.Concat(&quot;\u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e \\&quot;&quot;, msg.Command, &quot;\\&quot; \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e&quot;));                         var delegateRef = pi.GetValue(this, null) as Delegate;                          \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0435                         if (delegateRef != null) ThreadPool.QueueUserWorkItem(state =&gt; delegateRef.DynamicInvoke(msg.prms));                     }                     catch (Exception ex)                     {                         throw new Exception(string.Concat(&quot;\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \\&quot;&quot;, msg.Command, &quot;\\&quot;&quot;), ex);                     }                 }             }         }         catch (TaskCanceledException)         {             return;         }         catch (Exception ex)         {             if (Events.OnError != null) Events.OnError.BeginInvoke(ex, null, null);         }         finally         {             _Dicsonnect();         }          Thread.Sleep(2000);     } } <\/code><\/pre>\n<p>  \u0417\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u043c\u0435\u0442\u043e\u0434\u044b:  <\/p>\n<pre><code class=\"cs\">private Message Execute(string MethodName, object[] parameters) {     lock (syncLock)     {         _syncResult = new Message(MethodName, parameters);         _syncResult.IsSync = true;          _OnResponce.Reset();          SendData(_syncResult);         _OnResponce.Wait();  \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430          if (_syncResult.IsEmpty)         {\/\/ \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u0435\u043b \u0434\u0438\u0441\u043a\u043e\u043d\u0435\u043a\u0442, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d             throw new Exception(string.Concat(&quot;\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \\&quot;&quot;, MethodName, &quot;\\&quot;&quot;));         }          if (_syncResult.Exception != null) throw _syncResult.Exception;  \/\/ \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c         return _syncResult;     } }  private void SyncResult(Message msg) {  \/\/ \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b      _syncResult = msg;     _syncResult.IsEmpty = false;      _OnResponce.Set();  \/\/ \u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0442\u043e\u043a } <\/code><\/pre>\n<p>  \u0412 \u043e\u0431\u0449\u0438\u0445 \u0447\u0435\u0440\u0442\u0430\u0445 \u043a\u043b\u0438\u0435\u043d\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<br \/>  \u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u043a \u043f\u0440\u043e\u043a\u0441\u0438-\u0441\u0435\u0440\u0432\u0435\u0440\u0443. \u041f\u0440\u043e\u043a\u0441\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438\u043c\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0435\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0442\u043e\u043a \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u0420\u0430\u0437\u0431\u043e\u0440 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. <\/p>\n<h5>\u0421\u0435\u0440\u0432\u0435\u0440<\/h5>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u044c \u0442\u0435\u0445 \u0438\u043b\u0438 \u0438\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:  <\/p>\n<pre><code class=\"cs\">public class Cat_Ring0 : Ring2, ICat {     public Cat_Ring0(User u) : base(u)     {         up.UserType = UserType.Cat;     }      public void CutTheText(ref string Text)     {         Text = Text.Remove(Text.Length - 1);     } }  public class Dog_Ring0 : Dog_Ring1, IDog {     public Dog_Ring0(User u) : base(u)     {         up.UserType = UserType.Dog;     }      public int Bark(int nTimes)     {         var ConnectedDogs = ConnectedUsers.ToArray().Where(x =&gt; x.UserType == UserType.Dog).Select(x =&gt; x.nStream);         ConnectedDogs.AsParallel().ForAll(nStream =&gt;         {             SendMessage(nStream, new Message(&quot;OnBark&quot;, new object[] { nTimes}));         });          return ConnectedDogs.Count();     } }  public class Dog_Ring1 : Ring2 {     public Dog_Ring1(User u): base(u)     {         up.UserType = UserType.Dog;     }      public bool TryFindObject(out object obj)     {         obj = &quot;TheBall&quot;;         return true;     } }  public class Ring2 : Ring, ICommon {     public Ring2(User u) : base(u) { }      public string[] GetAvailableUsers()     {         return new string[] { &quot;Dog0&quot;, &quot;Dog1&quot;, &quot;Tom&quot; };     }      public void ChangePrivileges(string Animal, string password)     {         switch (Animal)         {             case &quot;Dog0&quot;:                 if (password != &quot;groovy!&quot;) throw new Exception(&quot;\u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c&quot;);                 up.ClassInstance = new Dog_Ring0(up);                 break;             case &quot;Dog1&quot;:                 if (password != &quot;_password&quot;) throw new Exception(&quot;\u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c&quot;);                 up.ClassInstance = new Dog_Ring1(up);                 break;             case &quot;Tom&quot;:                 if (password != &quot;TheCat&quot;) throw new Exception(&quot;\u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c&quot;);                 up.ClassInstance = new Cat_Ring0(up);                 break;             default:                 throw new Exception(&quot;\u0422\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442&quot;);         }     } }  public abstract class Ring {     public readonly User up;      public Ring(User up)     {         this.up = up;     } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u201c\u043a\u043e\u043b\u044c\u0446\u043e\u201d \u0447\u0442\u043e \u0431\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 \u0438\u043c\u0435\u043b \u043a \u043d\u0435\u0439 \u0434\u043e\u0441\u0442\u0443\u043f. Ring0 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u044d\u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043a \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u043c\u0441\u044f \u0432 \u043d\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430\u043c, \u043d\u043e \u0438 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430\u043c \u0432\u043e \u0432\u0441\u0435\u0445 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430\u0445. \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u0432 Ring2, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0432\u0441\u0435\u043c. \u0414\u0430\u043b\u0435\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <i>ChangePrivileges()<\/i> \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442, \u043f\u0440\u043e\u0439\u0434\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e, \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u00ab\u043a\u043e\u043b\u044c\u0446\u0430\u00bb \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0430.<\/p>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443:  <\/p>\n<pre><code class=\"cs\">private void ProcessMessage(Message msg, User u) {     string MethodName = msg.Command;     if (MethodName == &quot;OnPing&quot;) return;      \/\/ \u0438\u0449\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f     MethodInfo method = u.RingType.GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);      try     {         if (method == null)         {             throw new Exception(string.Concat(&quot;\u041c\u0435\u0442\u043e\u0434 \\&quot;&quot;, MethodName, &quot;\\&quot; \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d&quot;));         }          try         {             \/\/ \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430             msg.ReturnValue = method.Invoke(u.ClassInstance, msg.prms);         }         catch (Exception ex)         {             throw ex.InnerException;         }          \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c ref \u0438 out \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b         msg.prms = method.GetParameters().Select(x =&gt; x.ParameterType.IsByRef ? msg.prms[x.Position] : null).ToArray();     }     catch (Exception ex)     {         msg.Exception = ex;     }     finally     {         \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430         SendMessage(u.nStream, msg);     } } <\/code><\/pre>\n<p>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e <i>ClassInstance<\/i> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u201c\u043a\u043e\u043b\u044c\u0446\u0430\u201d \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043f\u043e \u0435\u0435 \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u043b\u043e\u0433\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/4b0\/0d1\/d37\/4b00d1d374dc5d8084a6eb6dae366868.png\" alt=\"image\"\/><\/p>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0438 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u0435 WCF.<br \/>  \u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c <a href=\"https:\/\/github.com\/milesprower\/Uniserv\">\u0442\u0443\u0442<\/a> \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/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=\"http:\/\/habrahabr.ru\/post\/190548\/\"> http:\/\/habrahabr.ru\/post\/190548\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e TCP \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0437\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430\u043c \u0438 \u0441\u0436\u0430\u0442\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.  <\/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-190548","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/190548","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=190548"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/190548\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=190548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=190548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=190548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}