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; Player Player2; lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out 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 is null)) { try { h(this, Player); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace.ToString()); } } }
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 is null)) { try { h(this, Player); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace.ToString()); } } if (AllConnected) { this.State = MultiPlayerState.Ready; } if (Packet is null) { return; } } else { Player = (Player)Connection.StateObject; } this.GameDataReceived(Player, Connection, Packet); }
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; Player Player2; lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out 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) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace.ToString()); } } }
private void Peer_OnReceived(object Sender, byte[] Packet) { PeerConnection Connection = (PeerConnection)Sender; Player Player; if (Connection.StateObject == 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 == 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) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace.ToString()); } } if (AllConnected) this.State = MultiPlayerState.Ready; if (Packet == null) return; } else Player = (Player)Connection.StateObject; this.GameDataReceived(Player, Connection, Packet); }
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); }