-
Notifications
You must be signed in to change notification settings - Fork 2
/
MlapiServer.cs
102 lines (90 loc) · 3.34 KB
/
MlapiServer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Dissonance.Networking;
using JetBrains.Annotations;
using MLAPI;
using MLAPI.Messaging;
using MLAPI.Serialization.Pooled;
using MLAPI.Transports.UNET;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class MlapiServer : BaseServer<MlapiServer, MlapiClient, MlapiConn>
{
private MlapiCommsNetwork _network;
public MlapiServer(MlapiCommsNetwork network)
{
_network = network;
}
public override void Connect()
{
// Register receiving packets on the server from the client
CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToServer", (senderClientId, stream) =>
{
var client = new MlapiConn();
client.clientId = senderClientId;
Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
Byte[] buffer = new Byte[length];
stream.Read(buffer, 0, length);
base.NetworkReceivedPacket(client, new ArraySegment<byte>(buffer));
});
base.Connect();
}
public override void Disconnect()
{
base.Disconnect();
}
protected override void ReadMessages()
{
}
protected override void SendReliable(MlapiConn destination, ArraySegment<byte> packet)
{
using (PooledBitStream stream = PooledBitStream.Get())
{
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
{
var count = packet.Count;
var offset = packet.Offset;
for (var i = 0; i < count; i++)
{
writer.WriteByte(packet.Array[i + offset]);
}
if (NetworkingManager.Singleton.LocalClientId == destination.clientId)
{
// As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues
_network.client.NetworkReceivedPacket(packet);
}
else
{
CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, stream, "MLAPI_ANIMATION_UPDATE");
}
}
}
}
protected override void SendUnreliable(MlapiConn destination, ArraySegment<byte> packet)
{
using (PooledBitStream stream = PooledBitStream.Get())
{
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
{
var count = packet.Count;
var offset = packet.Offset;
for (var i = 0; i < count; i++)
{
writer.WriteByte(packet.Array[i + offset]);
}
if (NetworkingManager.Singleton.LocalClientId == destination.clientId)
{
// As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues
_network.client.NetworkReceivedPacket(packet);
}
else
{
CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, stream, "MLAPI_TIME_SYNC");
}
}
}
}
public void ClientDisconnected()
{
}
}