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 open_handler = new Action<TimeSpan> (Open);
72 close_handler = new Action<TimeSpan> (Close);
73 send_handler = new AsyncSendHandler (Send);
74 receive_handler = new AsyncReceiveHandler (Receive);
75 wait_handler = new AsyncWaitForMessageHandler (WaitForMessage);
76 try_receive_handler = new TryReceiveHandler (TryReceive);
80 Action<TimeSpan> open_handler;
82 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
84 return open_handler.BeginInvoke (timeout, callback, state);
87 protected override void OnEndOpen (IAsyncResult result)
89 open_handler.EndInvoke (result);
93 Action<TimeSpan> close_handler;
95 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
97 return close_handler.BeginInvoke (timeout, callback, state);
100 protected override void OnEndClose (IAsyncResult result)
102 close_handler.EndInvoke (result);
107 delegate void AsyncSendHandler (Message message, TimeSpan timeout);
108 AsyncSendHandler send_handler;
110 public virtual IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
112 return BeginSend (message, DefaultSendTimeout, callback, state);
115 public virtual IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
117 return send_handler.BeginInvoke (message, timeout, callback, state);
120 public virtual void EndSend (IAsyncResult result)
122 send_handler.EndInvoke (result);
125 public virtual void Send (Message message)
127 Send (message, this.DefaultSendTimeout);
130 public abstract void Send (Message message, TimeSpan timeout);
134 delegate Message AsyncReceiveHandler (TimeSpan timeout);
135 AsyncReceiveHandler receive_handler;
137 public virtual IAsyncResult BeginReceive (AsyncCallback callback, object state)
139 return BeginReceive (this.DefaultReceiveTimeout, callback, state);
142 public virtual IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
144 return receive_handler.BeginInvoke (timeout, callback, state);
147 public virtual Message EndReceive (IAsyncResult result)
149 return receive_handler.EndInvoke (result);
152 public virtual Message Receive ()
154 return Receive (this.DefaultReceiveTimeout);
157 public abstract Message Receive (TimeSpan timeout);
161 delegate bool TryReceiveHandler (TimeSpan timeout, out Message msg);
162 TryReceiveHandler try_receive_handler;
164 public virtual IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
167 return try_receive_handler.BeginInvoke (timeout, out dummy, callback, state);
170 public virtual bool EndTryReceive (IAsyncResult result, out Message message)
172 return try_receive_handler.EndInvoke (out message, result);
175 public virtual bool TryReceive (TimeSpan timeout, out Message message)
177 return EndTryReceive (BeginTryReceive (timeout, null, null), out message);
182 delegate bool AsyncWaitForMessageHandler (TimeSpan timeout);
183 AsyncWaitForMessageHandler wait_handler;
185 public virtual IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
187 return wait_handler.BeginInvoke (timeout, callback, state);
190 public virtual bool EndWaitForMessage (IAsyncResult result)
192 return wait_handler.EndInvoke (result);
195 public abstract bool WaitForMessage (TimeSpan timeout);