private void ContentReceived(MqttContent Content) { ContentReceivedEventHandler h = this.OnContentReceived; if (!(h is null)) { try { h(this, Content); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } }
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; }
private void ContentReceived(MqttContent Content) { ContentReceivedEventHandler h = this.OnContentReceived; if (h != null) { try { h(this, Content); } catch (Exception ex) { #if LineListener LLOut(ex.Message, Color.Yellow, Color.DarkRed); #endif } } }
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); }
private void mqttConnection_OnContentReceived(MqttConnection Sender, MqttContent Content) { 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 == 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) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace.ToString()); } } break; case 1: // Interconnect ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) break; Player = this.Deserialize(Input); if (Player == null) break; #if LineListener Console.Out.Write("Rx: INTERCONNECT(" + Player.ToString()); #endif int Index = 0; int i, c; LinkedList<Player> Players = new LinkedList<Networking.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 == 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; 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; } }