public static ProcessContainer Create(string name, string path, string pathParams, Dictionary <string, dynamic> options = null) { var proc = new ProcessContainer(name, path, pathParams, options); servers.Add(proc); OnServerAdded?.Invoke(proc); return(proc); }
///////////////////////////////////////////////////////////////////// // CONTROL (LOOP) THREAD ///////////////////////////////////////////////////////////////////// private void ControlThread(object uc) { var udpClient = uc as UdpClient; var checkTimer = new Marzersoft.Timer(); while (!isDisposed) { Thread.Sleep(250); //read all waiting packets while (udpClient.Available > 0) { //read packet IPEndPoint senderAnnounceEndpoint = null; byte[] packetData = null; if (CaptureException(() => { packetData = udpClient.Receive(ref senderAnnounceEndpoint); })) { continue; } //deserialize packet ServerDescription packet = null; IPEndPoint senderEndPoint = null; if (CaptureException(() => { packet = packetData.Deserialize <ServerDescription>(); senderEndPoint = new IPEndPoint(senderAnnounceEndpoint.Address, packet.Port); })) { continue; } //process lock (activeServers) { ServerDescription knownServer = null; if (!activeServers.TryGetValue(senderEndPoint, out knownServer)) { activeServers[senderEndPoint] = knownServer = new ServerDescription(senderEndPoint, packet); OnServerAdded?.Invoke(this, knownServer); } else { CaptureException(() => { knownServer.Update(senderEndPoint, packet); }); } } } //periodically check server inactive state and update pings if (checkTimer.Seconds >= 1.0) { lock (activeServers) { //cull inactive servers var inactiveServers = activeServers .Where((kvp) => { return(kvp.Value.LastUpdated >= 10.0); }) .ToList(); foreach (var inactive in inactiveServers) { activeServers.Remove(inactive.Key); inactive.Value.Active = false; //invokes event } //update pings if (autoPing) { foreach (var active in activeServers) { if (active.Value.LastPinged >= 10.0) { active.Value.UpdatePing(); } } } } checkTimer.Reset(); } } //close listener CaptureException(() => { udpClient.Close(); }); }