private Player Deserialize(BinaryInput Input) { IPAddress PublicAddress = IPAddress.Parse(Input.ReadString()); ushort PublicPort = Input.ReadUInt16(); IPEndPoint PublicEndpoint = new IPEndPoint(PublicAddress, PublicPort); IPAddress LocalAddress = IPAddress.Parse(Input.ReadString()); ushort LocalPort = Input.ReadUInt16(); IPEndPoint LocalEndpoint = new IPEndPoint(LocalAddress, LocalPort); Guid PlayerId = Input.ReadGuid(); bool LocalPlayer = PlayerId == this.localPlayer.PlayerId; int i, c = (int)Input.ReadUInt(); KeyValuePair <string, string>[] PlayerMetaInfo = LocalPlayer ? null : new KeyValuePair <string, string> [c]; string Key, Value; for (i = 0; i < c; i++) { Key = Input.ReadString(); Value = Input.ReadString(); if (!LocalPlayer) { PlayerMetaInfo[i] = new KeyValuePair <string, string>(Key, Value); } } if (LocalPlayer) { return(null); } else { return(new Player(PlayerId, PublicEndpoint, LocalEndpoint, PlayerMetaInfo)); } }
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 Peer_OnReceived(object Sender, byte[] Packet) { PeerConnection Connection = (PeerConnection)Sender; Player Player; if (Connection.StateObject is null) { BinaryInput Input = new BinaryInput(Packet); Guid PlayerId; IPAddress PlayerRemoteAddress; IPEndPoint PlayerRemoteEndpoint; try { PlayerId = Input.ReadGuid(); PlayerRemoteAddress = IPAddress.Parse(Input.ReadString()); PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16()); } catch (Exception) { Connection.Dispose(); return; } if (Input.BytesLeft == 0) { Packet = null; } else { Packet = Input.GetRemainingData(); } bool AllConnected; lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out Player)) { Connection.Dispose(); return; } if (Player.Connection is null) { this.connectionCount++; } else { Player.Connection.Dispose(); } Player.Connection = Connection; Connection.StateObject = Player; Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); AllConnected = this.connectionCount + 1 == this.playerCount; } MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected; if (h != null) { try { h(this, Player); } catch (Exception ex) { Events.Log.Critical(ex); } } if (AllConnected) { this.State = MultiPlayerState.Ready; } if (Packet is null) { return; } } else { Player = (Player)Connection.StateObject; } this.GameDataReceived(Player, Connection, Packet); }
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 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); }