/// <summary> /// Unsubscribes from information earlier subscribed to. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics</param> /// <returns>Packet identifier assigned to unsubscription.</returns> public ushort UNSUBSCRIBE(params string[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (string Topic in Topics) { Payload.WriteString(Topic); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.UNSUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); return(PacketIdentifier); }
private void P2pNetwork_OnPeerConnected(object Listener, PeerConnection Peer) { IPEndPoint Endpoint = (IPEndPoint)Peer.Tcp.Client.RemoteEndPoint; #if LineListener Console.Out.WriteLine("Receiving connection from " + Endpoint.ToString()); #endif lock (this.remotePlayersByEndpoint) { if (!this.remotePlayerIPs.ContainsKey(Endpoint.Address)) { Peer.Dispose(); return; } } Peer.OnClosed += new EventHandler(Peer_OnClosed); Peer.OnReceived += new BinaryEventHandler(Peer_OnReceived); BinaryOutput Output = new BinaryOutput(); Output.WriteGuid(this.localPlayer.PlayerId); Output.WriteString(this.ExternalEndpoint.Address.ToString()); Output.WriteUInt16((ushort)this.ExternalEndpoint.Port); Peer.SendTcp(Output.GetPacket()); }
private void Serialize(Player Player, BinaryOutput Output) { Output.WriteString(Player.PublicEndpoint.Address.ToString()); Output.WriteUInt16((ushort)Player.PublicEndpoint.Port); Output.WriteString(Player.LocalEndpoint.Address.ToString()); Output.WriteUInt16((ushort)Player.LocalEndpoint.Port); Output.WriteGuid(Player.PlayerId); Output.WriteUInt((uint)Player.Count); foreach (KeyValuePair <string, string> P in Player) { Output.WriteString(P.Key); Output.WriteString(P.Value); } }
private void PUBCOMP(ushort PacketIdentifier) { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PUBCOMP << 4); Packet.WriteUInt(2); Packet.WriteUInt16(PacketIdentifier); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, 0); }
private ushort PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, bool Duplicate, byte[] Data) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; Payload.WriteString(Topic); if (QoS > MqttQualityOfService.AtMostOne) { PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); } else { PacketIdentifier = 0; } Payload.WriteBytes(Data); byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.PUBLISH << 4); if (Duplicate) { b |= 8; } b |= (byte)((int)QoS << 1); if (Retain) { b |= 1; } Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); return(PacketIdentifier); }
/// <summary> /// Subscribes to information from a set of topics. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics together with Quality of Service levels for each topic.</param> /// <returns>Packet identifier assigned to subscription.</returns> public ushort SUBSCRIBE(params KeyValuePair <string, MqttQualityOfService>[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (KeyValuePair <string, MqttQualityOfService> Pair in Topics) { Payload.WriteString(Pair.Key); Payload.WriteByte((byte)Pair.Value); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.SUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); return(PacketIdentifier); }
private void Connection_OnReceived(object Sender, byte[] Packet) { PeerConnection Connection = (PeerConnection)Sender; Guid PlayerId; IPAddress PlayerRemoteAddress; IPEndPoint PlayerRemoteEndpoint; try { BinaryInput Input = new BinaryInput(Packet); PlayerId = Input.ReadGuid(); PlayerRemoteAddress = IPAddress.Parse(Input.ReadString()); PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16()); } catch (Exception) { Connection.Dispose(); return; } Player Player = (Player)Connection.StateObject; lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out Player Player2) || Player2.PlayerId != Player.PlayerId) { Connection.Dispose(); return; } Player.Connection = Connection; } Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); Connection.OnReceived -= new BinaryEventHandler(Connection_OnReceived); Connection.OnReceived += new BinaryEventHandler(Peer_OnReceived); Connection.OnSent += new BinaryEventHandler(Connection_OnSent); BinaryOutput Output = new BinaryOutput(); Output.WriteGuid(this.localPlayer.PlayerId); Output.WriteString(this.ExternalAddress.ToString()); Output.WriteUInt16((ushort)this.ExternalEndpoint.Port); Connection.SendTcp(Output.GetPacket()); MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected; if (h != null) { try { h(this, Player); } catch (Exception ex) { Events.Log.Critical(ex); } } }
private void PUBREL(ushort PacketIdentifier) { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)(((int)MqttControlPacketType.PUBREL << 4) | 2)); Packet.WriteUInt(2); Packet.WriteUInt16(PacketIdentifier); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); }
private ushort PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, bool Duplicate, byte[] Data) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; Payload.WriteString(Topic); if (QoS > MqttQualityOfService.AtMostOne) { PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) PacketIdentifier = this.packetIdentifier++; Payload.WriteUInt16(PacketIdentifier); } else PacketIdentifier = 0; Payload.WriteBytes(Data); byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.PUBLISH << 4); if (Duplicate) b |= 8; b |= (byte)((int)QoS << 1); if (Retain) b |= 1; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); return PacketIdentifier; }
/// <summary> /// Unsubscribes from information earlier subscribed to. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics</param> /// <returns>Packet identifier assigned to unsubscription.</returns> public ushort UNSUBSCRIBE(params string[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) PacketIdentifier = this.packetIdentifier++; Payload.WriteUInt16(PacketIdentifier); foreach (string Topic in Topics) Payload.WriteString(Topic); byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.UNSUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); return PacketIdentifier; }
/// <summary> /// Subscribes to information from a set of topics. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics together with Quality of Service levels for each topic.</param> /// <returns>Packet identifier assigned to subscription.</returns> public ushort SUBSCRIBE(params KeyValuePair<string, MqttQualityOfService>[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) PacketIdentifier = this.packetIdentifier++; Payload.WriteUInt16(PacketIdentifier); foreach (KeyValuePair<string, MqttQualityOfService> Pair in Topics) { Payload.WriteString(Pair.Key); Payload.WriteByte((byte)Pair.Value); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.SUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier); return PacketIdentifier; }
private async Task <bool> Connection_OnReceived(object Sender, byte[] Buffer, int Offset, int Count) { PeerConnection Connection = (PeerConnection)Sender; Guid PlayerId; IPAddress PlayerRemoteAddress; IPEndPoint PlayerRemoteEndpoint; try { BinaryInput Input = new BinaryInput(BinaryTcpClient.ToArray(Buffer, Offset, Count)); PlayerId = Input.ReadGuid(); PlayerRemoteAddress = IPAddress.Parse(Input.ReadString()); PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16()); } catch (Exception) { Connection.Dispose(); return(true); } Player Player = (Player)Connection.StateObject; lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out Player Player2) || Player2.PlayerId != Player.PlayerId) { Connection.Dispose(); return(true); } Player.Connection = Connection; } Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); Connection.OnReceived -= this.Connection_OnReceived; Connection.OnReceived += this.Peer_OnReceived; Connection.OnSent += this.Connection_OnSent; BinaryOutput Output = new BinaryOutput(); Output.WriteGuid(this.localPlayer.PlayerId); Output.WriteString(this.ExternalAddress.ToString()); Output.WriteUInt16((ushort)this.ExternalEndpoint.Port); await Connection.SendTcp(Output.GetPacket()); MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected; if (!(h is null)) { try { await h(this, Player); } catch (Exception ex) { Log.Critical(ex); } } return(true); }