-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFcmConnection.cs
More file actions
122 lines (104 loc) · 4.13 KB
/
FcmConnection.cs
File metadata and controls
122 lines (104 loc) · 4.13 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Firebase.Cloud.Messaging
{
class FcmConnection : IFcmConnection
{
private const string host = "mtalk.google.com";
private const int port = 5228;
private TcpClient client;
private SslStream sslStream;
private byte[] buffer = new byte[2048];
private CancellationTokenSource cts = new CancellationTokenSource();
public event EventHandler<DataReceivedEventArgs> DataReceived;
public async Task ConnectAsync()
{
client = new TcpClient(host, port);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 60);
client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 60);
sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
await sslStream.AuthenticateAsClientAsync(host, null, SslProtocols.Tls13 | SslProtocols.Tls12, false).ConfigureAwait(false);
}
public async Task SendAsync(byte[] data, CancellationToken cancellationToken)
{
CancellationToken linkedToken = CreateLinkedToken(cancellationToken);
await sslStream.WriteAsync(data, 0, data.Length, linkedToken).ConfigureAwait(false);
}
public void Send(byte[] data)
{
sslStream.Write(data, 0, data.Length);
}
private CancellationToken CreateLinkedToken(CancellationToken cancellationToken)
{
return CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token).Token;
}
public async Task ReceiveAsync(CancellationToken cancellationToken)
{
CancellationToken linkedToken = CreateLinkedToken(cancellationToken);
try
{
while (linkedToken.IsCancellationRequested == false)
{
int bytesRead = await sslStream.ReadAsync(buffer, 0, buffer.Length, linkedToken).ConfigureAwait(false);
if (bytesRead > 0 && DataReceived != null)
{
DataReceived.Invoke(this, new DataReceivedEventArgs(buffer, bytesRead));
}
// it seems that if the underlying socket is disconnected no exception is thrown
if (!client.Connected || bytesRead == 0)
{
throw new SocketException(1);
}
}
}
catch (SocketException)
{
}
catch (OperationCanceledException)
{
}
}
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
cts.Cancel();
cts.Dispose();
sslStream?.Dispose();
client?.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
interface IFcmConnection : IDisposable
{
event EventHandler<DataReceivedEventArgs> DataReceived;
Task SendAsync(byte[] data, CancellationToken cancellationToken);
void Send(byte[] data);
Task ConnectAsync();
Task ReceiveAsync(CancellationToken cancellationToken);
}
}