// 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
{
/// Represents a connection to a or .
public class UdpConnection : Connection, IEquatable
{
/// The endpoint representing the other end of the connection.
public readonly IPEndPoint RemoteEndPoint;
/// The local peer this connection is associated with.
private readonly UdpPeer peer;
/// Initializes the connection.
/// The endpoint representing the other end of the connection.
/// The local peer this connection is associated with.
internal UdpConnection(IPEndPoint remoteEndPoint, UdpPeer peer)
{
RemoteEndPoint = remoteEndPoint;
this.peer = peer;
}
///
protected internal override void Send(byte[] dataBuffer, int amount)
{
peer.Send(dataBuffer, amount, RemoteEndPoint);
}
///
public override string ToString() => RemoteEndPoint.ToStringBasedOnIPFormat();
///
public override bool Equals(object obj) => Equals(obj as UdpConnection);
///
public bool Equals(UdpConnection other)
{
if (other is null)
return false;
if (ReferenceEquals(this, other))
return true;
return RemoteEndPoint.Equals(other.RemoteEndPoint);
}
///
public override int GetHashCode()
{
return -288961498 + EqualityComparer.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
}
}