2 // HandlerTransportBindingElement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Net.Security;
32 using System.Threading;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Description;
38 namespace MonoTests.System.ServiceModel.Channels
40 public delegate Message RequestSender (Message input);
41 public delegate Message RequestReceiver ();
42 public delegate void ReplyHandler (Message input);
44 public class HandlerTransportBindingElement : TransportBindingElement
48 ReplyHandler reply_handler;
49 RequestReceiver receiver;
51 public HandlerTransportBindingElement (RequestSender sender)
56 public HandlerTransportBindingElement (ReplyHandler handler, RequestReceiver receiver)
58 this.reply_handler = handler;
59 this.receiver = receiver;
62 public RequestSender RequestSender {
63 get { return sender; }
66 public ReplyHandler ReplyHandler {
67 get { return reply_handler; }
70 public RequestReceiver RequestReceiver {
71 get { return receiver; }
74 public override string Scheme {
75 get { return "stream"; }
78 public override BindingElement Clone ()
81 return new HandlerTransportBindingElement (sender);
83 return new HandlerTransportBindingElement (reply_handler, receiver);
86 public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
88 return typeof (TChannel) == typeof (IRequestChannel) ||
89 typeof (TChannel) == typeof (IRequestChannel);
92 public override bool CanBuildChannelListener<TChannel> (BindingContext context)
94 return typeof (TChannel) == typeof (IReplyChannel) ||
95 typeof (TChannel) == typeof (IInputChannel);
98 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (BindingContext context)
100 return new HandlerTransportChannelFactory<TChannel> (this);
103 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (BindingContext context)
106 return new HandlerTransportChannelListener<TChannel> (this, new Uri ("stream:dummy"));
110 public class HandlerTransportChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
112 HandlerTransportBindingElement source;
114 public HandlerTransportChannelFactory (HandlerTransportBindingElement source)
116 this.source = source;
119 public HandlerTransportBindingElement Source {
120 get { return source; }
123 protected override TChannel OnCreateChannel (EndpointAddress address, Uri via)
125 if (typeof (TChannel) == typeof (IRequestChannel))
126 return (TChannel) (object) new HandlerTransportRequestChannel ((HandlerTransportChannelFactory<IRequestChannel>) (object) this, address, via);
127 if (typeof (TChannel) == typeof (IOutputChannel))
128 return (TChannel) (object) new HandlerTransportOutputChannel ((HandlerTransportChannelFactory<IOutputChannel>) (object) this, address, via);
130 throw new NotSupportedException (String.Format ("Channel '{0}' is not supported.", typeof (TChannel)));
133 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
135 throw new NotSupportedException ();
138 protected override void OnEndOpen (IAsyncResult result)
140 throw new NotSupportedException ();
143 protected override void OnOpen (TimeSpan timeout)
149 public class HandlerTransportOutputChannel : OutputChannelBase
151 HandlerTransportChannelFactory<IOutputChannel> source;
152 EndpointAddress address;
155 public HandlerTransportOutputChannel (HandlerTransportChannelFactory<IOutputChannel> source, EndpointAddress address, Uri via)
158 this.source = source;
159 this.address = address;
163 public override EndpointAddress RemoteAddress {
164 get { return address; }
167 public override Uri Via {
171 public override void Send (Message input, TimeSpan timeout)
173 source.Source.RequestSender (input);
176 class OutputAsyncResult : IAsyncResult
180 bool completed = true;
182 public OutputAsyncResult (Message message, object state)
184 this.message = message;
188 public Message Message {
189 get { return message; }
192 public object AsyncState {
193 get { return state; }
196 public WaitHandle AsyncWaitHandle {
200 public bool CompletedSynchronously {
204 public bool IsCompleted {
205 get { return completed; }
206 internal set { completed = value; }
210 public override IAsyncResult BeginSend (Message input, TimeSpan timeout, AsyncCallback callback, object state)
212 // FIXME: timeout is not considered here.
213 return new OutputAsyncResult (input, state);
216 public override void EndSend (IAsyncResult result)
218 source.Source.RequestSender (((OutputAsyncResult) result).Message);
222 protected override void OnAbort ()
226 protected override void OnOpen (TimeSpan timeout)
228 // throw new NotImplementedException ("OnOpen");
231 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
233 throw new NotImplementedException ("OnBeginOpen");
236 protected override void OnEndOpen (IAsyncResult result)
238 throw new NotImplementedException ("OnEndOpen");
241 protected override void OnClose (TimeSpan timeout)
243 // throw new NotImplementedException ("OnClose");
246 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
248 throw new NotImplementedException ("OnBeginClose");
251 protected override void OnEndClose (IAsyncResult result)
253 throw new NotImplementedException ("OnEndClose");
257 public class HandlerTransportRequestChannel : RequestChannelBase
259 HandlerTransportChannelFactory<IRequestChannel> source;
260 EndpointAddress address;
263 public HandlerTransportRequestChannel (HandlerTransportChannelFactory<IRequestChannel> source, EndpointAddress address, Uri via)
266 this.source = source;
267 this.address = address;
271 public override EndpointAddress RemoteAddress {
272 get { return address; }
275 public override Uri Via {
279 public override Message Request (Message input, TimeSpan timeout)
281 return source.Source.RequestSender (input);
284 class RequestAsyncResult : IAsyncResult
288 bool completed = true;
290 public RequestAsyncResult (Message message, object state)
292 this.message = message;
296 public Message Message {
297 get { return message; }
300 public object AsyncState {
301 get { return state; }
304 public WaitHandle AsyncWaitHandle {
308 public bool CompletedSynchronously {
312 public bool IsCompleted {
313 get { return completed; }
314 internal set { completed = value; }
318 public override IAsyncResult BeginRequest (Message input, TimeSpan timeout, AsyncCallback callback, object state)
320 // FIXME: timeout is not considered here.
321 return new RequestAsyncResult (input, state);
324 public override Message EndRequest (IAsyncResult result)
326 return source.Source.RequestSender (((RequestAsyncResult) result).Message);
330 protected override void OnAbort ()
334 protected override void OnOpen (TimeSpan timeout)
336 // throw new NotImplementedException ("OnOpen");
339 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
341 throw new NotImplementedException ("OnBeginOpen");
344 protected override void OnEndOpen (IAsyncResult result)
346 throw new NotImplementedException ("OnEndOpen");
349 protected override void OnClose (TimeSpan timeout)
351 // throw new NotImplementedException ("OnClose");
354 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
356 throw new NotImplementedException ("OnBeginClose");
359 protected override void OnEndClose (IAsyncResult result)
361 throw new NotImplementedException ("OnEndClose");
365 public class HandlerTransportChannelListener<TChannel>
366 : ChannelListenerBase<TChannel>
367 where TChannel : class, IChannel
369 HandlerTransportBindingElement source;
372 public HandlerTransportChannelListener (HandlerTransportBindingElement source, Uri uri)
374 this.source = source;
378 public HandlerTransportBindingElement Source {
379 get { return source; }
383 public override Uri Uri {
387 protected override TChannel OnAcceptChannel (TimeSpan timeout)
389 if (typeof (TChannel) == typeof (IReplyChannel))
390 return (TChannel) (object) new HandlerTransportReplyChannel ((HandlerTransportChannelListener<IReplyChannel>) (object) this);
392 throw new NotSupportedException ();
395 protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
397 throw new NotImplementedException ("OnBeginAcceptChannel");
400 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
402 throw new NotImplementedException ("EndAcceptChannel");
405 protected override bool OnWaitForChannel (TimeSpan timeout)
410 protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
412 throw new NotImplementedException ("OnBeginWaitForChannel");
415 protected override bool OnEndWaitForChannel (IAsyncResult result)
417 throw new NotImplementedException ("EndWaitForChannel");
421 protected override void OnAbort ()
425 protected override void OnOpen (TimeSpan timeout)
427 // throw new NotImplementedException ("OnOpen");
430 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
432 throw new NotImplementedException ("OnBeginOpen");
435 protected override void OnEndOpen (IAsyncResult result)
437 throw new NotImplementedException ("EndOpen");
440 protected override void OnClose (TimeSpan timeout)
442 //throw new NotImplementedException ("Close");
445 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
447 throw new NotImplementedException ("OnBeginClose");
450 protected override void OnEndClose (IAsyncResult result)
452 throw new NotImplementedException ("OnEndClose");
456 public class HandlerTransportReplyChannel : ReplyChannelBase
458 EndpointAddress address;
459 HandlerTransportChannelListener<IReplyChannel> source;
461 public HandlerTransportReplyChannel (HandlerTransportChannelListener<IReplyChannel> source)
464 this.source = source;
465 address = new EndpointAddress (source.Uri);
468 public HandlerTransportChannelListener<IReplyChannel> Source {
469 get { return source; }
472 public override EndpointAddress LocalAddress {
473 get { return address; }
476 class ReceiveRequestAsyncResult : IAsyncResult
479 bool completed = true;
481 public ReceiveRequestAsyncResult (object state)
486 public object AsyncState {
487 get { return state; }
490 public WaitHandle AsyncWaitHandle {
494 public bool CompletedSynchronously {
498 public bool IsCompleted {
499 get { return completed; }
500 internal set { completed = value; }
504 public override RequestContext ReceiveRequest (TimeSpan timeout)
507 if (!TryReceiveRequest (timeout, out ret))
508 throw new Exception ();
512 public override IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
514 return new ReceiveRequestAsyncResult (state);
517 public override RequestContext EndReceiveRequest (IAsyncResult result)
519 //ReceiveRequestAsyncResult r =
520 // (ReceiveRequestAsyncResult) result;
521 return new HandlerRequestContext (this);
524 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext ret)
526 ret = new HandlerRequestContext (this);
530 public override IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
533 return new ReceiveRequestAsyncResult (state);
536 public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext ret)
539 //ReceiveRequestAsyncResult r =
540 // (ReceiveRequestAsyncResult) result;
541 return TryReceiveRequest (TimeSpan.FromSeconds (5), out ret);
544 public override bool WaitForRequest (TimeSpan timeout)
546 throw new NotImplementedException ();
549 public override IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
551 throw new NotImplementedException ("BeginWaitForRequest");
554 public override bool EndWaitForRequest (IAsyncResult result)
556 throw new NotImplementedException ("EndWaitForRequest");
560 protected override void OnAbort ()
564 protected override void OnOpen (TimeSpan timeout)
566 // throw new NotImplementedException ("OnOpen");
569 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
571 throw new NotImplementedException ("OnBeginOpen");
574 protected override void OnEndOpen (IAsyncResult result)
576 throw new NotImplementedException ("EndOpen");
579 protected override void OnClose (TimeSpan timeout)
581 //throw new NotImplementedException ("Close");
584 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
586 throw new NotImplementedException ("OnBeginClose");
589 protected override void OnEndClose (IAsyncResult result)
591 throw new NotImplementedException ("OnEndClose");
595 public class HandlerRequestContext : RequestContext
597 HandlerTransportReplyChannel source;
598 Message request_message;
600 public HandlerRequestContext (HandlerTransportReplyChannel source)
602 this.source = source;
603 if (source.Source.Source.RequestReceiver != null)
604 request_message = source.Source.Source.RequestReceiver ();
608 public override void Abort ()
612 public override void Close ()
616 public override void Close (TimeSpan timeout)
620 public override Message RequestMessage {
621 get { return request_message; }
624 public override void Reply (Message msg)
626 source.Source.Source.ReplyHandler (msg);
629 public override void Reply (Message msg, TimeSpan timeout)
631 source.Source.Source.ReplyHandler (msg);
634 public override IAsyncResult BeginReply (Message msg, AsyncCallback callback, object state)
636 throw new NotImplementedException ("BeginReply");
639 public override IAsyncResult BeginReply (Message msg, TimeSpan timeout, AsyncCallback callback, object state)
641 throw new NotImplementedException ("BeginReply");
644 public override void EndReply (IAsyncResult result)
646 throw new NotImplementedException ("EndReply");