2 // DuplexSessionChannelBase.cs
5 // Marcos Cobena (marcoscobena@gmail.com)
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
10 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.ServiceModel.Channels;
35 namespace System.ServiceModel.Channels
37 internal abstract class DuplexChannelBase : ChannelBase, IDuplexChannel
39 ChannelFactoryBase channel_factory_base;
40 ChannelListenerBase channel_listener_base;
41 EndpointAddress local_address;
42 EndpointAddress remote_address;
45 public DuplexChannelBase (ChannelFactoryBase factory, EndpointAddress remoteAddress, Uri via) : base (factory)
47 channel_factory_base = factory;
48 remote_address = remoteAddress;
53 public DuplexChannelBase (ChannelListenerBase listener) : base (listener)
55 channel_listener_base = listener;
59 public abstract EndpointAddress LocalAddress { get; }
61 public EndpointAddress RemoteAddress {
62 get { return remote_address; }
69 void SetupDelegates ()
71 send_handler = new AsyncSendHandler (Send);
72 receive_handler = new AsyncReceiveHandler (Receive);
73 wait_handler = new AsyncWaitForMessageHandler (WaitForMessage);
78 delegate void AsyncSendHandler (Message message, TimeSpan timeout);
79 AsyncSendHandler send_handler;
81 public virtual IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
83 return BeginSend (message, DefaultSendTimeout, callback, state);
86 public virtual IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
88 return send_handler.BeginInvoke (message, timeout, callback, state);
91 public virtual void EndSend (IAsyncResult result)
93 send_handler.EndInvoke (result);
96 public virtual void Send (Message message)
98 Send (message, this.DefaultSendTimeout);
101 public abstract void Send (Message message, TimeSpan timeout);
105 delegate Message AsyncReceiveHandler (TimeSpan timeout);
106 AsyncReceiveHandler receive_handler;
108 public virtual IAsyncResult BeginReceive (AsyncCallback callback, object state)
110 return BeginReceive (this.DefaultReceiveTimeout, callback, state);
113 public virtual IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
115 return receive_handler.BeginInvoke (timeout, callback, state);
118 public virtual Message EndReceive (IAsyncResult result)
120 return receive_handler.EndInvoke (result);
123 public virtual Message Receive ()
125 return Receive (this.DefaultReceiveTimeout);
128 public abstract Message Receive (TimeSpan timeout);
131 // FIXME: apply those "async to call sync" pattern too (but how?)
133 public abstract IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state);
135 public abstract bool EndTryReceive (IAsyncResult result, out Message message);
137 public virtual bool TryReceive (TimeSpan timeout, out Message message)
139 return EndTryReceive (BeginTryReceive (timeout, null, null), out message);
144 delegate bool AsyncWaitForMessageHandler (TimeSpan timeout);
145 AsyncWaitForMessageHandler wait_handler;
147 public virtual IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
149 return wait_handler.BeginInvoke (timeout, callback, state);
152 public virtual bool EndWaitForMessage (IAsyncResult result)
154 return wait_handler.EndInvoke (result);
157 public abstract bool WaitForMessage (TimeSpan timeout);