Skip to content

Commit

Permalink
add connect by service name to TcpSocketClient
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavisau committed Jan 10, 2016
1 parent 7bec44f commit 731e946
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Sockets/Sockets.Implementation.NET/TcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using Sockets.Plugin.Abstractions.Resources;
using PlatformSocketException = System.Net.Sockets.SocketException;
using PclSocketException = Sockets.Plugin.Abstractions.SocketException;

Expand Down Expand Up @@ -93,6 +94,20 @@ internal TcpSocketClient(TcpClient backingClient, int bufferSize)
}
}

/// <summary>
/// Establishes a TCP connection with the endpoint at the specified address/port pair.
/// </summary>
/// <param name="address">The address of the endpoint to connect to.</param>
/// <param name="service">The service name of the endpoint to connect to.</param>
/// <param name="secure">True to enable TLS on the socket.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
public Task ConnectAsync(string address, string service, bool secure = false,
CancellationToken cancellationToken = default(CancellationToken))
{
var port = ServiceNames.PortForTcpServiceName(service);
return ConnectAsync(address, port, secure, cancellationToken);
}

#region Secure Sockets Details

private bool ServerValidationCallback (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
Expand Down
15 changes: 14 additions & 1 deletion Sockets/Sockets.Implementation.WinRT/TcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,22 @@ internal TcpSocketClient(StreamSocket nativeSocket, int bufferSize)
/// <param name="secure">True to enable TLS on the socket.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
public Task ConnectAsync(string address, int port, bool secure = false, CancellationToken cancellationToken = default(CancellationToken))
{
var service = port.ToString();
return ConnectAsync(address, service, secure, cancellationToken);
}

/// <summary>
/// Establishes a TCP connection with the endpoint at the specified address/port pair.
/// </summary>
/// <param name="address">The address of the endpoint to connect to.</param>
/// <param name="service">The service of the endpoint to connect to.</param>
/// <param name="secure">True to enable TLS on the socket.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
public Task ConnectAsync(string address, string service, bool secure = false, CancellationToken cancellationToken = default(CancellationToken))
{
var hn = new HostName(address);
var sn = port.ToString();
var sn = service;
var spl = secure ? _secureSocketProtectionLevel : SocketProtectionLevel.PlainSocket;

return _backingStreamSocket
Expand Down
9 changes: 9 additions & 0 deletions Sockets/Sockets.Plugin.Abstractions/ITcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public interface ITcpSocketClient : IDisposable
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
Task ConnectAsync(string address, int port, bool secure = false, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Establishes a TCP connection with the endpoint at the specified address/port pair.
/// </summary>
/// <param name="address">The address of the endpoint to connect to.</param>
/// <param name="service">The service of the endpoint to connect to.</param>
/// <param name="secure">True to enable TLS on the socket.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
Task ConnectAsync(string address, string service, bool secure = false, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Disconnects from an endpoint previously connected to using <code>ConnectAsync</code>.
/// Should not be called on a <code>TcpSocketClient</code> that is not already connected.
Expand Down
12 changes: 12 additions & 0 deletions Sockets/Sockets.Plugin/TcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public TcpSocketClient(int bufferSize) : this()
throw new NotImplementedException(PCL.BaitWithoutSwitchMessage);
}

/// <summary>
/// Establishes a TCP connection with the endpoint at the specified address/port pair.
/// </summary>
/// <param name="address">The address of the endpoint to connect to.</param>
/// <param name="service">The service of the endpoint to connect to.</param>
/// <param name="secure">True to enable TLS on the socket.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
public Task ConnectAsync(string address, string service, bool secure = false, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException(PCL.BaitWithoutSwitchMessage);
}

/// <summary>
/// Disconnects from an endpoint previously connected to using <code>ConnectAsync</code>.
/// Should not be called on a <code>TcpSocketClient</code> that is not already connected.
Expand Down

0 comments on commit 731e946

Please sign in to comment.