private void Initialize(IPEndPoint address, bool connect) { lock (sendLoopLock) { ClientId = default; closed = 0; smoothedRtt = MaxRetryInterval; smoothedRttVar = TimeSpan.Zero; currentRto = MaxRetryInterval; lastSentPingId = 0; lastReceivedPingId = 0; lastMessageTimer.Restart(); initPacketCheck = null; packetAckManager.Clear(); receiveQueueCommand.Clear(); receiveQueueCommandLow.Clear(); receiveWindowVoice.Reset(); receiveWindowVoiceWhisper.Reset(); Array.Clear(packetCounter, 0, packetCounter.Length); Array.Clear(generationCounter, 0, generationCounter.Length); NetworkStats.Reset(); socket?.Dispose(); try { if (connect) { remoteAddress = address; socket = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); socket.Bind(new IPEndPoint(address.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0)); var socketEventArgs = new SocketAsyncEventArgs(); socketEventArgs.SetBuffer(new byte[4096], 0, 4096); socketEventArgs.Completed += FetchPacketEvent; socketEventArgs.UserToken = this; socketEventArgs.RemoteEndPoint = remoteAddress; socket.ReceiveFromAsync(socketEventArgs); } else { remoteAddress = null; socket = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); socket.Bind(address); // TODO init socketevargs stuff } } catch (SocketException ex) { throw new TsException("Could not connect", ex); } pingCheckRunning = 0; pingCheck = Tools.Now; if (resendTimer == null) { resendTimer = new Timer((_) => { using (MappedDiagnosticsContext.SetScoped("BotId", id)) ResendLoop(); }, null, ClockResolution, ClockResolution); } } }
public PacketHandler(TsCrypt ts3Crypt, Id id) { receiveQueueCommand = new RingQueue <Packet <TIn> >(ReceivePacketWindowSize, ushort.MaxValue + 1); receiveQueueCommandLow = new RingQueue <Packet <TIn> >(ReceivePacketWindowSize, ushort.MaxValue + 1); receiveWindowVoice = new GenerationWindow(ushort.MaxValue + 1); receiveWindowVoiceWhisper = new GenerationWindow(ushort.MaxValue + 1); NetworkStats = new NetworkStats(); packetCounter = new ushort[TsCrypt.PacketTypeKinds]; generationCounter = new uint[TsCrypt.PacketTypeKinds]; this.tsCrypt = ts3Crypt; this.id = id; }