public PacketOperation <T> Enqueue(T request)
        {
            lock (this.QueueSyncRoot)
            {
                this.EnsureCanEnqueue(request);

                var operation     = new PacketOperation <T>(request);
                var currentThread = Thread.CurrentThread;

                if (currentThread == this.ThreadMain)
                {
                    try
                    {
                        this.ProcessPacket(this.BaseStream, operation);
                    }
                    catch (Exception e)
                    {
                        this.OnExcepted(e);
                        throw;
                    }
                }
                else
                {
                    this.Queue.Add(operation);
                    this.QueueResetEvent.SetQuietly();
                }

                return(operation);
            }
        }
 protected virtual void ProcessPacket(Stream stream, PacketOperation <T> operation)
 {
     try
     {
         lock (this.ProcessSyncRoot)
         {
             if (this.Mode == ProcessMode.Exchange)
             {
                 var response = this.OnExchangePacket(stream, operation.Request);
                 operation.Set(response, null);
             }
             else if (this.Mode == ProcessMode.Individual)
             {
                 this.OnIndividualRequest(stream, operation.Request);
                 operation.Set(null, null);
             }
             else
             {
                 operation.Set(null, null);
             }
         }
     }
     catch (Exception e)
     {
         operation.Set(null, e);
         throw;
     }
 }
Example #3
0
 protected virtual void OnResponsed(PacketOperation <T> e)
 {
     this.Responsed?.Invoke(this, e);
 }