UDP server management changes, UDP server options

Related to issue #228
This commit is contained in:
Travis Nickles 2018-08-06 05:00:37 -05:00
parent f3c64143e2
commit 5c52cead54
5 changed files with 3011 additions and 1821 deletions

View File

@ -119,18 +119,42 @@ namespace DS4Windows
//return meta;
}
private object busThrLck = new object();
private bool busThrRunning = false;
private Queue<Action> busEvtQueue = new Queue<Action>();
private object busEvtQueueLock = new object();
public ControlService()
{
//sp.Stream = Properties.Resources.EE;
// Cause thread affinity to not be tied to main GUI thread
tempThread = new Thread(() => { x360Bus = new X360Device(); _udpServer = new UdpServer(GetPadDetailForIdx); });
tempThread = new Thread(() => {
x360Bus = new X360Device();
//_udpServer = new UdpServer(GetPadDetailForIdx);
busThrRunning = true;
while (busThrRunning)
{
lock (busEvtQueueLock)
{
Action tempAct = null;
for (int actInd = 0, actLen = busEvtQueue.Count; actInd < actLen; actInd++)
{
tempAct = busEvtQueue.Dequeue();
tempAct.Invoke();
}
}
lock (busThrLck)
Monitor.Wait(busThrLck);
}
});
tempThread.Priority = ThreadPriority.AboveNormal;
tempThread.IsBackground = true;
tempThread.Start();
while (tempThread.IsAlive)
{
Thread.SpinWait(500);
}
//while (_udpServer == null)
//{
// Thread.SpinWait(500);
//}
for (int i = 0, arlength = DS4Controllers.Length; i < arlength; i++)
{
@ -143,6 +167,109 @@ namespace DS4Windows
}
}
private void TestQueueBus(Action temp)
{
lock (busEvtQueueLock)
{
busEvtQueue.Enqueue(temp);
}
lock (busThrLck)
Monitor.Pulse(busThrLck);
}
public void ChangeUDPStatus(bool state)
{
if (state && _udpServer == null)
{
TestQueueBus(() =>
{
udpChangeStatus = true;
_udpServer = new UdpServer(GetPadDetailForIdx);
var UDP_SERVER_PORT = Global.getUDPServerPortNum();
try
{
_udpServer.Start(UDP_SERVER_PORT);
LogDebug("UDP server listening on port " + UDP_SERVER_PORT);
}
catch (System.Net.Sockets.SocketException ex)
{
var errMsg = String.Format("Couldn't start UDP server on port {0}, outside applications won't be able to access pad data ({1})", UDP_SERVER_PORT, ex.SocketErrorCode);
LogDebug(errMsg, true);
Log.LogToTray(errMsg, true, true);
}
udpChangeStatus = false;
});
}
else if (!state && _udpServer != null)
{
TestQueueBus(() =>
{
udpChangeStatus = true;
_udpServer.Stop();
_udpServer = null;
Log.LogToGui("Closed UDP server", false);
udpChangeStatus = false;
});
}
}
public void ChangeMotionEventStatus(bool state)
{
IEnumerable<DS4Device> devices = DS4Devices.getDS4Controllers();
if (state)
{
foreach (DS4Device dev in devices)
{
dev.queueEvent(() =>
{
dev.Report += dev.MotionEvent;
});
}
}
else
{
foreach (DS4Device dev in devices)
{
dev.queueEvent(() =>
{
dev.Report -= dev.MotionEvent;
});
}
}
}
private bool udpChangeStatus = false;
public bool changingUDPPort = false;
public async void UseUDPPort()
{
changingUDPPort = true;
IEnumerable<DS4Device> devices = DS4Devices.getDS4Controllers();
foreach (DS4Device dev in devices)
{
dev.queueEvent(() =>
{
dev.Report -= dev.MotionEvent;
});
}
await Task.Delay(100);
_udpServer.Start(getUDPServerPortNum());
foreach (DS4Device dev in devices)
{
dev.queueEvent(() =>
{
dev.Report += dev.MotionEvent;
});
}
changingUDPPort = false;
}
private void WarnExclusiveModeFailure(DS4Device device)
{
if (DS4Devices.isExclusiveMode && !device.isExclusive())
@ -258,6 +385,15 @@ namespace DS4Windows
LogDebug(DS4Devices.isExclusiveMode ? Properties.Resources.UsingExclusive : Properties.Resources.UsingShared);
}
if (isUsingUDPServer() && _udpServer == null)
{
ChangeUDPStatus(true);
while (udpChangeStatus == true)
{
Task.Delay(100);
}
}
try
{
DS4Devices.findControllers();
@ -317,17 +453,17 @@ namespace DS4Windows
this.On_Report(sender, e, tempIdx);
};
if (_udpServer != null)
{
EventHandler<EventArgs> tempEvnt = (sender, args) =>
{
DualShockPadMeta padDetail = new DualShockPadMeta();
GetPadDetailForIdx(tempIdx, ref padDetail);
_udpServer.NewReportIncoming(ref padDetail, CurrentState[tempIdx]);
};
device.Report += tempEvnt;
device.MotionEvent = tempEvnt;
if (_udpServer != null)
{
device.Report += tempEvnt;
}
TouchPadOn(i, device);
@ -365,7 +501,8 @@ namespace DS4Windows
if (_udpServer != null)
{
var UDP_SERVER_PORT = 26760;
//var UDP_SERVER_PORT = 26760;
var UDP_SERVER_PORT = Global.getUDPServerPortNum();
try
{
@ -458,7 +595,8 @@ namespace DS4Windows
DS4Devices.stopControllers();
if (_udpServer != null)
_udpServer.Stop();
ChangeUDPStatus(false);
//_udpServer.Stop();
if (showlog)
LogDebug(Properties.Resources.StoppedDS4Windows);
@ -530,17 +668,17 @@ namespace DS4Windows
this.On_Report(sender, e, tempIdx);
};
if (_udpServer != null)
{
EventHandler<EventArgs> tempEvnt = (sender, args) =>
{
DualShockPadMeta padDetail = new DualShockPadMeta();
GetPadDetailForIdx(tempIdx, ref padDetail);
_udpServer.NewReportIncoming(ref padDetail, CurrentState[tempIdx]);
};
device.Report += tempEvnt;
device.MotionEvent = tempEvnt;
if (_udpServer != null)
{
device.Report += tempEvnt;
}
if (!getDInputOnly(Index) && device.isSynced())

View File

@ -569,6 +569,24 @@ namespace DS4Windows
return m_Config.flashWhenLateAt;
}
public static bool isUsingUDPServer()
{
return m_Config.useUDPServ;
}
public static void setUsingUDPServer(bool state)
{
m_Config.useUDPServ = state;
}
public static int getUDPServerPortNum()
{
return m_Config.udpServPort;
}
public static void setUDPServerPort(int value)
{
m_Config.udpServPort = value;
}
public static bool UseWhiteIcon
{
set { m_Config.useWhiteIcon = value; }
@ -1472,6 +1490,8 @@ namespace DS4Windows
public bool useWhiteIcon;
public bool flashWhenLate = true;
public int flashWhenLateAt = 20;
public bool useUDPServ = false;
public int udpServPort = 26760;
// Cache whether profile has custom action
public bool[] containsCustomAction = new bool[5] { false, false, false, false, false };
@ -3006,6 +3026,10 @@ namespace DS4Windows
catch { missingSetting = true; }
try { Item = m_Xdoc.SelectSingleNode("/Profile/WhiteIcon"); Boolean.TryParse(Item.InnerText, out useWhiteIcon); }
catch { missingSetting = true; }
try { Item = m_Xdoc.SelectSingleNode("/Profile/UseUDPServer"); Boolean.TryParse(Item.InnerText, out useUDPServ); }
catch { missingSetting = true; }
try { Item = m_Xdoc.SelectSingleNode("/Profile/UDPServerPort"); int temp; int.TryParse(Item.InnerText, out temp); udpServPort = Math.Min(Math.Max(temp, 1024), 65535); }
catch { missingSetting = true; }
for (int i = 0; i < 4; i++)
{
@ -3074,6 +3098,8 @@ namespace DS4Windows
XmlNode xmlFlashWhenLate = m_Xdoc.CreateNode(XmlNodeType.Element, "FlashWhenLate", null); xmlFlashWhenLate.InnerText = flashWhenLate.ToString(); Node.AppendChild(xmlFlashWhenLate);
XmlNode xmlFlashWhenLateAt = m_Xdoc.CreateNode(XmlNodeType.Element, "FlashWhenLateAt", null); xmlFlashWhenLateAt.InnerText = flashWhenLateAt.ToString(); Node.AppendChild(xmlFlashWhenLateAt);
XmlNode xmlWhiteIcon = m_Xdoc.CreateNode(XmlNodeType.Element, "WhiteIcon", null); xmlWhiteIcon.InnerText = useWhiteIcon.ToString(); Node.AppendChild(xmlWhiteIcon);
XmlNode xmlUseUDPServ = m_Xdoc.CreateNode(XmlNodeType.Element, "UseUDPServer", null); xmlUseUDPServ.InnerText = useUDPServ.ToString(); Node.AppendChild(xmlUseUDPServ);
XmlNode xmlUDPServPort = m_Xdoc.CreateNode(XmlNodeType.Element, "UDPServerPort", null); xmlUDPServPort.InnerText = udpServPort.ToString(); Node.AppendChild(xmlUDPServPort);
for (int i = 0; i < 4; i++)
{

View File

@ -154,6 +154,10 @@
this.lbUseXIPorts = new System.Windows.Forms.Label();
this.nUDXIPorts = new System.Windows.Forms.NumericUpDown();
this.lbLastXIPort = new System.Windows.Forms.Label();
this.panel4 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.ckUdpServ = new System.Windows.Forms.CheckBox();
this.nUDUdpPortNum = new System.Windows.Forms.NumericUpDown();
this.languagePackComboBox1 = new DS4Windows.DS4Forms.LanguagePackComboBox();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.linkProfiles = new System.Windows.Forms.LinkLabel();
@ -199,6 +203,8 @@
((System.ComponentModel.ISupportInitialize)(this.nUDUpdateTime)).BeginInit();
this.pnlXIPorts.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nUDXIPorts)).BeginInit();
this.panel4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nUDUdpPortNum)).BeginInit();
this.flowLayoutPanel1.SuspendLayout();
this.tabLog.SuspendLayout();
this.panel3.SuspendLayout();
@ -958,6 +964,7 @@
this.fLPSettings.Controls.Add(this.cBUpdate);
this.fLPSettings.Controls.Add(this.pNUpdate);
this.fLPSettings.Controls.Add(this.pnlXIPorts);
this.fLPSettings.Controls.Add(this.panel4);
this.fLPSettings.Controls.Add(this.languagePackComboBox1);
this.fLPSettings.Controls.Add(this.flowLayoutPanel1);
this.fLPSettings.Name = "fLPSettings";
@ -1209,6 +1216,47 @@
resources.ApplyResources(this.lbLastXIPort, "lbLastXIPort");
this.lbLastXIPort.Name = "lbLastXIPort";
//
// panel4
//
this.panel4.Controls.Add(this.label2);
this.panel4.Controls.Add(this.ckUdpServ);
this.panel4.Controls.Add(this.nUDUdpPortNum);
resources.ApplyResources(this.panel4, "panel4");
this.panel4.Name = "panel4";
//
// label2
//
resources.ApplyResources(this.label2, "label2");
this.label2.Name = "label2";
//
// ckUdpServ
//
resources.ApplyResources(this.ckUdpServ, "ckUdpServ");
this.ckUdpServ.Name = "ckUdpServ";
this.ckUdpServ.UseVisualStyleBackColor = true;
this.ckUdpServ.CheckedChanged += new System.EventHandler(this.CkUdpServ_CheckedChanged);
//
// nUDUdpPortNum
//
resources.ApplyResources(this.nUDUdpPortNum, "nUDUdpPortNum");
this.nUDUdpPortNum.Maximum = new decimal(new int[] {
65535,
0,
0,
0});
this.nUDUdpPortNum.Minimum = new decimal(new int[] {
1024,
0,
0,
0});
this.nUDUdpPortNum.Name = "nUDUdpPortNum";
this.nUDUdpPortNum.Value = new decimal(new int[] {
26760,
0,
0,
0});
this.nUDUdpPortNum.Leave += new System.EventHandler(this.NUDUdpPortNum_Leave);
//
// languagePackComboBox1
//
resources.ApplyResources(this.languagePackComboBox1, "languagePackComboBox1");
@ -1399,6 +1447,9 @@
this.pnlXIPorts.ResumeLayout(false);
this.pnlXIPorts.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nUDXIPorts)).EndInit();
this.panel4.ResumeLayout(false);
this.panel4.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nUDUdpPortNum)).EndInit();
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
this.tabLog.ResumeLayout(false);
@ -1555,6 +1606,10 @@
private System.Windows.Forms.ToolStripMenuItem discon3ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem discon4ToolStripMenuItem;
private System.Windows.Forms.CheckBox mintoTaskCheckBox;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox ckUdpServ;
private System.Windows.Forms.NumericUpDown nUDUdpPortNum;
//private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;
}
}

View File

@ -400,6 +400,9 @@ namespace DS4Windows
StartWindowsCheckBox.CheckedChanged += new EventHandler(StartWindowsCheckBox_CheckedChanged);
new ToolTip().SetToolTip(StartWindowsCheckBox, Properties.Resources.RunAtStartup);
ckUdpServ.Checked = isUsingUDPServer();
nUDUdpPortNum.Value = getUDPServerPortNum();
populateHoverTextDict();
cBController1.KeyPress += CBController_KeyPress;
@ -2544,6 +2547,38 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question
e.Handled = true;
}
private async void CkUdpServ_CheckedChanged(object sender, EventArgs e)
{
bool state = ckUdpServ.Checked;
setUsingUDPServer(state);
if (!state)
{
Program.rootHub.ChangeMotionEventStatus(state);
await TaskRunner.Delay(100);
Program.rootHub.ChangeUDPStatus(state);
}
else
{
Program.rootHub.ChangeUDPStatus(state);
Program.rootHub.ChangeMotionEventStatus(state);
}
nUDUdpPortNum.Enabled = state;
}
private void NUDUdpPortNum_Leave(object sender, EventArgs e)
{
nUDUdpPortNum.Enabled = false;
setUDPServerPort((int)nUDUdpPortNum.Value);
WaitUDPPortChange();
}
private async void WaitUDPPortChange()
{
await TaskRunner.Run(() => Program.rootHub.UseUDPPort());
nUDUdpPortNum.Enabled = true;
}
private void cBFlashWhenLate_CheckedChanged(object sender, EventArgs e)
{
FlashWhenLate = cBFlashWhenLate.Checked;

File diff suppressed because it is too large Load Diff