Commit 33554edc authored by Marc Gravell's avatar Marc Gravell

Completely untested, but I *think* this comes close to fixing the Connect API problem

parent 01817068
...@@ -299,9 +299,10 @@ public enum CompletionType ...@@ -299,9 +299,10 @@ public enum CompletionType
/// </summary> /// </summary>
Async = 2 Async = 2
} }
#if !CORE_CLR
internal class CompletionTypeHelper internal class CompletionTypeHelper
{ {
public static void RunWithCompletionType(Func<AsyncCallback, IAsyncResult> beginAsync, AsyncCallback callback, CompletionType completionType) public static void RunWithCompletionType(Func<AsyncCallback, IAsyncResult> beginAsync, AsyncCallback callback, CompletionType completionType)
{ {
AsyncCallback proxyCallback; AsyncCallback proxyCallback;
...@@ -343,6 +344,7 @@ public static void RunWithCompletionType(Func<AsyncCallback, IAsyncResult> begin ...@@ -343,6 +344,7 @@ public static void RunWithCompletionType(Func<AsyncCallback, IAsyncResult> begin
return; return;
} }
} }
#endif
#if VERBOSE #if VERBOSE
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
namespace StackExchange.Redis namespace StackExchange.Redis
{ {
...@@ -172,45 +173,56 @@ internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, C ...@@ -172,45 +173,56 @@ internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, C
this.ShouldForceConnectCompletionType(ref connectCompletionType); this.ShouldForceConnectCompletionType(ref connectCompletionType);
var formattedEndpoint = Format.ToString(endpoint); var formattedEndpoint = Format.ToString(endpoint);
var tuple = Tuple.Create(socket, callback);
if (endpoint is DnsEndPoint) if (endpoint is DnsEndPoint)
{ {
// A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state) // A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state)
DnsEndPoint dnsEndpoint = (DnsEndPoint)endpoint; DnsEndPoint dnsEndpoint = (DnsEndPoint)endpoint;
CompletionTypeHelper.RunWithCompletionType(
(cb) =>
{
multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
#if CORE_CLR #if CORE_CLR
throw new NotImplementedException("TODO CORE CLR"); multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
socket.ConnectAsync(dnsEndpoint.Host, dnsEndpoint.Port).ContinueWith(t =>
{
multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
EndConnectImpl(t, multiplexer, log, tuple);
multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
});
#else #else
return socket.BeginConnect(dnsEndpoint.Host, dnsEndpoint.Port, cb, Tuple.Create(socket, callback)); CompletionTypeHelper.RunWithCompletionType(
#endif (cb) => {
multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
return socket.BeginConnect(dnsEndpoint.Host, dnsEndpoint.Port, cb, tuple);
}, },
(ar) => (ar) => {
{ multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint); EndConnectImpl(ar, multiplexer, log, tuple);
EndConnectImpl(ar, multiplexer, log);
multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint); multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
}, },
connectCompletionType); connectCompletionType);
#endif
} }
else else
{ {
#if CORE_CLR
multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
socket.ConnectAsync(endpoint).ContinueWith(t =>
{
multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
EndConnectImpl(t, multiplexer, log, tuple);
});
#else
CompletionTypeHelper.RunWithCompletionType( CompletionTypeHelper.RunWithCompletionType(
(cb) => { (cb) => {
multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint); multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
#if CORE_CLR return socket.BeginConnect(endpoint, cb, tuple);
throw new NotImplementedException("TODO CORE CLR");
#else
return socket.BeginConnect(endpoint, cb, Tuple.Create(socket, callback));
#endif
}, },
(ar) => { (ar) => {
multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint); multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
EndConnectImpl(ar, multiplexer, log); EndConnectImpl(ar, multiplexer, log, tuple);
multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint); multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
}, },
connectCompletionType); connectCompletionType);
#endif
} }
} }
catch (NotImplementedException ex) catch (NotImplementedException ex)
...@@ -273,19 +285,17 @@ internal void Shutdown(SocketToken token) ...@@ -273,19 +285,17 @@ internal void Shutdown(SocketToken token)
Shutdown(token.Socket); Shutdown(token.Socket);
} }
private void EndConnectImpl(IAsyncResult ar, ConnectionMultiplexer multiplexer, TextWriter log) private void EndConnectImpl(IAsyncResult ar, ConnectionMultiplexer multiplexer, TextWriter log, Tuple<Socket, ISocketCallback> tuple)
{ {
Tuple<Socket, ISocketCallback> tuple = null;
try try
{ {
tuple = (Tuple<Socket, ISocketCallback>)ar.AsyncState;
bool ignoreConnect = false; bool ignoreConnect = false;
ShouldIgnoreConnect(tuple.Item2, ref ignoreConnect); ShouldIgnoreConnect(tuple.Item2, ref ignoreConnect);
if (ignoreConnect) return; if (ignoreConnect) return;
var socket = tuple.Item1; var socket = tuple.Item1;
var callback = tuple.Item2; var callback = tuple.Item2;
#if CORE_CLR #if CORE_CLR
throw new NotImplementedException("TODO CORE CLR"); multiplexer.Wait((Task)ar); // make it explode if invalid (note: already complete at this point)
#else #else
socket.EndConnect(ar); socket.EndConnect(ar);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment