public void OnNeighborAuthenticated(IPeerNeighbor neighbor)
 {
     if (this.state == State.Created)
     {
         throw Fx.AssertAndThrow("Connector not expected to be in Created state");
     }
     if (!PeerNeighborStateHelper.IsAuthenticatedOrClosed(neighbor.State))
     {
         throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Authenticated or Closed, actual state: {0}", new object[] { neighbor.State }));
     }
     if (!neighbor.TrySetState(PeerNeighborState.Connecting))
     {
         if (neighbor.State < PeerNeighborState.Faulted)
         {
             throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Faulted or Closed, actual state: {0}", new object[] { neighbor.State }));
         }
     }
     else if (this.AddTimer(neighbor) && neighbor.IsInitiator)
     {
         if (this.neighborManager.ConnectedNeighborCount < this.config.MaxNeighbors)
         {
             this.SendConnect(neighbor);
         }
         else
         {
             this.neighborManager.CloseNeighbor(neighbor, PeerCloseReason.NodeBusy, PeerCloseInitiator.LocalNode);
         }
     }
 }
Пример #2
0
        public override void ProcessLinkUtility(IPeerNeighbor neighbor, UtilityInfo utilityInfo)
        {
            if (!PeerNeighborStateHelper.IsConnected(neighbor.State))
            {
                neighbor.Abort(PeerCloseReason.InvalidNeighbor, PeerCloseInitiator.LocalNode);
                return;
            }

            try
            {
                UtilityExtension.ProcessLinkUtility(neighbor, utilityInfo);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                if (null != CloseNeighborIfKnownException(neighborManager, e, neighbor))
                {
                    throw;
                }
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
            }
        }
Пример #3
0
 public override void ProcessLinkUtility(IPeerNeighbor neighbor, UtilityInfo utilityInfo)
 {
     if (!PeerNeighborStateHelper.IsConnected(neighbor.State))
     {
         neighbor.Abort(PeerCloseReason.InvalidNeighbor, PeerCloseInitiator.LocalNode);
     }
     else
     {
         try
         {
             UtilityExtension.ProcessLinkUtility(neighbor, utilityInfo);
         }
         catch (Exception exception)
         {
             if (Fx.IsFatal(exception))
             {
                 throw;
             }
             if (PeerFlooderBase <Message, UtilityInfo> .CloseNeighborIfKnownException(base.neighborManager, exception, neighbor) != null)
             {
                 throw;
             }
             DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
         }
     }
 }
Пример #4
0
        void SendUtilityMessage(uint useful, uint total)
        {
            IPeerNeighbor host = owner;

            if (host == null || !PeerNeighborStateHelper.IsConnected(host.State) || total == 0)
            {
                return;
            }
            UtilityInfo  umessage = new UtilityInfo(useful, total);
            IAsyncResult result   = null;
            Message      message  = MessageConverter.ToMessage(umessage, MessageVersion.Soap12WSAddressing10);
            bool         fatal    = false;

            try
            {
                result = host.BeginSend(message, Fx.ThunkCallback(new AsyncCallback(UtilityMessageSent)), new AsyncUtilityState(message, umessage));
                if (result.CompletedSynchronously)
                {
                    host.EndSend(result);
                    EventHandler handler = UtilityInfoSent;
                    if (handler != null)
                    {
                        handler(this, EventArgs.Empty);
                    }
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    fatal = true;
                    throw;
                }
                if (null != HandleSendException(host, e, umessage))
                {
                    throw;
                }
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
            }
            finally
            {
                if (!fatal && (result == null || result.CompletedSynchronously))
                {
                    message.Close();
                }
            }
        }
 private void UtilityMessageSent(IAsyncResult result)
 {
     if ((result != null) && (result.AsyncState != null))
     {
         IPeerNeighbor owner = this.owner;
         if (((owner != null) && PeerNeighborStateHelper.IsConnected(owner.State)) && !result.CompletedSynchronously)
         {
             AsyncUtilityState asyncState = (AsyncUtilityState)result.AsyncState;
             Message           message    = asyncState.message;
             UtilityInfo       umessage   = asyncState.info;
             bool flag = false;
             if (umessage == null)
             {
                 throw Fx.AssertAndThrow("expecting a UtilityInfo message in the AsyncState!");
             }
             try
             {
                 owner.EndSend(result);
             }
             catch (Exception exception)
             {
                 if (Fx.IsFatal(exception))
                 {
                     flag = true;
                     throw;
                 }
                 if (this.HandleSendException(owner, exception, umessage) != null)
                 {
                     throw;
                 }
                 DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
             }
             finally
             {
                 if (!flag)
                 {
                     message.Close();
                 }
             }
             EventHandler utilityInfoSent = this.UtilityInfoSent;
             if (utilityInfoSent != null)
             {
                 utilityInfoSent(this, EventArgs.Empty);
             }
         }
     }
 }
Пример #6
0
        // Process neighbor authenticated notification
        public void OnNeighborAuthenticated(IPeerNeighbor neighbor)
        {
            if (!(this.state != State.Created))
            {
                throw Fx.AssertAndThrow("Connector not expected to be in Created state");
            }

            if (!(PeerNeighborStateHelper.IsAuthenticatedOrClosed(neighbor.State)))
            {
                throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Authenticated or Closed, actual state: {0}", neighbor.State));
            }

            // setting the state fails if neighbor is already closed or closing
            // If so, we have nothing to do.
            if (!neighbor.TrySetState(PeerNeighborState.Connecting))
            {
                if (!(neighbor.State >= PeerNeighborState.Faulted))
                {
                    throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Faulted or Closed, actual state: {0}", neighbor.State));
                }
                return;
            }

            // Add a timer to timer table to transition the neighbor to connected state
            // within finite duration. The neighbor is closed if the timer fires and the
            // neighbor has not reached connected state.
            // The timer is not added if neighbor or connector are closed
            if (AddTimer(neighbor))
            {
                // Need to send connect message if the neighbor is the initiator
                if (neighbor.IsInitiator)
                {
                    if (this.neighborManager.ConnectedNeighborCount < this.config.MaxNeighbors)
                    {
                        SendConnect(neighbor);
                    }
                    else
                    {
                        // We have max connected neighbors already. So close this one.
                        this.neighborManager.CloseNeighbor(neighbor, PeerCloseReason.NodeBusy,
                                                           PeerCloseInitiator.LocalNode);
                    }
                }
            }
        }
        private void SendUtilityMessage(uint useful, uint total)
        {
            IPeerNeighbor owner = this.owner;

            if (((owner != null) && PeerNeighborStateHelper.IsConnected(owner.State)) && (total != 0))
            {
                UtilityInfo  typedMessage = new UtilityInfo(useful, total);
                IAsyncResult result       = null;
                Message      message      = this.MessageConverter.ToMessage(typedMessage, MessageVersion.Soap12WSAddressing10);
                bool         flag         = false;
                try
                {
                    result = owner.BeginSend(message, Fx.ThunkCallback(new AsyncCallback(this.UtilityMessageSent)), new AsyncUtilityState(message, typedMessage));
                    if (result.CompletedSynchronously)
                    {
                        owner.EndSend(result);
                        EventHandler utilityInfoSent = this.UtilityInfoSent;
                        if (utilityInfoSent != null)
                        {
                            utilityInfoSent(this, EventArgs.Empty);
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        flag = true;
                        throw;
                    }
                    if (this.HandleSendException(owner, exception, typedMessage) != null)
                    {
                        throw;
                    }
                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                }
                finally
                {
                    if (!flag && ((result == null) || result.CompletedSynchronously))
                    {
                        message.Close();
                    }
                }
            }
        }
        protected virtual IAsyncResult FloodMessageToNeighbors(MessageBuffer messageBuffer, TimeSpan timeout, AsyncCallback callback, object state, int index, MessageHeader hopHeader, IPeerNeighbor except, EventHandler OnMessageSentCallback)
        {
            Interlocked.Increment(ref this.messageSequence);
            FloodAsyncResult fresult = new FloodAsyncResult(this.neighborManager, timeout, callback, state);

            fresult.OnMessageSent += OnMessageSentCallback;
            foreach (IPeerNeighbor neighbor in this.Neighbors)
            {
                if (!neighbor.Equals(except) && PeerNeighborStateHelper.IsConnected(neighbor.State))
                {
                    Message message = messageBuffer.CreateMessage();
                    if (index != -1)
                    {
                        message.Headers.ReplaceAt(index, hopHeader);
                    }
                    if (PeerNeighborStateHelper.IsConnected(neighbor.State))
                    {
                        this.BeginSendHelper(neighbor, timeout, message, fresult);
                    }
                }
            }
            fresult.MarkEnd(true);
            return(fresult);
        }
Пример #9
0
        protected virtual IAsyncResult FloodMessageToNeighbors(MessageBuffer messageBuffer,
                                                               TimeSpan timeout, AsyncCallback callback, object state,
                                                               int index, MessageHeader hopHeader, IPeerNeighbor except,
                                                               EventHandler OnMessageSentCallback)
        {
            long             temp    = Interlocked.Increment(ref messageSequence);
            FloodAsyncResult fresult = new FloodAsyncResult(this.neighborManager, timeout, callback, state);

            fresult.OnMessageSent += OnMessageSentCallback;
            List <IPeerNeighbor> neighbors = this.Neighbors;

            foreach (IPeerNeighbor neighbor in neighbors)
            {
                if (neighbor.Equals(except))
                {
                    continue;
                }
                // Don't do anything if the neighbor is not connected
                if (PeerNeighborStateHelper.IsConnected(neighbor.State))
                {
                    Message fmessage = messageBuffer.CreateMessage();
                    if (index != -1)
                    {
                        fmessage.Headers.ReplaceAt(index, hopHeader);
                    }

                    // Don't do anything if the neighbor is not connected
                    if (PeerNeighborStateHelper.IsConnected(neighbor.State))
                    {
                        BeginSendHelper(neighbor, timeout, fmessage, fresult);
                    }
                }
            }
            fresult.MarkEnd(true);
            return(fresult);
        }
Пример #10
0
        void UtilityMessageSent(IAsyncResult result)
        {
            if (result == null || result.AsyncState == null)
            {
                return;
            }
            IPeerNeighbor host = this.owner;

            if (host == null || !PeerNeighborStateHelper.IsConnected(host.State))
            {
                return;
            }
            if (result.CompletedSynchronously)
            {
                return;
            }

            AsyncUtilityState state = (AsyncUtilityState)result.AsyncState;

            Fx.Assert(state != null, "IAsyncResult.AsyncState does not contain AsyncUtilityState");
            Message     message  = state.message;
            UtilityInfo umessage = state.info;
            bool        fatal    = false;

            if (!(umessage != null))
            {
                throw Fx.AssertAndThrow("expecting a UtilityInfo message in the AsyncState!");
            }

            try
            {
                host.EndSend(result);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    fatal = true;
                    throw;
                }
                if (null != HandleSendException(host, e, umessage))
                {
                    throw;
                }
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
            }
            finally
            {
                if (!fatal)
                {
                    Fx.Assert(!result.CompletedSynchronously, "result.CompletedSynchronously");
                    message.Close();
                }
            }
            EventHandler handler = UtilityInfoSent;

            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }