Skip to content

Refactored to support netstandard2.0 and net4.5 #847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "HttpTwo"]
path = HttpTwo
url = git@github.com:Redth/HttpTwo.git
1 change: 0 additions & 1 deletion HttpTwo
Submodule HttpTwo deleted from 7ba7b0
4 changes: 1 addition & 3 deletions PushSharp.Amazon/AdmConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace PushSharp.Amazon
namespace PushSharp.Amazon
{
public class AdmConfiguration
{
Expand Down
11 changes: 4 additions & 7 deletions PushSharp.Amazon/AdmConnection.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Net;
using PushSharp.Core;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using PushSharp.Common;

namespace PushSharp.Amazon
{
Expand Down
5 changes: 2 additions & 3 deletions PushSharp.Amazon/AdmNotification.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using PushSharp.Core;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using PushSharp.Common;

namespace PushSharp.Amazon
{
Expand Down
3 changes: 1 addition & 2 deletions PushSharp.Amazon/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using PushSharp.Core;
using PushSharp.Common;

namespace PushSharp.Amazon
{
Expand Down
27 changes: 0 additions & 27 deletions PushSharp.Amazon/Properties/AssemblyInfo.cs

This file was deleted.

61 changes: 13 additions & 48 deletions PushSharp.Amazon/PushSharp.Amazon.csproj
Original file line number Diff line number Diff line change
@@ -1,56 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2468C63B-C964-4FC3-9B16-13DC17CF7D11}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>PushSharp.Amazon</RootNamespace>
<TargetFrameworks>netstandard2.0;net45;</TargetFrameworks>
<AssemblyName>PushSharp.Amazon</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\PushSharp-Signing.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<RootNamespace>PushSharp.Amazon</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Net.Http" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AdmConnection.cs" />
<Compile Include="AdmConfiguration.cs" />
<Compile Include="AdmNotification.cs" />
<Compile Include="Exceptions.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<ProjectReference Include="..\PushSharp.Core\PushSharp.Core.csproj">
<Project>{2B44A8DA-60BC-4577-A2D7-D9D53F164B2E}</Project>
<Name>PushSharp.Core</Name>
</ProjectReference>

<ItemGroup Condition="'$(TargetFramework)'=='net45'">
<PackageReference Include="System.Net.Http" Version="4.3.2" />
</ItemGroup>

<ItemGroup>
<None Include="packages.config" />
<ProjectReference Include="..\PushSharp.Common\PushSharp.Common.csproj" />
</ItemGroup>
</Project>

</Project>
4 changes: 0 additions & 4 deletions PushSharp.Amazon/packages.config

This file was deleted.

32 changes: 28 additions & 4 deletions PushSharp.Apple/ApnsConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -82,7 +83,7 @@ void Initialize (ApnsServerEnvironment serverEnvironment, X509Certificate2 certi

FeedbackIntervalMinutes = 10;
FeedbackTimeIsUTC = false;

AdditionalCertificates = new List<X509Certificate2> ();
AddLocalAndMachineCertificateStores = false;

Expand All @@ -100,7 +101,6 @@ void Initialize (ApnsServerEnvironment serverEnvironment, X509Certificate2 certi
InternalBatchFailureRetryCount = 1;
}


void CheckIsApnsCertificate ()
{
if (Certificate != null) {
Expand Down Expand Up @@ -136,6 +136,22 @@ public void OverrideFeedbackServer (string host, int port)
FeedbackPort = port;
}

public void SetProxy(string proxyHost, int proxyPort)
{
UseProxy = true;
ProxyHost = proxyHost;
ProxyPort = proxyPort;
ProxyCredentials = CredentialCache.DefaultNetworkCredentials;
}

public void SetProxy(string proxyHost, int proxyPort, string userName, string password, string domain)
{
UseProxy = true;
ProxyHost = proxyHost;
ProxyPort = proxyPort;
ProxyCredentials = new NetworkCredential(userName, password, domain);
}

public string Host { get; private set; }

public int Port { get; private set; }
Expand All @@ -144,6 +160,14 @@ public void OverrideFeedbackServer (string host, int port)

public int FeedbackPort { get; private set; }

public bool UseProxy { get; private set; }

public string ProxyHost { get; private set; }

public int ProxyPort { get; private set; }

public NetworkCredential ProxyCredentials { get; private set; }

public X509Certificate2 Certificate { get; private set; }

public List<X509Certificate2> AdditionalCertificates { get; private set; }
Expand Down Expand Up @@ -205,4 +229,4 @@ public enum ApnsServerEnvironment {
Production
}
}
}
}
91 changes: 67 additions & 24 deletions PushSharp.Apple/ApnsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
using System.Collections.Generic;
using System.Threading;
using System.Net;
using PushSharp.Core;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using PushSharp.Common;
using System.Diagnostics;

namespace PushSharp.Apple
{
Expand Down Expand Up @@ -68,7 +72,6 @@ public ApnsConnection (ApnsConfiguration configuration)
byte[] buffer = new byte[6];
int id;


SemaphoreSlim connectingSemaphore = new SemaphoreSlim (1);
SemaphoreSlim batchSendSemaphore = new SemaphoreSlim (1);
object notificationBatchQueueLock = new object ();
Expand Down Expand Up @@ -127,40 +130,56 @@ async Task SendBatch ()

Log.Info ("APNS-Client[{0}]: Sending Batch ID={1}, Count={2}", id, batchId, toSend.Count);

try {
try
{

var data = createBatch (toSend);

if (data != null && data.Length > 0) {
if (data != null && data.Length > 0)
{

for (var i = 0; i <= Configuration.InternalBatchFailureRetryCount; i++) {
for (var i = 0; i <= Configuration.InternalBatchFailureRetryCount; i++)
{

await connectingSemaphore.WaitAsync ();

try {
try
{
// See if we need to connect
if (!socketCanWrite () || i > 0)
await connect ();
} finally {
}
finally
{
connectingSemaphore.Release ();
}

try {
try
{
await networkStream.WriteAsync(data, 0, data.Length).ConfigureAwait(false);
break;
} catch (Exception ex) when (i != Configuration.InternalBatchFailureRetryCount) {
}
catch (Exception ex) when (i != Configuration.InternalBatchFailureRetryCount)
{
Log.Info("APNS-CLIENT[{0}]: Retrying Batch: Batch ID={1}, Error={2}", id, batchId, ex);
}
}

foreach (var n in toSend)
sent.Add (new SentNotification (n));
}

} catch (Exception ex) {
}
catch (ApnsConnectionException ex)
{
Log.Error ("APNS-CLIENT[{0}]: Send Batch Error: Batch ID={1}, Error={2}", id, batchId, ex);
foreach (var n in toSend)
n.CompleteFailed (new ApnsNotificationException (ApnsNotificationErrorStatusCode.ConnectionError, n.Notification, ex));
return;
}
catch (Exception ex) {
Log.Error ("APNS-CLIENT[{0}]: Send Batch Error: Batch ID={1}, Error={2}", id, batchId, ex);
foreach (var n in toSend)
n.CompleteFailed(new ApnsNotificationException(ApnsNotificationErrorStatusCode.ConnectionError, n.Notification, ex));
}

Log.Info ("APNS-Client[{0}]: Sent Batch, waiting for possible response...", id);
Expand Down Expand Up @@ -239,7 +258,7 @@ async Task Reader ()
sent.Clear ();
return;
}

// If we make it here, we did get data back, so we have errors

Log.Info ("APNS-Client[{0}]: Batch (ID={1}) completed with error response...", id, batchId);
Expand Down Expand Up @@ -321,25 +340,48 @@ async Task connect ()

Log.Info ("APNS-Client[{0}]: Connecting (Batch ID={1})", id, batchId);

client = new TcpClient ();
client = new TcpClient();

try {
await client.ConnectAsync (Configuration.Host, Configuration.Port).ConfigureAwait (false);
try
{
if (!Configuration.UseProxy)
{
await client.ConnectAsync (Configuration.Host, Configuration.Port).ConfigureAwait (false);
}
else
{
var proxyHelper = new ProxyHelper { ProxyConnectionExceptionCreator = (message) => new ApnsConnectionException(message) };
proxyHelper.BeforeConnect += () => Log.Info("APNS-Client[{0}]: Connecting Proxy (Batch ID={1})", id, batchId);
proxyHelper.AfterConnect += (status) => Log.Info("APNS-Client[{0}]: Proxy Connected (Batch ID={1}) : {2}", id, batchId, status);
await proxyHelper.Connect(client, Configuration.Host, Configuration.Port, Configuration.ProxyHost, Configuration.ProxyPort, Configuration.ProxyCredentials).ConfigureAwait(false);
}

//Set keep alive on the socket may help maintain our APNS connection
try {
client.Client.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
} catch {
try
{
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
}
catch
{
}

//Really not sure if this will work on MONO....
// This may help windows azure users
try {
SetSocketKeepAliveValues (client.Client, (int)Configuration.KeepAlivePeriod.TotalMilliseconds, (int)Configuration.KeepAliveRetryPeriod.TotalMilliseconds);
} catch {
try
{
SetSocketKeepAliveValues(client.Client, (int)Configuration.KeepAlivePeriod.TotalMilliseconds, (int)Configuration.KeepAliveRetryPeriod.TotalMilliseconds);
}
} catch (Exception ex) {
throw new ApnsConnectionException ("Failed to Connect, check your firewall settings!", ex);
catch
{
}
}
catch (ApnsConnectionException)
{
throw;
}
catch (Exception ex)
{
throw new ApnsConnectionException("Failed to Connect, check your firewall settings!", ex);
}

// We can configure skipping ssl all together, ie: if we want to hit a test server
Expand All @@ -354,7 +396,8 @@ async Task connect ()
(sender, targetHost, localCerts, remoteCert, acceptableIssuers) => certificate);

try {
stream.AuthenticateAsClient (Configuration.Host, certificates, System.Security.Authentication.SslProtocols.Tls, false);
var tls = System.Security.Authentication.SslProtocols.Tls | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12;
stream.AuthenticateAsClient (Configuration.Host, certificates, tls, false);
} catch (System.Security.Authentication.AuthenticationException ex) {
throw new ApnsConnectionException ("SSL Stream Failed to Authenticate as Client", ex);
}
Expand Down
Loading