-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathApnsConnection.cs
519 lines (401 loc) · 19.8 KB
/
ApnsConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using System;
using System.IO;
using System.Net.Sockets;
using System.Net.Security;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using PushSharp.Core;
using System.Diagnostics;
namespace PushSharp.Apple
{
public class ApnsConnection
{
static int ID = 0;
public ApnsConnection (ApnsConfiguration configuration)
{
id = ++ID;
if (id >= int.MaxValue)
ID = 0;
Configuration = configuration;
certificate = Configuration.Certificate;
certificates = new X509CertificateCollection ();
// Add local/machine certificate stores to our collection if requested
if (Configuration.AddLocalAndMachineCertificateStores) {
var store = new X509Store (StoreLocation.LocalMachine);
certificates.AddRange (store.Certificates);
store = new X509Store (StoreLocation.CurrentUser);
certificates.AddRange (store.Certificates);
}
// Add optionally specified additional certs into our collection
if (Configuration.AdditionalCertificates != null) {
foreach (var addlCert in Configuration.AdditionalCertificates)
certificates.Add (addlCert);
}
// Finally, add the main private cert for authenticating to our collection
if (certificate != null)
certificates.Add (certificate);
timerBatchWait = new Timer (new TimerCallback (async state => {
await batchSendSemaphore.WaitAsync ();
try {
await SendBatch ().ConfigureAwait (false);
} finally {
batchSendSemaphore.Release ();
}
}), null, Timeout.Infinite, Timeout.Infinite);
}
public ApnsConfiguration Configuration { get; private set; }
X509CertificateCollection certificates;
X509Certificate2 certificate;
TcpClient client;
SslStream stream;
Stream networkStream;
byte[] buffer = new byte[6];
int id;
SemaphoreSlim connectingSemaphore = new SemaphoreSlim (1);
SemaphoreSlim batchSendSemaphore = new SemaphoreSlim (1);
object notificationBatchQueueLock = new object ();
//readonly object connectingLock = new object ();
Queue<CompletableApnsNotification> notifications = new Queue<CompletableApnsNotification> ();
List<SentNotification> sent = new List<SentNotification> ();
Timer timerBatchWait;
public void Send (CompletableApnsNotification notification)
{
lock (notificationBatchQueueLock) {
notifications.Enqueue (notification);
if (notifications.Count >= Configuration.InternalBatchSize) {
// Make the timer fire immediately and send a batch off
timerBatchWait.Change (0, Timeout.Infinite);
return;
}
// Restart the timer to wait for more notifications to be batched
// This timer will keep getting 'restarted' before firing as long as notifications
// are queued before the timer's due time
// if the timer is actually called, it means no more notifications were queued,
// so we should flush out the queue instead of waiting for more to be batched as they
// might not ever come and we don't want to leave anything stranded in the queue
timerBatchWait.Change (Configuration.InternalBatchingWaitPeriod, Timeout.InfiniteTimeSpan);
}
}
long batchId = 0;
async Task SendBatch ()
{
batchId++;
if (batchId >= long.MaxValue)
batchId = 1;
// Pause the timer
timerBatchWait.Change (Timeout.Infinite, Timeout.Infinite);
if (notifications.Count <= 0)
return;
// Let's store the batch items to send internally
var toSend = new List<CompletableApnsNotification> ();
while (notifications.Count > 0 && toSend.Count < Configuration.InternalBatchSize) {
var n = notifications.Dequeue ();
toSend.Add (n);
}
Log.Info ("APNS-Client[{0}]: Sending Batch ID={1}, Count={2}", id, batchId, toSend.Count);
try
{
var data = createBatch (toSend);
if (data != null && data.Length > 0)
{
for (var i = 0; i <= Configuration.InternalBatchFailureRetryCount; i++)
{
await connectingSemaphore.WaitAsync ();
try
{
// See if we need to connect
if (!socketCanWrite () || i > 0)
await connect ();
}
finally
{
connectingSemaphore.Release ();
}
try
{
await networkStream.WriteAsync(data, 0, data.Length).ConfigureAwait(false);
break;
}
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 (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);
try {
await Reader ();
} catch (Exception ex) {
Log.Error ("APNS-Client[{0}]: Reader Exception: {1}", id, ex);
}
Log.Info ("APNS-Client[{0}]: Done Reading for Batch ID={1}, reseting batch timer...", id, batchId);
// Restart the timer for the next batch
timerBatchWait.Change (Configuration.InternalBatchingWaitPeriod, Timeout.InfiniteTimeSpan);
}
byte[] createBatch (List<CompletableApnsNotification> toSend)
{
if (toSend == null || toSend.Count <= 0)
return null;
var batchData = new List<byte> ();
// Add all the frame data
foreach (var n in toSend)
batchData.AddRange (n.Notification.ToBytes ());
return batchData.ToArray ();
}
async Task Reader ()
{
var readCancelToken = new CancellationTokenSource ();
// We are going to read from the stream, but the stream *may* not ever have any data for us to
// read (in the case that all the messages sent successfully, apple will send us nothing
// So, let's make our read timeout after a reasonable amount of time to wait for apple to tell
// us of any errors that happened.
readCancelToken.CancelAfter (750);
int len = -1;
while (!readCancelToken.IsCancellationRequested) {
// See if there's data to read
if (client.Client.Available > 0) {
Log.Info ("APNS-Client[{0}]: Data Available...", id);
len = await networkStream.ReadAsync (buffer, 0, buffer.Length).ConfigureAwait (false);
Log.Info ("APNS-Client[{0}]: Finished Read.", id);
break;
}
// Let's not tie up too much CPU waiting...
await Task.Delay (50).ConfigureAwait (false);
}
Log.Info ("APNS-Client[{0}]: Received {1} bytes response...", id, len);
// If we got no data back, and we didn't end up canceling, the connection must have closed
if (len == 0) {
Log.Info ("APNS-Client[{0}]: Server Closed Connection...", id);
// Connection was closed
disconnect ();
return;
} else if (len < 0) { //If we timed out waiting, but got no data to read, everything must be ok!
Log.Info ("APNS-Client[{0}]: Batch (ID={1}) completed with no error response...", id, batchId);
//Everything was ok, let's assume all 'sent' succeeded
foreach (var s in sent)
s.Notification.CompleteSuccessfully ();
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);
// If we made it here, we did receive some data, so let's parse the error
var status = buffer [1];
var identifier = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, 2));
// Let's handle the failure
//Get the index of our failed notification (by identifier)
var failedIndex = sent.FindIndex (n => n.Identifier == identifier);
// If we didn't find an index for the failed notification, something is wrong
// Let's just return
if (failedIndex < 0)
return;
// Get all the notifications before the failed one and mark them as sent!
if (failedIndex > 0) {
// Get all the notifications sent before the one that failed
// We can assume that these all succeeded
var successful = sent.GetRange (0, failedIndex); //TODO: Should it be failedIndex - 1?
// Complete all the successfully sent notifications
foreach (var s in successful)
s.Notification.CompleteSuccessfully ();
// Remove all the successful notifications from the sent list
// This should mean the failed notification is now at index 0
sent.RemoveRange (0, failedIndex);
}
//Get the failed notification itself
var failedNotification = sent [0];
//Fail and remove the failed index from the list
Log.Info ("APNS-Client[{0}]: Failing Notification {1}", id, failedNotification.Identifier);
failedNotification.Notification.CompleteFailed (
new ApnsNotificationException (status, failedNotification.Notification.Notification));
// Now remove the failed notification from the sent list
sent.RemoveAt (0);
// The remaining items in the list were sent after the failed notification
// we can assume these were ignored by apple so we need to send them again
// Requeue the remaining notifications
foreach (var s in sent)
notifications.Enqueue (s.Notification);
// Clear our sent list
sent.Clear ();
// Apple will close our connection after this anyway
disconnect ();
}
bool socketCanWrite ()
{
if (client == null)
return false;
if (networkStream == null || !networkStream.CanWrite)
return false;
if (!client.Client.Connected)
return false;
var p = client.Client.Poll (1000, SelectMode.SelectWrite);
Log.Info ("APNS-Client[{0}]: Can Write? {1}", id, p);
return p;
}
async Task connect ()
{
if (client != null)
disconnect ();
Log.Info ("APNS-Client[{0}]: Connecting (Batch ID={1})", id, batchId);
client = new TcpClient();
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
{
}
//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
{
}
}
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
if (Configuration.SkipSsl) {
networkStream = client.GetStream ();
} else {
// Create our ssl stream
stream = new SslStream (client.GetStream (),
false,
ValidateRemoteCertificate,
(sender, targetHost, localCerts, remoteCert, acceptableIssuers) => certificate);
try {
stream.AuthenticateAsClient (Configuration.Host, certificates, System.Security.Authentication.SslProtocols.Tls, false);
} catch (System.Security.Authentication.AuthenticationException ex) {
throw new ApnsConnectionException ("SSL Stream Failed to Authenticate as Client", ex);
}
if (!stream.IsMutuallyAuthenticated)
throw new ApnsConnectionException ("SSL Stream Failed to Authenticate", null);
if (!stream.CanWrite)
throw new ApnsConnectionException ("SSL Stream is not Writable", null);
networkStream = stream;
}
Log.Info ("APNS-Client[{0}]: Connected (Batch ID={1})", id, batchId);
}
void disconnect ()
{
Log.Info ("APNS-Client[{0}]: Disconnecting (Batch ID={1})", id, batchId);
//We now expect apple to close the connection on us anyway, so let's try and close things
// up here as well to get a head start
//Hopefully this way we have less messages written to the stream that we have to requeue
try { stream.Close (); } catch { }
try { stream.Dispose (); } catch { }
try { networkStream.Close (); } catch { }
try { networkStream.Dispose (); } catch { }
try { client.Client.Shutdown (SocketShutdown.Both); } catch { }
try { client.Client.Dispose (); } catch { }
try { client.Close (); } catch { }
client = null;
networkStream = null;
stream = null;
Log.Info ("APNS-Client[{0}]: Disconnected (Batch ID={1})", id, batchId);
}
bool ValidateRemoteCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
{
if (Configuration.ValidateServerCertificate)
return policyErrors == SslPolicyErrors.None;
return true;
}
public class SentNotification
{
public SentNotification (CompletableApnsNotification notification)
{
this.Notification = notification;
this.SentAt = DateTime.UtcNow;
this.Identifier = notification.Notification.Identifier;
}
public CompletableApnsNotification Notification { get; set; }
public DateTime SentAt { get; set; }
public int Identifier { get; set; }
}
public class CompletableApnsNotification
{
public CompletableApnsNotification (ApnsNotification notification)
{
Notification = notification;
completionSource = new TaskCompletionSource<Exception> ();
}
public ApnsNotification Notification { get; private set; }
TaskCompletionSource<Exception> completionSource;
public Task<Exception> WaitForComplete ()
{
return completionSource.Task;
}
public void CompleteSuccessfully ()
{
completionSource.SetResult (null);
}
public void CompleteFailed (Exception ex)
{
completionSource.SetResult (ex);
}
}
/// <summary>
/// Using IOControl code to configue socket KeepAliveValues for line disconnection detection(because default is toooo slow)
/// </summary>
/// <param name="tcpc">TcpClient</param>
/// <param name="KeepAliveTime">The keep alive time. (ms)</param>
/// <param name="KeepAliveInterval">The keep alive interval. (ms)</param>
public static void SetSocketKeepAliveValues (Socket socket, int KeepAliveTime, int KeepAliveInterval)
{
//KeepAliveTime: default value is 2hr
//KeepAliveInterval: default value is 1s and Detect 5 times
uint dummy = 0; //lenth = 4
byte[] inOptionValues = new byte[System.Runtime.InteropServices.Marshal.SizeOf (dummy) * 3]; //size = lenth * 3 = 12
BitConverter.GetBytes ((uint)1).CopyTo (inOptionValues, 0);
BitConverter.GetBytes ((uint)KeepAliveTime).CopyTo (inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf (dummy));
BitConverter.GetBytes ((uint)KeepAliveInterval).CopyTo (inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf (dummy) * 2);
// of course there are other ways to marshal up this byte array, this is just one way
// call WSAIoctl via IOControl
// .net 3.5 type
socket.IOControl (IOControlCode.KeepAliveValues, inOptionValues, null);
}
}
}