public static int Command(this ITestableNetworkStream netStream, char commandkey) { try { if (netStream.CanWrite && Commands.ContainsKey(commandkey)) { System.Diagnostics.Debug.WriteLine("\t\tTransmit Command('{0}'): ASC({1} --> 0x{2})", commandkey, Commands[commandkey].Key, BitConverter.ToString(Noxon.IntToByteArray(Commands[commandkey].Key))); netStream.Write(Noxon.IntToByteArray(Commands[commandkey].Key), 0, sizeof(int)); return(0); } else { return(-1); } } catch (System.IO.IOException e) { System.Diagnostics.Debug.WriteLine("\t\tTransmit Command() failed ({0})", e.Message); Close(); Open(); if (netStream != null && netStream.CanWrite) { netStream.Write(Noxon.IntToByteArray(Commands[commandkey].Key), 0, sizeof(int)); } return(0); } catch { return(-1); } }
static IEnumerable <XElement> StreamiRadioNet(ITestableNetworkStream netStream) { var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment, CheckCharacters = false }; XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None, Encoding.GetEncoding("ISO-8859-1")); // needed to avoid exception "WDR 3 zum Nachhören" string[] waiting = new string[] { @" \ ", " | ", " / ", " - " }; int waited = 0; using (reader = XmlReader.Create(netStream.GetStream(), settings, context)) // ^--- { while (true) { if (reader.EOF) { Thread.Sleep(200); // need to re-open netstream, but how? string waitingForSignal = " waiting for signal " + waiting[waited++ % 4] + " "; // + "connected=" + netStream.Socket.connected; ConsoleShow.Status(new XElement("value", waitingForSignal), Lines.Waiting); if (waited > 5 * 1000 / 200) // 60s { XElement el = new XElement("CloseStream"); yield return(el); } } // reader.MoveToContent(); while (!reader.EOF) { if (reader.NodeType == XmlNodeType.Element) { XElement el; try { el = XElement.ReadFrom(reader) as XElement; // can ReadFrom() forever, if iRadio = "Nicht verfügbar" or "NOXON" } catch { el = new XElement("ConsoleStreamiRadioExceptionXElementAfterReadFromFails"); } if (el != null) { yield return(el); } } else { try { reader.Read(); } catch { // continue } } } } } }
public static bool Close() { tcpClient.Client.Shutdown(SocketShutdown.Both); // tcpClient.GetStream().Close(); tcpClient.Client.Disconnect(true); netStream.Close(); tcpClient.Client.Close(); tcpClient.Close(); netStream = null; tcpClient = null; return(true); }
public static int String(this ITestableNetworkStream netStream, string str) { MultiPressCommand[] mpc = MultiPress.CreateMultiPressCommands(str); foreach (MultiPressCommand m in mpc) { for (int i = 0; i < m.Times; i++) { netStream.Command(Convert.ToChar(48 + m.Digit)); Thread.Sleep(MultiPressDelayForSameKey); } Thread.Sleep(MultiPressDelayForNextKey); } return(0); }
public static bool Open() { // https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.connect?view=netcore-3.1 // Uses a remote endpoint to establish a socket connection. tcpClient = new TcpClient(); IPAddress ip = Noxon.IP; // IPAddress.Parse(iRadioConsole.Properties.Resources.NoxonIP); try { tcpClient.Connect(ip, 10100); // connect to iRadio server port netStream = new TestableNetworkStream(tcpClient.GetStream()); return(true); } catch (SocketException se) { Console.WriteLine("Connect to NOXON iRadio failed ({0}, {1}), now try all IPs on gateway", se.SocketErrorCode, se.Message); } if (PingHosts()) { ip = IP; } IPEndPoint ipEndPoint = new IPEndPoint(ip, 10100); // using iRadio Telnet port 10100 while (!tcpClient.Connected) { try { tcpClient.Connect(ipEndPoint); } catch (SocketException ex) { Console.WriteLine("Connect to NOXON iRadio failed ({0}, {1})", ex.SocketErrorCode, ex.Message); } } // https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.getstream?view=netcore-3.1 // Uses the GetStream public method to return the NetworkStream. netStream = new TestableNetworkStream(tcpClient.GetStream()); tcpClient.Client.LingerState = new LingerOption(false, 0); return(true); }
public static IEnumerable <XElement> StreamiRadioNet(ITestableNetworkStream netStream) { var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment, CheckCharacters = false, Async = true }; XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None, Encoding.GetEncoding("ISO-8859-1")); // needed to avoid exception on strings like "WDR 3 zum Nachhören" CancellationTokenSource cancellation = new CancellationTokenSource(); // must not be (static) class variable System.Timers.Timer timeoutTimer = new System.Timers.Timer(Properties.Settings.Default.ReadTimeout); // check if ReadFrom(reader) times out timeoutTimer.Elapsed += (sender, e) => ParseTimeout(sender, e, cancellation); using (reader = XmlReader.Create(netStream.GetStream(), settings, context)) { while (true) { while (!reader.EOF) { if (reader.NodeType == XmlNodeType.Element) { XElement el; TaskStatus tstat = TaskStatus.Created; AggregateException tex = null; try { if (!FormShow.Browsing) { timeoutTimer.Start(); } // https://docs.microsoft.com/de-de/dotnet/core/porting/ Task <XNode> t = XNode.ReadFromAsync(reader, cancellation.Token); el = t.Result as XElement; // ToDo: if iRadio = "Nicht verfügbar" or "NOXON" ==> ReadFromAsync() is canceled (OK!) but does not resume normal reading tstat = t.Status; // also: no more data received if <browse> menu tex = t.Exception; if (!FormShow.Browsing) { timeoutTimer.Stop(); } } catch (Exception ex) { el = new XElement("CloseStream", "FormStreamiRadioExceptionXElementAfterReadFromFails" + ex.Message + "=" + tex?.Message); } if (el != null) { yield return(el); } } else { try { reader.Read(); } catch { // continue } } } } } // cancellation.Dispose(); }