/
AnnouncementService.cs
executable file
·223 lines (197 loc) · 8.84 KB
/
AnnouncementService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Author: Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2009,2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Discovery.Version11;
using System.ServiceModel.Discovery.VersionApril2005;
using System.ServiceModel.Discovery.VersionCD1;
using System.Threading;
namespace System.ServiceModel.Discovery
{
// To be usable as ServiceType, it must implement contract interfaces.
// However, there is no public interfaces, so they must be internal.
[ServiceBehavior (InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class AnnouncementService : IAnnouncementContract11, IAnnouncementContractApril2005, IAnnouncementContractCD1
{
public AnnouncementService ()
: this (0)
{
}
public AnnouncementService (int duplicateMessageHistoryLength)
{
}
public event EventHandler<AnnouncementEventArgs> OfflineAnnouncementReceived;
public event EventHandler<AnnouncementEventArgs> OnlineAnnouncementReceived;
Action<object,AnnouncementEventArgs> online, offline;
protected virtual IAsyncResult OnBeginOfflineAnnouncement (DiscoveryMessageSequence messageSequence, EndpointDiscoveryMetadata endpointDiscoveryMetadata, AsyncCallback callback, object state)
{
// FIXME: this is a workaround for (similar to) bug #633945.
switch (Environment.OSVersion.Platform) {
case PlatformID.Unix:
case PlatformID.MacOSX:
if (offline == null)
offline = new Action<object,AnnouncementEventArgs> (OfflineAnnouncementReceived ?? delegate {});
return offline.BeginInvoke (this, new AnnouncementEventArgs (endpointDiscoveryMetadata, messageSequence), callback, state);
default:
if (OfflineAnnouncementReceived != null)
OfflineAnnouncementReceived (this, new AnnouncementEventArgs (endpointDiscoveryMetadata, messageSequence));
var result = new TempAsyncResult (null, state);
if (callback != null)
callback (result);
return result;
}
}
protected virtual IAsyncResult OnBeginOnlineAnnouncement (DiscoveryMessageSequence messageSequence, EndpointDiscoveryMetadata endpointDiscoveryMetadata, AsyncCallback callback, object state)
{
// FIXME: this is a workaround for (similar to) bug #633945.
switch (Environment.OSVersion.Platform) {
case PlatformID.Unix:
case PlatformID.MacOSX:
if (online == null)
online = new Action<object,AnnouncementEventArgs> (OnlineAnnouncementReceived ?? delegate {});
return online.BeginInvoke (this, new AnnouncementEventArgs (endpointDiscoveryMetadata, messageSequence), callback, state);
default:
if (OnlineAnnouncementReceived != null)
OnlineAnnouncementReceived (this, new AnnouncementEventArgs (endpointDiscoveryMetadata, messageSequence));
var result = new TempAsyncResult (null, state);
if (callback != null)
callback (result);
return result;
}
}
protected virtual void OnEndOfflineAnnouncement (IAsyncResult result)
{
// FIXME: this is a workaround for (similar to) bug #633945.
switch (Environment.OSVersion.Platform) {
case PlatformID.Unix:
case PlatformID.MacOSX:
offline.EndInvoke (result);
break;
default:
// do nothing
break;
}
}
protected virtual void OnEndOnlineAnnouncement (IAsyncResult result)
{
// FIXME: this is a workaround for (similar to) bug #633945.
switch (Environment.OSVersion.Platform) {
case PlatformID.Unix:
case PlatformID.MacOSX:
online.EndInvoke (result);
break;
default:
// do nothing
break;
}
}
// BeginOnlineAnnouncement
IAsyncResult IAnnouncementContract11.BeginOnlineAnnouncement (MessageContracts11.OnlineAnnouncement msg, AsyncCallback callback, object state)
{
var msq = msg.MessageSequence != null ? msg.MessageSequence.ToDiscoveryMessageSequence () : null;
var edm = msg.EndpointDiscoveryMetadata != null ? msg.EndpointDiscoveryMetadata.ToEndpointDiscoveryMetadata () : null;
return OnBeginOnlineAnnouncement (msq, edm, callback, state);
}
IAsyncResult IAnnouncementContractApril2005.BeginOnlineAnnouncement (MessageContractsApril2005.OnlineAnnouncement msg, AsyncCallback callback, object state)
{
var msq = msg.MessageSequence != null ? msg.MessageSequence.ToDiscoveryMessageSequence () : null;
var edm = msg.EndpointDiscoveryMetadata != null ? msg.EndpointDiscoveryMetadata.ToEndpointDiscoveryMetadata () : null;
return OnBeginOnlineAnnouncement (msq, edm, callback, state);
}
IAsyncResult IAnnouncementContractCD1.BeginOnlineAnnouncement (MessageContractsCD1.OnlineAnnouncement msg, AsyncCallback callback, object state)
{
var msq = msg.MessageSequence != null ? msg.MessageSequence.ToDiscoveryMessageSequence () : null;
var edm = msg.EndpointDiscoveryMetadata != null ? msg.EndpointDiscoveryMetadata.ToEndpointDiscoveryMetadata () : null;
return OnBeginOnlineAnnouncement (msq, edm, callback, state);
}
// EndOnlineAnnouncement
void IAnnouncementContract11.EndOnlineAnnouncement (IAsyncResult result)
{
OnEndOnlineAnnouncement (result);
}
void IAnnouncementContractApril2005.EndOnlineAnnouncement (IAsyncResult result)
{
OnEndOnlineAnnouncement (result);
}
void IAnnouncementContractCD1.EndOnlineAnnouncement (IAsyncResult result)
{
OnEndOnlineAnnouncement (result);
}
// BeginOfflineAnnouncement
IAsyncResult IAnnouncementContract11.BeginOfflineAnnouncement (MessageContracts11.OfflineAnnouncement msg, AsyncCallback callback, object state)
{
var msq = msg.MessageSequence != null ? msg.MessageSequence.ToDiscoveryMessageSequence () : null;
var edm = msg.EndpointDiscoveryMetadata != null ? msg.EndpointDiscoveryMetadata.ToEndpointDiscoveryMetadata () : null;
return OnBeginOfflineAnnouncement (msq, edm, callback, state);
}
IAsyncResult IAnnouncementContractApril2005.BeginOfflineAnnouncement (MessageContractsApril2005.OfflineAnnouncement msg, AsyncCallback callback, object state)
{
var msq = msg.MessageSequence != null ? msg.MessageSequence.ToDiscoveryMessageSequence () : null;
var edm = msg.EndpointDiscoveryMetadata != null ? msg.EndpointDiscoveryMetadata.ToEndpointDiscoveryMetadata () : null;
return OnBeginOfflineAnnouncement (msq, edm, callback, state);
}
IAsyncResult IAnnouncementContractCD1.BeginOfflineAnnouncement (MessageContractsCD1.OfflineAnnouncement msg, AsyncCallback callback, object state)
{
var msq = msg.MessageSequence != null ? msg.MessageSequence.ToDiscoveryMessageSequence () : null;
var edm = msg.EndpointDiscoveryMetadata != null ? msg.EndpointDiscoveryMetadata.ToEndpointDiscoveryMetadata () : null;
return OnBeginOfflineAnnouncement (msq, edm, callback, state);
}
// EndOfflineAnnouncement
void IAnnouncementContract11.EndOfflineAnnouncement (IAsyncResult result)
{
OnEndOfflineAnnouncement (result);
}
void IAnnouncementContractApril2005.EndOfflineAnnouncement (IAsyncResult result)
{
OnEndOfflineAnnouncement (result);
}
void IAnnouncementContractCD1.EndOfflineAnnouncement (IAsyncResult result)
{
OnEndOfflineAnnouncement (result);
}
}
// FIXME: It is introduced for a workaround for (similar to) bug #633945. Remove this class and all of its usage once that bug gets fixed.
class TempAsyncResult : IAsyncResult
{
public TempAsyncResult (object returnValue, object state)
{
ReturnValue = returnValue;
AsyncState = state;
CompletedSynchronously = true;
IsCompleted = true;
AsyncWaitHandle = new ManualResetEvent (true);
}
public object ReturnValue { get; set; }
public object AsyncState { get; set; }
public bool CompletedSynchronously { get; set; }
public bool IsCompleted { get; set; }
public WaitHandle AsyncWaitHandle { get; set; }
}
}