// Analysis disable once FunctionNeverReturns public static void Daemon(ushort port, Array uri, Guid serverID) { Raw raw = new Raw (port); SimpleDiscovery sd = new SimpleDiscovery (raw); HTTPRequest httpRequest = new HTTPRequest (); Dictionary<string, object> args = new Dictionary<string, object> (); args.Add ("URI", uri); args.Add ("SERVERID", serverID.ToString ()); httpRequest.Arguments = args; sd.SetMessageInformation (httpRequest.Arguments); sd.DiscoverMessageReceived += (sender, e) => { try { if ((string)(e.Request.Arguments ["SERVERID"]) != serverID.ToString ()) Console.Error.WriteLine ("Another daemon is running on the network."); } catch { Console.Error.WriteLine ("Another program is using our port."); } }; try { while (true) System.Threading.Thread.Sleep (1000); } finally { raw.Stop (); } }
public SimpleDiscovery(Raw raw) { this.raw = raw; if (raw == null) throw new ArgumentNullException ("raw"); raw.SetReceiveCallback (MessageReceived); req = new HTTPRequest (); req.Method = "NOTIFY"; req.URI = "*"; req.Version = "HTTP/1.1"; }
public CachedDiscoveryManager(short port) { Guid id = Guid.NewGuid (); Raw r = new Raw (port); SimpleDiscovery sd = new SimpleDiscovery (r); Dictionary<string, object> arguments = new Dictionary<string, object> (); arguments.Add ("ID", id.ToString ()); arguments.Add ("HOST", System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties ().DomainName); arguments.Add ("PORT", port.ToString ()); sd.DiscoverMessageReceived += (sender, e) => { Guid msgID = Guid.Parse (e.Request.Arguments ["ID"].ToString ()); if (hosts.ContainsKey (msgID)) { lock (hosts) hosts.Add (msgID, e.Request); } else { lock (hosts) hosts [msgID].Arguments = e.Request.Arguments; } }; sd.SetMessageInformation (arguments); }
static IEnumerable<Uri> PerformAutoDiscovery() { object tempURI = null; System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent (false); Raw raw = new Raw (18100); SimpleDiscovery sd = new SimpleDiscovery (raw); sd.DiscoverMessageReceived += (sender, e) => { tempURI = e.Request.Arguments ["URI"]; mre.Set (); return; }; Console.WriteLine ("Awaiting auto discovery service..."); if (mre.WaitOne (15000)) { sd.Dispose (); if (tempURI == null) { Console.Error.WriteLine ("Auto discovery failed with an error."); yield break; } Console.WriteLine ("AutoDiscovery url is {0}", tempURI); if (tempURI is string) { yield return new Uri ((string)tempURI); yield break; } string[] tempURIarr = tempURI as string[]; if (tempURIarr != null) { foreach (var tempURIitem in tempURIarr) { yield return new Uri (tempURIitem); } yield break; } } sd.Dispose (); Console.Error.WriteLine ("Auto discovery timed out."); yield break; }