Esempio n. 1
0
        internal async Task RunAsync()
        {
            // 첫 접속 패킷을 보내자
            NetPacket sendPacket = NetPool.PacketPool.Alloc(PacketProperty.ResponseConnection, 8);

            sendPacket.SessionIdForConnection = SessionId;
            FastBitConverter.GetBytes(sendPacket.RawData, 5, _connectId);

            SendRawAsync(sendPacket, DeliveryMethod.Tcp);

            _isPossibleUpdate = true;

            try
            {
                await ProcessPacketAsync();
            }
            finally
            {
                _isPossibleUpdate = false;
                Close();
            }
        }
Esempio n. 2
0
        internal void OnReceivedRawUdpData(byte[] data, int size, NetPacket cachedPacket, SocketError error, IPEndPoint endPoint)
        {
            //Console.WriteLine($"buffer size : {size}   packet size : {BitConverter.ToUInt16(data, 0)}");

            if (size < NetPacket.HeaderSize)
                return;

            try
            {
                PacketProperty property = cachedPacket.Property;
                DeliveryMethod deliveryMethod = cachedPacket.DeliveryMethod;

                switch (property)
                {

                    case PacketProperty.MtuCheck:
                        {
                            NetPacket packet = NetPool.PacketPool.Alloc(NetPacket.GetHeaderSize(PacketProperty.MtuOk));

                            try
                            {
                                byte getMtuId = data[3];

                                packet.Property = PacketProperty.MtuOk;
                                packet.DeliveryMethod = DeliveryMethod.Unreliable;
                                packet.RawData[3] = getMtuId;

                                SendTo(packet.RawData, 0, packet.Size, SendMode.Immediately);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError(ex, "Exception happened in MtuCheck");
                            }
                            finally
                            {
                                NetPool.PacketPool.Free(packet);
                            }
                        }
                        break;
                    case PacketProperty.MtuOk:
                        {
                            try
                            {
                                byte getMtuId = data[3];

                                if (getMtuId >= _mtuId)
                                {
                                    _mtuId = getMtuId;
                                    _mtu = NetPacket.PossibleMtu[getMtuId];

                                    if (_mtuId >= NetPacket.PossibleMtu.Length)
                                        _finishMtu = true;

                                    //Console.WriteLine($"SetMtu : {_mtu}");
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError(ex, "Exception happened in MtuOk");
                            }
                        }
                        break;
                    case PacketProperty.Ping:
                        {
                            ushort sequence = BitConverter.ToUInt16(data, 3);

                            if (NetUtil.RelativeSequenceNumber(sequence, _pongPacket.Sequence) > 0)
                            {
                                FastBitConverter.GetBytes(_pongPacket.RawData, 5, DateTime.UtcNow.Ticks);
                                _pongPacket.Sequence = sequence;
                                SendTo(_pongPacket.RawData, 0, _pongPacket.Size, SendMode.Immediately);
                            }
                        }
                        break;
                    case PacketProperty.Pong:
                        {
                            ushort sequence = BitConverter.ToUInt16(data, 3);

                            if (sequence == _pingPacket.Sequence)
                            {
                                _pingTimer.Stop();
                                int elapsedMs = (int)_pingTimer.ElapsedMilliseconds;
                                _remoteTickDelta = BitConverter.ToInt64(data, 5) + (elapsedMs * TimeSpan.TicksPerMillisecond) / 2 - DateTime.UtcNow.Ticks;
                                UpdateRoundTripTime(elapsedMs);

                                //Console.WriteLine($"Pong sequence : {sequence}  {elapsedMs} ms  {_remoteTickDelta} microseconds");
                            }
                        }
                        break;
                    default:
                        {
                            ReadPacket(data, size, _channelOption.PacketFilter);
                        }
                        break;
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception happened in OnReceivedRawUdpData");
                Close();
            }
        }
Esempio n. 3
0
 private void IncreaseRequestRpc(byte[] data, int offset, int length)
 {
     _statistic.IncreaseRequestRpc(FastBitConverter.ToInt32(data, offset + 1), length);
 }
Esempio n. 4
0
 public static void SetP2pSessionId(byte[] data, ushort relaySessionId)
 {
     FastBitConverter.GetBytes(data, 5, relaySessionId);
 }