예제 #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>();
        }
예제 #2
0
파일: Statsd.cs 프로젝트: Elders/StatsD
 public Statsd(string host, int port, string prefix = null)
 {
     this.prefix = prefix;
     hasPrefix = !String.IsNullOrEmpty(prefix);
     udpChannel = new UdpChannel(host, port);
     Current = this;
 }
예제 #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);
        }
예제 #4
0
파일: Statsd.cs 프로젝트: Elders/StatsD
 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();
        }
예제 #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));
 }
예제 #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);
        }
예제 #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}");
        }
예제 #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);
        }
예제 #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);
            }
        }
예제 #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)
 {
 }
예제 #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);
 }
예제 #16
0
 public UnreliableChannel(IChannelOption channelOption, ILogger logger, NetStatistic statistic, UdpChannel channel)
     : base(channelOption, logger, statistic)
 {
     _udpChannel = channel;
 }
예제 #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);
 }
예제 #19
0
 public static IChannel Create(int localPort, string hostname, int port, CancellationToken token)
 {
     return(UdpChannel.Create(localPort, hostname, port, token));
 }
예제 #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);
 }
예제 #22
0
 public AckSendQueue(UdpChannel channel, int initialCapacity)
     : base(channel, initialCapacity)
 {
 }
예제 #23
0
 public static IChannel Create(UdpClient client, IPEndPoint remoteEP, CancellationToken token)
 {
     return(UdpChannel.Create(client, remoteEP, token));
 }
예제 #24
0
파일: Receiver.cs 프로젝트: ziachap/Osprey
 public Receiver(UdpChannel client)
 {
     _client    = client;
     Discovered = new ConcurrentDictionary <string, NodeInfoEntry>();
 }
예제 #25
0
 public static IChannel Create(int localPort, IPEndPoint remoteEP, CancellationToken token)
 {
     return(UdpChannel.Create(localPort, remoteEP, token));
 }
예제 #26
0
 public Broadcaster(UdpChannel channel, NodeInfo info)
 {
     _channel = channel;
     _info    = info;
 }