made sending messages lower level

handTracking
Kyle Johnsen 2022-01-04 17:39:13 -05:00
parent 34732b43f6
commit 001e89509f
8 changed files with 45 additions and 56 deletions

View File

@ -1819,7 +1819,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 03a4d4e1a7fd74c7ab2eccca4ce168db, type: 3} m_Script: {fileID: 11500000, guid: 03a4d4e1a7fd74c7ab2eccca4ce168db, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
host: localhost host: neko.ugavel.com
port: 3290 port: 3290
userid: -1 userid: -1
room: room:

View File

@ -279,6 +279,7 @@ public class NetworkManager : MonoBehaviour
try try
{ {
socketConnection = new TcpClient(host, port); socketConnection = new TcpClient(host, port);
socketConnection.NoDelay = true;
Byte[] bytes = new Byte[1024]; Byte[] bytes = new Byte[1024];
string partialMessage = ""; string partialMessage = "";
while (true) while (true)

View File

@ -5,11 +5,10 @@ using UnityEngine;
/// <summary> /// <summary>
/// This is a base class for all objects that a player can instantiated/owned /// This is a base class for all objects that a player can instantiated/owned
/// </summary> /// </summary>
public abstract class NetworkObject: MonoBehaviour, NetworkSyncable public abstract class NetworkObject: MonoBehaviour
{ {
public NetworkPlayer owner; public NetworkPlayer owner;
public string networkId; //this is forged from the combination of the creator's id (-1 in the case of a scene object) and an object id, so it's always unique for a room public string networkId; //this is forged from the combination of the creator's id (-1 in the case of a scene object) and an object id, so it's always unique for a room
public string prefabName; //this may be empty if it's not a prefab (scene object) public string prefabName; //this may be empty if it's not a prefab (scene object)
public abstract byte[] getSyncMessage(); public abstract void handleMessage(string identifier, byte[] message);
public abstract void handleSyncMessage(byte[] message);
} }

View File

@ -99,8 +99,9 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
{ {
case "1": //update my object's data case "1": //update my object's data
{ {
byte[] message = Convert.FromBase64String(sections[1]); string identifier = sections[1];
myObject.handleSyncMessage(message); byte[] message = Convert.FromBase64String(sections[2]);
myObject.handleMessage(identifier, message);
break; break;
} }
case "2": //audio data case "2": //audio data
@ -144,13 +145,14 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
{ {
string objectKey = sections[1]; string objectKey = sections[1];
string syncMessage = sections[2]; string identifier = sections[2];
string syncMessage = sections[3];
byte[] messageBytes = Convert.FromBase64String(syncMessage); byte[] messageBytes = Convert.FromBase64String(syncMessage);
if (manager.objects.ContainsKey(objectKey)) if (manager.objects.ContainsKey(objectKey))
{ {
if(manager.objects[objectKey].owner == this) if(manager.objects[objectKey].owner == this)
{ {
manager.objects[objectKey].handleSyncMessage(messageBytes); manager.objects[objectKey].handleMessage(identifier, messageBytes);
} }
} }
@ -223,7 +225,6 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
public void setDissonanceID(string id) //this sort of all initializes dissonance public void setDissonanceID(string id) //this sort of all initializes dissonance
{ {
dissonanceID = id; dissonanceID = id;
Debug.Log("here");
manager.sendTo(NetworkManager.MessageType.OTHERS, "3," + id+";"); manager.sendTo(NetworkManager.MessageType.OTHERS, "3," + id+";");
commsNetwork.comms.TrackPlayerPosition(this); commsNetwork.comms.TrackPlayerPosition(this);
} }
@ -234,17 +235,18 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
//if I'm master, I'm now responsible for updating all scene objects //if I'm master, I'm now responsible for updating all scene objects
//FindObjectsOfType<NetworkObject>(); //FindObjectsOfType<NetworkObject>();
} }
public void syncObject(NetworkObject obj)
public void sendMessage(NetworkObject obj, string identifier, byte[] data)
{ {
byte[] data = obj.getSyncMessage();
if (obj == myObject) if (obj == myObject)
{ {
manager.sendTo(NetworkManager.MessageType.OTHERS, "1," + Convert.ToBase64String(data)); manager.sendTo(NetworkManager.MessageType.OTHERS, "1," + identifier +"," + Convert.ToBase64String(data));
} }
else else
{ {
manager.sendTo(NetworkManager.MessageType.OTHERS, "5," + obj.networkId + "," + Convert.ToBase64String(data)); manager.sendTo(NetworkManager.MessageType.OTHERS, "5," + obj.networkId + "," + identifier + "," + Convert.ToBase64String(data));
} }
} }

View File

@ -1,13 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This makes serialization somewhat uniform
/// </summary>
public interface NetworkSyncable
{
public byte[] getSyncMessage(); //local owner asks for this and sends it periodically
public void handleSyncMessage(byte[] message); //remote owner will call this
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 8e4266960bfa444998dda57f911448f0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -10,7 +10,7 @@ public class PlayerController : NetworkObject
public Quaternion targetRotation; public Quaternion targetRotation;
public override byte[] getSyncMessage() public byte[] getSyncMessage()
{ {
float[] data = new float[7]; float[] data = new float[7];
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -25,8 +25,11 @@ public class PlayerController : NetworkObject
return toReturn; return toReturn;
} }
public override void handleSyncMessage(byte[] message) public override void handleMessage(string identifier, byte[] message)
{ {
switch (identifier)
{
case "s":
float[] data = new float[7]; float[] data = new float[7];
Buffer.BlockCopy(message, 0, data, 0, message.Length); Buffer.BlockCopy(message, 0, data, 0, message.Length);
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -35,6 +38,8 @@ public class PlayerController : NetworkObject
targetRotation[i] = data[i + 3]; targetRotation[i] = data[i + 3];
} }
targetRotation[3] = data[6]; targetRotation[3] = data[6];
break;
}
} }
// Start is called before the first frame update // Start is called before the first frame update
@ -50,7 +55,7 @@ public class PlayerController : NetworkObject
if (owner != null && owner.isLocal) if (owner != null && owner.isLocal)
{ {
owner.syncObject(this); owner.sendMessage(this, "s", getSyncMessage());
} }
yield return new WaitForSeconds(.1f); yield return new WaitForSeconds(.1f);
} }

View File

@ -16,7 +16,7 @@ public class SyncTransform : NetworkObject
public Quaternion targetRotation; public Quaternion targetRotation;
public override byte[] getSyncMessage() public byte[] getSyncMessage()
{ {
float[] data = new float[7]; float[] data = new float[7];
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
@ -31,8 +31,11 @@ public class SyncTransform : NetworkObject
return toReturn; return toReturn;
} }
public override void handleSyncMessage(byte[] message) public override void handleMessage(string identifier, byte[] message)
{ {
switch (identifier)
{
case "s":
float[] data = new float[7]; float[] data = new float[7];
Buffer.BlockCopy(message, 0, data, 0, message.Length); Buffer.BlockCopy(message, 0, data, 0, message.Length);
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -41,6 +44,9 @@ public class SyncTransform : NetworkObject
targetRotation[i] = data[i + 3]; targetRotation[i] = data[i + 3];
} }
targetRotation[3] = data[6]; targetRotation[3] = data[6];
break;
}
} }
// Start is called before the first frame update // Start is called before the first frame update
@ -56,7 +62,7 @@ public class SyncTransform : NetworkObject
if (owner != null && owner.isLocal) if (owner != null && owner.isLocal)
{ {
owner.syncObject(this); owner.sendMessage(this, "s", getSyncMessage());
} }
yield return new WaitForSeconds(.1f); yield return new WaitForSeconds(.1f);
} }