-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathInternalMessageSender.cs
More file actions
144 lines (125 loc) · 7.44 KB
/
InternalMessageSender.cs
File metadata and controls
144 lines (125 loc) · 7.44 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
using System;
using System.Collections.Generic;
using MLAPI.Configuration;
using MLAPI.Internal;
using MLAPI.Logging;
using MLAPI.Profiling;
using MLAPI.Security;
using MLAPI.Transports;
using BitStream = MLAPI.Serialization.BitStream;
namespace MLAPI.Messaging
{
internal static class InternalMessageSender
{
internal static void Send(ulong clientId, byte messageType, Channel channel, BitStream messageStream, SecuritySendFlags flags)
{
messageStream.PadStream();
if (NetworkingManager.Singleton.IsServer && clientId == NetworkingManager.Singleton.ServerClientId)
return;
using (BitStream stream = MessagePacker.WrapMessage(messageType, clientId, messageStream, flags))
{
NetworkProfiler.StartEvent(TickType.Send, (uint) stream.Length, channel,
MLAPIConstants.MESSAGE_NAMES[messageType]);
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientId, new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
ProfilerStatManager.bytesSent.Record((int)stream.Length);
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
NetworkProfiler.EndEvent();
}
}
internal static void Send(byte messageType, Channel channel, BitStream messageStream, SecuritySendFlags flags)
{
bool encrypted = ((flags & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
bool authenticated = ((flags & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
if (authenticated || encrypted)
{
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
{
Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, messageType, channel, messageStream, flags);
}
}
else
{
messageStream.PadStream();
using (BitStream stream = MessagePacker.WrapMessage(messageType, 0, messageStream, flags))
{
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channel, MLAPIConstants.MESSAGE_NAMES[messageType]);
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
{
if (NetworkingManager.Singleton.IsServer && NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == NetworkingManager.Singleton.ServerClientId)
continue;
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
ProfilerStatManager.bytesSent.Record((int)stream.Length);
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
}
NetworkProfiler.EndEvent();
}
}
}
internal static void Send(byte messageType, Channel channel, List<ulong> clientIds, BitStream messageStream, SecuritySendFlags flags)
{
if (clientIds == null)
{
Send(messageType, channel, messageStream, flags);
return;
}
bool encrypted = ((flags & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
bool authenticated = ((flags & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
if (authenticated || encrypted)
{
for (int i = 0; i < clientIds.Count; i++)
{
Send(clientIds[i], messageType, channel, messageStream, flags);
}
}
else
{
messageStream.PadStream();
using (BitStream stream = MessagePacker.WrapMessage(messageType, 0, messageStream, flags))
{
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channel, MLAPIConstants.MESSAGE_NAMES[messageType]);
for (int i = 0; i < clientIds.Count; i++)
{
if (NetworkingManager.Singleton.IsServer && clientIds[i] == NetworkingManager.Singleton.ServerClientId)
continue;
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientIds[i], new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
ProfilerStatManager.bytesSent.Record((int)stream.Length);
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
}
NetworkProfiler.EndEvent();
}
}
}
internal static void Send(byte messageType, Channel channel, ulong clientIdToIgnore, BitStream messageStream, SecuritySendFlags flags)
{
bool encrypted = ((flags & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
bool authenticated = ((flags & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
if (encrypted || authenticated)
{
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
{
if (NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == clientIdToIgnore)
continue;
Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, messageType, channel, messageStream, flags);
}
}
else
{
messageStream.PadStream();
using (BitStream stream = MessagePacker.WrapMessage(messageType, 0, messageStream, flags))
{
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channel, MLAPIConstants.MESSAGE_NAMES[messageType]);
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
{
if (NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == clientIdToIgnore ||
(NetworkingManager.Singleton.IsServer && NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == NetworkingManager.Singleton.ServerClientId))
continue;
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
ProfilerStatManager.bytesSent.Record((int)stream.Length);
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
}
NetworkProfiler.EndEvent();
}
}
}
}
}