Bloxy.Startup.Main C# (CSharp) Method

Main() static private method

static private Main ( string args ) : void
args string
return void
        static void Main(string[] args)
        {
            try
              {
            //Parse command line arguments
            ushort? vid = null;
            ushort? pid = null;
            string buddy = String.Empty;
            int? inport = null;
            int? outport = null;
            foreach (var arg in args)
            {
              if (arg.StartsWith("/") && arg.Contains("="))
              {
            var key = arg.Substring(1, arg.IndexOf("=") - 1);
            var value = arg.Substring(arg.IndexOf("=") + 1);

            switch (key.ToLower())
            {
              case "vid":
                {
                  vid = Convert.ToUInt16(value, 16);
                  break;
                }
              case "pid":
                {
                  pid = Convert.ToUInt16(value, 16);
                  break;
                }
              case "inport":
                {
                  inport = Convert.ToInt32(value);
                  break;
                }
              case "outport":
                {
                  outport = Convert.ToInt32(value);
                  break;
                }
              case "buddy":
                {
                  buddy = value;
                  break;
                }
              default:
                {
                  Logger.WriteLine("Unknown command line argument: " + arg);
                  break;
                }
            }
              }
            }

            //Validate parameters
            if (String.IsNullOrEmpty(buddy))
              throw new ArgumentException("No buddy name/IP specified");
            if (!inport.HasValue || !outport.HasValue)
              throw new ArgumentException("No incoming/outgoing port(s) specified");
            if (!vid.HasValue || !pid.HasValue)
              throw new ArgumentException("No vendor/product ID(s) specified");

            //Initialize the USB Bluetooth adapter device
            Properties.Adapter = new USBBluetoothAdapter(vid.Value, pid.Value);
            Properties.Adapter.Open();

            //Make sure we have device configurations; get them if not
            if (!File.Exists(REAL_CONFIG_FILENAME))
              if (!_CreateConfiguration(REAL_CONFIG_FILENAME)) return;
            if (!File.Exists(EMULATED_CONFIG_FILENAME))
              if (!_CreateConfiguration(EMULATED_CONFIG_FILENAME)) return;

            //Retrieve device configurations
            Properties.RealConfiguration = DeviceConfiguration.Load(REAL_CONFIG_FILENAME);
            Properties.EmulatedConfiguration = DeviceConfiguration.Load(EMULATED_CONFIG_FILENAME);

            //Set ourselves as the device from the configuration file
            Properties.Adapter.SetLocalName(Properties.EmulatedConfiguration.RemoteName);
            Properties.Adapter.SetDeviceClass(Properties.EmulatedConfiguration.DeviceClass);

            //Set ourselves discoverable
            Properties.Adapter.SetDiscoverableMode(true);

            //Set up the server for talking to the other PC
            Properties.Server = new BloxyServer(buddy, inport.Value, outport.Value);
            Properties.Server.Start();

            //Main loop...
            Logger.WriteLine("Waiting for event...");
            while (true)
            {
              if (Console.KeyAvailable)
            if (Console.ReadKey(true).Key == ConsoleKey.Escape)
              break;

              Thread.Sleep(100);
            }

            //Clean up
            Properties.Server.Stop();
            Logger.WriteLine("Done.");
              }
              catch (Exception ex)
              {
            Logger.WriteLine("ERROR: " + ex.ToString());
              }
              finally
              {
            try
            {
              //More cleanup...
              if (Properties.Adapter != null)
            Properties.Adapter.Close();
            }
            catch
            {
              //Whatever...
            }
              }
        }