2009-07-10 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / DuplexChannelBase.cs
1 // 
2 // DuplexSessionChannelBase.cs
3 // 
4 // Author:
5 //     Marcos Cobena (marcoscobena@gmail.com)
6 //      Atsushi Enomoto  <atsushi@ximian.com>
7 // 
8 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
9 // 
10 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.ServiceModel.Channels;
34
35 namespace System.ServiceModel.Channels
36 {
37         internal abstract class DuplexChannelBase : ChannelBase, IDuplexChannel
38         {
39                 ChannelFactoryBase channel_factory_base;
40                 ChannelListenerBase channel_listener_base;
41                 EndpointAddress local_address;
42                 EndpointAddress remote_address;
43                 Uri via;
44                 
45                 public DuplexChannelBase (ChannelFactoryBase factory, EndpointAddress remoteAddress, Uri via) : base (factory)
46                 {
47                         channel_factory_base = factory;
48                         remote_address = remoteAddress;
49                         this.via = via;
50                         SetupDelegates ();
51                 }
52                 
53                 public DuplexChannelBase (ChannelListenerBase listener) : base (listener)
54                 {
55                         channel_listener_base = listener;
56                         SetupDelegates ();
57                 }
58
59                 public abstract EndpointAddress LocalAddress { get; }
60                 
61                 public EndpointAddress RemoteAddress {
62                         get { return remote_address; }
63                 }
64
65                 public Uri Via {
66                         get { return via; }
67                 }
68
69                 void SetupDelegates ()
70                 {
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);
77                 }
78
79                 // Open
80                 Action<TimeSpan> open_handler;
81
82                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
83                 {
84                         return open_handler.BeginInvoke (timeout, callback, state);
85                 }
86
87                 protected override void OnEndOpen (IAsyncResult result)
88                 {
89                         open_handler.EndInvoke (result);
90                 }
91
92                 // Close
93                 Action<TimeSpan> close_handler;
94
95                 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
96                 {
97                         return close_handler.BeginInvoke (timeout, callback, state);
98                 }
99
100                 protected override void OnEndClose (IAsyncResult result)
101                 {
102                         close_handler.EndInvoke (result);
103                 }
104
105                 // Send
106
107                 delegate void AsyncSendHandler (Message message, TimeSpan timeout);
108                 AsyncSendHandler send_handler;
109
110                 public virtual IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
111                 {
112                         return BeginSend (message, DefaultSendTimeout, callback, state);
113                 }
114
115                 public virtual IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
116                 {
117                         return send_handler.BeginInvoke (message, timeout, callback, state);
118                 }
119
120                 public virtual void EndSend (IAsyncResult result)
121                 {
122                         send_handler.EndInvoke (result);
123                 }
124
125                 public virtual void Send (Message message)
126                 {
127                         Send (message, this.DefaultSendTimeout);
128                 }
129
130                 public abstract void Send (Message message, TimeSpan timeout);
131
132                 // Receive
133
134                 delegate Message AsyncReceiveHandler (TimeSpan timeout);
135                 AsyncReceiveHandler receive_handler;
136
137                 public virtual IAsyncResult BeginReceive (AsyncCallback callback, object state)
138                 {
139                         return BeginReceive (this.DefaultReceiveTimeout, callback, state);
140                 }
141
142                 public virtual IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
143                 {
144                         return receive_handler.BeginInvoke (timeout, callback, state);
145                 }
146
147                 public virtual Message EndReceive (IAsyncResult result)
148                 {
149                         return receive_handler.EndInvoke (result);
150                 }
151
152                 public virtual Message Receive ()
153                 {
154                         return Receive (this.DefaultReceiveTimeout);
155                 }
156
157                 public abstract Message Receive (TimeSpan timeout);
158
159                 // TryReceive
160
161                 delegate bool TryReceiveHandler (TimeSpan timeout, out Message msg);
162                 TryReceiveHandler try_receive_handler;
163
164                 public virtual IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
165                 {
166                         Message dummy;
167                         return try_receive_handler.BeginInvoke (timeout, out dummy, callback, state);
168                 }
169                 
170                 public virtual bool EndTryReceive (IAsyncResult result, out Message message)
171                 {
172                         return try_receive_handler.EndInvoke (out message, result);
173                 }
174                 
175                 public virtual bool TryReceive (TimeSpan timeout, out Message message)
176                 {
177                         return EndTryReceive (BeginTryReceive (timeout, null, null), out message);
178                 }
179
180                 // WaitForMessage
181
182                 delegate bool AsyncWaitForMessageHandler (TimeSpan timeout);
183                 AsyncWaitForMessageHandler wait_handler;
184
185                 public virtual IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
186                 {
187                         return wait_handler.BeginInvoke (timeout, callback, state);
188                 }
189                 
190                 public virtual bool EndWaitForMessage (IAsyncResult result)
191                 {
192                         return wait_handler.EndInvoke (result);
193                 }
194                 
195                 public abstract bool WaitForMessage (TimeSpan timeout);
196         }
197 }