2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / ChannelListenerBase_1.cs
1 //
2 // ChannelListenerBase.cs
3 //
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
5 //
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;
28 using System.Collections.Generic;
29 using System.ServiceModel;
30
31 namespace System.ServiceModel.Channels
32 {
33         internal abstract class InternalChannelListenerBase<TChannel>
34                 : ChannelListenerBase<TChannel>
35                 where TChannel : class, IChannel
36         {
37                 protected InternalChannelListenerBase ()
38                         : base ()
39                 {
40                 }
41
42                 protected InternalChannelListenerBase (IDefaultCommunicationTimeouts timeouts)
43                         : base (timeouts)
44                 {
45                 }
46
47                 Func<TimeSpan,TChannel> accept_channel_delegate;
48                 Func<TimeSpan,bool> wait_delegate;
49                 Action<TimeSpan> open_delegate, close_delegate;
50
51                 protected override IAsyncResult OnBeginAcceptChannel (
52                         TimeSpan timeout, AsyncCallback callback,
53                         object asyncState)
54                 {
55                         if (accept_channel_delegate == null)
56                                 accept_channel_delegate = new Func<TimeSpan,TChannel> (OnAcceptChannel);
57                         return accept_channel_delegate.BeginInvoke (timeout, callback, asyncState);
58                 }
59
60                 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
61                 {
62                         if (accept_channel_delegate == null)
63                                 throw new InvalidOperationException ("Async AcceptChannel operation has not started");
64                         return accept_channel_delegate.EndInvoke (result);
65                 }
66
67                 protected override IAsyncResult OnBeginWaitForChannel (
68                         TimeSpan timeout, AsyncCallback callback, object state)
69                 {
70                         if (wait_delegate == null)
71                                 wait_delegate = new Func<TimeSpan,bool> (OnWaitForChannel);
72                         return wait_delegate.BeginInvoke (timeout, callback, state);
73                 }
74
75                 protected override bool OnEndWaitForChannel (IAsyncResult result)
76                 {
77                         if (wait_delegate == null)
78                                 throw new InvalidOperationException ("Async WaitForChannel operation has not started");
79                         return wait_delegate.EndInvoke (result);
80                 }
81
82                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
83                         AsyncCallback callback, object state)
84                 {
85                         if (open_delegate == null)
86                                 open_delegate = new Action<TimeSpan> (OnOpen);
87                         return open_delegate.BeginInvoke (timeout, callback, state);
88                 }
89
90                 protected override void OnEndOpen (IAsyncResult result)
91                 {
92                         if (open_delegate == null)
93                                 throw new InvalidOperationException ("Async Open operation has not started");
94                         open_delegate.EndInvoke (result);
95                 }
96
97                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
98                         AsyncCallback callback, object state)
99                 {
100                         if (close_delegate == null)
101                                 close_delegate = new Action<TimeSpan> (OnClose);
102                         return close_delegate.BeginInvoke (timeout, callback, state);
103                 }
104
105                 protected override void OnEndClose (IAsyncResult result)
106                 {
107                         if (close_delegate == null)
108                                 throw new InvalidOperationException ("Async Close operation has not started");
109                         close_delegate.EndInvoke (result);
110                 }
111         }
112
113         public abstract class ChannelListenerBase<TChannel>
114                 : ChannelListenerBase, IChannelListener<TChannel>, 
115                 IChannelListener,  ICommunicationObject
116                 where TChannel : class, IChannel
117         {
118                 IDefaultCommunicationTimeouts timeouts;
119
120                 protected ChannelListenerBase ()
121                         : this (DefaultCommunicationTimeouts.Instance)
122                 {
123                 }
124
125                 protected ChannelListenerBase (
126                         IDefaultCommunicationTimeouts timeouts)
127                 {
128                         if (timeouts == null)
129                                 throw new ArgumentNullException ("timeouts");
130                         this.timeouts = timeouts;
131                 }
132
133                 public TChannel AcceptChannel ()
134                 {
135                         return AcceptChannel (timeouts.ReceiveTimeout);
136                 }
137
138                 public TChannel AcceptChannel (TimeSpan timeout)
139                 {
140                         ThrowIfDisposedOrNotOpen ();
141                         return OnAcceptChannel (timeout);
142                 }
143
144                 public IAsyncResult BeginAcceptChannel (
145                         AsyncCallback callback, object asyncState)
146                 {
147                         return BeginAcceptChannel (
148                                 timeouts.ReceiveTimeout, callback, asyncState);
149                 }
150
151                 public IAsyncResult BeginAcceptChannel (TimeSpan timeout,
152                         AsyncCallback callback, object asyncState)
153                 {
154                         return OnBeginAcceptChannel (timeout, callback, asyncState);
155                 }
156
157                 public TChannel EndAcceptChannel (IAsyncResult result)
158                 {
159                         return OnEndAcceptChannel (result);
160                 }
161
162                 protected abstract TChannel OnAcceptChannel (TimeSpan timeout);
163
164                 protected abstract IAsyncResult OnBeginAcceptChannel (TimeSpan timeout,
165                         AsyncCallback callback, object asyncState);
166
167                 protected abstract TChannel OnEndAcceptChannel (IAsyncResult result);
168         }
169 }