提交 77d12442 编写于 作者: A Atsushi Eno 提交者: Andrew Jorgensen

Rework TCP listening model, and fix AcceptTcpClient() to not drop timed out...

Rework TCP listening model, and fix AcceptTcpClient() to not drop timed out requests that actually steals next requests in the air.
上级 3ad4bb8e
......@@ -3,9 +3,30 @@
//
// Author:
// Marcos Cobena (marcoscobena@gmail.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
// Copyright 2009-2010 Novell, Inc (http://www.novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
......@@ -25,6 +46,7 @@ namespace System.ServiceModel.Channels
BindingContext context;
TcpChannelInfo info;
TcpListener tcp_listener;
Thread tcp_acceptor_thread;
public TcpChannelListener (TcpTransportBindingElement source, BindingContext context)
: base (context)
......@@ -46,8 +68,9 @@ namespace System.ServiceModel.Channels
info = new TcpChannelInfo (source, MessageEncoder, quotas);
}
List<ManualResetEvent> accept_handles = new List<ManualResetEvent> ();
List<TChannel> accepted_channels = new List<TChannel> ();
SynchronizedCollection<ManualResetEvent> accept_handles = new SynchronizedCollection<ManualResetEvent> ();
Queue<TcpClient> accepted_clients = new Queue<TcpClient> ();
SynchronizedCollection<TChannel> accepted_channels = new SynchronizedCollection<TChannel> ();
protected override TChannel OnAcceptChannel (TimeSpan timeout)
{
......@@ -89,32 +112,28 @@ namespace System.ServiceModel.Channels
{
DateTime start = DateTime.Now;
TcpClient client = null;
if (tcp_listener.Pending ()) {
client = tcp_listener.AcceptTcpClient ();
} else {
TcpClient client = accepted_clients.Count == 0 ? null : accepted_clients.Dequeue ();
if (client == null) {
var wait = new ManualResetEvent (false);
tcp_listener.BeginAcceptTcpClient (delegate (IAsyncResult result) {
client = tcp_listener.EndAcceptTcpClient (result);
wait.Set ();
accept_handles.Add (wait);
if (!wait.WaitOne (timeout)) {
accept_handles.Remove (wait);
}, null);
if (State == CommunicationState.Closing)
return null;
accept_handles.Add (wait);
wait.WaitOne (timeout);
}
accept_handles.Remove (wait);
// recurse with new timeout, or return null if it's either being closed or timed out.
timeout -= (DateTime.Now - start);
return State == CommunicationState.Opened && timeout > TimeSpan.Zero ? AcceptTcpClient (timeout) : null;
}
// This may be optional though ...
if (client != null) {
foreach (var ch in accepted_channels) {
var dch = ch as TcpDuplexSessionChannel;
if (dch == null || dch.TcpClient == null && !dch.TcpClient.Connected)
continue;
if (((IPEndPoint) dch.TcpClient.Client.RemoteEndPoint).Equals (client.Client.RemoteEndPoint))
// ... then it should be handled in another BeginTryReceive/EndTryReceive loop in ChannelDispatcher.
return AcceptTcpClient (timeout - (DateTime.Now - start));
}
// There might be bettwe way to exclude those TCP clients though ...
foreach (var ch in accepted_channels) {
var dch = ch as TcpDuplexSessionChannel;
if (dch == null || dch.TcpClient == null && !dch.TcpClient.Connected)
continue;
if (((IPEndPoint) dch.TcpClient.Client.RemoteEndPoint).Equals (client.Client.RemoteEndPoint))
// ... then it should be handled in another BeginTryReceive/EndTryReceive loop in ChannelDispatcher.
return AcceptTcpClient (timeout - (DateTime.Now - start));
}
return client;
......@@ -132,25 +151,28 @@ namespace System.ServiceModel.Channels
{
if (State == CommunicationState.Closed)
return;
ProcessClose ();
ProcessClose (TimeSpan.Zero);
}
protected override void OnClose (TimeSpan timeout)
{
if (State == CommunicationState.Closed)
return;
ProcessClose ();
ProcessClose (timeout);
}
void ProcessClose ()
void ProcessClose (TimeSpan timeout)
{
if (tcp_listener == null)
throw new InvalidOperationException ("Current state is " + State);
lock (accept_handles) {
foreach (var wait in accept_handles)
wait.Set ();
if (tcp_acceptor_thread != null) {
tcp_acceptor_thread.Abort ();
tcp_acceptor_thread.Join (timeout);
tcp_acceptor_thread = null;
}
tcp_listener.Stop ();
var l = new List<ManualResetEvent> (accept_handles);
foreach (var wait in l) // those handles will disappear from accepted_handles
wait.Set ();
tcp_listener = null;
}
......@@ -163,7 +185,26 @@ namespace System.ServiceModel.Channels
int explicitPort = Uri.Port;
tcp_listener = new TcpListener (entry.AddressList [0], explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
tcp_acceptor_thread = new Thread (new ThreadStart (TcpAcceptorLoop));
tcp_acceptor_thread.Start ();
}
void TcpAcceptorLoop ()
{
tcp_listener.Start ();
try {
while (State == CommunicationState.Opened) {
// With async operation works with aborting the thread.
var cli = tcp_listener.EndAcceptTcpClient (tcp_listener.BeginAcceptTcpClient (null, null));
if (cli != null) {
accepted_clients.Enqueue (cli);
if (accept_handles.Count > 0)
accept_handles [0].Set ();
}
}
} finally {
tcp_listener.Stop ();
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册