sharp-chat/SharpChatCommon/ConnectionInfo.cs

37 lines
1.1 KiB
C#

using System;
using System.Net;
namespace SharpChat.SockChat {
public abstract class ConnectionInfo {
public DateTimeOffset LastPing { get; private set; }
public long UserId { get; private set; } = 0;
public string RemoteAddress { get; }
public ushort RemotePort { get; }
public string RemoteEndPoint { get; }
public ConnectionInfo(IPAddress remoteAddr, ushort remotePort) {
BumpPing();
RemoteAddress = remoteAddr.ToString();
RemotePort = remotePort;
RemoteEndPoint = string.Format(
RemoteAddress.Contains(':') ? "[{0}]:{1}" : "{0}:{1}",
RemoteAddress, RemotePort
);
}
// please call this through ConnectionsContext
public void SetUserId(long userId) {
if(UserId > 0)
throw new InvalidOperationException("This connection is already associated with a user.");
UserId = userId;
}
public void BumpPing() {
LastPing = DateTimeOffset.UtcNow;
}
}
}