Created
February 12, 2020 16:19
-
-
Save rdeioris/6062c9955a36a372484cdfe4c86af6de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System.Net; | |
using System.Net.Sockets; | |
using System; | |
public class PacketsManager : MonoBehaviour | |
{ | |
[SerializeField] | |
string address = "0.0.0.0"; | |
[SerializeField] | |
int port = 5000; | |
[SerializeField] | |
int maxPacketsPerFrame = 100; | |
[SerializeField] | |
int maxPacketSize = 256; | |
Dictionary<uint, GameObject> gameObjectsMapping; | |
Socket socket; | |
byte[] data; | |
void Awake() | |
{ | |
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | |
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(address), port); | |
socket.Blocking = false; | |
socket.Bind(endPoint); | |
data = new byte[maxPacketSize]; | |
gameObjectsMapping = new Dictionary<uint, GameObject>(); | |
} | |
public void RegisterNetworkObject(uint networkId, GameObject networkObject) | |
{ | |
gameObjectsMapping[networkId] = networkObject; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
for (int i = 0; i < maxPacketsPerFrame; i++) | |
{ | |
EndPoint sender = new IPEndPoint(0, 0); | |
int rlen = -1; | |
try | |
{ | |
rlen = socket.ReceiveFrom(data, maxPacketSize, SocketFlags.None, ref sender); | |
} | |
catch (SocketException e) | |
{ | |
break; | |
} | |
if (rlen > 0) | |
{ | |
ManagePacket(rlen); | |
} | |
} | |
} | |
void ManagePacket(int size) | |
{ | |
if (data[0] == 1) | |
{ | |
if (size != 38) | |
return; | |
byte objectIndex = data[1]; | |
if (!gameObjectsMapping.ContainsKey(objectIndex)) | |
return; | |
GameObject gameObjectToMove = gameObjectsMapping[objectIndex]; | |
float x = BitConverter.ToSingle(data, 2); | |
float y = BitConverter.ToSingle(data, 6); | |
float z = BitConverter.ToSingle(data, 10); | |
float rx = BitConverter.ToSingle(data, 14); | |
float ry = BitConverter.ToSingle(data, 18); | |
float rz = BitConverter.ToSingle(data, 22); | |
float sx = BitConverter.ToSingle(data, 26); | |
float sy = BitConverter.ToSingle(data, 30); | |
float sz = BitConverter.ToSingle(data, 34); | |
gameObjectToMove.transform.position = new Vector3(x, y, z); | |
gameObjectToMove.transform.rotation = Quaternion.Euler(rx, ry, rz); | |
gameObjectToMove.transform.localScale = new Vector3(sx, sy, sz); | |
return; | |
} | |
if (data[0] == 2) | |
{ | |
if (size != 14) | |
return; | |
byte objectIndex = data[1]; | |
if (!gameObjectsMapping.ContainsKey(objectIndex)) | |
return; | |
GameObject gameObjectToColor = gameObjectsMapping[objectIndex]; | |
float r = BitConverter.ToSingle(data, 2); | |
float g = BitConverter.ToSingle(data, 6); | |
float b = BitConverter.ToSingle(data, 10); | |
gameObjectToColor.GetComponent<MeshRenderer>().sharedMaterial.color = new Color(r, g, b); | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment