private void Initialize(IPEndPoint address, bool connect) { lock (sendLoopLock) { ClientId = 0; 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 Ts3Exception("Could not connect", ex); } pingCheckRunning = 0; pingCheck = Util.Now; if (resendTimer == null) { resendTimer = new Timer((_) => { using (MappedDiagnosticsContext.SetScoped("BotId", id)) ResendLoop(); }, null, ClockResolution, ClockResolution); } } }
private void Initialize(IPEndPoint address, bool connect) { var resendThread = new Thread(ResendLoop) { Name = "PacketHandler" }; resendThreadId = resendThread.ManagedThreadId; lock (sendLoopLock) { ClientId = 0; ExitReason = null; 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(); udpClient?.Dispose(); try { if (connect) { remoteAddress = address; udpClient = new UdpClient(address.AddressFamily); udpClient.Connect(address); } else { remoteAddress = null; udpClient = new UdpClient(address); } } catch (SocketException ex) { throw new Ts3Exception("Could not connect", ex); } } try { resendThread.Start(); } catch (SystemException ex) { throw new Ts3Exception("Error initializing internal stuctures", ex); } }