More cleanup, better debugging output

This commit is contained in:
2018-03-28 18:04:51 -05:00
committed by Chris Pickett
parent 579d15fb4d
commit 8336f45c82
5 changed files with 65 additions and 79 deletions

View File

@ -1,6 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ScreenLogicConnect
{
@ -15,38 +18,28 @@ namespace ScreenLogicConnect
public EasyTouchUnit(UdpReceiveResult result)
{
if (BitConverter.ToInt32(result.Buffer, 0) == 2)
{
int i;
byte[] temp = new byte[4];
for (i = 4; i < 8; i++)
{
temp[i - 4] = result.Buffer[i];
}
try
{
this.ipAddress = result.RemoteEndPoint.Address;
port = BitConverter.ToInt16(result.Buffer, 8);
gatewayType = result.Buffer[10];
gatewaySubType = result.Buffer[11];
int nameDataSize = 28;
i = 0;
while (i < nameDataSize)
ipAddress = result.RemoteEndPoint.Address;
using (var ms = new MemoryStream(result.Buffer))
{
i++;
if (result.Buffer[i + 12] == 0)
using (var br = new BinaryReader(ms))
{
nameDataSize = i;
}
}
char[] nameData = new char[nameDataSize];
for (i = 0; i < nameDataSize; i++)
var unitType = br.ReadInt32();
if (unitType == 2)
{
nameData[i] = (char)result.Buffer[i + 12];
}
gatewayName = new String(nameData);
br.ReadBytes(4);
port = br.ReadInt16();
gatewayType = br.ReadByte();
gatewaySubType = br.ReadByte();
gatewayName = Encoding.ASCII.GetString(result.Buffer.Skip((int)ms.Position).TakeWhile(x => x != 0).ToArray());
isValid = true;
}
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
@ -54,4 +47,3 @@ namespace ScreenLogicConnect
}
}
}
}

View File

@ -1,21 +0,0 @@
namespace ScreenLogicConnect
{
public class FindUnitBroadcast
{
public static readonly byte[] data = new byte[]
{
1,
0,
0,
0,
0,
0,
0,
0,
};
public FindUnitBroadcast()
{
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
@ -8,12 +7,18 @@ namespace ScreenLogicConnect
{
public class FindUnits
{
public static readonly byte[] broadcastData = new byte[]
{
1, 0, 0, 0,
0, 0, 0, 0,
};
protected const short multicastPort = 1444;
private Socket searchSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
public static async Task<IEnumerable<EasyTouchUnit>> Find()
public static async Task<List<EasyTouchUnit>> Find()
{
List<EasyTouchUnit> units = new List<EasyTouchUnit>();
var units = new List<EasyTouchUnit>();
using (var udpClient = new UdpClient(new IPEndPoint(GetMyIP(), 53112))
{
@ -21,7 +26,7 @@ namespace ScreenLogicConnect
MulticastLoopback = false,
})
{
await udpClient.SendAsync(FindUnitBroadcast.data, FindUnitBroadcast.data.Length, new IPEndPoint(IPAddress.Broadcast, multicastPort));
await udpClient.SendAsync(broadcastData, broadcastData.Length, new IPEndPoint(IPAddress.Broadcast, multicastPort));
var buf = await udpClient.ReceiveAsync();
var findServerResponse = new EasyTouchUnit(buf);

View File

@ -139,12 +139,12 @@ namespace ScreenLogicConnect.Messages
public bool isSpaActive()
{
return this.circuitArray != null && this.circuitArray.Any(x => x.id == CircuitUpdateDataStructure.SPA_CIRCUIT_ID && x.state == 1);
return this.circuitArray?.Any(x => x.id == CircuitUpdateDataStructure.SPA_CIRCUIT_ID && x.state == 1) ?? false;
}
public bool isPoolActive()
{
return this.circuitArray != null && this.circuitArray.Any(x => x.id == CircuitUpdateDataStructure.POOL_CIRCUIT_ID && x.state == 1);
return this.circuitArray?.Any(x => x.id == CircuitUpdateDataStructure.POOL_CIRCUIT_ID && x.state == 1) ?? false;
}
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ScreenLogicConnect
@ -22,36 +24,39 @@ namespace ScreenLogicConnect
var connMsg = CreateConnectServerSoftMessage();
var stream = client.GetStream();
stream.Write(connMsg, 0, connMsg.Length);
Debug.WriteLine("sending challenge string");
stream.SendHLMessage(Messages.ChallengeString.QUERY(0));
Debug.WriteLine("sent challenge string");
var recvBuf = new byte[1024];
var readBytes = stream.Read(recvBuf, 0, recvBuf.Length);
Debug.WriteLine("read {0}", readBytes);
Debug.WriteLine("sending login message");
stream.SendHLMessage(createLoginMessage(new byte[16]));
Debug.WriteLine("sent login message");
readBytes = stream.Read(recvBuf, 0, recvBuf.Length);
Debug.WriteLine("read {0}", readBytes);
}
public Messages.GetPoolStatus GetPoolStatus()
{
Debug.WriteLine("sending status message");
client.GetStream().SendHLMessage(Messages.GetPoolStatus.QUERY(0));
Debug.WriteLine("sent status message");
return new Messages.GetPoolStatus(getMessage(client.GetStream()));
}
public Messages.GetControllerConfig GetControllerConfig()
{
Debug.WriteLine("sending controller config message");
client.GetStream().SendHLMessage(Messages.GetControllerConfig.QUERY(0));
Debug.WriteLine("sent controller config message");
return new Messages.GetControllerConfig(getMessage(client.GetStream()));
}
public Messages.GetMode GetMode()
{
Debug.WriteLine("sending get-mode message");
client.GetStream().SendHLMessage(Messages.GetMode.QUERY(0));
Debug.WriteLine("sent get-mode message");
return new Messages.GetMode(getMessage(client.GetStream()));
}
@ -71,39 +76,46 @@ namespace ScreenLogicConnect
}
catch { }
}
int msgDataSize = Messages.HLMessage.extractDataSize(headerBuffer);
if (msgDataSize <= 0 || msgDataSize >= 100000)
{
return null;
}
byte[] dataBuffer = new byte[msgDataSize];
bytesRead = 0;
while (bytesRead < msgDataSize)
{
bytesRead += ns.Read((byte[])(Array)dataBuffer, bytesRead, msgDataSize - bytesRead);
if (bytesRead < 0)
{
return null;
}
}
Debug.WriteLine($"read {headerBuffer.Length + dataBuffer.Length} bytes of message data ({headerBuffer.Length} header, {dataBuffer.Length} data)");
return new Messages.HLMessage(headerBuffer, dataBuffer);
}
private static string connectionMessage = "CONNECTSERVERHOST";
private byte[] CreateConnectServerSoftMessage()
{
int iLen = connectionMessage.Length;
byte[] bytes = new byte[(iLen + 4)];
var connBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(connectionMessage);
for (int i = 0; i < iLen; i++)
using (var ms = new MemoryStream())
{
bytes[i] = connBytes[i];
using (var bw = new BinaryWriter(ms))
{
bw.Write(Encoding.ASCII.GetBytes(connectionMessage));
bw.Write((byte)'\r');
bw.Write((byte)'\n');
bw.Write((byte)'\r');
bw.Write((byte)'\n');
}
return ms.ToArray();
}
bytes[iLen + 0] = (byte)'\r';
bytes[iLen + 1] = (byte)'\n';
bytes[iLen + 2] = (byte)'\r';
bytes[iLen + 3] = (byte)'\n';
return bytes;
}
private Messages.HLMessage createLoginMessage(byte[] encodedPwd)
@ -112,20 +124,18 @@ namespace ScreenLogicConnect
login.m_schema = 348;
login.m_connectionType = 0;
login.m_version = "ScreenLogicConnect library";
login.m_procID = 2;
if (encodedPwd.Length > 16)
{
byte[] temp = new byte[16];
for (int i = 0; i < 16; i++)
{
temp[i] = encodedPwd[i];
}
login.m_byteArray = temp;
login.m_byteArray = new byte[16];
Array.Copy(encodedPwd, login.m_byteArray, 16);
}
else
{
login.m_byteArray = encodedPwd;
}
login.m_procID = 2;
return login;
}