From 79377ced7fd9ef2c1b651856f7ebc9853ca682e0 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Fri, 2 Nov 2018 13:03:08 -0500 Subject: [PATCH] Added locking and volatile usage for UdpServer Related to issue #454. --- DS4Windows/DS4Control/UdpServer.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/DS4Windows/DS4Control/UdpServer.cs b/DS4Windows/DS4Control/UdpServer.cs index a979715..bc4ada9 100644 --- a/DS4Windows/DS4Control/UdpServer.cs +++ b/DS4Windows/DS4Control/UdpServer.cs @@ -5,6 +5,7 @@ using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.ComponentModel; +using System.Threading; namespace DS4Windows { @@ -70,7 +71,8 @@ namespace DS4Windows private bool running; private byte[] recvBuffer = new byte[1024]; private SocketAsyncEventArgs[] argsList; - private int listInd = 0; + private volatile int listInd = 0; + private object poolLock = new object(); public delegate void GetPadDetail(int padIdx, ref DualShockPadMeta meta); @@ -182,8 +184,14 @@ namespace DS4Windows FinishPacket(packetData); //try { udpSock.SendTo(packetData, clientEP); } - SocketAsyncEventArgs args = argsList[listInd]; - listInd = ++listInd % 20; + int temp = 0; + lock (poolLock) + { + listInd = ++listInd % 20; + temp = listInd; + } + + SocketAsyncEventArgs args = argsList[temp]; args.RemoteEndPoint = clientEP; args.SetBuffer(packetData, 0, packetData.Length); try { @@ -642,8 +650,14 @@ namespace DS4Windows foreach (var cl in clientsList) { //try { udpSock.SendTo(outputData, cl); } - SocketAsyncEventArgs args = argsList[listInd]; - listInd = ++listInd % 20; + int temp = 0; + lock (poolLock) + { + listInd = ++listInd % 20; + temp = listInd; + } + + SocketAsyncEventArgs args = argsList[temp]; args.RemoteEndPoint = cl; args.SetBuffer(outputData, 0, outputData.Length); try {