Handle ENETDOWN error if defined.
[mono.git] / mcs / class / System.ServiceProcess / System.ServiceProcess / ServiceBase.cs
1 //
2 // System.ServiceProcess.ServiceBase.cs
3 //
4 // Authors:
5 //      Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
6 //      Duncan Mak (duncan@ximian.com)
7 //      Joerg Rosenkranz (joergr@voelcker.com)
8 //
9 // (C) 2003, Ximian Inc and Cesar Octavio Lopez Nataren.
10 // (C) 2005, Voelcker Informatik AG
11 //
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.ComponentModel;
35 using System.Globalization;
36 using System.Diagnostics;
37 using System.Runtime.InteropServices;
38
39 namespace System.ServiceProcess
40 {
41 #if ONLY_1_1
42         [Designer ("Microsoft.VisualStudio.Install.UserNTServiceDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, "System.ComponentModel.Design.IRootDesigner")]
43 #endif
44         [InstallerType (typeof (ServiceProcessInstaller))]
45         public class ServiceBase : Component
46         {
47                 internal delegate void RunServiceCallback (ServiceBase [] services);
48
49                 // This member is used for interoperation with mono-service
50                 internal static RunServiceCallback RunService;
51
52                 public const int MaxNameLength = 80;
53
54                 bool hasStarted;
55                 bool auto_log = true;
56                 bool can_handle_power_event;
57                 bool can_pause_and_continue;
58                 bool can_shutdown;
59                 bool can_stop = true;
60                 EventLog event_log;
61                 string service_name;
62 #if NET_2_0
63                 bool can_handle_session_change_event;
64 #endif
65
66                 public ServiceBase ()
67                 {
68                 }
69
70                 [DefaultValue (true)]
71                 [ServiceProcessDescription ("Whether the service should automatically write to the event log on common events such as Install and Start.")]
72                 public bool AutoLog {
73                         get { return auto_log; }
74                         set { auto_log = value; }
75                 }
76
77                 [DefaultValue (false)]
78                 public bool CanHandlePowerEvent {
79                         get { return can_handle_power_event; }
80                         set {
81                                 if (hasStarted)
82                                         throw new InvalidOperationException (
83                                                         Locale.GetText ("Cannot modify this property " +
84                                                                                         "after the service has started."));
85
86                                 can_handle_power_event = value;
87                         }
88                 }
89
90 #if NET_2_0
91                 [DefaultValue (false)]
92                 [MonoTODO]
93                 [ComVisible (false)]
94                 public bool CanHandleSessionChangeEvent {
95                         get { return can_handle_session_change_event; }
96                         set {
97                                 if (hasStarted)
98                                         throw new InvalidOperationException (
99                                                         Locale.GetText ("Cannot modify this property " +
100                                                                                         "after the service has started."));
101
102                                 can_handle_session_change_event = value;
103                         }
104                 }
105 #endif
106
107                 [DefaultValue (false)]
108                 public bool CanPauseAndContinue {
109                         get { return can_pause_and_continue; }
110                         set {
111                                 if (hasStarted)
112                                         throw new InvalidOperationException (
113                                                         Locale.GetText ("Cannot modify this property " +
114                                                                                         "after the service has started."));
115
116                                 can_pause_and_continue = value;
117                         }
118                 }
119
120                 [DefaultValue (false)]
121                 public bool CanShutdown {
122                         get { return can_shutdown; }
123                         set {
124                                 if (hasStarted)
125                                         throw new InvalidOperationException (
126                                                         Locale.GetText ("Cannot modify this property " +
127                                                                                         "after the service has started."));
128
129                                 can_shutdown = value;
130                         }
131                 }
132
133                 [DefaultValue (true)]
134                 public bool CanStop {
135                         get { return can_stop; }
136                         set {
137                                 if (hasStarted)
138                                         throw new InvalidOperationException (
139                                                         Locale.GetText ("Cannot modify this property " +
140                                                                                         "after the service has started."));
141
142                                 can_stop = value;
143                         }
144                 }
145
146                 [Browsable (false)]
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148                 public virtual EventLog EventLog {
149                         get {
150                                 if (event_log == null)
151                                         event_log = new EventLog ("Application", ".", service_name);
152                                 return event_log;
153                         }
154                 }
155
156 #if NET_2_0
157                 [MonoTODO]
158                 [ComVisible (false)]
159                 public int ExitCode {
160                         get { throw new NotImplementedException (); }
161                         set { throw new NotImplementedException (); }
162                 }
163
164                 [MonoTODO]
165                 [EditorBrowsable (EditorBrowsableState.Advanced)]
166                 protected IntPtr ServiceHandle {
167                         get { throw new NotImplementedException (); }
168                 }
169 #endif
170
171                 [ServiceProcessDescription ("The name by which the service is identified to the system.")]
172                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
173                 public string ServiceName {
174                         get { return service_name; }
175                         set {
176                                 if (hasStarted)
177                                         throw new InvalidOperationException (
178                                                         Locale.GetText ("Cannot modify this property " +
179                                                                                         "after the service has started."));
180
181                                 service_name = value;
182                         }
183                 }
184
185                 protected override void Dispose (bool disposing)
186                 {
187                 }
188
189                 protected virtual void OnStart (string [] args)
190                 {
191                 }
192
193                 protected virtual void OnStop ()
194                 {
195                 }
196
197                 protected virtual void OnContinue ()
198                 {
199                 }
200
201                 protected virtual void OnCustomCommand (int command)
202                 {
203                 }
204
205                 protected virtual void OnPause ()
206                 {
207                 }
208
209                 protected virtual bool OnPowerEvent (PowerBroadcastStatus powerStatus)
210                 {
211                         return true;
212                 }
213
214                 protected virtual void OnShutdown ()
215                 {
216                 }
217
218 #if NET_2_0
219                 protected virtual void OnSessionChange (SessionChangeDescription changeDescription)
220                 {
221                 }
222
223                 [ComVisible (false)]
224                 [MonoTODO]
225                 public void RequestAdditionalTime (int milliseconds)
226                 {
227                         throw new NotImplementedException ();
228                 }
229
230                 [ComVisible (false)]
231                 [EditorBrowsable (EditorBrowsableState.Never)]
232                 [MonoTODO]
233                 public void ServiceMainCallback (int argCount, IntPtr argPointer)
234                 {
235                         throw new NotImplementedException ();
236                 }
237
238                 [MonoTODO]
239                 public void Stop ()
240                 {
241                 }
242 #endif
243
244                 public static void Run (ServiceBase service)
245                 {
246                         Run (new ServiceBase [] { service });
247                 }
248
249                 public static void Run (ServiceBase [] services)
250                 {
251                         if (RunService != null)
252                                 RunService (services);
253                 }
254         }
255 }