-
Notifications
You must be signed in to change notification settings - Fork 2
/
MlapiClient.cs
86 lines (75 loc) · 2.85 KB
/
MlapiClient.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
using Dissonance.Networking;
using MLAPI;
using MLAPI.Messaging;
using MLAPI.Serialization.Pooled;
using MLAPI.Transports.UNET;
using System;
using System.IO;
using UnityEngine;
public class MlapiClient : BaseClient<MlapiServer, MlapiClient, MlapiConn>
{
private MlapiCommsNetwork _network;
public MlapiClient(MlapiCommsNetwork network) : base(network)
{
_network = network;
}
public override void Connect()
{
// Register receiving packets on the client from the server
CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToClient", (senderClientId, stream) =>
{
Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
Byte[] buffer = new Byte[length];
stream.Read(buffer, 0, length);
base.NetworkReceivedPacket(new ArraySegment<byte>(buffer));
});
Connected();
}
protected override void ReadMessages()
{
}
protected override void SendReliable(ArraySegment<byte> packet)
{
if (NetworkingManager.Singleton.IsHost)
{
// As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loopback issues
_network.server.NetworkReceivedPacket(new MlapiConn(), packet);
}
else
{
using (PooledBitStream stream = PooledBitStream.Get())
{
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
{
for (var i = 0; i < packet.Count; i++)
{
writer.WriteByte(packet.Array[i + packet.Offset]);
}
CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkingManager.Singleton.ServerClientId, stream, "MLAPI_ANIMATION_UPDATE");
}
}
}
}
protected override void SendUnreliable(ArraySegment<byte> packet)
{
if (NetworkingManager.Singleton.IsHost)
{
// As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loopback issues
_network.server.NetworkReceivedPacket(new MlapiConn(), packet);
}
else
{
using (PooledBitStream stream = PooledBitStream.Get())
{
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
{
for (var i = 0; i < packet.Count; i++)
{
writer.WriteByte(packet.Array[i + packet.Offset]);
}
CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkingManager.Singleton.ServerClientId, stream, "MLAPI_TIME_SYNC");
}
}
}
}
}