public static async Task <List <EasyTouchUnit> > Find() { var units = new List <EasyTouchUnit>(); var myIP = GetMyIP(); if (myIP == null) { return(units); } using (var udpClient = new UdpClient(new IPEndPoint(myIP, 53112)) { EnableBroadcast = true, MulticastLoopback = false, }) { await udpClient.SendAsync(broadcastData, broadcastData.Length, new IPEndPoint(IPAddress.Broadcast, multicastPort)); var buf = await udpClient.ReceiveAsync().TimeoutAfter(TimeSpan.FromSeconds(1)); if (buf.RemoteEndPoint != null) { var findServerResponse = new EasyTouchUnit(buf); if (findServerResponse.IsValid) { units.Add(findServerResponse); } } } return(units); }
public async Task <bool> ConnectTo(EasyTouchUnit unit, string?password = null) { if (unit?.IPAddress == null) { return(false); } if (client != null) { client.Dispose(); } client = new TcpClient(); await client.ConnectAsync(unit.IPAddress, unit.Port); var stream = client.GetStream(); stream.Write(ConnectionMessageBytes); Debug.WriteLine("sending challenge string"); stream.SendHLMessage(new Messages.ChallengeString()); var recvBuf = new byte[8]; var readBytes = await stream.ReadAsync(recvBuf); Debug.WriteLine("read {0} bytes (header)", readBytes); var recvBody = new byte[Messages.HLMessage.ExtractDataSize(recvBuf)]; if (recvBody.Length == 0) { return(false); } readBytes = await stream.ReadAsync(recvBody); Debug.WriteLine("read {0} bytes (body)", readBytes); string challengeStr = Messages.HLMessageTypeHelper.ExtractString(recvBody); Debug.WriteLine("sending login message"); stream.SendHLMessage(CreateLoginMessage(new HLEncoder(password).GetEncryptedPassword(challengeStr))); readBytes = await stream.ReadAsync(recvBuf); Debug.WriteLine("read {0}", readBytes); recvBody = new byte[Messages.HLMessage.ExtractDataSize(recvBuf)]; if (recvBody.Length > 0) { await stream.ReadAsync(recvBody); } return(recvBuf[2] == Messages.ClientLogin.LoginAnswerId); }
public static async Task <EasyTouchUnit?> GetGatewayInfo(string systemName, short senderId = 0) { using var client = new TcpClient(); await client.ConnectAsync(ServerDispatcherURL, ServerDispatcherPort); var ns = client.GetStream(); ns.SendHLMessage(new Messages.GetGatewayData(senderId) { GatewayName = systemName }); var msg = await UnitConnection.BuildMessageFromStream <Messages.GetGatewayData>(ns); if (msg == null) { return(null); } return(EasyTouchUnit.Create(new Messages.GetGatewayData(msg))); }