diff --git a/DS4Windows/DS4Control/UdpServer.cs b/DS4Windows/DS4Control/UdpServer.cs index 7e87fd8..1dd6820 100644 --- a/DS4Windows/DS4Control/UdpServer.cs +++ b/DS4Windows/DS4Control/UdpServer.cs @@ -69,6 +69,7 @@ namespace DS4Windows private uint serverId; private bool running; private byte[] recvBuffer = new byte[1024]; + private Stack argsStack; public delegate void GetPadDetail(int padIdx, ref DualShockPadMeta meta); @@ -77,6 +78,13 @@ namespace DS4Windows public UdpServer(GetPadDetail getPadDetailDel) { portInfoGet = getPadDetailDel; + argsStack = new Stack(20); + for (int num = 0; num <= 19; num++) + { + SocketAsyncEventArgs args = new SocketAsyncEventArgs(); + args.Completed += ClearSentData; + argsStack.Push(args); + } } enum MessageType @@ -174,10 +182,9 @@ namespace DS4Windows FinishPacket(packetData); //try { udpSock.SendTo(packetData, clientEP); } - SocketAsyncEventArgs args = new SocketAsyncEventArgs(); + SocketAsyncEventArgs args = argsStack.Pop(); args.RemoteEndPoint = clientEP; args.SetBuffer(packetData, 0, packetData.Length); - args.Completed += ClearSentData; try { udpSock.SendToAsync(args); } catch (Exception e) { } } @@ -632,11 +639,10 @@ namespace DS4Windows foreach (var cl in clientsList) { //try { udpSock.SendTo(outputData, cl); } - SocketAsyncEventArgs args = new SocketAsyncEventArgs(); + SocketAsyncEventArgs args = argsStack.Pop(); args.RemoteEndPoint = cl; args.SetBuffer(outputData, 0, outputData.Length); - args.Completed += ClearSentData; - try { udpSock.SendToAsync(args); } + try { bool result = udpSock.SendToAsync(args); if (!result) argsStack.Push(args); } catch (SocketException ex) { } } } @@ -647,8 +653,9 @@ namespace DS4Windows private void ClearSentData(object sender, SocketAsyncEventArgs args) { - args.Dispose(); - args = null; + argsStack.Push(args); + //args.Dispose(); + //args = null; } } }