public static List <MinimumAvatarPacket> BufferToPackets(byte[] buf) { if (buf != null && buf.Length > 0) { var numPackets = BitConverter.ToInt32(buf, 0); var supposedBufSize = numPackets * 48 + 16; if (buf.Length == supposedBufSize) { List <MinimumAvatarPacket> packets = new List <MinimumAvatarPacket>(); for (int i = 0; i < numPackets; ++i) { var beginOffset = i * 48; var idLen = buf[16 + beginOffset]; byte[] bStr = new byte[idLen]; Buffer.BlockCopy(buf, 16 + 1 + beginOffset, bStr, 0, idLen); var userId = System.Text.Encoding.UTF8.GetString(bStr); var x = BitConverter.ToSingle(buf, 32 + beginOffset); var y = BitConverter.ToSingle(buf, 32 + sizeof(float) + beginOffset); var z = BitConverter.ToSingle(buf, 32 + sizeof(float) * 2 + beginOffset); var radY = BitConverter.ToSingle(buf, 32 + sizeof(float) * 3 + beginOffset); var qx = BitConverter.ToSingle(buf, 48 + beginOffset); var qy = BitConverter.ToSingle(buf, 48 + sizeof(float) + beginOffset); var qz = BitConverter.ToSingle(buf, 48 + sizeof(float) * 2 + beginOffset); var qw = BitConverter.ToSingle(buf, 48 + sizeof(float) * 3 + beginOffset); MinimumAvatarPacket packet = new MinimumAvatarPacket(userId, new Vector3(x, y, z), radY, new Vector4(qx, qy, qz, qw)); packets.Add(packet); } return(packets); } } return(null); }
public void PacketTest1() { var packet = new MinimumAvatarPacket("packet1", new Vector3(2.2f, 5.2f, 6.8f), 50, new Vector4(2.1f, 4.6f, 3.0f, 3.1f)); var buff = Utility.PacketsToBuffer(new List <MinimumAvatarPacket> { packet }); var decodedPacket = Utility.BufferToPackets(buff); Assert.Equal(packet.PaketId, decodedPacket ![0].PaketId);
public void PacketTest1() { var packet = new MinimumAvatarPacket(1, new Vector3(2, 5, 6), 10, new Vector4(2, 4, 3, 120), 0); Assert.True(packet.CheckRange(), "packet.CheckRange()"); var packets = new List <MinimumAvatarPacket> { packet }; var buff = Utility.PacketsToBuffer(ref packets); var decodedPacket = Utility.BufferToPackets(buff); Assert.Equal(packet.PaketId, decodedPacket ![0].PaketId);
protected override async Task Update(int count) { try { var tasks = new List <Task>(); var packet = new MinimumAvatarPacket(Name, _position, 0.0f + count, new Vector4()); var buf = Utility.PacketToBuffer(packet); tasks.Add(udp.SendAsync(buf, buf.Length)); await Task.WhenAll(tasks); } catch (OperationCanceledException) { Printer.PrintDbg("Sender stopped"); throw; } }
public async Task Spawn(MinimumAvatarPacket packet) { var taskFactory = new TaskFactory(); await taskFactory.StartNew(() => { try { remotePlayerPrefab.GetComponent <RemoteTransformRegister>().userId = packet.PaketId; remotePlayerPrefab.GetComponent <ChangeName>().Change(packet.PaketId); _remotePlayers.Add(Instantiate(remotePlayerPrefab, new Vector3(packet.Position.X, packet.Position.Y, packet.Position.Z), new Quaternion(packet.NeckRotation.X, packet.NeckRotation.Y, packet.NeckRotation.Z, packet.NeckRotation.W))); } catch (Exception e) { Debug.Log(e); } }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler); }
protected override async Task Update(int count) { try { var tasks = new List <Task>(); var packet = new MinimumAvatarPacket(id, _position, (0 + count) % 127, new Vector4(), 0); if (!packet.CheckRange()) { throw new MinimumAvatarPacketCreativeException("the value must be less than or equal to 127 in absolute value."); } var buf = Utility.PacketToBuffer(packet); tasks.Add(udp.SendAsync(buf, buf.Length)); await Task.WhenAll(tasks); } catch (OperationCanceledException) { Printer.PrintDbg("Sender stopped"); throw; } catch (SocketException) { Printer.PrintDbg("Waiting for remote"); } }