31 lines
842 B
C#
31 lines
842 B
C#
using DotNetty.Transport.Channels;
|
|
using System.Net;
|
|
|
|
namespace JSMachine.WMS.Netty.Common.Util
|
|
{
|
|
public static class ChannelHandlerContextUtil
|
|
{
|
|
public static IPInfo ToIPInfo(this IChannelHandlerContext channel, AddressOnwer addressOnwer)
|
|
{
|
|
IPEndPoint iPAddress = addressOnwer == AddressOnwer.Local ? (IPEndPoint)channel.Channel.LocalAddress
|
|
: (IPEndPoint)channel.Channel.RemoteAddress;
|
|
string ip = iPAddress?.Address.MapToIPv4().ToString();
|
|
int port = iPAddress == null ? 0 : iPAddress.Port;
|
|
|
|
return new IPInfo { IP = ip, Port = port };
|
|
}
|
|
}
|
|
|
|
public class IPInfo
|
|
{
|
|
public string IP { get; set; }
|
|
public int Port { get; set; }
|
|
}
|
|
|
|
public enum AddressOnwer
|
|
{
|
|
Local = 0,
|
|
Remote = 1
|
|
}
|
|
}
|