Skip to content

Commit 01edd4c

Browse files
committed
Fixed various issues and began testing and creating examples for the wiki
1 parent 9321efe commit 01edd4c

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

MLAPI/Data/NetworkingConfiguration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Security.Cryptography;
6+
using UnityEngine;
57
using UnityEngine.Networking;
68

79
namespace MLAPI
@@ -11,11 +13,11 @@ public class NetworkingConfiguration
1113
public ushort ProtocolVersion = 0;
1214
public Dictionary<string, QosType> Channels = new Dictionary<string, QosType>();
1315
public List<string> MessageTypes = new List<string>();
14-
public int MessageBufferSize = 65536;
16+
public int MessageBufferSize = 65535;
1517
public int MaxMessagesPerFrame = 150;
1618
public int MaxConnections = 100;
1719
public int Port = 7777;
18-
public string Address;
20+
public string Address = "127.0.0.1";
1921
public int ClientConnectionBufferTimeout = 10;
2022
public bool ConnectionApproval = false;
2123
public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback;
@@ -27,7 +29,6 @@ public class NetworkingConfiguration
2729
//TODO
2830
public bool EncryptMessages = false;
2931

30-
3132
//Cached config hash
3233
private byte[] ConfigHash = null;
3334
public byte[] GetConfig(bool cache = true)
@@ -68,7 +69,7 @@ public byte[] GetConfig(bool cache = true)
6869

6970
public bool CompareConfig(byte[] hash)
7071
{
71-
return hash == GetConfig();
72+
return hash.SequenceEqual(GetConfig());
7273
}
7374
}
7475
}

MLAPI/MLAPI.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
@@ -37,7 +38,8 @@
3738
<Reference Include="System.Data.DataSetExtensions" />
3839
<Reference Include="System.Data" />
3940
<Reference Include="System.Xml" />
40-
<Reference Include="UnityEngine">
41+
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
42+
<SpecificVersion>False</SpecificVersion>
4143
<HintPath>..\..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll</HintPath>
4244
</Reference>
4345
</ItemGroup>

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ internal bool isHost
3434
private Dictionary<ushort, int> messageHandlerCounter;
3535
private Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
3636
private int localConnectionId;
37-
public Scene PlayScene;
38-
public Scene MenuScene;
37+
public int PlaySceneIndex;
38+
public int MenuSceneIndex;
3939
private Dictionary<uint, NetworkedObject> spawnedObjects;
4040
private List<uint> spawnedObjectIds;
4141
private Stack<uint> releasedNetworkObjectIds;
@@ -212,7 +212,7 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
212212
{
213213
if(NetworkConfig.ConnectionApprovalCallback == null)
214214
{
215-
Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection aproval will timeout");
215+
Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout");
216216
}
217217
}
218218

@@ -229,17 +229,14 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
229229
HashSet<string> channelNames = new HashSet<string>();
230230
foreach (KeyValuePair<string, QosType> pair in NetworkConfig.Channels)
231231
{
232-
if(pair.Key.StartsWith("MLAPI_"))
233-
{
234-
Debug.LogWarning("MLAPI: Channel names are not allowed to start with MLAPI_. This is to prevent name conflicts");
235-
continue;
236-
}
237-
else if(channelNames.Contains(pair.Key))
232+
if(channelNames.Contains(pair.Key))
238233
{
239234
Debug.LogWarning("MLAPI: Duplicate channel name: " + pair.Key);
240235
continue;
241236
}
242-
channels.Add(pair.Key, cConfig.AddChannel(pair.Value));
237+
int channelId = cConfig.AddChannel(pair.Value);
238+
channels.Add(pair.Key, channelId);
239+
channelNames.Add(pair.Key);
243240
}
244241
//0-32 are reserved for MLAPI messages
245242
for (ushort i = 32; i < NetworkConfig.MessageTypes.Count; i++)
@@ -252,37 +249,38 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
252249

253250
public void StartServer(NetworkingConfiguration netConfig)
254251
{
252+
SceneManager.LoadScene(PlaySceneIndex);
255253
ConnectionConfig cConfig = Init(netConfig);
256254
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
257-
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
255+
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
258256
isServer = true;
259257
isClient = false;
260258
isListening = true;
261-
SceneManager.LoadScene(PlayScene.buildIndex);
262259
}
263260

264261
public void StartClient(NetworkingConfiguration netConfig)
265262
{
266263
ConnectionConfig cConfig = Init(netConfig);
267264
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
268-
hostId = NetworkTransport.AddHost(hostTopology, 0);
265+
hostId = NetworkTransport.AddHost(hostTopology, 0, null);
266+
//NetworkTransport.AddSceneId(PlaySceneIndex);
269267

270-
localConnectionId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
271268
isServer = false;
272269
isClient = true;
273270
isListening = true;
271+
localConnectionId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
274272
}
275273

276274
public void StartHost(NetworkingConfiguration netConfig)
277275
{
276+
SceneManager.LoadScene(PlaySceneIndex);
278277
ConnectionConfig cConfig = Init(netConfig);
279278
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
280279
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
281280
isServer = true;
282281
isClient = true;
283282
isListening = true;
284283
connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 });
285-
SceneManager.LoadScene(PlayScene.buildIndex);
286284
}
287285

288286
private void OnEnable()
@@ -316,12 +314,12 @@ private void Update()
316314
do
317315
{
318316
messagesProcessed++;
319-
eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, NetworkConfig.MessageBufferSize, out receivedSize, out error);
317+
eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
320318
NetworkError networkError = (NetworkError)error;
321319
if (networkError != NetworkError.Ok)
322320
{
323321
Debug.LogWarning("MLAPI: NetworkTransport receive error: " + networkError.ToString());
324-
return;
322+
continue;
325323
}
326324
switch (eventType)
327325
{
@@ -342,12 +340,13 @@ private void Update()
342340
{
343341
writer.Write(NetworkConfig.ConnectionData);
344342
}
345-
Send(connectionId, "MLAPI_CONNECTION_REQUEST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray());
346343
}
344+
Send(connectionId, "MLAPI_CONNECTION_REQUEST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray());
347345
}
348346
}
349347
break;
350348
case NetworkEventType.DataEvent:
349+
Debug.Log("RECIEVE");
351350
HandleIncomingData(connectionId, ref messageBuffer);
352351
break;
353352
}
@@ -405,7 +404,7 @@ private void HandleIncomingData(int connectonId, ref byte[] data)
405404
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
406405
{
407406
byte[] configHash = messageReader.ReadBytes(32);
408-
if (NetworkConfig.CompareConfig(configHash) == false)
407+
if (!NetworkConfig.CompareConfig(configHash))
409408
{
410409
Debug.LogWarning("MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match.");
411410
DisconnectClient(connectionId);
@@ -427,7 +426,7 @@ private void HandleIncomingData(int connectonId, ref byte[] data)
427426
case 1: //Server informs client it has been approved:
428427
if (isClient)
429428
{
430-
SceneManager.LoadScene(PlayScene.buildIndex);
429+
SceneManager.LoadScene(PlaySceneIndex);
431430
using (MemoryStream messageReadStream = new MemoryStream(reader.ReadBytes(int.MaxValue)))
432431
{
433432
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
@@ -477,7 +476,9 @@ internal void Send(int connectionId, string messageType, string channelName, byt
477476
{
478477
writer.Write(messageTypes[messageType]);
479478
writer.Write(data);
480-
NetworkTransport.Send(hostId, connectionId, channels[channelName], data, data.Length, out error);
479+
//2 bytes for message type
480+
int size = data.Length + 2;
481+
NetworkTransport.Send(hostId, connectionId, channels[channelName], stream.ToArray(), size, out error);
481482
}
482483
}
483484
}
@@ -549,9 +550,8 @@ private void HandleApproval(int connectionId, bool approved)
549550
writer.Write(spawnedObjects[spawnedObjectIds[i]].SpawnablePrefabId);
550551
}
551552
}
552-
553-
Send(connectionId, "MLAPI_CLIENT_LIST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray());
554553
}
554+
Send(connectionId, "MLAPI_CLIENT_LIST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray());
555555
}
556556
}
557557
else

0 commit comments

Comments
 (0)