2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
30 using System.Net.NetworkInformation;
31 using System.Net.Sockets;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Discovery;
35 using System.Threading;
38 namespace System.ServiceModel.Discovery.Udp
40 internal class UdpDuplexChannel : ChannelBase, IDuplexChannel
43 public UdpDuplexChannel (UdpChannelFactory factory, BindingContext context, EndpointAddress address, Uri via)
47 throw new ArgumentNullException ("factory");
49 throw new ArgumentNullException ("context");
51 throw new ArgumentNullException ("address");
53 binding_element = factory.Source;
54 RemoteAddress = address;
56 FillMessageEncoder (context);
59 public UdpDuplexChannel (UdpChannelListener listener)
62 binding_element = listener.Source;
63 LocalAddress = new EndpointAddress (listener.Uri);
64 FillMessageEncoder (listener.Context);
67 MessageEncoder message_encoder;
69 IPAddress multicast_address;
70 UdpTransportBindingElement binding_element;
73 public EndpointAddress LocalAddress { get; private set; }
75 public EndpointAddress RemoteAddress { get; private set; }
77 public Uri Via { get; private set; }
79 void FillMessageEncoder (BindingContext ctx)
81 var mbe = (MessageEncodingBindingElement) ctx.Binding.Elements.FirstOrDefault (be => be is MessageEncodingBindingElement);
83 mbe = new TextMessageEncodingBindingElement ();
84 message_encoder = mbe.CreateMessageEncoderFactory ().Encoder;
87 public void Send (Message message)
89 Send (message, DefaultSendTimeout);
92 static readonly Random rnd = new Random ();
94 UdpClient GetSenderClient (Message message)
96 if (RemoteAddress != null)
99 var rmp = message.Properties [RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
101 throw new ArgumentException ("This duplex channel from the channel listener cannot send messages without RemoteEndpointMessageProperty");
102 var cli = new UdpClient ();
103 cli.Connect (IPAddress.Parse (rmp.Address), rmp.Port);
107 public void Send (Message message, TimeSpan timeout)
109 if (State != CommunicationState.Opened)
110 throw new InvalidOperationException ("The UDP channel must be opened before sending a message.");
112 var cli = GetSenderClient (message);
114 SendCore (cli, message, timeout);
121 void SendCore (UdpClient cli, Message message, TimeSpan timeout)
123 var ms = new MemoryStream ();
124 message_encoder.WriteMessage (message, ms);
125 // It seems .NET sends the same Message a couple of times so that the receivers don't miss it. So, do the same hack.
126 for (int i = 0; i < 3; i++) {
127 // FIXME: use MaxAnnouncementDelay. It is fixed now.
128 Thread.Sleep (rnd.Next (50, 500));
129 cli.Send (ms.GetBuffer (), (int) ms.Length);
133 public bool WaitForMessage (TimeSpan timeout)
135 throw new NotImplementedException ();
138 public Message Receive ()
140 return Receive (DefaultReceiveTimeout);
143 public Message Receive (TimeSpan timeout)
146 if (!TryReceive (timeout, out msg))
147 throw new TimeoutException ();
151 public bool TryReceive (TimeSpan timeout, out Message msg)
153 DateTime start = DateTime.Now;
154 ThrowIfDisposedOrNotOpen ();
157 if (client == null) // could be invoked while being closed.
160 byte [] bytes = null;
161 IPEndPoint ip = new IPEndPoint (IPAddress.Any, 0);
162 ManualResetEvent wait = new ManualResetEvent (false);
163 var ar = client.BeginReceive (delegate (IAsyncResult result) {
165 UdpClient cli = (UdpClient) result.AsyncState;
167 bytes = cli.EndReceive (result, ref ip);
168 } catch (ObjectDisposedException) {
169 if (State == CommunicationState.Opened)
171 // Otherwise, called during shutdown. Ignore it.
178 if (!ar.IsCompleted && !wait.WaitOne (timeout))
180 if (bytes == null || bytes.Length == 0)
183 // Clients will send the same message many times, and this receiver has to
185 // FIXME: give maxSizeOfHeaders
186 msg = message_encoder.ReadMessage (new MemoryStream (bytes), int.MaxValue);
187 var id = msg.Headers.MessageId;
188 if (message_ids.Contains (id))
189 return TryReceive (timeout - (DateTime.Now - start), out msg);
191 message_ids.Enqueue (id);
192 if (message_ids.Count >= binding_element.TransportSettings.DuplicateMessageHistoryLength)
193 message_ids.Dequeue ();
195 msg.Properties.Add ("Via", LocalAddress.Uri);
196 msg.Properties.Add ("Encoder", message_encoder);
197 msg.Properties.Add (RemoteEndpointMessageProperty.Name, new RemoteEndpointMessageProperty (ip.Address.ToString (), ip.Port));
201 Queue<UniqueId> message_ids = new Queue<UniqueId> ();
203 protected override void OnAbort ()
205 OnClose (TimeSpan.Zero);
208 Action<TimeSpan> open_delegate, close_delegate;
210 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
212 if (close_delegate == null)
213 close_delegate = new Action<TimeSpan> (OnClose);
214 return close_delegate.BeginInvoke (timeout, callback, state);
217 protected override void OnEndClose (IAsyncResult result)
219 close_delegate.EndInvoke (result);
222 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
224 if (open_delegate == null)
225 open_delegate = new Action<TimeSpan> (OnOpen);
226 return open_delegate.BeginInvoke (timeout, callback, state);
229 protected override void OnEndOpen (IAsyncResult result)
231 open_delegate.EndInvoke (result);
234 protected override void OnClose (TimeSpan timeout)
236 if (client != null) {
237 if (multicast_address != null) {
238 client.DropMulticastGroup (multicast_address, LocalAddress.Uri.Port);
239 multicast_address = null;
246 protected override void OnOpen (TimeSpan timeout)
248 if (RemoteAddress != null) {
249 client = new UdpClient ();
250 var uri = Via ?? RemoteAddress.Uri;
251 client.Connect (uri.Host, uri.Port);
253 var ip = IPAddress.Parse (LocalAddress.Uri.Host);
254 bool isMulticast = NetworkInterface.GetAllNetworkInterfaces ().Any (nic => nic.SupportsMulticast && nic.GetIPProperties ().MulticastAddresses.Any (mca => mca.Address.Equals (ip)));
255 int port = LocalAddress.Uri.Port;
257 multicast_address = ip;
258 client = new UdpClient (new IPEndPoint (IPAddress.Any, port));
259 client.JoinMulticastGroup (ip, binding_element.TransportSettings.TimeToLive);
262 client = new UdpClient (new IPEndPoint (ip, port));
265 client.EnableBroadcast = true;
267 // FIXME: apply UdpTransportSetting here.
270 Func<TimeSpan,Message> receive_delegate;
272 public IAsyncResult BeginReceive (AsyncCallback callback, object state)
274 return BeginReceive (DefaultReceiveTimeout, callback, state);
277 public IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
279 if (receive_delegate == null)
280 receive_delegate = new Func<TimeSpan,Message> (Receive);
281 return receive_delegate.BeginInvoke (timeout, callback, state);
284 public Message EndReceive (IAsyncResult result)
286 return receive_delegate.EndInvoke (result);
289 delegate bool TryReceiveDelegate (TimeSpan timeout, out Message msg);
290 TryReceiveDelegate try_receive_delegate;
292 public IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
294 if (try_receive_delegate == null)
295 try_receive_delegate = new TryReceiveDelegate (TryReceive);
297 return try_receive_delegate.BeginInvoke (timeout, out dummy, callback, state);
300 public bool EndTryReceive (IAsyncResult result, out Message msg)
302 return try_receive_delegate.EndInvoke (out msg, result);
305 Func<TimeSpan,bool> wait_delegate;
307 public IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
309 if (wait_delegate == null)
310 wait_delegate = new Func<TimeSpan,bool> (WaitForMessage);
311 return wait_delegate.BeginInvoke (timeout, callback, state);
314 public bool EndWaitForMessage (IAsyncResult result)
316 return wait_delegate.EndInvoke (result);
319 Action<Message,TimeSpan> send_delegate;
321 public IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
323 return BeginSend (message, DefaultSendTimeout, callback, state);
326 public IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
328 if (send_delegate == null)
329 send_delegate = new Action<Message,TimeSpan> (Send);
330 return send_delegate.BeginInvoke (message, timeout, callback, state);
333 public void EndSend (IAsyncResult result)
335 send_delegate.EndInvoke (result);