/// <summary> /// Accept an object from the server to be run on the client /// </summary> /// <param name="controlCode"></param> /// <param name="bytes"></param> private void RunOnClient(NetworkControlCode controlCode, byte[] bytes) { object o = NetSerialize.Deserialize(bytes); //If object is a ping, respond immediately, to minimize lag //This should be replaced with raw data and custom control codes that are faster to deserialize to help minimize lag from object deserialization if (o is Ping p) { float time = NetTime.now; p.clientNumber = clientNumber; p.clientTime = time; udp.Send(NetworkControlCode.runOnServer, NetSerialize.Serialize(p)); } else { lock (objectQueue) { objectQueue.Enqueue(o); } #if UNITY_EDITOR AddToObjectList("FromServer: " + o.ToString()); #endif } }
public override void Send(NetworkControlCode controlCode, byte[] data) { //if(debug) //{ // UnityEngine.Debug.Log("Sending: " + client.Client.RemoteEndPoint.ToString()); //} int l = data.Length + 2 * sizeof(int); byte[] output = new byte[l]; byte[] cCode = ToBytes((int)controlCode); byte[] len = ToBytes(data.Length); Array.Copy(cCode, 0, output, 0, sizeof(int)); Array.Copy(len, 0, output, sizeof(int), sizeof(int)); Array.Copy(data, 0, output, sizeof(int) * 2, data.Length); //IPEndPoint endPoint = new IPEndPoint(remoteAddress, remotePort); //if(connected) //{ // client.Send(output, output.Length); //} //else //{ // client.Send(output, l, endPoint); //} //UnityEngine.Debug.Log("Send: " + client.Send(output, l)); client.Send(output, l); }
/// <summary> /// Serialize an object and send it to the server over TCP /// </summary> /// <param name="controlCode"></param> /// <param name="o"></param> private void _SendObjectToServerTCP(NetworkControlCode controlCode, object o) { LogFile.WriteLineToLog(LogFile.FileNames.clientObjectLog, "Sending Object To Server TCP: " + o.GetType().ToString()); #if UNITY_EDITOR objectList.Enqueue("ToServer: " + o.ToString()); #endif byte[] bytes = NetSerialize.Serialize(o); instance.tcp.Send(controlCode, bytes); }
/// <summary> /// Send an object to the server over UDP /// </summary> /// <param name="controlCode"></param> /// <param name="o"></param> public void _SendObjectToServerUdp(NetworkControlCode controlCode, object o) { LogFile.WriteLineToLog(LogFile.FileNames.clientObjectLog, "Sending Object To Server UDP: " + o.GetType().ToString()); #if UNITY_EDITOR lock (objectList) { AddToObjectList("ToServerUdp: " + o.ToString()); } #endif byte[] arr = NetSerialize.Serialize(o); instance.udp.Send(controlCode, arr); }
public override void Send(NetworkControlCode controlCode, byte[] data, IPEndPoint endPoint) { int l = data.Length + 2 * sizeof(int); byte[] output = new byte[l]; byte[] cCode = ToBytes((int)controlCode); byte[] len = ToBytes(data.Length); Array.Copy(cCode, 0, output, 0, sizeof(int)); Array.Copy(len, 0, output, sizeof(int), sizeof(int)); Array.Copy(data, 0, output, sizeof(int) * 2, data.Length); client.Send(output, l, endPoint); }
public override (NetworkControlCode controlCode, byte[] data, IPEndPoint endPoint) Receive() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); byte[] inData = client.Receive(ref endPoint); int controlCode; int l; byte[] data; using (MemoryStream ms = new MemoryStream(inData)) { using (BinaryReader br = new BinaryReader(ms)) { controlCode = br.ReadInt32(); l = br.ReadInt32(); data = br.ReadBytes(l); } } return((NetworkControlCode)controlCode, data, endPoint); }
public override void Send(NetworkControlCode controlCode, byte[] data) { lock (stream) { byte[] cCode = ToBytes((int)controlCode); for (int i = 0; i < cCode.Length; i++) { stream.WriteByte(cCode[i]); } byte[] len = ToBytes(data.Length); for (int i = 0; i < cCode.Length; i++) { stream.WriteByte(len[i]); } for (int i = 0; i < data.Length; i++) { stream.WriteByte(data[i]); } } }
/// <summary> /// Send an object to the server over UDP /// </summary> /// <param name="controlCode"></param> /// <param name="o"></param> public static void SendObjectToServerUdp(NetworkControlCode controlCode, object o) { instance?._SendObjectToServerUdp(controlCode, o); }
public override void Send(NetworkControlCode controlCode, byte[] data, IPEndPoint endPoint) { throw new NotImplementedException(); }
public abstract void Send(NetworkControlCode controlCode, byte[] data, IPEndPoint endPoint);
public abstract void Send(NetworkControlCode controlCode, byte[] data);