Test using stack of SocketAsyncEventArgs

This commit is contained in:
Travis Nickles 2018-09-14 03:35:54 -05:00
parent 18335e06c3
commit 53ec1e2eef

View File

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