Ejemplo n.º 1
0
        public ReliableChannel(IChannelOption channelOption, ILogger logger, NetStatistic statistic, UdpChannel channel, bool ordered)
            : base(channelOption, logger, statistic)
        {
            _udpChannel = channel;
            _sendQueue  = new Queue <NetPacket>(WindowSize);

            _pendingPackets = new ReliableSendInfo[WindowSize];
            for (int i = 0; i < _pendingPackets.Length; i++)
            {
                _pendingPackets[i] = new ReliableSendInfo();
            }

            _ordered = ordered;

            if (_ordered)
            {
                _receivedPackets = new NetPacket[WindowSize];
                _deliveryMethod  = DeliveryMethod.ReliableOrdered;
            }
            else
            {
                _earlyReceived  = new bool[WindowSize];
                _deliveryMethod = DeliveryMethod.ReliableUnordered;
            }

            int bytesCount = (WindowSize - 1) / BitsInByte + 2;

            _ackPacket = new NetPacket(PacketProperty.Ack, bytesCount);
            _ackPacket.DeliveryMethod = _deliveryMethod;
        }
Ejemplo n.º 2
0
        public bool TrySend(long currentTime, int disconnectTimeoutMs, UdpChannel udpChannel, NetStatistic statistic)
        {
            if (_packet == null)
            {
                return(true);
            }

            if (_isSent)
            {
                if (currentTime >= _createTime + disconnectTimeoutMs * TimeSpan.TicksPerMillisecond)
                {
                    return(false);
                }

                long resendDelay    = udpChannel.ResendDelay * TimeSpan.TicksPerMillisecond;
                long packetHoldTime = currentTime - _lastSentTime;
                if (packetHoldTime < resendDelay)
                {
                    return(true);
                }

                Interlocked.Increment(ref statistic.UdpResentCount);
            }

            _lastSentTime = currentTime;
            _isSent       = true;

            udpChannel?.SendTo(_packet.RawData, 0, _packet.Size, UdpChannel.SendMode.Buffered);

            return(true);
        }
Ejemplo n.º 3
0
        public ServerSession(SessionCreateInfo createInfo)
        {
            SessionId   = createInfo.SessionId;
            _tcpChannel = createInfo.TcpChannel;
            if (_tcpChannel != null)
            {
                _tcpChannel.PacketReceived = OnReceiveFromChannel;
            }

            _udpChannel = createInfo.UdpChannel;
            if (_udpChannel != null)
            {
                _udpChannel.PacketReceived = OnReceiveFromChannel;
            }

            _request = new SessionRequest(this, createInfo.Statistic);
        }
Ejemplo n.º 4
0
        public SequencedChannel(IChannelOption channelOption, ILogger logger, NetStatistic statistic, UdpChannel udpChannel, bool reliable)
            : base(channelOption, logger, statistic)
        {
            _sendQueue  = new Queue <NetPacket>(64);
            _udpChannel = udpChannel;
            _reliable   = reliable;

            if (_reliable)
            {
                _deliveryMethod = DeliveryMethod.ReliableSequenced;

                _ackPacket = new NetPacket(PacketProperty.Ack, 0);
                _ackPacket.DeliveryMethod = _deliveryMethod;
            }
            else
            {
                _deliveryMethod = DeliveryMethod.Sequenced;
            }
        }