// 32feet.NET - Personal Area Networking for .NET // // Copyright (c) 2010 In The Hand Ltd, All rights reserved. // Copyright (c) 2010 Alan J. McFarlane, All rights reserved. // This source code is licensed under the In The Hand Community License - see License.txt using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net.Sockets; using System.Threading; using System.Diagnostics; using ConsoleMenuTesting; namespace DeviceMenuTesting { class RemoteConsole : IAuxConsole { public const int Port = 0x616c; //=24940 const string ActiveSync_PcHostname = "PPP_PEER"; // readonly MenuSystem _menus; // readonly Stream _peer; readonly List _thingsToKeepAlive = new List(); readonly TextWriter _wtr; internal RemoteConsole(MenuSystem menuSystem) : this(ActiveSync_PcHostname, menuSystem) { } internal RemoteConsole(string hostname, MenuSystem menuSystem) { _menus = menuSystem; TcpClient cli; try { cli = new TcpClient(hostname, Port); } catch (SocketException ex) { menuSystem.WriteLine("Connect failed: " + ex.ErrorCode); return; } _peer = cli.GetStream(); // Arrrgggh NETCF's TcpClient Disposes the socket in it Finalizer!! _thingsToKeepAlive.Add(cli); _wtr = new StreamWriter(_peer); ThreadPool.QueueUserWorkItem(ThreadRunner); } void ThreadRunner(object state) { var rdr = new StreamReader(_peer); while (true) { string line; try { line = rdr.ReadLine(); } catch (EndOfStreamException) { break; } var splits = SplitAtFirst(line, Commands.Separator); switch (splits[0]) { case Commands.MENUOPTION: var option = SelectOrDefault(_menus.Options, splits[1]); if (option == null) { Send(Commands.Error, "Unknown menu option: " + splits[1]); } else { ThreadPool.QueueUserWorkItem(MenuOption_Runner, option); Send(Commands.RUNNING, splits[1]); } break; default: Send(Commands.Error, "Unsupported command: " + splits[0]); Trace.Assert(false, "Unsupported command: " + splits[0]); break; } }//while } private object SelectOrDefault(IList