2020-08-10 19:34:56 +02:00
|
|
|
|
using System.Net;
|
2020-08-05 03:21:04 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
2020-08-05 18:50:28 +02:00
|
|
|
|
using NeonTea.Quakeball.TeaNet.Packets;
|
2020-08-02 02:50:01 +02:00
|
|
|
|
|
2020-08-05 18:50:28 +02:00
|
|
|
|
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>
|
2020-08-02 02:50:01 +02:00
|
|
|
|
public class Connection {
|
|
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
|
/// <summary>The IP end point of the connection</summary>
|
2020-08-02 02:50:01 +02:00
|
|
|
|
public IPEndPoint Endpoint;
|
2020-08-06 20:49:51 +02:00
|
|
|
|
/// <summary>The unique identifier of the connection.</summary>
|
2020-08-05 19:39:52 +02:00
|
|
|
|
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>
|
2020-08-05 19:39:52 +02:00
|
|
|
|
public ConnectionInternalData Internal = new ConnectionInternalData();
|
2020-08-02 02:50:01 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public Connection(IPEndPoint endpoint, ConnectionStatus status = ConnectionStatus.Establishing) {
|
|
|
|
|
Endpoint = endpoint;
|
|
|
|
|
Status = status;
|
2020-08-05 19:39:52 +02:00
|
|
|
|
|
|
|
|
|
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>
|
2020-08-05 19:39:52 +02:00
|
|
|
|
public bool IsReady() {
|
|
|
|
|
return Status == ConnectionStatus.Ready;
|
2020-08-02 02:50:01 +02:00
|
|
|
|
}
|
2020-08-05 19:39:52 +02:00
|
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
|
/// <summary>Is the connection disconnected. Shorthand for weather Status is Rejected, Closed, Stopped or Lost.</summary>
|
2020-08-05 19:39:52 +02:00
|
|
|
|
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>
|
2020-08-05 19:39:52 +02:00
|
|
|
|
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>
|
2020-08-05 19:39:52 +02:00
|
|
|
|
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-05 19:39:52 +02:00
|
|
|
|
|
2020-08-10 22:18:37 +02:00
|
|
|
|
/// <summary>The id of the first packet in the queue to be sent</summary>
|
|
|
|
|
public int CurrentFirstPacket;
|
|
|
|
|
|
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 02:50:01 +02:00
|
|
|
|
}
|
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
|
|
|
|
}
|
2020-08-02 02:50:01 +02:00
|
|
|
|
}
|