forked from AscensionGameDev/Intersect-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientNetwork.cs
More file actions
147 lines (120 loc) · 5.2 KB
/
Copy pathClientNetwork.cs
File metadata and controls
147 lines (120 loc) · 5.2 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
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
using System.Collections.Concurrent;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Security.Cryptography;
using Intersect.Core;
using Intersect.Framework.Core.Security;
using Intersect.Network.Events;
using Intersect.Network.LiteNetLib;
using Microsoft.Extensions.Logging;
namespace Intersect.Network;
public partial class ClientNetwork : AbstractNetwork, IClient
{
public static readonly HashSet<string> UnresolvableHostNames = [];
private static readonly NetworkLayerInterfaceFactory DefaultNetworkLayerInterfaceFactory =
(network, parameters) => new LiteNetLibInterface(network, parameters);
internal static NetworkLayerInterfaceFactory? NetworkLayerInterfaceFactory { get; set; }
private readonly INetworkLayerInterface _interface;
private readonly Ping _ping = new();
private bool _isConnected;
public ClientNetwork(
IApplicationContext applicationContext,
NetworkConfiguration configuration,
RSAParameters rsaParameters
) : base(
applicationContext,
configuration
)
{
Id = Guid.Empty;
_isConnected = false;
_interface =
(NetworkLayerInterfaceFactory ?? DefaultNetworkLayerInterfaceFactory).Invoke(this, rsaParameters);
_interface.OnConnected += HandleInterfaceOnConnected;
_interface.OnConnectionApproved += HandleInterfaceOnConnectonApproved;
_interface.OnConnectionDenied += HandleInterfaceOnConnectonDenied;
_interface.OnDisconnected += HandleInterfaceOnDisconnected;
AddNetworkLayerInterface(_interface);
StartInterfaces();
}
public override event HandleConnectionEvent? OnConnected;
public override event HandleConnectionEvent? OnConnectionApproved;
public override event HandleConnectionEvent? OnConnectionDenied;
public override event HandleConnectionRequest? OnConnectionRequested;
public override event HandleConnectionEvent? OnDisconnected;
public override event HandlePacketAvailable? OnPacketAvailable;
public override event HandleUnconnectedMessage? OnUnconnectedMessage;
public IConnection? Connection => Connections.FirstOrDefault();
public override bool IsConnected => _isConnected;
public bool IsServerOnline => IsConnected;
public int Ping
{
get
{
if (Connection is { } connection)
{
return (int)connection.Statistics.Ping;
}
if (Configuration.Host is not { } hostNameOrAddress || UnresolvableHostNames.Contains(hostNameOrAddress))
{
// Return a distinct ping value for unresolved host addresses so that it's clear why it's not showing
return -2;
}
return -1;
}
}
public bool Connect()
{
return IsConnected || _interface.Connect();
}
protected override bool SendUnconnected(IPEndPoint endPoint, UnconnectedPacket packet) =>
_interface.SendUnconnectedPacket(endPoint, packet);
public override bool Send(IPacket packet, TransmissionMode mode = TransmissionMode.All)
{
return _interface?.SendPacket(packet, (IConnection)null, mode) ?? false;
}
public override bool Send(IConnection connection, IPacket packet, TransmissionMode mode = TransmissionMode.All)
{
return Send(packet, mode);
}
public override bool Send(ICollection<IConnection> connections, IPacket packet, TransmissionMode mode = TransmissionMode.All)
{
return Send(packet, mode);
}
protected virtual void HandleInterfaceOnConnected(INetworkLayerInterface sender, ConnectionEventArgs connectionEventArgs)
{
ApplicationContext.Logger.LogInformation($"Connected [{connectionEventArgs.Connection?.Guid}].");
_isConnected = true;
OnConnected?.Invoke(sender, connectionEventArgs);
}
protected virtual void HandleInterfaceOnConnectonApproved(INetworkLayerInterface sender, ConnectionEventArgs connectionEventArgs)
{
ApplicationContext.Logger.LogInformation($"Connection approved [{connectionEventArgs.Connection?.Guid}].");
OnConnectionApproved?.Invoke(sender, connectionEventArgs);
}
protected virtual void HandleInterfaceOnConnectonDenied(INetworkLayerInterface sender, ConnectionEventArgs connectionEventArgs)
{
ApplicationContext.Logger.LogInformation($"Connection denied [{connectionEventArgs.Connection?.Guid}].");
OnConnectionDenied?.Invoke(sender, connectionEventArgs);
}
protected virtual void HandleInterfaceOnDisconnected(INetworkLayerInterface sender, ConnectionEventArgs connectionEventArgs)
{
ApplicationContext.Logger.LogInformation($"Disconnected [{connectionEventArgs.Connection?.Guid ?? Guid.Empty}].");
_isConnected = false;
OnDisconnected?.Invoke(sender, connectionEventArgs);
PermissionSet.ActivePermissionSet = PermissionSet.Default;
}
public override void Close()
{
StopInterfaces("closing");
}
internal void AssignId(Guid id)
{
Id = id;
}
protected override IDictionary<TKey, TValue> CreateDictionaryLegacy<TKey, TValue>()
{
return new ConcurrentDictionary<TKey, TValue>();
}
}