Files
K-C-Multiplayer/Riptide/Transports/Udp/UdpConnection.cs
2025-12-14 21:08:19 +01:00

81 lines
2.9 KiB
C#

// This file is provided under The MIT License as part of RiptideNetworking.
// Copyright (c) Tom Weiland
// For additional information please see the included LICENSE.md file or view it on GitHub:
// https://github.com/RiptideNetworking/Riptide/blob/main/LICENSE.md
using Riptide.Utils;
using System;
using System.Collections.Generic;
using System.Net;
namespace Riptide.Transports.Udp
{
/// <summary>Represents a connection to a <see cref="UdpServer"/> or <see cref="UdpClient"/>.</summary>
public class UdpConnection : Connection, IEquatable<UdpConnection>
{
/// <summary>The endpoint representing the other end of the connection.</summary>
public readonly IPEndPoint RemoteEndPoint;
/// <summary>The local peer this connection is associated with.</summary>
private readonly UdpPeer peer;
/// <summary>Initializes the connection.</summary>
/// <param name="remoteEndPoint">The endpoint representing the other end of the connection.</param>
/// <param name="peer">The local peer this connection is associated with.</param>
internal UdpConnection(IPEndPoint remoteEndPoint, UdpPeer peer)
{
RemoteEndPoint = remoteEndPoint;
this.peer = peer;
}
/// <inheritdoc/>
protected internal override void Send(byte[] dataBuffer, int amount)
{
peer.Send(dataBuffer, amount, RemoteEndPoint);
}
/// <inheritdoc/>
public override string ToString() => RemoteEndPoint.ToStringBasedOnIPFormat();
/// <inheritdoc/>
public override bool Equals(object obj) => Equals(obj as UdpConnection);
/// <inheritdoc/>
public bool Equals(UdpConnection other)
{
if (other is null)
return false;
if (ReferenceEquals(this, other))
return true;
return RemoteEndPoint.Equals(other.RemoteEndPoint);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return -288961498 + EqualityComparer<IPEndPoint>.Default.GetHashCode(RemoteEndPoint);
}
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static bool operator ==(UdpConnection left, UdpConnection right)
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
{
if (left is null)
{
if (right is null)
return true;
return false; // Only the left side is null
}
// Equals handles case of null on right side
return left.Equals(right);
}
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static bool operator !=(UdpConnection left, UdpConnection right) => !(left == right);
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}
}