Mono.Zeroconf – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | // |
2 | // RegisterService.cs |
||
3 | // |
||
4 | // Authors: |
||
5 | // Aaron Bockover <abockover@novell.com> |
||
6 | // |
||
7 | // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com) |
||
8 | // |
||
9 | // Permission is hereby granted, free of charge, to any person obtaining |
||
10 | // a copy of this software and associated documentation files (the |
||
11 | // "Software"), to deal in the Software without restriction, including |
||
12 | // without limitation the rights to use, copy, modify, merge, publish, |
||
13 | // distribute, sublicense, and/or sell copies of the Software, and to |
||
14 | // permit persons to whom the Software is furnished to do so, subject to |
||
15 | // the following conditions: |
||
16 | // |
||
17 | // The above copyright notice and this permission notice shall be |
||
18 | // included in all copies or substantial portions of the Software. |
||
19 | // |
||
20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
21 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
22 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
23 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||
24 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||
25 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||
26 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
27 | // |
||
28 | |||
29 | using System; |
||
30 | using System.Net; |
||
31 | using System.Threading; |
||
32 | using System.Runtime.InteropServices; |
||
2 | office | 33 | using System.Text; |
1 | office | 34 | |
35 | namespace Mono.Zeroconf.Providers.Bonjour |
||
36 | { |
||
37 | public sealed class RegisterService : Service, IRegisterService, IDisposable |
||
38 | { |
||
39 | private Thread thread; |
||
40 | private ServiceRef sd_ref; |
||
41 | private bool auto_rename = true; |
||
42 | |||
43 | private Native.DNSServiceRegisterReply register_reply_handler; |
||
44 | |||
45 | public event RegisterServiceEventHandler Response; |
||
2 | office | 46 | |
1 | office | 47 | public RegisterService() |
48 | { |
||
49 | SetupCallback(); |
||
50 | } |
||
51 | |||
52 | public RegisterService(string name, string replyDomain, string regtype) : base(name, replyDomain, regtype) |
||
53 | { |
||
54 | SetupCallback(); |
||
55 | } |
||
56 | |||
57 | private void SetupCallback() |
||
58 | { |
||
59 | register_reply_handler = new Native.DNSServiceRegisterReply(OnRegisterReply); |
||
60 | } |
||
61 | |||
62 | public void Register() |
||
63 | { |
||
64 | Register(true); |
||
65 | } |
||
66 | |||
67 | public void Register(bool @async) |
||
68 | { |
||
69 | if(thread != null) { |
||
70 | throw new InvalidOperationException("RegisterService registration already in process"); |
||
71 | } |
||
72 | |||
73 | if(@async) { |
||
74 | thread = new Thread(new ThreadStart(ThreadedRegister)); |
||
75 | thread.IsBackground = true; |
||
76 | thread.Start(); |
||
77 | } else { |
||
78 | ProcessRegister(); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | public void RegisterSync() |
||
83 | { |
||
84 | Register(false); |
||
85 | } |
||
86 | |||
87 | private void ThreadedRegister() |
||
88 | { |
||
89 | try { |
||
90 | ProcessRegister(); |
||
91 | } catch(ThreadAbortException) { |
||
92 | Thread.ResetAbort(); |
||
93 | } |
||
94 | |||
95 | thread = null; |
||
96 | } |
||
97 | |||
98 | public void ProcessRegister() |
||
99 | { |
||
100 | ushort txt_rec_length = 0; |
||
101 | byte [] txt_rec = null; |
||
102 | |||
103 | if(TxtRecord != null) { |
||
104 | txt_rec_length = ((TxtRecord)TxtRecord.BaseRecord).RawLength; |
||
105 | txt_rec = new byte[txt_rec_length]; |
||
106 | Marshal.Copy(((TxtRecord)TxtRecord.BaseRecord).RawBytes, txt_rec, 0, txt_rec_length); |
||
107 | } |
||
108 | |||
109 | ServiceError error = Native.DNSServiceRegister(out sd_ref, |
||
110 | auto_rename ? ServiceFlags.None : ServiceFlags.NoAutoRename, InterfaceIndex, |
||
2 | office | 111 | Encoding.UTF8.GetBytes(Name), RegType, ReplyDomain, HostTarget, (ushort)IPAddress.HostToNetworkOrder((short)port), txt_rec_length, txt_rec, |
1 | office | 112 | register_reply_handler, IntPtr.Zero); |
113 | |||
114 | if(error != ServiceError.NoError) { |
||
115 | throw new ServiceErrorException(error); |
||
116 | } |
||
117 | |||
118 | sd_ref.Process(); |
||
119 | } |
||
120 | |||
121 | public void Dispose() |
||
122 | { |
||
123 | if(thread != null) { |
||
124 | thread.Abort(); |
||
125 | thread = null; |
||
126 | } |
||
127 | |||
128 | sd_ref.Deallocate(); |
||
129 | } |
||
130 | |||
131 | private void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode, |
||
2 | office | 132 | IntPtr name, string regtype, string domain, IntPtr context) |
1 | office | 133 | { |
134 | RegisterServiceEventArgs args = new RegisterServiceEventArgs(); |
||
135 | |||
136 | args.Service = this; |
||
137 | args.IsRegistered = false; |
||
138 | args.ServiceError = (ServiceErrorCode)errorCode; |
||
139 | |||
140 | if(errorCode == ServiceError.NoError) { |
||
2 | office | 141 | Name = Native.Utf8toString(name); |
1 | office | 142 | RegType = regtype; |
143 | ReplyDomain = domain; |
||
144 | args.IsRegistered = true; |
||
145 | } |
||
146 | |||
147 | RegisterServiceEventHandler handler = Response; |
||
148 | if(handler != null) { |
||
149 | handler(this, args); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | public bool AutoRename { |
||
154 | get { return auto_rename; } |
||
155 | set { auto_rename = value; } |
||
156 | } |
||
157 | } |
||
158 | } |