Example #1
0
        public IEnumerable <SimEvent> Run()
        {
            var incomingGet = IncomingFrames.Get();
            var sendGet     = _sendRequests.Get();

            while (true)
            {
                yield return(incomingGet.Or(sendGet));

                if (incomingGet.Succeeded)
                {
                    var p = incomingGet.Value;
                    if (p.SessionId == _currSessionId)
                    {
                        Logger.InfoFormat("Received a new frame: {0}", p);
                        _arrivedFrames.Add(p);
                        Logger.InfoFormat("Reconstructing packets from {0} frames", _arrivedFrames.Count);
                        var packets = ReconstructPackets();
                        if (packets.Count != 0)
                        {
                            G.Clients[Id].Receive(packets);
                            Logger.InfoFormat("Reconstructed and sent {0} packets", packets.Count);
                        }
                    }
                    incomingGet = IncomingFrames.Get();
                }
                if (sendGet.Succeeded)
                {
                    var p = sendGet.Value;
                    var w = G.Latency + p.Len / G.Bandwidth;
                    Logger.InfoFormat("Sending {0}, holding for {1}", p, Ts(w));
                    yield return(Env.Timeout(w));

                    G.Switch.Receive(p);
                    sendGet = _sendRequests.Get();
                }
            }
        }
Example #2
0
        public IEnumerable <SimEvent> Run()
        {
            var incomingGet = IncomingFrames.Get();
            var sendGet     = _sendRequests.Get();

            while (true)
            {
                yield return(incomingGet.Or(sendGet));

                if (incomingGet.Succeeded)
                {
                    var packet    = incomingGet.Value;
                    var reqPacket = new RequestPacket(packet.Src, packet.SessionId);
                    Logger.InfoFormat("Sending packet {0} to server", reqPacket);
                    G.Servers[Id].Receive(reqPacket);
                    incomingGet = IncomingFrames.Get();
                }
                if (!sendGet.Succeeded)
                {
                    continue;
                }

                var sr        = sendGet.Value;
                var cp        = sr.Packet;
                var sessionId = sr.SessionId;

                // At this point we need to break the code packet into some UDP packets and send them
                var nUdp           = (int)Math.Ceiling(cp.Len / (double)G.MTU);
                var lastPacketSize = cp.Len - nUdp * G.MTU;
                if (lastPacketSize != 0)
                {
                    nUdp++;
                }
                Logger.InfoFormat("Breaking packet into {0} UDP packets", nUdp);

                UdpPacket p;
                double    w;
                for (var i = 0; i < nUdp - 1; ++i)
                {
                    p = new UdpPacket(sessionId, Id, cp.Owner, G.MTU, PacketType.Answer, nUdp);
                    w = G.Latency + p.Len / G.Bandwidth;
                    Logger.InfoFormat("Sending {0}, holding for {1}", p, Ts(w));
                    yield return(Env.Timeout(w));

                    G.Switch.Receive(p);
                }
                // Must send the remaining part of the packet
                if (lastPacketSize == 0)
                {
                    p = new UdpPacket(sessionId, Id, cp.Owner, G.MTU, PacketType.Answer, nUdp);
                }
                else
                {
                    p = new UdpPacket(sessionId, Id, cp.Owner, lastPacketSize, PacketType.Answer, nUdp);
                }
                w = G.Latency + p.Len / G.Bandwidth;
                Logger.InfoFormat("Sending {0}, holding for {1}", p, Ts(w));
                yield return(Env.Timeout(w));

                G.Switch.Receive(p);
                sendGet = _sendRequests.Get();
            }
        }