-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathNetickNetProvider.cs
More file actions
106 lines (89 loc) · 3.4 KB
/
NetickNetProvider.cs
File metadata and controls
106 lines (89 loc) · 3.4 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
#if NETICK
using System;
using System.Collections.Generic;
using Netick.Unity;
using UnityEngine;
namespace MetaVoiceChat.NetProviders.Netick
{
[RequireComponent(typeof(MetaVc))]
public class NetickNetProvider : NetworkBehaviour, INetProvider
{
#region Singleton
public static NetickNetProvider LocalPlayerInstance { get; private set; }
private readonly static List<NetickNetProvider> instances = new();
public static IReadOnlyList<NetickNetProvider> Instances => instances;
#endregion
bool INetProvider.IsLocalPlayerDeafened => LocalPlayerInstance.MetaVc.isDeafened;
public MetaVc MetaVc { get; private set; }
private MetaVoiceChatNetick VoiceDataTransmitter;
private int playerID;
public override void NetworkStart()
{
#region Singleton
if (IsInputSource)
{
LocalPlayerInstance = this;
}
instances.Add(this);
#endregion
if (Sandbox.TryGetComponent<MetaVoiceChatNetick>(out VoiceDataTransmitter))
{
if (Sandbox.IsServer)
{
playerID = InputSource.PlayerId;
VoiceDataTransmitter.ConnectionIdToPlayerObjectID.Add(playerID, Object.Id);
}
}
else
Debug.LogError("Your Sandbox Prefab doesnt have the MetaVoiceChatNetick component");
static int GetMaxDataBytesPerPacket()
{
int bytes = 1000; //safe number
bytes -= sizeof(int); // Index
bytes -= sizeof(double); // Timestamp
bytes -= MetaVoiceChatNetick.additionalLatencySize;
bytes -= sizeof(int); // Player id
//bytes -= sizeof(ushort); // Array length (not needed?)
return bytes;
}
MetaVc = GetComponent<MetaVc>();
MetaVc.StartClient(this, IsInputSource, GetMaxDataBytesPerPacket());
}
public override void NetworkDestroy()
{
#region Singleton
if (IsInputSource)
{
LocalPlayerInstance = null;
}
instances.Remove(this);
#endregion
MetaVc.StopClient();
if (Sandbox.IsServer && VoiceDataTransmitter != null)
VoiceDataTransmitter.ConnectionIdToPlayerObjectID.Remove(playerID);
}
void INetProvider.RelayFrame(int index, double timestamp, ReadOnlySpan<byte> data)
{
float additionalLatency = GetAdditionalLatency();
if (Sandbox.IsServer)
{
//send the data to all clients
VoiceDataTransmitter.SendServerVoiceToClients(index, timestamp, additionalLatency, data, playerID);
}
else
{
//send the data from client to server
VoiceDataTransmitter.SendVoiceDataToServer(index, timestamp, additionalLatency, data);
}
}
public static float GetAdditionalLatency()
{
return Time.deltaTime;
}
public void ReceiveFrame(int index, double timestamp, float additionalLatency, ReadOnlySpan<byte> data)
{
MetaVc.ReceiveFrame(index, timestamp, additionalLatency, data);
}
}
}
#endif