using System.Net;
using System;
using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.TeaNet.Peers {
/// Represents a connection to a remot host over the internet.
public class Connection {
/// The IP end point of the connection
public IPEndPoint Endpoint;
/// The unique identifier of the connection.
public ulong uid;
/// Connection status of the current connection.
public ConnectionStatus Status;
/// Reason why the connection closed. Null if no reason.
public ClosingReason ClosingReason;
/// Internal data for the connection. Do not touch, unless you know what you're doing.
public ConnectionInternalData Internal = new ConnectionInternalData();
public Connection(IPEndPoint endpoint, ConnectionStatus status = ConnectionStatus.Establishing) {
Endpoint = endpoint;
Status = status;
Internal.LastMessage = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Internal.LatestInwardReliable = -1;
Internal.LatestInwardUnreliable = -1;
Internal.LatestOutwardReliable = -1;
Internal.LatestOutwardUnreliable = -1;
}
/// Is the connection ready and established for sending packets.
public bool IsReady() {
return Status == ConnectionStatus.Ready;
}
/// Is the connection disconnected. Shorthand for weather Status is Rejected, Closed, Stopped or Lost.
public bool IsDisconnected() {
return !(Status == ConnectionStatus.Ready
|| Status == ConnectionStatus.Awaiting
|| Status == ConnectionStatus.Establishing);
}
}
public struct ConnectionInternalData {
/// The protocol identifier, which this connection uses.
public byte AssignedProtocol;
/// Last unix timestamp in milliseconds, when this connection was last heard of.
public long LastMessage;
/// Last reliable Packet ID the connection has told us they have
public int LatestOutwardReliable;
/// Last unreliablePacket ID the connection has told us they have
public int LatestOutwardUnreliable;
/// Last reliable Packet ID we've received from the connection
public int LatestInwardReliable;
/// Last unreliable Packet ID we've received from the connection
public int LatestInwardUnreliable;
/// The id of the first packet in the queue to be sent
public int CurrentFirstPacket;
/// Reliable Packet ID counter for packets we're sending them
public int ReliablePacketIDCounter;
/// Unreliable Packet ID counter for packets we're sending them
public int UnreliablePacketIDCounter;
}
/// Initiali
public enum ConnectionStatus {
/// Awaiting the other endpoint to establish the connection.
Awaiting,
/// Attempting to establish the connection.
Establishing,
/// Ready for packet sending
Ready,
/// Rejected connection at endpoint, sending information that it was rejected.
Rejected,
/// Closed the endpoint, and informing the connection that it should stop.
Closed,
/// Connection is stopped and waiting for timeout.
Stopped,
/// Connection Lost
Lost,
}
}