public IEncodable Decode(BinaryInput stream) { Name = stream.ReadString(); ID = stream.ReadInt32(); EntityType = (EntityType)stream.ReadByte(); ShadowType = (ShadowType)stream.ReadByte(); ShadowType = ShadowType.Perspective; isSubEntity = stream.ReadBoolean(); ShadowOffset = stream.ReadInt32(); stream.ReadInt32(); CollisionMap = stream.ReadObject <CollisionMap>(); Texture = stream.ReadObject <TileableTexture>(); int c = stream.ReadInt32(); for (int i = 0; i < c; i++) { Animations.Add(stream.ReadObject <Animation>()); } return(this); }
#pragma warning enable /// <summary> /// Parses a header. /// </summary> /// <param name="Input">Binary input.</param> /// <returns>Header.</returns> public static MqttHeader Parse(BinaryInput Input) { MqttHeader Result = new MqttHeader(); // Fixed part: byte b = Input.ReadByte(); Result.ControlPacketType = (MqttControlPacketType)(b >> 4); Result.Retain = (b & 1) != 0; Result.QualityOfService = (MqttQualityOfService)((b >> 1) & 3); Result.Duplicate = (b & 8) != 0; b = Input.ReadByte(); int Offset = 0; Result.RemainingLength = b & 127; while ((b & 128) != 0) { b = Input.ReadByte(); Offset += 7; Result.RemainingLength |= (b & 127) << Offset; } // Variable part: switch (Result.ControlPacketType) { case MqttControlPacketType.CONNECT: case MqttControlPacketType.CONNACK: case MqttControlPacketType.PINGREQ: case MqttControlPacketType.PINGRESP: case MqttControlPacketType.DISCONNECT: case MqttControlPacketType.PUBLISH: default: Result.PacketIdentifier = 0; break; case MqttControlPacketType.PUBACK: case MqttControlPacketType.PUBREC: case MqttControlPacketType.PUBREL: case MqttControlPacketType.PUBCOMP: case MqttControlPacketType.SUBSCRIBE: case MqttControlPacketType.SUBACK: case MqttControlPacketType.UNSUBSCRIBE: case MqttControlPacketType.UNSUBACK: Result.PacketIdentifier = Input.ReadByte(); Result.PacketIdentifier <<= 8; Result.PacketIdentifier |= Input.ReadByte(); Result.RemainingLength -= 2; break; } return Result; }
#pragma warning enable /// <summary> /// Parses a header. /// </summary> /// <param name="Input">Binary input.</param> /// <returns>Header.</returns> public static MqttHeader Parse(BinaryInput Input) { MqttHeader Result = new MqttHeader(); // Fixed part: byte b = Input.ReadByte(); Result.ControlPacketType = (MqttControlPacketType)(b >> 4); Result.Retain = (b & 1) != 0; Result.QualityOfService = (MqttQualityOfService)((b >> 1) & 3); Result.Duplicate = (b & 8) != 0; b = Input.ReadByte(); int Offset = 0; Result.RemainingLength = b & 127; while ((b & 128) != 0) { b = Input.ReadByte(); Offset += 7; Result.RemainingLength |= (b & 127) << Offset; } // Variable part: switch (Result.ControlPacketType) { case MqttControlPacketType.CONNECT: case MqttControlPacketType.CONNACK: case MqttControlPacketType.PINGREQ: case MqttControlPacketType.PINGRESP: case MqttControlPacketType.DISCONNECT: case MqttControlPacketType.PUBLISH: default: Result.PacketIdentifier = 0; break; case MqttControlPacketType.PUBACK: case MqttControlPacketType.PUBREC: case MqttControlPacketType.PUBREL: case MqttControlPacketType.PUBCOMP: case MqttControlPacketType.SUBSCRIBE: case MqttControlPacketType.SUBACK: case MqttControlPacketType.UNSUBSCRIBE: case MqttControlPacketType.UNSUBACK: Result.PacketIdentifier = Input.ReadByte(); Result.PacketIdentifier <<= 8; Result.PacketIdentifier |= Input.ReadByte(); Result.RemainingLength -= 2; break; } return(Result); }
public ISerializable Deserialize(BinaryInput input) { Name = input.ReadString(); TimePerFrame = input.ReadSingle(); Frames = input.ReadList<KeyFrame>(); AnimationFlag = (AnimationFlags) input.ReadByte(); FlipEffect = (SpriteEffects) input.ReadByte(); Rotation = input.ReadSingle(); return this; }
public IEncodable Decode(BinaryInput stream) { this.Pattern = (MovementPattern)stream.ReadByte(); this.Speed = (MovementSpeed)stream.ReadByte(); this.Count = stream.ReadInt16(); this.CurrentIndex = stream.ReadInt16(); this.Inverted = stream.ReadBoolean(); int c = stream.ReadInt32(); for (int i = 0; i < c; i++) { Movements.Add(stream.ReadObject <Movement>()); } return(this); }
public override IEncodable Decode(BinaryInput stream) { base.Decode(stream); value = stream.ReadByte(); return(this); }
public IEffect Read() { BinaryInput stream = this._stream as BinaryInput; int type = stream.ReadByte(); if (type == 0) { return(stream.ReadObject <LockEffect>()); } return(null); }
public IEncodable Decode(BinaryInput stream) { this.Name = stream.ReadString(); this.TimePerFrame = stream.ReadSingle(); this.Flags = (AnimationFlags) stream.ReadByte(); int count = stream.ReadInt32(); for (int i = 0; i < count; i++) { this.Indices.Add(stream.ReadInt32()); } return this; }
public IEncodable Decode(BinaryInput stream) { Name = stream.ReadString(); ID = stream.ReadInt32(); EntityType = (EntityType) stream.ReadByte(); ShadowType = (ShadowType) stream.ReadByte(); ShadowType = ShadowType.Perspective; isSubEntity = stream.ReadBoolean(); ShadowOffset = stream.ReadInt32(); stream.ReadInt32(); CollisionMap = stream.ReadObject<CollisionMap>(); Texture = stream.ReadObject<TileableTexture>(); int c = stream.ReadInt32(); for (int i = 0; i < c; i++) { Animations.Add(stream.ReadObject<Animation>()); } return this; }
public override IEncodable Decode(BinaryInput stream) { base.Decode(stream); this.Facing = (Facing)stream.ReadByte(); EffectIO io = new EffectIO(stream); int c = stream.ReadInt32(); for (int i = 0; i < c; i++) { Effects.Add(io.Read()); } return(this); }
public IEncodable Decode(BinaryInput stream) { this.Name = stream.ReadString(); this.TimePerFrame = stream.ReadSingle(); this.Flags = (AnimationFlags)stream.ReadByte(); int count = stream.ReadInt32(); for (int i = 0; i < count; i++) { this.Indices.Add(stream.ReadInt32()); } return(this); }
public MapEntity Read() { MapEntity result = null; BinaryInput stream = _stream as BinaryInput; EntityType type = (EntityType)stream.ReadByte(); if (type == EntityType.Building) { result = new BuildingEntity(factory, ScrollAffected); } IEncodable encodable = result as IEncodable; if (encodable != null) { encodable.Decode(stream); } return(result); }
public IEncodable Decode(BinaryInput stream) { this.Pattern = (MovementPattern) stream.ReadByte(); this.Speed = (MovementSpeed) stream.ReadByte(); this.Count = stream.ReadInt16(); this.CurrentIndex = stream.ReadInt16(); this.Inverted = stream.ReadBoolean(); int c = stream.ReadInt32(); for (int i = 0; i < c; i++) { Movements.Add(stream.ReadObject<Movement>()); } return this; }
private async void MqttConnection_OnContentReceived(object Sender, MqttContent Content) { try { BinaryInput Input = Content.DataInput; byte Command = Input.ReadByte(); switch (Command) { case 0: // Hello string ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) { break; } Player Player = this.Deserialize(Input); if (Player is null) { break; } #if LineListener Console.Out.WriteLine("Rx: HELLO(" + Player.ToString() + ")"); #endif IPEndPoint ExpectedEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); lock (this.remotePlayersByEndpoint) { this.remotePlayersByEndpoint[ExpectedEndpoint] = Player; this.remotePlayerIPs[ExpectedEndpoint.Address] = true; this.playersById[Player.PlayerId] = Player; this.UpdateRemotePlayersLocked(); } MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerAvailable; if (h != null) { try { h(this, Player); } catch (Exception ex) { Events.Log.Critical(ex); } } break; case 1: // Interconnect ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) { break; } Player = this.Deserialize(Input); if (Player is null) { break; } #if LineListener Console.Out.Write("Rx: INTERCONNECT(" + Player.ToString()); #endif int Index = 0; int i, c; LinkedList <Player> Players = new LinkedList <Player>(); bool LocalPlayerIncluded = false; Player.Index = Index++; Players.AddLast(Player); c = (int)Input.ReadUInt(); for (i = 0; i < c; i++) { Player = this.Deserialize(Input); if (Player is null) { #if LineListener Console.Out.Write("," + this.localPlayer.ToString()); #endif this.localPlayer.Index = Index++; LocalPlayerIncluded = true; } else { #if LineListener Console.Out.Write("," + Player.ToString()); #endif Player.Index = Index++; Players.AddLast(Player); } } #if LineListener Console.Out.WriteLine(")"); #endif if (!LocalPlayerIncluded) { break; } this.mqttConnection.Dispose(); this.mqttConnection = null; lock (this.remotePlayersByEndpoint) { this.remotePlayersByEndpoint.Clear(); this.remotePlayerIPs.Clear(); this.remotePlayersByIndex.Clear(); this.playersById.Clear(); this.remotePlayersByIndex[this.localPlayer.Index] = this.localPlayer; this.playersById[this.localPlayer.PlayerId] = this.localPlayer; foreach (Player Player2 in Players) { ExpectedEndpoint = Player2.GetExpectedEndpoint(this.p2pNetwork); this.remotePlayersByIndex[Player2.Index] = Player2; this.remotePlayersByEndpoint[ExpectedEndpoint] = Player2; this.remotePlayerIPs[ExpectedEndpoint.Address] = true; this.playersById[Player2.PlayerId] = Player2; } this.UpdateRemotePlayersLocked(); } this.State = MultiPlayerState.ConnectingPlayers; await this.StartConnecting(); break; case 2: // Bye ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) { break; } Guid PlayerId = Input.ReadGuid(); lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out Player)) { break; } #if LineListener Console.Out.WriteLine("Rx: BYE(" + Player.ToString() + ")"); #endif ExpectedEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); this.playersById.Remove(PlayerId); this.remotePlayersByEndpoint.Remove(ExpectedEndpoint); this.remotePlayersByIndex.Remove(Player.Index); IPAddress ExpectedAddress = ExpectedEndpoint.Address; bool AddressFound = false; foreach (IPEndPoint EP in this.remotePlayersByEndpoint.Keys) { if (IPAddress.Equals(EP.Address, ExpectedAddress)) { AddressFound = true; break; } } if (!AddressFound) { this.remotePlayerIPs.Remove(ExpectedAddress); } this.UpdateRemotePlayersLocked(); } break; } } catch (Exception ex) { Log.Critical(ex); } }
private bool ProcessInputPacket() { try { BinaryInput Packet = new BinaryInput(this.inputPacket); MqttHeader Header = MqttHeader.Parse(Packet); switch (Header.ControlPacketType) { case MqttControlPacketType.CONNECT: default: throw new Exception("Received command from server that is not handled: " + Header.ControlPacketType.ToString()); case MqttControlPacketType.CONNACK: bool SessionPresent = (Packet.ReadByte() & 1) != 0; byte ReturnCode = Packet.ReadByte(); try { switch (ReturnCode) { case 0: this.State = MqttState.Connected; this.nextPing = DateTime.Now.AddMilliseconds(this.keepAliveSeconds * 500); break; case 1: throw new IOException("Connection Refused, unacceptable protocol version."); case 2: throw new IOException("Connection Refused, identifier rejected."); case 3: throw new IOException("Connection Refused, Server unavailable."); case 4: throw new IOException("Connection Refused, bad user name or password."); case 5: throw new IOException("Connection Refused, not authorized."); default: throw new IOException("Unrecognized error code returned: " + ReturnCode.ToString()); } } catch (Exception ex) { this.ConnectionError(ex); this.stream.Close(); this.client.Close(); return false; } break; case MqttControlPacketType.PINGREQ: this.PINGRESP(); break; case MqttControlPacketType.PINGRESP: EventHandler h = this.OnPingResponse; if (h != null) { try { h(this, new EventArgs()); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.PUBLISH: string Topic = Packet.ReadString(); if (Header.QualityOfService > MqttQualityOfService.AtMostOne) Header.PacketIdentifier = Packet.ReadUInt16(); else Header.PacketIdentifier = 0; int c = Packet.BytesLeft; byte[] Data = Packet.ReadBytes(c); MqttContent Content = new MqttContent(Header, Topic, Data); switch (Header.QualityOfService) { case MqttQualityOfService.AtMostOne: this.ContentReceived(Content); break; case MqttQualityOfService.AtLeastOne: this.PUBACK(Header.PacketIdentifier); this.ContentReceived(Content); break; case MqttQualityOfService.ExactlyOne: lock (this.contentCache) { this.contentCache[Header.PacketIdentifier] = Content; } this.PUBREC(Header.PacketIdentifier); break; } break; case MqttControlPacketType.PUBACK: this.PacketDelivered(Header.PacketIdentifier); PacketAcknowledgedEventHandler h2 = this.OnPublished; if (h2 != null) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.PUBREC: this.PacketDelivered(Header.PacketIdentifier); this.PUBREL(Header.PacketIdentifier); break; case MqttControlPacketType.PUBREL: lock (this.contentCache) { if (this.contentCache.TryGetValue(Header.PacketIdentifier, out Content)) this.contentCache.Remove(Header.PacketIdentifier); else Content = null; } this.PUBCOMP(Header.PacketIdentifier); if (Content != null) this.ContentReceived(Content); break; case MqttControlPacketType.PUBCOMP: this.PacketDelivered(Header.PacketIdentifier); h2 = this.OnPublished; if (h2 != null) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.SUBACK: this.PacketDelivered(Header.PacketIdentifier); h2 = this.OnSubscribed; if (h2 != null) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.UNSUBACK: this.PacketDelivered(Header.PacketIdentifier); h2 = this.OnUnsubscribed; if (h2 != null) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; } } catch (Exception ex) { this.Error(ex); } return true; }
public IEncodable Decode(BinaryInput stream) { this.Facing = (Facing) stream.ReadByte(); this.Speed = (MovementSpeed) stream.ReadByte(); return this; }
public override IEncodable Decode(BinaryInput stream) { base.Decode(stream); this.Facing = (Facing) stream.ReadByte(); EffectIO io = new EffectIO(stream); int c = stream.ReadInt32(); for (int i = 0; i < c; i++) { Effects.Add(io.Read()); } return this; }
public IEncodable Decode(BinaryInput stream) { this.BehaviorId = stream.ReadByte(); return this; }
public IEncodable Decode(BinaryInput stream) { this.BehaviorId = stream.ReadByte(); return(this); }
public IEncodable Decode(BinaryInput stream) { this.Facing = (Facing)stream.ReadByte(); this.Speed = (MovementSpeed)stream.ReadByte(); return(this); }
private bool ProcessInputPacket() { try { BinaryInput Packet = new BinaryInput(this.inputPacket); MqttHeader Header = MqttHeader.Parse(Packet); switch (Header.ControlPacketType) { case MqttControlPacketType.CONNECT: default: throw new Exception("Received command from server that is not handled: " + Header.ControlPacketType.ToString()); case MqttControlPacketType.CONNACK: bool SessionPresent = (Packet.ReadByte() & 1) != 0; byte ReturnCode = Packet.ReadByte(); try { switch (ReturnCode) { case 0: this.State = MqttState.Connected; this.nextPing = DateTime.Now.AddMilliseconds(this.keepAliveSeconds * 500); break; case 1: throw new IOException("Connection Refused, unacceptable protocol version."); case 2: throw new IOException("Connection Refused, identifier rejected."); case 3: throw new IOException("Connection Refused, Server unavailable."); case 4: throw new IOException("Connection Refused, bad user name or password."); case 5: throw new IOException("Connection Refused, not authorized."); default: throw new IOException("Unrecognized error code returned: " + ReturnCode.ToString()); } } catch (Exception ex) { this.ConnectionError(ex); this.stream.Close(); this.client.Close(); return(false); } break; case MqttControlPacketType.PINGREQ: this.PINGRESP(); break; case MqttControlPacketType.PINGRESP: EventHandler h = this.OnPingResponse; if (!(h is null)) { try { h(this, new EventArgs()); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.PUBLISH: string Topic = Packet.ReadString(); if (Header.QualityOfService > MqttQualityOfService.AtMostOne) { Header.PacketIdentifier = Packet.ReadUInt16(); } else { Header.PacketIdentifier = 0; } int c = Packet.BytesLeft; byte[] Data = Packet.ReadBytes(c); MqttContent Content = new MqttContent(Header, Topic, Data); switch (Header.QualityOfService) { case MqttQualityOfService.AtMostOne: this.ContentReceived(Content); break; case MqttQualityOfService.AtLeastOne: this.PUBACK(Header.PacketIdentifier); this.ContentReceived(Content); break; case MqttQualityOfService.ExactlyOne: lock (this.contentCache) { this.contentCache[Header.PacketIdentifier] = Content; } this.PUBREC(Header.PacketIdentifier); break; } break; case MqttControlPacketType.PUBACK: this.PacketDelivered(Header.PacketIdentifier); PacketAcknowledgedEventHandler h2 = this.OnPublished; if (!(h2 is null)) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.PUBREC: this.PacketDelivered(Header.PacketIdentifier); this.PUBREL(Header.PacketIdentifier); break; case MqttControlPacketType.PUBREL: lock (this.contentCache) { if (this.contentCache.TryGetValue(Header.PacketIdentifier, out Content)) { this.contentCache.Remove(Header.PacketIdentifier); } else { Content = null; } } this.PUBCOMP(Header.PacketIdentifier); if (!(Content is null)) { this.ContentReceived(Content); } break; case MqttControlPacketType.PUBCOMP: this.PacketDelivered(Header.PacketIdentifier); h2 = this.OnPublished; if (!(h2 is null)) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.SUBACK: this.PacketDelivered(Header.PacketIdentifier); h2 = this.OnSubscribed; if (!(h2 is null)) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; case MqttControlPacketType.UNSUBACK: this.PacketDelivered(Header.PacketIdentifier); h2 = this.OnUnsubscribed; if (!(h2 is null)) { try { h2(this, Header.PacketIdentifier); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } break; } } catch (Exception ex) { this.Error(ex); } return(true); }