public async Task <ILog> Listen() { ILog error = null; while (clientState.Connected) { try { var buffer = new ArraySegment <byte>(new byte[64 * 1024 * 1024]); int read = await socket.ReceiveAsync(buffer, SocketFlags.None); Serializable.Context3D context = Serializable.Context3D.Parser.ParseFrom(buffer.Take(read).ToArray()); if (context.Client) { contextHandler.HandleContext(context); } else { contextHandler.HandleContext(context); contextHandler.SendContext(context.Tick); } } catch (Exception e) { clientState.Connected = false; if (socket == null) { return(null); } return(new Error("[UDP] " + e)); } } return(error); }
public void SendContext(int tick) { Serializable.Context3D context = new Serializable.Context3D { Tick = tick, RigidBodies = { clientContextManager.playerOwnedRigidbodiesByGUID.Select(r => r.Value.Export()) }, }; // Debug.Log(context.RigidBodies.Count); client.Send(context); }
public async void Send(Serializable.Context3D context) { if (!udpClient.Active) { Debug.Log("[UDP] Sending packet failed. UDP client not active."); return; } // Debug.Log("[UDP] Sent packet (" + context.RigidBodies.Count + ")"); await udpClient.Send(context.ToByteArray()); // Debug.Log("[UDP] Sent context."); }
//public async Error Send(Packet packet) public async Task <ILog> Send(Serializable.Context3D packet) { ArraySegment <byte> sendBuffer = new ArraySegment <byte>(packet.ToByteArray()); //if (debug) Debug.Log(string.Format("WS - {0} {1}", "Sending packet", packet.Header.OpCode)); using (CancellationTokenSource cts = new CancellationTokenSource()) { try { await clientWebSocket.SendAsync(sendBuffer, WebSocketMessageType.Binary, true, cts.Token); return(null); } catch { return(new Error("[WS] Client failed to send packet")); } } }
public void HandleContext(Serializable.Context3D context) { // foreach (Serializable.Transform st in context.Transforms) // { // NetSynced.Transform t; // if (ContextManager.worldOwnedTransformsByGUID.TryGetValue(st.ID, out t)) // { // t.transform.position = st.Position.ToUnityVector(); // } // } // if (context.Client) foreach (Serializable.Rigidbody3D sr in context.RigidBodies) { NetSynced.Rigidbody3D r; if (simulationContextManager.worldOwnedRigidbodiesByGUID.TryGetValue(sr.ID, out r)) { r.Sync(sr.Position.ToUnityVector(), sr.Rotation.ToUnityQuaterion(), sr.Velocity.ToUnityVector()); } } }
public void HandleContext(Serializable.Context3D context) { foreach (Serializable.Transform st in context.Transforms) { NetSynced.Transform t; if (clientContextManager.worldOwnedTransformsByGUID.TryGetValue(st.ID, out t)) { t.transform.position = st.Position.ToUnityVector(); } } foreach (Serializable.Rigidbody3D sr in context.RigidBodies) { NetSynced.Rigidbody3D r; if (clientContextManager.worldOwnedRigidbodiesByGUID.TryGetValue(sr.ID, out r)) { r.Sync(sr.Position.ToUnityVector(), sr.Rotation.ToUnityQuaterion(), sr.Velocity.ToUnityVector()); } } // Debug.Log("Synced " + context.RigidBodies.Count + " rigidbodies"); }