GodotTicTacToe/scripts/net/ConnectionList.cs

54 lines
1.4 KiB
C#

using System.Collections.Generic;
using Util;
namespace Network {
public class ConnectionList {
private Peer Peer;
private List<Connection> Connections = new List<Connection>();
public ConnectionList(Peer peer) {
Peer = peer;
}
public int Length { get { return Connections.Count; } }
public Connection this[int i] {
get {
return Connections[i];
}
}
public bool AddConnection(Connection conn) {
if (!Contains(conn)) {
Connections.Add(conn);
Peer.Connected(conn);
return true;
}
return false;
}
public bool Contains(Connection conn) {
for (int i = 0; i < Connections.Count; i++) {
Connection inList = Connections[i];
if (inList.Equals(conn)) {
return true;
}
}
return false;
}
public Optional<Connection> GetOriginal(Connection conn) {
foreach (Connection inList in Connections) {
if (inList.Equals(conn)) {
return inList;
}
}
return Optional<Connection>.None;
}
public Optional<Connection> GetOriginal(string address, int port) {
return GetOriginal(new Connection(address, port));
}
}
}