/// <summary> /// Default constructor. /// </summary> /// <remarks> /// <note> /// Instances created with this constructor will only be able to /// start native Windows services. Use the constructor below to load information /// about the location and type of the service executables to be able to /// start other types of services. /// </note> /// </remarks> public ServiceControl() { this.id = Guid.Empty; this.isOpen = false; this.inbox = null; this.outbox = null; this.keyPrefix = null; this.setting = null; this.services = null; }
/// <summary> /// Readies the class to accept ServiceControl commands. /// </summary> /// <remarks> /// This must be called when the derived class is instantiated. /// </remarks> public void Open() { using (TimedLock.Lock(this)) { inbox = new SharedMemInbox(); inbox.Open(ServiceControl.ServiceMemPrefix + service.Name, ServiceControl.MaxMsgSize, new SharedMemInboxReceiveDelegate(OnReceive)); outbox = new SharedMemOutbox(ServiceControl.MaxMsgSize, ServiceControl.MaxWaitTime); } }
/// <summary> /// Releases and resources associated with the ServiceControl /// infrastructure. /// </summary> /// <remarks> /// This must be called when the derived class is stopped. /// </remarks> public void Close() { using (TimedLock.Lock(this)) { if (inbox != null) { inbox.Close(); inbox = null; } if (outbox != null) { outbox.Close(); outbox = null; } } }
public void SharedMemIO_Basic() { SharedMemOutbox outBox; ready = false; recvMsg = null; thread = new Thread(new ThreadStart(BasicThreadFunc)); thread.Start(); while (!ready) { Thread.Sleep(100); } outBox = new SharedMemOutbox(100, TimeSpan.FromSeconds(10)); outBox.Send("BasicIn", Encoding.UTF8.GetBytes("Hello World!")); outBox.Close(); thread.Join(); Assert.AreEqual("Hello World!", recvMsg); }
/// <summary> /// Releases any resources associated with the service control instance. /// </summary> public void Close() { id = Guid.Empty; isOpen = false; services = null; if (inbox != null) { inbox.Close(); inbox = null; } if (outbox != null) { outbox.Close(); outbox = null; } if (onReply != null) { onReply.Close(); onReply = null; } }
private SharedMemOutbox outbox; // Delivers the responses /// <summary> /// Default constructor. /// </summary> public ServiceBase() { this.service = (IService)this; this.inbox = null; this.outbox = null; }
/// <summary> /// Readies the service control instance for use. /// </summary> public void Open() { if (isOpen) { return; } // Load the configuration settings Config config = new Config(keyPrefix); string[] settings; int p, pEnd; string name, path, s, m; StartAs mode; settings = config.GetArray(setting); services = new ArrayList(settings.Length); for (int i = 0; i < settings.Length; i++) { s = settings[i]; try { p = 0; pEnd = s.IndexOf(';', p); if (pEnd == -1) { throw new Exception(); } name = s.Substring(0, pEnd - p).Trim(); p = pEnd + 1; if (string.IsNullOrWhiteSpace(name)) { throw new Exception(); } pEnd = s.IndexOf(';', p); if (pEnd == -1) { throw new Exception(); } path = s.Substring(p, pEnd - p).Trim(); p = pEnd + 1; m = s.Substring(p); switch (m.ToLowerInvariant()) { case "native": mode = StartAs.Native; break; case "form": mode = StartAs.Form; break; case "console": mode = StartAs.Console; break; default: throw new Exception(); } } catch { throw new FormatException(string.Format(null, "Invalid service configuration [{0}].", s)); } services.Add(new ServiceInfo(name, path, mode)); } // Initialize the shared memory and events necessary to communicate // with the services. id = Helper.NewGuid(); inbox = new SharedMemInbox(); inbox.Open(ControlMemPrefix + id.ToString(), ServiceControl.MaxMsgSize, new SharedMemInboxReceiveDelegate(OnReceive)); outbox = new SharedMemOutbox(MaxMsgSize, MaxWaitTime); onReply = new GlobalAutoResetEvent(null); isOpen = true; // Poll the services to find out which ones are already running. foreach (ServiceInfo info in services) { var state = this.GetStatus(info.Name); info.Started = state == ServiceState.Running || state == ServiceState.Starting; } }