Ejemplo n.º 1
0
        public NetClient(ClientOption clientOption, ILoggerFactory loggerFactory = null)
        {
            _clientOption = clientOption;
            //_clientOptions.PacketFilter = _clientOptions.PacketFilter ?? new XorPacketFilter();

            _loggerFactory = loggerFactory ?? DefaultLoggerFactory.Create(builder => { builder.AddConsoleLogger(); });

            _logger = _loggerFactory.CreateLogger(nameof(NetClient));
            _receivedPacketQueue = new ConcurrentQueue <NetPacket>();
            _packetReader        = new NetDataReader();

            _statistic = new NetStatistic();

            _tcpChannel = new TcpChannel(
                _clientOption,
                _loggerFactory.CreateLogger(nameof(TcpChannel)),
                _statistic);

            if (_clientOption.IsServiceUdp == true)
            {
                _udpChannel = new UdpChannel(
                    _clientOption,
                    _loggerFactory.CreateLogger(nameof(UdpChannel)),
                    _statistic,
                    0);
            }

            _request = new SessionRequest(this, _statistic);

            _rpcHandlers = new List <IRpcInvokable>();
        }
Ejemplo n.º 2
0
 public Statsd(string host, int port, string prefix = null)
 {
     this.prefix = prefix;
     hasPrefix = !String.IsNullOrEmpty(prefix);
     udpChannel = new UdpChannel(host, port);
     Current = this;
 }
Ejemplo n.º 3
0
        public void Should_send_bytes_over_the_network(string statsdHost, AddressFamily family)
        {
            using var server = new Socket(family, SocketType.Dgram, ProtocolType.Udp)
                  {
                      ReceiveTimeout = 3000
                  };
            server.Bind(new IPEndPoint(family == AddressFamily.InterNetwork ? IPAddress.Loopback : IPAddress.IPv6Loopback, 8125));

            byte[] message = Encoding.UTF8.GetBytes("Hello server!");

            var settings = new UdpChannelSettings
            {
                Host          = statsdHost,
                Port          = 8125,
                AddressFamily = family
            };

            using var channel = new UdpChannel(settings);
            channel.Send(message, message.Length);
            var receiveBuffer = new byte[message.Length];

            server.Receive(receiveBuffer);

            receiveBuffer.ShouldBeEquivalentTo(message);
        }
Ejemplo n.º 4
0
 public Statsd(XmlConfiguration xml)
 {
     StatsDConfigurationSection cfg = (StatsDConfigurationSection)ConfigurationManager.GetSection("statsD");
     UdpChannel channel = new UdpChannel(cfg.Server.Host, cfg.Server.Port);
     this.prefix = cfg.Server.Prefix;
     hasPrefix = !String.IsNullOrEmpty(prefix);
     Current = this;
 }
        public void Should_not_fail_on_dispose()
        {
            var channel = new UdpChannel("localhost", 8125);
            var client  = new BackgroundStatsdClient(channel);

            client.Dispose();
            client.Dispose();
        }
Ejemplo n.º 6
0
 internal Node(string id, string name, IPAddress ip)
 {
     Info = new NodeInfo
     {
         Id      = id,
         Name    = name,
         Ip      = ip.ToString(),
         UdpPort = Address.GetUdpPort(),
     };
     _broadcastChannel = new UdpChannel(new IPEndPoint(IPAddress.Loopback, 12345));
 }
Ejemplo n.º 7
0
        public void Should_not_throw_if_address_is_invalid()
        {
            byte[] message = Encoding.UTF8.GetBytes("Hello server!");

            var settings = new UdpChannelSettings
            {
                Host                   = "no.such.host.is.known",
                Port                   = 8125,
                AddressFamily          = AddressFamily.InterNetwork,
                IgnoreSocketExceptions = true
            };

            using var channel = new UdpChannel(settings);
            channel.Send(message, message.Length);
        }
Ejemplo n.º 8
0
        public RtpUdpSource(IPAddress address)
        {
            int rtpPort, rtcpPort;

            if (!FindAvailablePorts(out rtpPort, out rtcpPort))
            {
                // Becasue we use a pretty big range this is very unlikely to occur.

                // TODO(frank.lamar): Determine a better exception for this
                throw new SystemException("Unable to locate two consecutive ports to bind to.");
            }

            _rtpChannel  = new UdpChannel(address, rtpPort);
            _rtcpChannel = new UdpChannel(address, rtcpPort);

            LOG.Debug($"Created RtpUdpSource with channels {rtpPort}-{rtcpPort}");
        }
Ejemplo n.º 9
0
        public ServerSession(SessionCreateInfo createInfo)
        {
            SessionId   = createInfo.SessionId;
            _tcpChannel = createInfo.TcpChannel;
            if (_tcpChannel != null)
            {
                _tcpChannel.PacketReceived = OnReceiveFromChannel;
            }

            _udpChannel = createInfo.UdpChannel;
            if (_udpChannel != null)
            {
                _udpChannel.PacketReceived = OnReceiveFromChannel;
            }

            _request = new SessionRequest(this, createInfo.Statistic);
        }
Ejemplo n.º 10
0
        public DefaultSessionFactory(
            ServerOption serverOption,
            ILoggerFactory loggerFactory,
            NetStatistic statistic,
            Func <SessionCreateInfo, ServerSession> createSessionFunc = null)
        {
            createSessionFunc = createSessionFunc ?? CreateSession;

            _sessionQueue = new ConcurrentQueue <ISession>();
            _maxSession   = serverOption.MaxSession;

            var createInfo = new SessionCreateInfo();

            for (int i = 0; i < _maxSession; ++i)
            {
                TcpChannel tcpChannel = new TcpChannel(
                    serverOption,
                    loggerFactory.CreateLogger(nameof(TcpChannel)),
                    statistic);

                UdpChannel udpChannel = null;

                if (serverOption.IsServiceUdp)
                {
                    udpChannel = new UdpChannel(
                        serverOption,
                        loggerFactory.CreateLogger(nameof(UdpChannel)),
                        statistic,
                        0);
                }

                createInfo.SessionId  = (ushort)(i + 1);
                createInfo.TcpChannel = tcpChannel;
                createInfo.UdpChannel = udpChannel;
                createInfo.Statistic  = statistic;

                ServerSession session = createSessionFunc(createInfo);

                _sessionQueue.Enqueue(session);
            }
        }
Ejemplo n.º 11
0
        public P2pSession(NetClient netClient, ushort sessionId, IPEndPoint remoteEp, IPEndPoint localEp)
        {
            _netClient = netClient;
            _sessionId = sessionId;

            _cts = new CancellationTokenSource();

            _udpSocket = new UdpSocketEx(
                netClient.LoggerFactory.CreateLogger("P2pSessionUdpSocket"),
                netClient.OnPreProcessUdpRawData,
                netClient.UdpSocketReceiveRawAsyncObject);

            _udpSocket.CreateClient();

            _tcpChannel = null;

            _udpChannel = new UdpChannel(
                _netClient.ClientOption,
                _netClient.LoggerFactory.CreateLogger(nameof(UdpChannel)),
                _netClient.Statistic,
                sessionId);

            _udpChannel.Init(_cts);
            _udpChannel.PacketReceived = OnReceiveFromChannel;
            _udpChannel.SetSocket(netClient.UdpSocket);

            _udpChannel.RemoteEndPoint = remoteEp;
            _udpChannel.LocalEndPoint  = localEp;
            _udpChannel.TempEndPoint   = remoteEp;
            _udpChannel.RelayEndPoint  = NetUtil.GetEndPoint(netClient.ClientOption.UdpServerAddress, netClient.ClientOption.UdpServerPort);

            _udpChannel.IsRunMtu  = false;
            _udpChannel.IsRunPing = false;

            _receivedPacketQueue = new ConcurrentQueue <NetPacket>();
            _packetReader        = new NetDataReader();
            _request             = new SessionRequest(this, netClient.Statistic);

            _state = SessionState.Connected;
        }
 public UnreliableSendQueue(UdpChannel channel, int initialCapacity) : base(channel, initialCapacity)
 {
 }
 internal ReliableReceiveQueue(UdpChannel channel, int initialCapacity)
     : base(channel, initialCapacity)
 {
 }
Ejemplo n.º 14
0
        private object GetChannel(XElement element)
        {
            string channelType = GetAttribute(element, "Type");
            string name        = GetAttribute(element, "Name");

            IObjectWithName channel = null;

            if (channelType == "TcpServerChannel")
            {
                int port = int.Parse(GetAttribute(element, "LocalPort"));

                TcpServerChannel tcpServerChannel = new TcpServerChannel(name, port);

                channel = tcpServerChannel;
            }
            else if (channelType == "TcpClientChannel")
            {
                TcpClientChannel tcpClientChannel = null;

                string ip   = GetAttribute(element, "RemoteIP");
                int    port = int.Parse(GetAttribute(element, "RemotePort"));


                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);

                if (element.Attribute("RetryInterval") != null && !string.IsNullOrEmpty(element.Attribute("RetryInterval").Value))
                {
                    int retryInterval = int.Parse(GetAttribute(element, "RetryInterval"));
                    tcpClientChannel = new TcpClientChannel(name, ipEndPoint, retryInterval);
                }
                else
                {
                    tcpClientChannel = new TcpClientChannel(name, ipEndPoint);
                }

                channel = tcpClientChannel;
            }
            else if (channelType == "UdpChannel")
            {
                UdpChannel udpChannel = null;

                int remotePort = int.Parse(GetAttribute(element, "RemotePort"));
                int localPort  = int.Parse(GetAttribute(element, "LocalPort"));

                if (element.Attribute("LocalIP") != null && !string.IsNullOrEmpty(element.Attribute("LocalIP").Value))
                {
                    string localIP = GetAttribute(element, "LocalIP");
                    udpChannel = new UdpChannel(name, remotePort, localPort, IPAddress.Parse(localIP));
                }
                else
                {
                    udpChannel = new UdpChannel(name, remotePort, localPort);
                }

                channel = udpChannel;
            }
            else if (channelType == "HttpReaderChannel")
            {
                HttpReaderChannel httpReaderChannel = null;

                int    remotePort = int.Parse(GetAttribute(element, "RemotePort"));
                string remoteIP   = GetAttribute(element, "RemoteIP");
                string requestUrl = GetAttribute(element, "RequestUrl");
                string userName   = string.Empty;
                string password   = string.Empty;

                if (element.Attribute("UserName") != null && !string.IsNullOrEmpty(element.Attribute("UserName").Value))
                {
                    userName = GetAttribute(element, "UserName");
                }

                if (element.Attribute("Password") != null && !string.IsNullOrEmpty(element.Attribute("Password").Value))
                {
                    password = GetAttribute(element, "Password");
                }

                httpReaderChannel = new HttpReaderChannel(name, remoteIP, remotePort, requestUrl, userName, password);

                channel = httpReaderChannel;
            }
            else
            {
                throw new UnknownElementException("Unknown channel type:" + channelType);
            }

            _adapterObjects[_currentAdapterName].Add(channel.Name, channel);

            return(channel);
        }
 protected SendQueueBase(UdpChannel channel, int initialCapacity)
 {
     _channel  = channel;
     _commands = new Queue <OutgoingCommand>(initialCapacity);
 }
Ejemplo n.º 16
0
 public UnreliableChannel(IChannelOption channelOption, ILogger logger, NetStatistic statistic, UdpChannel channel)
     : base(channelOption, logger, statistic)
 {
     _udpChannel = channel;
 }
Ejemplo n.º 17
0
 public void Setup()
 {
     tested = new UdpChannel(new UdpClient(0));
 }
 public ReliableSendQueue(UdpChannel channel, int initialCapacity) : base(channel, initialCapacity)
 {
     _sentCommands = new List <OutgoingCommand>(initialCapacity);
 }
Ejemplo n.º 19
0
 public static IChannel Create(int localPort, string hostname, int port, CancellationToken token)
 {
     return(UdpChannel.Create(localPort, hostname, port, token));
 }
Ejemplo n.º 20
0
        private object GetChannel(XElement element)
        {
            string channelType = GetAttribute(element, "Type");
            string name = GetAttribute(element, "Name");

            IObjectWithName channel = null;

            if (channelType == "TcpServerChannel")
            {

                int port = int.Parse(GetAttribute(element, "LocalPort"));

                TcpServerChannel tcpServerChannel = new TcpServerChannel(name, port);

                channel = tcpServerChannel;
            }
            else if (channelType == "TcpClientChannel")
            {
                TcpClientChannel tcpClientChannel = null;

                string ip = GetAttribute(element, "RemoteIP");
                int port = int.Parse(GetAttribute(element, "RemotePort"));

                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);

                if (element.Attribute("RetryInterval") != null && !string.IsNullOrEmpty(element.Attribute("RetryInterval").Value))
                {
                    int retryInterval = int.Parse(GetAttribute(element, "RetryInterval"));
                    tcpClientChannel = new TcpClientChannel(name, ipEndPoint, retryInterval);
                }
                else
                {
                    tcpClientChannel = new TcpClientChannel(name, ipEndPoint);
                }

                channel = tcpClientChannel;
            }
            else if (channelType == "UdpChannel")
            {
                UdpChannel udpChannel = null;

                int remotePort = int.Parse(GetAttribute(element, "RemotePort"));
                int localPort = int.Parse(GetAttribute(element, "LocalPort"));

                if (element.Attribute("LocalIP") != null && !string.IsNullOrEmpty(element.Attribute("LocalIP").Value))
                {
                    string localIP = GetAttribute(element, "LocalIP");
                    udpChannel = new UdpChannel(name, remotePort, localPort, IPAddress.Parse(localIP));
                }
                else
                {
                    udpChannel = new UdpChannel(name, remotePort, localPort);
                }

                channel = udpChannel;
            }
            else if (channelType == "HttpReaderChannel")
            {
                HttpReaderChannel httpReaderChannel = null;

                int remotePort = int.Parse(GetAttribute(element, "RemotePort"));
                string remoteIP = GetAttribute(element, "RemoteIP");
                string requestUrl = GetAttribute(element, "RequestUrl");
                string userName = string.Empty;
                string password = string.Empty;

                if (element.Attribute("UserName") != null && !string.IsNullOrEmpty(element.Attribute("UserName").Value))
                {
                    userName = GetAttribute(element, "UserName");
                }

                if (element.Attribute("Password") != null && !string.IsNullOrEmpty(element.Attribute("Password").Value))
                {
                    password = GetAttribute(element, "Password");
                }

                httpReaderChannel = new HttpReaderChannel(name, remoteIP, remotePort, requestUrl, userName, password);

                channel = httpReaderChannel;
            }
            else
            {
                throw new UnknownElementException("Unknown channel type:" + channelType);
            }

            _adapterObjects[_currentAdapterName].Add(channel.Name, channel);

            return channel;
        }
 internal ReceiveQueueBase(UdpChannel channel, int initialCapacity)
 {
     _channel  = channel;
     _commands = new Dictionary <long, IncomingCommand>(initialCapacity);
 }
Ejemplo n.º 22
0
 public AckSendQueue(UdpChannel channel, int initialCapacity)
     : base(channel, initialCapacity)
 {
 }
Ejemplo n.º 23
0
 public static IChannel Create(UdpClient client, IPEndPoint remoteEP, CancellationToken token)
 {
     return(UdpChannel.Create(client, remoteEP, token));
 }
Ejemplo n.º 24
0
 public Receiver(UdpChannel client)
 {
     _client    = client;
     Discovered = new ConcurrentDictionary <string, NodeInfoEntry>();
 }
Ejemplo n.º 25
0
 public static IChannel Create(int localPort, IPEndPoint remoteEP, CancellationToken token)
 {
     return(UdpChannel.Create(localPort, remoteEP, token));
 }
Ejemplo n.º 26
0
 public Broadcaster(UdpChannel channel, NodeInfo info)
 {
     _channel = channel;
     _info    = info;
 }