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 bool running;
private byte[] recvBuffer = new byte[1024];
private Stack<SocketAsyncEventArgs> 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<SocketAsyncEventArgs>(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;
}
}
}