fixes for mouse dragger, visualization for audio range, added some more callbacks to velnetmanager, added color syncing extension, take ownership of synced textbox, fix for deleting all scene objects
parent
dc61c63563
commit
682c470d46
|
|
@ -8,6 +8,8 @@ namespace VelNet
|
||||||
{
|
{
|
||||||
public static class BinaryWriterExtensions
|
public static class BinaryWriterExtensions
|
||||||
{
|
{
|
||||||
|
#region Writers
|
||||||
|
|
||||||
public static void Write(this BinaryWriter writer, Vector3 v)
|
public static void Write(this BinaryWriter writer, Vector3 v)
|
||||||
{
|
{
|
||||||
writer.Write(v.x);
|
writer.Write(v.x);
|
||||||
|
|
@ -23,6 +25,18 @@ namespace VelNet
|
||||||
writer.Write(q.w);
|
writer.Write(q.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Write(this BinaryWriter writer, Color c)
|
||||||
|
{
|
||||||
|
writer.Write(c.r);
|
||||||
|
writer.Write(c.g);
|
||||||
|
writer.Write(c.b);
|
||||||
|
writer.Write(c.a);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Readers
|
||||||
|
|
||||||
public static Vector3 ReadVector3(this BinaryReader reader)
|
public static Vector3 ReadVector3(this BinaryReader reader)
|
||||||
{
|
{
|
||||||
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
||||||
|
|
@ -31,19 +45,32 @@ namespace VelNet
|
||||||
public static Quaternion ReadQuaternion(this BinaryReader reader)
|
public static Quaternion ReadQuaternion(this BinaryReader reader)
|
||||||
{
|
{
|
||||||
return new Quaternion(
|
return new Quaternion(
|
||||||
reader.ReadSingle(),
|
reader.ReadSingle(),
|
||||||
reader.ReadSingle(),
|
reader.ReadSingle(),
|
||||||
reader.ReadSingle(),
|
reader.ReadSingle(),
|
||||||
reader.ReadSingle()
|
reader.ReadSingle()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Color ReadColor(this BinaryReader reader)
|
||||||
|
{
|
||||||
|
return new Color(
|
||||||
|
reader.ReadSingle(),
|
||||||
|
reader.ReadSingle(),
|
||||||
|
reader.ReadSingle(),
|
||||||
|
reader.ReadSingle()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Compresses the list of bools into bytes using a bitmask
|
/// Compresses the list of bools into bytes using a bitmask
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static byte[] GetBitmasks(this IEnumerable<bool> bools)
|
public static byte[] GetBitmasks(this IEnumerable<bool> bools)
|
||||||
{
|
{
|
||||||
List<bool> values = bools.ToList();
|
List<bool> values = bools.ToList();
|
||||||
List<byte> bytes = new List<byte>();
|
List<byte> bytes = new List<byte>();
|
||||||
for (int b = 0; b < Mathf.Ceil(values.Count / 8f); b++)
|
for (int b = 0; b < Mathf.Ceil(values.Count / 8f); b++)
|
||||||
{
|
{
|
||||||
|
|
@ -61,7 +88,7 @@ namespace VelNet
|
||||||
|
|
||||||
return bytes.ToArray();
|
return bytes.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<bool> GetBitmaskValues(this IEnumerable<byte> bytes)
|
public static List<bool> GetBitmaskValues(this IEnumerable<byte> bytes)
|
||||||
{
|
{
|
||||||
List<bool> l = new List<bool>();
|
List<bool> l = new List<bool>();
|
||||||
|
|
@ -72,7 +99,7 @@ namespace VelNet
|
||||||
|
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<bool> GetBitmaskValues(this byte b)
|
public static List<bool> GetBitmaskValues(this byte b)
|
||||||
{
|
{
|
||||||
List<bool> l = new List<bool>();
|
List<bool> l = new List<bool>();
|
||||||
|
|
@ -83,11 +110,10 @@ namespace VelNet
|
||||||
|
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool GetBitmaskValue(this byte b, int index)
|
public static bool GetBitmaskValue(this byte b, int index)
|
||||||
{
|
{
|
||||||
return (b & (1 << index)) != 0;
|
return (b & (1 << index)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
|
@ -8,8 +7,6 @@ using System.Threading;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
using System.Runtime.Serialization.Formatters.Binary;
|
|
||||||
using System.Runtime.Serialization;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace VelNet
|
namespace VelNet
|
||||||
|
|
@ -42,10 +39,11 @@ namespace VelNet
|
||||||
private Thread clientReceiveThread;
|
private Thread clientReceiveThread;
|
||||||
private Thread clientReceiveThreadUDP;
|
private Thread clientReceiveThreadUDP;
|
||||||
public int userid = -1;
|
public int userid = -1;
|
||||||
private int messagesReceived = 0;
|
|
||||||
|
|
||||||
public readonly Dictionary<int, VelNetPlayer> players = new Dictionary<int, VelNetPlayer>();
|
public readonly Dictionary<int, VelNetPlayer> players = new Dictionary<int, VelNetPlayer>();
|
||||||
|
|
||||||
|
#region Callbacks
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// We just joined a room
|
/// We just joined a room
|
||||||
/// string - the room name
|
/// string - the room name
|
||||||
|
|
@ -69,8 +67,12 @@ namespace VelNet
|
||||||
public static Action<VelNetPlayer> OnPlayerLeft;
|
public static Action<VelNetPlayer> OnPlayerLeft;
|
||||||
|
|
||||||
public static Action OnConnectedToServer;
|
public static Action OnConnectedToServer;
|
||||||
public static Action LoggedIn;
|
public static Action OnLoggedIn;
|
||||||
public static Action<string[], int> RoomsReceived;
|
public static Action<RoomsMessage> RoomsReceived;
|
||||||
|
|
||||||
|
public static Action<Message> MessageReceived;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
public bool connected;
|
public bool connected;
|
||||||
|
|
||||||
|
|
@ -92,7 +94,7 @@ namespace VelNet
|
||||||
public static VelNetPlayer LocalPlayer => instance != null ? instance.players.Where(p => p.Value.isLocal).Select(p => p.Value).FirstOrDefault() : null;
|
public static VelNetPlayer LocalPlayer => instance != null ? instance.players.Where(p => p.Value.isLocal).Select(p => p.Value).FirstOrDefault() : null;
|
||||||
public static bool InRoom => LocalPlayer != null && LocalPlayer.room != "-1" && LocalPlayer.room != "";
|
public static bool InRoom => LocalPlayer != null && LocalPlayer.room != "-1" && LocalPlayer.room != "";
|
||||||
public static string Room => LocalPlayer?.room;
|
public static string Room => LocalPlayer?.room;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The player count in this room.
|
/// The player count in this room.
|
||||||
/// -1 if not in a room.
|
/// -1 if not in a room.
|
||||||
|
|
@ -107,46 +109,59 @@ namespace VelNet
|
||||||
|
|
||||||
public static bool IsConnected => instance != null && instance.connected && instance.udpConnected;
|
public static bool IsConnected => instance != null && instance.connected && instance.udpConnected;
|
||||||
|
|
||||||
|
|
||||||
//this is for sending udp packets
|
//this is for sending udp packets
|
||||||
static byte[] toSend = new byte[1024];
|
private static readonly byte[] toSend = new byte[1024];
|
||||||
|
|
||||||
// Use this for initialization
|
// Use this for initialization
|
||||||
public abstract class Message
|
public abstract class Message
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ListedRoom
|
public class ListedRoom
|
||||||
{
|
{
|
||||||
public string name;
|
public string name;
|
||||||
public int numUsers;
|
public int numUsers;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return "Room Name: " + name + "\tUsers: " + numUsers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class LoginMessage: Message
|
|
||||||
|
public class LoginMessage : Message
|
||||||
{
|
{
|
||||||
public int userId;
|
public int userId;
|
||||||
}
|
}
|
||||||
public class RoomsMessage: Message
|
|
||||||
|
public class RoomsMessage : Message
|
||||||
{
|
{
|
||||||
public List<ListedRoom> rooms;
|
public List<ListedRoom> rooms;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return string.Join("\n", rooms);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class JoinMessage: Message
|
|
||||||
|
public class JoinMessage : Message
|
||||||
{
|
{
|
||||||
public int userId;
|
public int userId;
|
||||||
public string room;
|
public string room;
|
||||||
}
|
}
|
||||||
public class DataMessage: Message
|
|
||||||
|
public class DataMessage : Message
|
||||||
{
|
{
|
||||||
public int senderId;
|
public int senderId;
|
||||||
public byte[] data;
|
public byte[] data;
|
||||||
}
|
}
|
||||||
public class ChangeMasterMessage: Message
|
|
||||||
|
public class ChangeMasterMessage : Message
|
||||||
{
|
{
|
||||||
public int masterId;
|
public int masterId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ConnectedMessage: Message
|
public class ConnectedMessage : Message
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly List<Message> receivedMessages = new List<Message>();
|
public readonly List<Message> receivedMessages = new List<Message>();
|
||||||
|
|
@ -180,6 +195,15 @@ namespace VelNet
|
||||||
//Debug.Log(messagesReceived++);
|
//Debug.Log(messagesReceived++);
|
||||||
receivedMessages.Add(m);
|
receivedMessages.Add(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MessageReceived?.Invoke(m);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
|
|
@ -191,216 +215,236 @@ namespace VelNet
|
||||||
{
|
{
|
||||||
switch (m)
|
switch (m)
|
||||||
{
|
{
|
||||||
case ConnectedMessage connected:
|
case ConnectedMessage msg:
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
try
|
OnConnectedToServer?.Invoke();
|
||||||
{
|
|
||||||
OnConnectedToServer?.Invoke();
|
|
||||||
}
|
|
||||||
// prevent errors in subscribers from breaking our code
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
// prevent errors in subscribers from breaking our code
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
case LoginMessage lm:
|
case LoginMessage lm:
|
||||||
|
{
|
||||||
|
userid = lm.userId;
|
||||||
|
Debug.Log("Joined server " + userid);
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
userid = lm.userId;
|
OnLoggedIn?.Invoke();
|
||||||
Debug.Log("joined server " + userid);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
LoggedIn?.Invoke();
|
|
||||||
}
|
|
||||||
// prevent errors in subscribers from breaking our code
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
//start the udp thread
|
|
||||||
clientReceiveThreadUDP = new Thread(ListenForDataUDP);
|
|
||||||
clientReceiveThreadUDP.IsBackground = true;
|
|
||||||
clientReceiveThreadUDP.Start();
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case RoomsMessage rm: {
|
// prevent errors in subscribers from breaking our code
|
||||||
Debug.Log("Got Rooms Message");
|
catch (Exception e)
|
||||||
|
{
|
||||||
break;
|
Debug.LogError(e);
|
||||||
}
|
}
|
||||||
case JoinMessage jm: {
|
|
||||||
if(userid == jm.userId) //this is us
|
//start the udp thread
|
||||||
|
clientReceiveThreadUDP = new Thread(ListenForDataUDP);
|
||||||
|
clientReceiveThreadUDP.Start();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RoomsMessage rm:
|
||||||
|
{
|
||||||
|
Debug.Log("Got Rooms Message:\n" + rm);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RoomsReceived?.Invoke(rm);
|
||||||
|
}
|
||||||
|
// prevent errors in subscribers from breaking our code
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case JoinMessage jm:
|
||||||
|
{
|
||||||
|
if (userid == jm.userId) //this is us
|
||||||
|
{
|
||||||
|
string oldRoom = LocalPlayer?.room;
|
||||||
|
|
||||||
|
// we clear the list, but will recreate as we get messages from people in our room
|
||||||
|
players.Clear();
|
||||||
|
masterPlayer = null;
|
||||||
|
|
||||||
|
if (jm.room != "")
|
||||||
{
|
{
|
||||||
string oldRoom = LocalPlayer?.room;
|
VelNetPlayer player = new VelNetPlayer
|
||||||
|
|
||||||
// we clear the list, but will recreate as we get messages from people in our room
|
|
||||||
players.Clear();
|
|
||||||
masterPlayer = null;
|
|
||||||
|
|
||||||
if (jm.room != "")
|
|
||||||
{
|
{
|
||||||
VelNetPlayer player = new VelNetPlayer
|
isLocal = true,
|
||||||
{
|
userid = jm.userId,
|
||||||
isLocal = true,
|
room = jm.room
|
||||||
userid = jm.userId,
|
};
|
||||||
room = jm.room
|
|
||||||
};
|
|
||||||
|
|
||||||
players.Add(userid, player);
|
players.Add(userid, player);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
OnJoinedRoom?.Invoke(jm.room);
|
OnJoinedRoom?.Invoke(jm.room);
|
||||||
}
|
|
||||||
// prevent errors in subscribers from breaking our code
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
// we just left a room
|
// prevent errors in subscribers from breaking our code
|
||||||
else
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// delete all networkobjects that aren't sceneobjects or are null now
|
Debug.LogError(e);
|
||||||
objects
|
|
||||||
.Where(kvp => kvp.Value == null || !kvp.Value.isSceneObject)
|
|
||||||
.Select(o => o.Key)
|
|
||||||
.ToList().ForEach(NetworkDestroy);
|
|
||||||
|
|
||||||
// then remove references to the ones that are left
|
|
||||||
objects.Clear();
|
|
||||||
|
|
||||||
// empty all the groups
|
|
||||||
foreach (string group in instance.groups.Keys)
|
|
||||||
{
|
|
||||||
SetupMessageGroup(group, new List<int>());
|
|
||||||
}
|
|
||||||
|
|
||||||
instance.groups.Clear();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
OnLeftRoom?.Invoke(oldRoom);
|
|
||||||
}
|
|
||||||
// prevent errors in subscribers from breaking our code
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// we just left a room
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
VelNetPlayer me = players[userid];
|
// delete all networkobjects that aren't sceneobjects or are null now
|
||||||
|
objects
|
||||||
|
.Where(kvp => kvp.Value == null || !kvp.Value.isSceneObject)
|
||||||
|
.Select(o => o.Key)
|
||||||
|
.ToList().ForEach(NetworkDestroy);
|
||||||
|
|
||||||
if (me.room != jm.room)
|
// then remove references to the ones that are left
|
||||||
|
objects.Clear();
|
||||||
|
|
||||||
|
// empty all the groups
|
||||||
|
foreach (string group in instance.groups.Keys)
|
||||||
{
|
{
|
||||||
// we got a left message, kill it
|
SetupMessageGroup(group, new List<int>());
|
||||||
// change ownership of all objects to master
|
}
|
||||||
List<string> deleteObjects = new List<string>();
|
|
||||||
foreach (KeyValuePair<string, NetworkObject> kvp in objects)
|
instance.groups.Clear();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
OnLeftRoom?.Invoke(oldRoom);
|
||||||
|
}
|
||||||
|
// prevent errors in subscribers from breaking our code
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
VelNetPlayer me = players[userid];
|
||||||
|
|
||||||
|
if (me.room != jm.room)
|
||||||
|
{
|
||||||
|
// we got a left message, kill it
|
||||||
|
// change ownership of all objects to master
|
||||||
|
List<string> deleteObjects = new List<string>();
|
||||||
|
foreach (KeyValuePair<string, NetworkObject> kvp in objects)
|
||||||
|
{
|
||||||
|
if (kvp.Value.owner == players[jm.userId]) // the owner is the player that left
|
||||||
{
|
{
|
||||||
if (kvp.Value.owner == players[jm.userId]) // the owner is the player that left
|
// if this object has locked ownership, delete it
|
||||||
|
if (kvp.Value.ownershipLocked)
|
||||||
{
|
{
|
||||||
// if this object has locked ownership, delete it
|
deleteObjects.Add(kvp.Value.networkId);
|
||||||
if (kvp.Value.ownershipLocked)
|
}
|
||||||
{
|
// I'm the local master player, so can take ownership immediately
|
||||||
deleteObjects.Add(kvp.Value.networkId);
|
else if (me.isLocal && me == masterPlayer)
|
||||||
}
|
{
|
||||||
// I'm the local master player, so can take ownership immediately
|
TakeOwnership(kvp.Key);
|
||||||
else if (me.isLocal && me == masterPlayer)
|
}
|
||||||
{
|
// the master player left, so everyone should set the owner null (we should get a new master shortly)
|
||||||
TakeOwnership(kvp.Key);
|
else if (players[jm.userId] == masterPlayer)
|
||||||
}
|
{
|
||||||
// the master player left, so everyone should set the owner null (we should get a new master shortly)
|
kvp.Value.owner = null;
|
||||||
else if (players[jm.userId] == masterPlayer)
|
|
||||||
{
|
|
||||||
kvp.Value.owner = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this may check for ownership in the future. We don't need ownership here
|
|
||||||
deleteObjects.ForEach(NetworkDestroy);
|
|
||||||
|
|
||||||
players.Remove(jm.userId);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// TODO this may check for ownership in the future. We don't need ownership here
|
||||||
|
deleteObjects.ForEach(NetworkDestroy);
|
||||||
|
|
||||||
|
VelNetPlayer leftPlayer = players[jm.userId];
|
||||||
|
players.Remove(jm.userId);
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// we got a join message, create it
|
OnPlayerLeft?.Invoke(leftPlayer);
|
||||||
VelNetPlayer player = new VelNetPlayer
|
|
||||||
{
|
|
||||||
isLocal = false,
|
|
||||||
room = jm.room,
|
|
||||||
userid = jm.userId
|
|
||||||
};
|
|
||||||
players.Add(jm.userId, player);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
OnPlayerJoined?.Invoke(player);
|
|
||||||
}
|
|
||||||
// prevent errors in subscribers from breaking our code
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
// prevent errors in subscribers from breaking our code
|
||||||
break;
|
catch (Exception e)
|
||||||
|
|
||||||
}
|
|
||||||
case DataMessage dm: {
|
|
||||||
if (players.ContainsKey(dm.senderId))
|
|
||||||
{
|
|
||||||
players[dm.senderId]?.HandleMessage(dm); //todo
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("Received message from player that doesn't exist ");
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
case ChangeMasterMessage cm: {
|
|
||||||
|
|
||||||
if (masterPlayer == null)
|
|
||||||
{
|
|
||||||
masterPlayer = players[cm.masterId];
|
|
||||||
|
|
||||||
// no master player yet, add the scene objects
|
|
||||||
|
|
||||||
for (int i = 0; i < sceneObjects.Length; i++)
|
|
||||||
{
|
{
|
||||||
sceneObjects[i].networkId = -1 + "-" + i;
|
Debug.LogError(e);
|
||||||
sceneObjects[i].owner = masterPlayer;
|
|
||||||
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
|
|
||||||
objects.Add(sceneObjects[i].networkId, sceneObjects[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
masterPlayer = players[cm.masterId];
|
// we got a join message, create it
|
||||||
|
VelNetPlayer player = new VelNetPlayer
|
||||||
|
{
|
||||||
|
isLocal = false,
|
||||||
|
room = jm.room,
|
||||||
|
userid = jm.userId
|
||||||
|
};
|
||||||
|
players.Add(jm.userId, player);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
OnPlayerJoined?.Invoke(player);
|
||||||
|
}
|
||||||
|
// prevent errors in subscribers from breaking our code
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
masterPlayer.SetAsMasterPlayer();
|
|
||||||
|
|
||||||
// master player should take over any objects that do not have an owner
|
|
||||||
foreach (KeyValuePair<string, NetworkObject> kvp in objects)
|
|
||||||
{
|
|
||||||
kvp.Value.owner ??= masterPlayer;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DataMessage dm:
|
||||||
|
{
|
||||||
|
if (players.ContainsKey(dm.senderId))
|
||||||
|
{
|
||||||
|
players[dm.senderId]?.HandleMessage(dm); //todo
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("Received message from player that doesn't exist ");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ChangeMasterMessage cm:
|
||||||
|
{
|
||||||
|
if (masterPlayer == null)
|
||||||
|
{
|
||||||
|
masterPlayer = players[cm.masterId];
|
||||||
|
|
||||||
|
// no master player yet, add the scene objects
|
||||||
|
|
||||||
|
for (int i = 0; i < sceneObjects.Length; i++)
|
||||||
|
{
|
||||||
|
sceneObjects[i].networkId = -1 + "-" + i;
|
||||||
|
sceneObjects[i].owner = masterPlayer;
|
||||||
|
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
|
||||||
|
objects.Add(sceneObjects[i].networkId, sceneObjects[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
masterPlayer = players[cm.masterId];
|
||||||
|
}
|
||||||
|
|
||||||
|
masterPlayer.SetAsMasterPlayer();
|
||||||
|
|
||||||
|
// master player should take over any objects that do not have an owner
|
||||||
|
foreach (KeyValuePair<string, NetworkObject> kvp in objects)
|
||||||
|
{
|
||||||
|
kvp.Value.owner ??= masterPlayer;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//MessageReceived?.Invoke(m);
|
//MessageReceived?.Invoke(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -421,7 +465,6 @@ namespace VelNet
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
clientReceiveThread = new Thread(ListenForData);
|
clientReceiveThread = new Thread(ListenForData);
|
||||||
clientReceiveThread.IsBackground = true;
|
|
||||||
clientReceiveThread.Start();
|
clientReceiveThread.Start();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
@ -432,10 +475,9 @@ namespace VelNet
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Runs in background clientReceiveThread; Listens for incomming data.
|
/// Runs in background clientReceiveThread; Listens for incoming data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
///
|
private static byte[] ReadExact(Stream stream, int N)
|
||||||
private byte[] ReadExact(NetworkStream stream, int N)
|
|
||||||
{
|
{
|
||||||
byte[] toReturn = new byte[N];
|
byte[] toReturn = new byte[N];
|
||||||
|
|
||||||
|
|
@ -446,20 +488,15 @@ namespace VelNet
|
||||||
numRead += stream.Read(toReturn, numRead, numLeft);
|
numRead += stream.Read(toReturn, numRead, numLeft);
|
||||||
numLeft = N - numRead;
|
numLeft = N - numRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int GetIntFromBytes(byte[] bytes)
|
private static int GetIntFromBytes(byte[] bytes)
|
||||||
{
|
{
|
||||||
if (BitConverter.IsLittleEndian)
|
return BitConverter.ToInt32(BitConverter.IsLittleEndian ? bytes.Reverse().ToArray() : bytes, 0);
|
||||||
{
|
|
||||||
return BitConverter.ToInt32(bytes.Reverse().ToArray(),0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return BitConverter.ToInt32(bytes, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ListenForData()
|
private void ListenForData()
|
||||||
{
|
{
|
||||||
connected = true;
|
connected = true;
|
||||||
|
|
@ -476,44 +513,43 @@ namespace VelNet
|
||||||
//SendToGroup("close", Encoding.UTF8.GetBytes("HelloGroup"));
|
//SendToGroup("close", Encoding.UTF8.GetBytes("HelloGroup"));
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Get a stream object for reading
|
// Get a stream object for reading
|
||||||
|
|
||||||
|
|
||||||
//read a byte
|
//read a byte
|
||||||
byte type = (byte)stream.ReadByte();
|
byte type = (byte)stream.ReadByte();
|
||||||
|
|
||||||
if (type == 0) //login
|
if (type == 0) //login
|
||||||
{
|
{
|
||||||
LoginMessage m = new LoginMessage();
|
LoginMessage m = new LoginMessage();
|
||||||
m.userId = GetIntFromBytes(ReadExact(stream, 4)); //not really the sender...
|
m.userId = GetIntFromBytes(ReadExact(stream, 4)); //not really the sender...
|
||||||
AddMessage(m);
|
AddMessage(m);
|
||||||
}
|
}
|
||||||
else if(type == 1) //rooms
|
else if (type == 1) //rooms
|
||||||
{
|
{
|
||||||
|
|
||||||
RoomsMessage m = new RoomsMessage();
|
RoomsMessage m = new RoomsMessage();
|
||||||
m.rooms = new List<ListedRoom>();
|
m.rooms = new List<ListedRoom>();
|
||||||
int N = GetIntFromBytes(ReadExact(stream, 4)); //the size of the payload
|
int N = GetIntFromBytes(ReadExact(stream, 4)); //the size of the payload
|
||||||
byte[] utf8data = ReadExact(stream, N);
|
byte[] utf8data = ReadExact(stream, N);
|
||||||
string roomMessage = Encoding.UTF8.GetString(utf8data);
|
string roomMessage = Encoding.UTF8.GetString(utf8data);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
string[] sections = roomMessage.Split(',');
|
string[] sections = roomMessage.Split(',');
|
||||||
foreach (string s in sections)
|
foreach (string s in sections)
|
||||||
{
|
{
|
||||||
string[] pieces = s.Split(':');
|
string[] pieces = s.Split(':');
|
||||||
if (pieces.Length == 2) {
|
if (pieces.Length == 2)
|
||||||
|
{
|
||||||
ListedRoom lr = new ListedRoom();
|
ListedRoom lr = new ListedRoom();
|
||||||
lr.name = pieces[0];
|
lr.name = pieces[0];
|
||||||
lr.numUsers = int.Parse(pieces[1]);
|
lr.numUsers = int.Parse(pieces[1]);
|
||||||
m.rooms.Add(lr);
|
m.rooms.Add(lr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AddMessage(m);
|
AddMessage(m);
|
||||||
}
|
}
|
||||||
else if(type == 2) //joined
|
else if (type == 2) //joined
|
||||||
{
|
{
|
||||||
JoinMessage m = new JoinMessage();
|
JoinMessage m = new JoinMessage();
|
||||||
m.userId = GetIntFromBytes(ReadExact(stream, 4));
|
m.userId = GetIntFromBytes(ReadExact(stream, 4));
|
||||||
|
|
@ -521,7 +557,8 @@ namespace VelNet
|
||||||
byte[] utf8data = ReadExact(stream, N); //the room name, encoded as utf-8
|
byte[] utf8data = ReadExact(stream, N); //the room name, encoded as utf-8
|
||||||
m.room = Encoding.UTF8.GetString(utf8data);
|
m.room = Encoding.UTF8.GetString(utf8data);
|
||||||
AddMessage(m);
|
AddMessage(m);
|
||||||
}else if(type == 3) //data
|
}
|
||||||
|
else if (type == 3) //data
|
||||||
{
|
{
|
||||||
DataMessage m = new DataMessage();
|
DataMessage m = new DataMessage();
|
||||||
m.senderId = GetIntFromBytes(ReadExact(stream, 4));
|
m.senderId = GetIntFromBytes(ReadExact(stream, 4));
|
||||||
|
|
@ -529,10 +566,10 @@ namespace VelNet
|
||||||
m.data = ReadExact(stream, N); //the message
|
m.data = ReadExact(stream, N); //the message
|
||||||
AddMessage(m);
|
AddMessage(m);
|
||||||
}
|
}
|
||||||
else if(type == 4) //new master
|
else if (type == 4) //new master
|
||||||
{
|
{
|
||||||
ChangeMasterMessage m = new ChangeMasterMessage();
|
ChangeMasterMessage m = new ChangeMasterMessage();
|
||||||
m.masterId = (int)GetIntFromBytes(ReadExact(stream, 4)); //sender is the new master
|
m.masterId = GetIntFromBytes(ReadExact(stream, 4)); //sender is the new master
|
||||||
AddMessage(m);
|
AddMessage(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -584,20 +621,24 @@ namespace VelNet
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
int numReceived = udpSocket.Receive(buffer);
|
int numReceived = udpSocket.Receive(buffer);
|
||||||
if (buffer[0] == 0)
|
switch (buffer[0])
|
||||||
{
|
{
|
||||||
Debug.Log("UDP connected");
|
case 0:
|
||||||
}else if (buffer[0] == 3)
|
Debug.Log("UDP connected");
|
||||||
{
|
break;
|
||||||
DataMessage m = new DataMessage();
|
case 3:
|
||||||
//we should get the sender address
|
{
|
||||||
byte[] senderBytes = new byte[4];
|
DataMessage m = new DataMessage();
|
||||||
Array.Copy(buffer, 1, senderBytes, 0, 4);
|
//we should get the sender address
|
||||||
m.senderId = GetIntFromBytes(senderBytes);
|
byte[] senderBytes = new byte[4];
|
||||||
byte[] messageBytes = new byte[numReceived - 5];
|
Array.Copy(buffer, 1, senderBytes, 0, 4);
|
||||||
Array.Copy(buffer, 5, messageBytes, 0, messageBytes.Length);
|
m.senderId = GetIntFromBytes(senderBytes);
|
||||||
m.data = messageBytes;
|
byte[] messageBytes = new byte[numReceived - 5];
|
||||||
AddMessage(m);
|
Array.Copy(buffer, 5, messageBytes, 0, messageBytes.Length);
|
||||||
|
m.data = messageBytes;
|
||||||
|
AddMessage(m);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -634,8 +675,7 @@ namespace VelNet
|
||||||
NetworkStream stream = instance.socketConnection.GetStream();
|
NetworkStream stream = instance.socketConnection.GetStream();
|
||||||
if (stream.CanWrite)
|
if (stream.CanWrite)
|
||||||
{
|
{
|
||||||
|
stream.Write(message, 0, message.Length);
|
||||||
stream.Write(message,0,message.Length);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SocketException socketException)
|
catch (SocketException socketException)
|
||||||
|
|
@ -652,9 +692,9 @@ namespace VelNet
|
||||||
{
|
{
|
||||||
return BitConverter.GetBytes(n).Reverse().ToArray();
|
return BitConverter.GetBytes(n).Reverse().ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Login(string username, string password)
|
public static void Login(string username, string password)
|
||||||
{
|
{
|
||||||
|
|
||||||
MemoryStream stream = new MemoryStream();
|
MemoryStream stream = new MemoryStream();
|
||||||
BinaryWriter writer = new BinaryWriter(stream);
|
BinaryWriter writer = new BinaryWriter(stream);
|
||||||
|
|
||||||
|
|
@ -667,14 +707,11 @@ namespace VelNet
|
||||||
writer.Write(pB);
|
writer.Write(pB);
|
||||||
|
|
||||||
SendTcpMessage(stream.ToArray());
|
SendTcpMessage(stream.ToArray());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void GetRooms()
|
public static void GetRooms()
|
||||||
{
|
{
|
||||||
|
SendTcpMessage(new byte[] { 1 }); //very simple message
|
||||||
SendTcpMessage(new byte[1] { 1 }); //very simple message
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -691,13 +728,8 @@ namespace VelNet
|
||||||
writer.Write((byte)R.Length);
|
writer.Write((byte)R.Length);
|
||||||
writer.Write(R);
|
writer.Write(R);
|
||||||
SendTcpMessage(stream.ToArray());
|
SendTcpMessage(stream.ToArray());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Leaves a room if we're in one
|
/// Leaves a room if we're in one
|
||||||
|
|
@ -712,12 +744,12 @@ namespace VelNet
|
||||||
|
|
||||||
public static void SendToRoom(byte[] message, bool include_self = false, bool reliable = true, bool ordered = false)
|
public static void SendToRoom(byte[] message, bool include_self = false, bool reliable = true, bool ordered = false)
|
||||||
{
|
{
|
||||||
byte sendType = (byte) MessageSendType.MESSAGE_OTHERS;
|
byte sendType = (byte)MessageSendType.MESSAGE_OTHERS;
|
||||||
if (include_self && ordered) sendType = (byte)MessageSendType.MESSAGE_ALL_ORDERED;
|
if (include_self && ordered) sendType = (byte)MessageSendType.MESSAGE_ALL_ORDERED;
|
||||||
if (include_self && !ordered) sendType = (byte)MessageSendType.MESSAGE_ALL;
|
if (include_self && !ordered) sendType = (byte)MessageSendType.MESSAGE_ALL;
|
||||||
if (!include_self && ordered) sendType = (byte)MessageSendType.MESSAGE_OTHERS_ORDERED;
|
if (!include_self && ordered) sendType = (byte)MessageSendType.MESSAGE_OTHERS_ORDERED;
|
||||||
|
|
||||||
|
|
||||||
if (reliable)
|
if (reliable)
|
||||||
{
|
{
|
||||||
MemoryStream stream = new MemoryStream();
|
MemoryStream stream = new MemoryStream();
|
||||||
|
|
@ -730,10 +762,10 @@ namespace VelNet
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//udp message needs the type
|
//udp message needs the type
|
||||||
toSend[0] = sendType; //we don't
|
toSend[0] = sendType; //we don't
|
||||||
Array.Copy(get_be_bytes(instance.userid), 0, toSend, 1, 4);
|
Array.Copy(get_be_bytes(instance.userid), 0, toSend, 1, 4);
|
||||||
Array.Copy(message, 0, toSend, 5, message.Length);
|
Array.Copy(message, 0, toSend, 5, message.Length);
|
||||||
SendUdpMessage(toSend,message.Length+5); //shouldn't be over 1024...
|
SendUdpMessage(toSend, message.Length + 5); //shouldn't be over 1024...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -759,7 +791,7 @@ namespace VelNet
|
||||||
//also need to send the group
|
//also need to send the group
|
||||||
toSend[5] = (byte)utf8bytes.Length;
|
toSend[5] = (byte)utf8bytes.Length;
|
||||||
Array.Copy(utf8bytes, 0, toSend, 6, utf8bytes.Length);
|
Array.Copy(utf8bytes, 0, toSend, 6, utf8bytes.Length);
|
||||||
Array.Copy(message, 0, toSend, 6+utf8bytes.Length, message.Length);
|
Array.Copy(message, 0, toSend, 6 + utf8bytes.Length, message.Length);
|
||||||
SendUdpMessage(toSend, 6 + utf8bytes.Length + message.Length);
|
SendUdpMessage(toSend, 6 + utf8bytes.Length + message.Length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -785,6 +817,7 @@ namespace VelNet
|
||||||
{
|
{
|
||||||
writer.Write(get_be_bytes(client_ids[i]));
|
writer.Write(get_be_bytes(client_ids[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
SendTcpMessage(stream.ToArray());
|
SendTcpMessage(stream.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -805,6 +838,7 @@ namespace VelNet
|
||||||
Debug.LogError("Can't instantiate object. Obj with that network ID was already instantiated.", instance.objects[networkId]);
|
Debug.LogError("Can't instantiate object. Obj with that network ID was already instantiated.", instance.objects[networkId]);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkObject newObject = Instantiate(prefab);
|
NetworkObject newObject = Instantiate(prefab);
|
||||||
newObject.networkId = networkId;
|
newObject.networkId = networkId;
|
||||||
newObject.prefabName = prefabName;
|
newObject.prefabName = prefabName;
|
||||||
|
|
@ -812,7 +846,7 @@ namespace VelNet
|
||||||
instance.objects.Add(newObject.networkId, newObject);
|
instance.objects.Add(newObject.networkId, newObject);
|
||||||
|
|
||||||
// only sent to others, as I already instantiated this. Nice that it happens immediately.
|
// only sent to others, as I already instantiated this. Nice that it happens immediately.
|
||||||
SendToRoom(Encoding.UTF8.GetBytes("7," + newObject.networkId + "," + prefabName),false,true);
|
SendToRoom(Encoding.UTF8.GetBytes("7," + newObject.networkId + "," + prefabName), false, true);
|
||||||
|
|
||||||
return newObject;
|
return newObject;
|
||||||
}
|
}
|
||||||
|
|
@ -842,6 +876,7 @@ namespace VelNet
|
||||||
instance.objects.Remove(networkId);
|
instance.objects.Remove(networkId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.isSceneObject)
|
if (obj.isSceneObject)
|
||||||
{
|
{
|
||||||
instance.deletedSceneObjects.Add(networkId);
|
instance.deletedSceneObjects.Add(networkId);
|
||||||
|
|
@ -864,7 +899,7 @@ namespace VelNet
|
||||||
Debug.LogError("Can't take ownership. No local player.");
|
Debug.LogError("Can't take ownership. No local player.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// obj must exist
|
// obj must exist
|
||||||
if (!instance.objects.ContainsKey(networkId))
|
if (!instance.objects.ContainsKey(networkId))
|
||||||
{
|
{
|
||||||
|
|
@ -878,7 +913,7 @@ namespace VelNet
|
||||||
Debug.LogError("Can't take ownership. Ownership for this object is locked.");
|
Debug.LogError("Can't take ownership. Ownership for this object is locked.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// immediately successful
|
// immediately successful
|
||||||
instance.objects[networkId].owner = LocalPlayer;
|
instance.objects[networkId].owner = LocalPlayer;
|
||||||
|
|
||||||
|
|
@ -888,4 +923,4 @@ namespace VelNet
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "edu.uga.engr.vel.velnet",
|
"name": "edu.uga.engr.vel.velnet",
|
||||||
"displayName": "VelNet",
|
"displayName": "VelNet",
|
||||||
"version": "1.0.7",
|
"version": "1.0.8",
|
||||||
"unity": "2019.1",
|
"unity": "2019.1",
|
||||||
"description": "A custom networking library for Unity.",
|
"description": "A custom networking library for Unity.",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue