private static void HandleMessage(NetMessageType type, NetConnection source, NetBuffer buffer) { switch (type) { case NetMessageType.DebugMessage: WriteToConsole(buffer.ReadString()); break; case NetMessageType.StatusChanged: WriteToConsole("New status: " + source.Status + " (" + buffer.ReadString() + ")"); UpdateStatisticsDisplay(source); break; case NetMessageType.Data: //System.IO.File.AppendAllText("C:\\receivedpackets.txt", s_userMessagesReceived.ToString() + ": " + msg.ReadString() + " (" + msg.m_sequenceNumber + ")" + Environment.NewLine); s_userMessagesReceived++; // simulate some processing of the message here for (int i = 0; i < buffer.LengthBytes - 2; i++) buffer.ReadByte(); // check checksum ushort checksum = NetChecksum.Adler16(buffer.Data, 0, buffer.LengthBytes - 2); ushort given = buffer.ReadUInt16(); if (checksum != given) WriteToConsole("Wrong checksum! Expected " + checksum + " found given " + given); double b = s_userMessagesReceived; for (int i = 0; i < 1000; i++) b += Math.Sqrt((double)i) / Math.Sin(s_tmp); s_tmp += b / 10000.0; break; default: break; } }
/// <summary> /// Reads a half precision Single written using WriteHalfPrecision(float) /// </summary> public static float ReadHalfPrecisionSingle(this NetBuffer message) { HalfSingle h = new HalfSingle(); h.PackedValue = message.ReadUInt16(); return(h.ToSingle()); }
/// <summary> /// Writes a Vector3 at half precision /// </summary> public static Vector3 ReadHalfPrecisionVector3(this NetBuffer message) { HalfSingle hx = new HalfSingle(); hx.PackedValue = message.ReadUInt16(); HalfSingle hy = new HalfSingle(); hy.PackedValue = message.ReadUInt16(); HalfSingle hz = new HalfSingle(); hz.PackedValue = message.ReadUInt16(); Vector3 retval; retval.x = hx.ToSingle(); retval.y = hy.ToSingle(); retval.z = hz.ToSingle(); return(retval); }
/// <summary> /// Read this message from the packet buffer /// </summary> /// <returns>new read pointer position</returns> internal void ReadFrom(NetBuffer buffer, IPEndPoint endpoint) { m_senderEndPoint = endpoint; // read header byte header = buffer.ReadByte(); m_type = (NetMessageLibraryType)(header & 7); m_sequenceChannel = (NetChannel)(header >> 3); m_sequenceNumber = buffer.ReadUInt16(); int payLen = (int)buffer.ReadVariableUInt32(); // copy payload into message buffer m_data.EnsureBufferSize(payLen * 8); buffer.ReadBytes(m_data.Data, 0, payLen); m_data.Reset(0, payLen * 8); return; }
/// <summary> /// Read this message from the packet buffer /// </summary> /// <returns>new read pointer position</returns> internal void ReadFrom(NetBuffer buffer, IPEndPoint endpoint) { m_senderEndPoint = endpoint; // read header byte header = buffer.ReadByte(); m_type = (NetMessageLibraryType)(header & 7); m_sequenceChannel = (NetChannel)(header >> 3); m_sequenceNumber = buffer.ReadUInt16(); int payLen = (int)buffer.ReadVariableUInt32(); // copy payload into message buffer m_data.EnsureBufferSize(payLen * 8); buffer.ReadBytes(m_data.Data, 0, payLen); m_data.Reset(0, payLen * 8); return; }
/// <summary> /// Read this message from the packet buffer /// </summary> /// <returns>new read pointer position</returns> internal bool ReadFrom(NetBuffer buffer, NetworkEndPoint endpoint) { m_senderEndPoint = endpoint; // read header byte header; // ignore zero padding in the beginning until we get something non-zero do { if (buffer.BytesRemaining == 0) { return(false); } header = buffer.ReadByte(); } while (header == 0); m_type = (NetMessageLibraryType)(header & 7); m_sequenceChannel = (NetChannel)(header >> 3); //TODO: do not read seqno for unreliable, just set it to 0 (DAVID) m_sequenceNumber = buffer.ReadUInt16(); int payLen = (int)buffer.ReadVariableUInt32(); if (payLen > buffer.BytesRemaining) { return(false); // bad packet } // copy payload into message buffer m_data.EnsureBufferSizeInBytes(payLen); buffer.ReadBytes(m_data.Data, 0, payLen); m_data.Reset(0, payLen * 8); return(true); }
public StateUpdateEventArgs(NetBuffer msg) : base(msg) { EntitiesUpdatedState = new List<StateUpdateData>(); RemarkableEvents = new List<RemarkableEventData>(); LastAcknowledgedActionID = msg.ReadUInt64(); Time = msg.ReadDouble(); Vec2 velocity = new Vec2(msg.ReadFloat(), msg.ReadFloat()); byte nbClients = msg.ReadByte(); for (byte i = 0; i < nbClients; ++i) { ulong id = msg.ReadUInt64(); Vec2 pos = new Vec2(msg.ReadFloat(), msg.ReadFloat()); ChampionAnimation anim = (ChampionAnimation)msg.ReadByte(); bool facingLeft = msg.ReadBoolean(); EntitiesUpdatedState.Add(new StateUpdateData(id, pos, velocity, anim, facingLeft)); } while (msg.Position != msg.LengthBits) { ServerCommand cmd = (ServerCommand)msg.ReadByte(); RemarkableEventData data = null; switch (cmd) { case ServerCommand.SpellCast: data = new SpellCastEventData( msg.ReadUInt64(), msg.ReadUInt64(), (SpellTypes)msg.ReadByte(), msg.ReadFloat(), new Vec2(msg.ReadFloat(), msg.ReadFloat()), new Vec2(msg.ReadFloat(), msg.ReadFloat()), TimeSpan.FromSeconds(msg.ReadFloat()), msg.ReadFloat(), msg.ReadFloat()); break; case ServerCommand.SpellDisappear: data = new SpellDisappearEventData(msg.ReadUInt64()); break; case ServerCommand.StatsChanged: data = new StatsChangedEventData(msg.ReadUInt64(), msg.ReadFloat()); break; case ServerCommand.ChampionDied: data = new ChampionDiedEventData(msg.ReadUInt64(), msg.ReadUInt64(), msg.ReadUInt32(), msg.ReadUInt32(), msg.ReadUInt32(), msg.ReadUInt32(), TimeSpan.FromSeconds(msg.ReadUInt16())); break; case ServerCommand.StructureStatsChanged: data = new StructureStatsChangedEventData(msg.ReadBoolean() ? Teams.Left : Teams.Right, (StructureTypes)msg.ReadByte(), msg.ReadFloat()); break; case ServerCommand.StructureDestroyed: data = new StructureDestroyedEventData(msg.ReadBoolean() ? Teams.Left : Teams.Right, (StructureTypes)msg.ReadByte()); break; case ServerCommand.EndOfGame: data = new EndOfGameEventData(msg.ReadBoolean() ? Teams.Left : Teams.Right); break; case ServerCommand.TowerPreparingToShoot: data = new TowerPreparingToShootEventData(msg.ReadBoolean() ? Teams.Left : Teams.Right, (StructureTypes)msg.ReadByte()); break; default: Debug.Fail("Unknown server command when updating (unknown remarkable event)"); break; } if (data != null) { RemarkableEvents.Add(data); } } }