public static void RemoveLocalService() { if (LocalService != null && LocalService.WasPublished) { InternetDiscoveryConnection.PushPublicSevice(LocalService); } LocalService = null; CallUpdateNotifiation(); }
internal static void RemovePublisService() { if (ServiceToUpdate != null) { ServiceToRemove = ServiceToUpdate; lock (Pending) { Pending.Add(PendingActions.Remove); Pending.Add(PendingActions.Pull); } ServiceToUpdate = null; CheckThread(); } UpdateTimer?.Change(0, ServiceUpdateIntervalSeconds); }
internal static void AddRemoteService(HostedService service) { if (service.IDKey == LocalService.IDKey) { return; } HostedService existing = KnownServices.Find((x) => x.IDKey == service.IDKey); if (existing != null) { KnownServices.Remove(existing); } KnownServices.Add(service); }
public static void RegisterLocalService(HostedService localService, bool publish) { localService.IsLocal = true; if (LocalService != null && LocalService.WasPublished) { InternetDiscoveryConnection.PushPublicSevice(LocalService); } LocalService = localService; if (publish) { InternetDiscoveryConnection.PushPublicSevice(localService); } CallUpdateNotifiation(); }
internal static void PushPublicSevice(HostedService service) { if (ServiceToUpdate != null) { ServiceToRemove = ServiceToUpdate; lock (Pending) Pending.Add(PendingActions.Remove); } service.WasPublished = true; ServiceToUpdate = service; lock (Pending) { Pending.Add(PendingActions.Push); Pending.Add(PendingActions.Pull); } CheckThread(); UpdateTimer?.Change(0, ServiceUpdateIntervalSeconds); }
protected void Connected(IAsyncResult result) { try { Client.EndConnect(result); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HostedService)); ServiceResult = serializer.ReadObject(Client.GetStream()) as HostedService; Client.Close(); lock (Client) { IsDone = true; IsError = ServiceResult == null; } } catch (Exception) { lock (Client) { IsDone = true; IsError = true; } } }
private static void CheckOnline() { if (CurrentWebClient == null) { CurrentWebClient = new WebClient(); } PendingActions action = PopPending(); while (action != PendingActions.Nothing) { switch (action) { case PendingActions.Pull: { DataContractJsonSerializer pullSerialzier = new DataContractJsonSerializer(typeof(HostedServicesList)); try { HostedServicesList list = pullSerialzier.ReadObject(CurrentWebClient.OpenRead(DefaultServiceURL + "action=get")) as HostedServicesList; if (list != null) { if (list.StructureVersion == 1) { foreach (var host in list.Services) { ServiceList.AddRemoteService(host); } } } } catch (Exception) { // ignore web exceptions, we'll get it on the next update I guess } ServiceList.CallUpdateNotifiation(); } break; case PendingActions.Push: try { DataContractJsonSerializer updateSerializer = new DataContractJsonSerializer(typeof(HostedService)); MemoryStream ms = new MemoryStream(); updateSerializer.WriteObject(ms, ServiceToUpdate); ms.Close(); byte[] responce = CurrentWebClient.UploadData(DefaultServiceURL + "?action=update", ms.ToArray()); ServiceToUpdate.GlobalAccessKey = Encoding.UTF8.GetString(responce); } catch (Exception) { // ignore web exceptions, we'll get it on the next update I guess } break; case PendingActions.Remove: if (ServiceToRemove == null) { break; } try { CurrentWebClient.UploadString(DefaultServiceURL + "?action=remove", ServiceToRemove.GlobalAccessKey); } catch (Exception) { // ignore web exceptions, it'll time out } lock (Pending) ServiceToRemove = null; break; case PendingActions.Nothing: default: // should never be hit break; } action = PopPending(); } WorkerThread = null; }