quakeball/Assets/Scripts/TeaNet/Peers/Connection.cs

84 lines
3.7 KiB
C#
Raw Permalink Normal View History

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