コード例 #1
0
        /// <summary>Inlet content</summary>
        /// <param name="content">Content</param>
        internal void Inlet(Content content)
        {
            // Last receipt time
            LastReceiptTime = (uint)Stopwatch.ElapsedMilliseconds;

            // Unreliable content
            if ((content.Protocol & Content.IDENTIFIABLE) == 0)
            {
                // Channel unreliable
                Unreliable.Inlet(content);
            }

            // Reliable content
            else
            {
                // Measure latency
                if (content.Protocol == Content.RESPONSE)
                {
                    MeasureLatency(content.Timestamp);
                }

                // Channel reliable
                Reliable.Inlet(content);
            }
        }
コード例 #2
0
        public void ShouldProcessOnMultipleThreads()
        {
            // Arrange
            var breaker = new CircuitBreaker();
            var source  = Enumerable.Range(0, 100).ToArray();

            // Act
            var usedThreadIds = new List <int>();

            Reliable.ParallelForEach(
                breaker,
                source,
                elements => usedThreadIds.Add(Thread.CurrentThread.ManagedThreadId),
                new RetryOptions
            {
                AllowedRetries = 5,
                RetryInterval  = TimeSpan.Zero
            },
                new ParallelOptions
            {
                MaxDegreeOfParallelism = 100
            });

            // Assert
            if (usedThreadIds.Distinct().Count() == 1)
            {
                Assert.Inconclusive("This test relies on multiple threads, however only one was ever spawned.");
            }
        }
コード例 #3
0
        /// <summary>
        /// Sends a datagram with the specified AckResolver info
        /// The datagram must be sent reliably, but the AckResolver
        /// is already in the buffer, so there's no need to add it again.
        /// </summary>
        /// <param name="resolver">The AckResolver with the datagram info</param>
        private void SendDatagram(AckResolver resolver)
        {
            byte[] msgBytes;

            // Append the ack index count to the message, to communicate to the
            // client what reliable datagram count it represents
            msgBytes = Encoding.ASCII.GetBytes(Reliable.CreateString(resolver.AckIndex, resolver.Message));
            _client.Send(msgBytes, msgBytes.Length);
        }
コード例 #4
0
 public void Broadcast(byte[] buf, int offset, int size, bool reliable = true)
 {
     if (reliable)
     {
         Reliable.Broadcast(buf, offset, size);
     }
     else
     {
         Unreliable.Broadcast(buf, offset, size);
     }
 }
コード例 #5
0
        /// <summary>Outlet content</summary>
        /// <param name="content">Content</param>
        internal void Outlet(Content content)
        {
            if (content == null)
            {
                return;
            }

            if (content.Protocol != Content.PONG)
            {
                content.Timestamp = (uint)Stopwatch.ElapsedMilliseconds;
            }

            // Channel unreliable
            Unreliable.Outlet(content);

            // Channel reliable
            Reliable.Outlet(content);
        }
コード例 #6
0
        /// <summary>
        /// Generically sends a datagram to the specified end points, either reliable or unreliable.
        /// </summary>
        /// <param name="message">The message to send</param>
        /// <param name="isReliable">Whether the handler should ensure
        /// the message reaches its intended recipient</param>
        /// <param name="endPoints">The client(s) to send the datagram to</param>
        public void SendDatagram(string message, bool isReliable)
        {
            byte[] msgBytes;

            if (isReliable)
            {
                if (_resolverBuffer.Count >= 100)
                {
                    return;
                }

                // Append the ack index count to the message, to communicate to the
                // client what reliable datagram count it represents
                msgBytes = Encoding.ASCII.GetBytes(Reliable.CreateString(_ackCurrentIndex, message));

                // Add a new AckResolver to the resolver buffer, which will
                // resend the datagram if the timeout is reached
                // before receiving an ACK
                AddAckResolver(
                    new AckResolver()
                {
                    AckIndex   = _ackCurrentIndex,
                    TicksStart = DateTime.Now.Ticks,
                    Message    = message
                }
                    );

                _ackCurrentIndex += 1;
            }
            else
            {
                msgBytes = Encoding.ASCII.GetBytes(Unreliable.CreateString(message));
            }

            try
            {
                _client.Send(msgBytes, msgBytes.Length);
            }
            catch (SocketException)
            {
                _displayDisconnection.Display();
            }
        }
コード例 #7
0
    {           /// <summary>
                /// Handles all reliable packets received from clients
                /// </summary>
        static public void Handle_Reliable(Reliable pkt, Client client)
        {       //Get the relevant stream
            Client.StreamState stream = client._streams[pkt.streamID];

            //Is the reliable number what we expected?
            if (pkt.rNumber > stream.C2S_Reliable)
            {                   //Report it!
                Log.write(TLog.Inane, "OOS Reliable Packet Stream[{0}]. {1} vs {2}", pkt.streamID, pkt.rNumber, stream.C2S_Reliable);
                client.reportOutOfSync(pkt, pkt.rNumber, pkt.streamID);
                return;
            }
            //A previously received reliable?
            else if (pkt.rNumber < stream.C2S_Reliable)
            {                   //Re-send the echo
                ReliableEcho resent = new ReliableEcho(pkt.streamID);
                resent.rNumber = (ushort)(stream.C2S_Reliable - 1);
                client.send(resent);
                return;
            }

            //Expect the next!
            stream.C2S_Reliable++;

            //Handle the message we're supposed to receive
            client._handler.handlePacket(pkt.packet, client);

            //If we have other packets in seqence waiting in store, use them too
            PacketBase unhandled;
            ushort     reliableNext = stream.C2S_Reliable;

            while (stream.oosReliable.TryGetValue(reliableNext, out unhandled))
            {                   //Handle it and go to next packet
                client._handler.handlePacket(unhandled, client);
                stream.oosReliable.Remove(reliableNext++);
            }

            //Prepare an echo for all received packets
            ReliableEcho echo = new ReliableEcho(pkt.streamID);

            echo.rNumber = (ushort)(stream.C2S_Reliable - 1);
            client.send(echo);
        }
コード例 #8
0
        public void ShouldProcessAllElementsInOrder()
        {
            // Arrange
            var breaker = new CircuitBreaker();
            var source  = Enumerable.Range(0, 10).ToArray();

            // Act
            var processedElements = new List <int>();

            Reliable.ForEach(
                breaker,
                source,
                processedElements.Add,
                new RetryOptions
            {
                AllowedRetries = 5,
                RetryInterval  = TimeSpan.Zero
            });

            // Assert
            CollectionAssert.AreEqual(source, processedElements);
        }
コード例 #9
0
    {           /// <summary>
                /// Creates a new system protocol packet.
                /// </summary>
        public PacketBase createSystemPacket(ushort typeID, byte[] buffer, int offset, int size)
        {       //Ready our packet base
            PacketBase packet = null;

            offset++;
            size--;

            //What are we dealing with?
            switch (typeID)
            {
            case CS_Initial.TypeID:
                packet = new CS_Initial(typeID, buffer, offset, size);
                break;

            case BoxPacket.TypeID:
                packet = new BoxPacket(typeID, buffer, offset, size);
                break;

            case Disconnect.TypeID:
                packet = new Disconnect(typeID, buffer, offset, size);
                break;

            case PingPacket.TypeID:
                packet = new PingPacket(typeID, buffer, offset, size);
                break;

            case CS_State.TypeID:
                packet = new CS_State(typeID, buffer, offset, size);
                break;

            case Reliable.TypeID:
                packet = new Reliable(typeID, buffer, offset, size, 0);
                break;

            case Reliable.TypeID + 1:
                packet = new Reliable(typeID, buffer, offset, size, 1);
                break;

            case Reliable.TypeID + 2:
                packet = new Reliable(typeID, buffer, offset, size, 2);
                break;

            case Reliable.TypeID + 3:
                packet = new Reliable(typeID, buffer, offset, size, 3);
                break;

            case OutOfSync.TypeID:
                packet = new OutOfSync(typeID, buffer, offset, size, 0);
                break;

            case OutOfSync.TypeID + 1:
                packet = new OutOfSync(typeID, buffer, offset, size, 1);
                break;

            case OutOfSync.TypeID + 2:
                packet = new OutOfSync(typeID, buffer, offset, size, 2);
                break;

            case OutOfSync.TypeID + 3:
                packet = new OutOfSync(typeID, buffer, offset, size, 3);
                break;

            case ReliableEcho.TypeID:
                packet = new ReliableEcho(typeID, buffer, offset, size, 0);
                break;

            case ReliableEcho.TypeID + 1:
                packet = new ReliableEcho(typeID, buffer, offset, size, 1);
                break;

            case ReliableEcho.TypeID + 2:
                packet = new ReliableEcho(typeID, buffer, offset, size, 2);
                break;

            case ReliableEcho.TypeID + 3:
                packet = new ReliableEcho(typeID, buffer, offset, size, 3);
                break;

            case ReliableBox.TypeID:
                packet = new ReliableBox(typeID, buffer, offset, size, 0);
                break;

            case ReliableBox.TypeID + 1:
                packet = new ReliableBox(typeID, buffer, offset, size, 1);
                break;

            case ReliableBox.TypeID + 2:
                packet = new ReliableBox(typeID, buffer, offset, size, 2);
                break;

            case ReliableBox.TypeID + 3:
                packet = new ReliableBox(typeID, buffer, offset, size, 3);
                break;

            case DataPacketRcv.TypeID:
                packet = new DataPacketRcv(typeID, buffer, offset, size, 0);
                break;

            case DataPacketRcv.TypeID + 1:
                packet = new DataPacketRcv(typeID, buffer, offset, size, 1);
                break;

            case DataPacketRcv.TypeID + 2:
                packet = new DataPacketRcv(typeID, buffer, offset, size, 2);
                break;

            case DataPacketRcv.TypeID + 3:
                packet = new DataPacketRcv(typeID, buffer, offset, size, 3);
                break;

            default:
                //An undefined packet.
                packet = new PacketDummy(typeID, buffer, offset, size);
                break;
            }

            return(packet);
        }
コード例 #10
0
        } = 0.30;                                           //30%

        public override SearchResult Search(BitBoard bb, int depth)
        {
            return(IsRevolution() ? Adventure.Search(bb, depth) : Reliable.Search(bb, depth));
        }
コード例 #11
0
 public override string toFullString()
 {
     return(Denomination + "; cc " + CreditCard + "; phone " + PhoneNumber + "; email " + EMail + "; reliable = " + Reliable.ToString() + "; " + Address.toFullString() + "; fiscalcode = " + ID);
 }
コード例 #12
0
 public Sub(Reliable <T> parent, IReliableObserver <T> observer)
 {
     _parent   = parent;
     _observer = observer;
 }
コード例 #13
0
 public Observer(Reliable <T> parent) => _parent = parent;
コード例 #14
0
        /// <summary>Timeout</summary>
        internal void Timeout()
        {
            // Time
            var time = (uint)Stopwatch.ElapsedMilliseconds;

            // Channel unreliable
            Unreliable.Timeout(time);

            // Channel reliable
            Reliable.Timeout(time);

            // Disconnecting
            if (
                (
                    Reliable.Muted() ||
                    Unreliable.Muted()
                ) || (
                    CheckStatus(Status.Close | Status.Wait) &&
                    Unreliable.Released() &&
                    Reliable.Released()
                    )
                )
            {
                // Change status
                ChangeStatus(Status.Close | Status.Done);

                if (Reliable.Muted() || Reliable.Muted())
                {
                    Network.Raise(EventType.Unlinked, Reason.Muted, this);
                }

                else
                {
                    Network.Raise(EventType.Unlinked, Reason.Unlinked, this);
                }
                return;
            }

            // Settings
            var downtime     = Network.Settings.Downtime;
            var pingInterval = Network.Settings.PingInterval;

            // Downtime
            if ((time - LastReceiptTime) > downtime)
            {
                // Timeout
                if (
                    CheckStatus(Status.Connect | Status.Wait) ||
                    CheckStatus(Status.Connect | Status.Done)
                    )
                {
                    Network.Raise(EventType.Unlinked, Reason.Timeout, this);
                }

                // Host not accessible
                else if (CheckStatus(Status.Connect))
                {
                    Network.Raise(EventType.Failed, Failure.NotAccessible, this);
                }

                // Change status
                ChangeStatus(Status.Close | Status.Done);
            }

            // Ping
            if (
                CheckStatus(Status.Connect | Status.Done) &&
                (time - LastPingTime) > pingInterval
                )
            {
                // Last ping interval
                LastPingTime = time;

                // Ping
                var content = Content.Unreliable();
                content.Protocol = Content.PING;

                // Set the current timestamp and pack
                content.Timestamp = time;
                content.Packing();

                // Send immediately
                var size = (uint)Network.Outlet(content, this);

                // Statistics
                Statistics.PacketSent(size, false);
                Network.Statistics.PacketSent(size, false);
            }
        }