Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Player.cs
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Net;
5
7{
11 public class Player : IEnumerable<KeyValuePair<string, string>>
12 {
13 private int index = -1;
14 private Guid playerId;
15 private IPEndPoint publicEndpoint;
16 private IPEndPoint localEndpoint;
17 private readonly Dictionary<string, string> playerMetaInfo;
18 private PeerConnection connection = null;
19
20 internal Player(Guid PlayerId, IPEndPoint PublicEndpoint, IPEndPoint LocalEndpoint, params KeyValuePair<string, string>[] PlayerMetaInfo)
21 {
22 this.playerId = PlayerId;
23 this.publicEndpoint = PublicEndpoint;
24 this.localEndpoint = LocalEndpoint;
25 this.playerMetaInfo = new Dictionary<string, string>();
26
27 foreach (KeyValuePair<string, string> P in PlayerMetaInfo)
28 this.playerMetaInfo[P.Key] = P.Value;
29 }
30
34 public int Index
35 {
36 get => this.index;
37 internal set => this.index = value;
38 }
39
43 public Guid PlayerId => this.playerId;
44
48 public IPEndPoint PublicEndpoint => this.publicEndpoint;
49
53 public IPEndPoint LocalEndpoint => this.localEndpoint;
54
55 internal void SetEndpoints(IPEndPoint PublicEndpoint, IPEndPoint LocalEndpoint)
56 {
57 this.publicEndpoint = PublicEndpoint;
58 this.localEndpoint = LocalEndpoint;
59 }
60
61 internal IPEndPoint GetExpectedEndpoint(PeerToPeerNetwork Network)
62 {
63 if (IPAddress.Equals(this.publicEndpoint.Address, Network.ExternalAddress))
64 return this.localEndpoint;
65 else
66 return this.publicEndpoint;
67 }
68
72 public int Count
73 {
74 get { return this.playerMetaInfo.Count; }
75 }
76
81 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
82 {
83 return this.playerMetaInfo.GetEnumerator();
84 }
85
90 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
91 {
92 return this.playerMetaInfo.GetEnumerator();
93 }
94
100 public string this[string Key]
101 {
102 get
103 {
104 if (this.playerMetaInfo.TryGetValue(Key, out string Value))
105 return Value;
106 else
107 return string.Empty;
108 }
109 }
110
115 {
116 get => this.connection;
117 internal set => this.connection = value;
118 }
119
121 public override string ToString()
122 {
123 StringBuilder sb = null;
124
125 foreach (KeyValuePair<string, string> P in this.playerMetaInfo)
126 {
127 if (sb is null)
128 {
129 sb = new StringBuilder(this.publicEndpoint.ToString());
130 sb.Append(":");
131 }
132 else
133 sb.Append(", ");
134
135 sb.Append(P.Key);
136 sb.Append('=');
137 sb.Append(P.Value);
138 }
139
140 if (sb is null)
141 return base.ToString();
142 else
143 return sb.ToString();
144 }
145
146 }
147}
Manages a peer-to-peer network that can receive connections from outside of a NAT-enabled firewall.
Class containing information about a player.
Definition: Player.cs:12
int Count
Number of meta information tags.
Definition: Player.cs:73
override string ToString()
Definition: Player.cs:121
PeerConnection Connection
Peer connection, if any.
Definition: Player.cs:115
IPEndPoint LocalEndpoint
Local Endpoint
Definition: Player.cs:53
IPEndPoint PublicEndpoint
Public Endpoint
Definition: Player.cs:48
IEnumerator< KeyValuePair< string, string > > GetEnumerator()
Gets an enumerator for player meta information.
Definition: Player.cs:81