private bool HandleServerPacket(byte[] data, int size) { if (size == 0) { Debug.Log("Disconnected from the server"); return(false); } int dataIndex = 0; // Keep reading while there is data left in the buffer while (dataIndex < size) { // Parse a packet from the data buffer Packet packet = PacketBuilder.Parse(data, ref dataIndex); // Process the packet switch (packet.Type) { case Packet.PacketType.Text: recvText = ((PacketText)packet).Data; receivedNewText = true; Debug.Log("[S->C]: " + recvText + " (" + packet.Size + " of " + size + " bytes)"); break; case Packet.PacketType.Spawn: spawn = ((PacketSpawn)packet).Data; justSpawned = true; Debug.Log("[S->C]: Spawn Position = " + spawn.Pos + " (" + packet.Size + " of " + size + " bytes)"); break; case Packet.PacketType.OtherClients: var others = ((PacketOtherClients)packet).Data; foreach (var c in others) { // Add or update an oponent by its Id (and restore its TTL) Oponent oponent = oponents.AddOponent(c.Id); for (int i = 0; i < c.TransCount; i++) { oponent.AddTransform(c.GetTrans(i)); } } break; case Packet.PacketType.Objects: List <Trans> objectsRecv = ((PacketObjects)packet).Data; foreach (var o in objectsRecv) { Trans t = objects.Where(x => x.Id == o.Id).FirstOrDefault(); if (t == null) { Debug.Log("Received " + o.Id + " for the first time"); objects.Add(o); } else { t.Pos = o.Pos; t.Rot = o.Rot; } } break; case Packet.PacketType.Benchmark: NetBenchmarks b = ((PacketBenchmark)packet).Data; b.recvTimeStamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); PacketBuilder.Build(Packet.PacketType.Benchmark, b).Send(socket, new AsyncCallback(SendCallback)); break; default: Debug.Assert(false); Debug.LogError("Invalid PacketType" + " (" + packet.Size + " of " + size + " bytes)"); break; } } return(true); }
private void OnMsgRecv(object sender, Client.ClientMsgEventArgs e) { if (e.Len == 0) { Debug.Log("Disconnected from the server"); online = false; return; } online = true; int dataIndex = 0; Packet packet = PacketBuilder.Parse(e.Buffer, ref dataIndex); // Process the packet switch (packet.Type) { case Packet.PacketType.Text: recvText = ((PacketText)packet).Data; receivedNewText = true; Debug.Log("[S->C]: " + recvText + " (" + packet.Size + " of " + e.Len + " bytes)"); break; case Packet.PacketType.Spawn: spawn = ((PacketSpawn)packet).Data; justSpawned = true; Debug.Log("[S->C]: Spawn Position = " + spawn.Pos + " (" + packet.Size + " of " + e.Len + " bytes)"); break; case Packet.PacketType.OtherClients: var others = ((PacketOtherClients)packet).Data; foreach (var c in others) { // Add or update an oponent by its Id (and restore its TTL) Oponent oponent = oponents.AddOponent(c.Id); for (int i = 0; i < c.TransCount; i++) { oponent.AddTransform(c.GetTrans(i)); } } break; case Packet.PacketType.Objects: List <Trans> objectsRecv = ((PacketObjects)packet).Data; foreach (var o in objectsRecv) { Trans t = objects.Where(x => x.Id == o.Id).FirstOrDefault(); if (t == null) { Debug.Log("Received " + o.Id + " for the first time"); objects.Add(o); } else { t.Pos = o.Pos; t.Rot = o.Rot; } } break; case Packet.PacketType.Benchmark: NetBenchmarks b = ((PacketBenchmark)packet).Data; b.recvTimeStamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); Packet p = PacketBuilder.Build(Packet.PacketType.Benchmark, b); client.Send(p.ToArray(), p.Size); break; default: Debug.Assert(false); Debug.LogError("Invalid PacketType" + " (" + packet.Size + " of " + e.Len + " bytes)"); break; } }