Ejemplo n.º 1
0
        public void Connect(string domain, int port)
        {
            if (!ipsCachedDictionary.ContainsKey(domain) || ipsCachedDictionary[domain].Expire < DateTime.UtcNow)
            {
                var ips       = Dns.GetHostAddresses(domain);
                var ipsCached = new LJPDNSCache
                {
                    Ips     = ips,
                    Expire  = DateTime.UtcNow.AddSeconds(this.options.ExpirationDNSCache),
                    Length  = ips.Length,
                    Current = 0
                };
                this.ipsCachedDictionary[domain] = ipsCached;
            }

            var currentIp = this.ipsCachedDictionary[domain].Current;

            var ip = this.ipsCachedDictionary[domain].Ips[currentIp];

            try
            {
                ClientConnector.Connect(ip, port);
            }
            catch (SocketException)
            {
                var ipCached = this.ipsCachedDictionary[domain];
                ipCached.Current = ipCached.Current < (ipCached.Ips.Length - 1) ? ++ipCached.Current : 0;
                throw;
            }
        }
Ejemplo n.º 2
0
        private void InitClientConnector()
        {
            _clientConnector = new ClientConnector(new ClientConnectorSettings()
            {
                PacketsMap        = new Dictionary <Tuple <int, int>, Type>(_packetsMap),
                ServerAddressList = new List <Tuple <string, int> >()
                {
                    new Tuple <string, int>(Program.AppSettingsClient.ServerAddress, 1112)
                }
            });

            _clientConnector.OnPacket     += ClientConnector_OnPacket;
            _clientConnector.OnConnect    += ClientConnector_OnConnect;
            _clientConnector.OnDisconnect += ClientConnector_OnDisconnect;
            _clientConnector.OnException  += ClientConnector_OnException;
            _clientConnector.OnDebugLog   += ClientConnector_OnDebugLog;

            _clientConnector.Connect();

            object loginResponse = null;

            try { loginResponse = _clientConnector.SendRequest(new LoginRequestPacket {
                    Username = "******"
                }); } catch (Exception ex) { Console.WriteLine("Exception on first packet:" + ex.ToString()); }
            Console.WriteLine($"Response loginResponse:{JsonConvert.SerializeObject(loginResponse)}");
        }
Ejemplo n.º 3
0
 public Form1()
 {
     connector = new ClientConnector(port, address);
     connector.Connect();
     InitializeComponent();
     timer1.Interval = 3000;
     timer1.Start();
 }
Ejemplo n.º 4
0
        public void checkForConnection()
        {
            ClientConnector client = new ClientConnector(port, address);

            client.Connect();
            Assert.IsTrue(client.CheckForConnection());
            ClientConnector client2 = new ClientConnector(port, address);

            Assert.IsFalse(client2.CheckForConnection());
        }
        public async Task TestReceiveAsync()
        {
            ClientConnector client = new ClientConnector(port, address);

            client.Connect();
            CancellationTokenSource source = new CancellationTokenSource();

            SendToClientOfListener();
            client.ReceiveMessage(source.Token);
            Thread.Sleep(10000);
            source.Cancel();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("TestSimpleEchoClient");


            _clientConnector = new ClientConnector(new ClientConnectorSettings()
            {
                PacketsMap = new Dictionary <Tuple <int, int>, Type>()
                {
                    { new Tuple <int, int>(1, 1), typeof(string) },
                },
                ServerAddressList = new List <Tuple <string, int> >()
                {
                    new Tuple <string, int>("127.0.0.1", 1112)
                }
            });

            _clientConnector.OnPacket     += ClientConnector_OnPacket;
            _clientConnector.OnConnect    += ClientConnector_OnConnect;
            _clientConnector.OnDisconnect += ClientConnector_OnDisconnect;
            _clientConnector.OnException  += ClientConnector_OnException;
            _clientConnector.OnDebugLog   += ClientConnector_OnDebugLog;

            _clientConnector.Connect();


            try { _clientConnector.Send(1, 1, "start"); } catch (Exception ex) { Console.WriteLine("Exception on first packet:" + ex.ToString()); }


            while (true)
            {
                try
                {
                    Console.WriteLine("Enter Input");
                    var inputLine = Console.ReadLine();

                    if (inputLine == "exp")
                    {
                        _clientConnector.SendRequest(1, 1, inputLine);
                    }
                    else
                    {
                        _clientConnector.Send(1, 1, inputLine);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception:" + ex.ToString());
                }
            }
        }
Ejemplo n.º 7
0
        /// <inheritdoc />
        public void Connect()
        {
            if (IsRunning)
            {
                throw new InvalidOperationException("Client is already running.");
            }

            if (IsConnected)
            {
                throw new InvalidOperationException("Client is already connected to remote host.");
            }

            if (ClientConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ClientConfiguration), "Undefined Client configuration.");
            }

            if (ClientConfiguration.Port <= 0)
            {
                throw new ArgumentException($"Invalid port number '{ClientConfiguration.Port}' in configuration.", nameof(ClientConfiguration.Port));
            }

            if (NetworkHelper.BuildIPAddress(ClientConfiguration.Host) == null)
            {
                throw new ArgumentException($"Invalid host address '{ClientConfiguration.Host}' in configuration", nameof(ClientConfiguration.Host));
            }

            SocketError error = connector.Connect(connectSocketArgs);

            if (!IsConnected && error != SocketError.Success)
            {
                OnError(new InvalidOperationException("Could not connect to login server."));
                return;
            }

            receiver = new ClientReceiver(this);
            if (!Socket.ReceiveAsync(receiveSocketArgs))
            {
                receiver.Receive(receiveSocketArgs);
            }

            IsRunning = true;
        }
Ejemplo n.º 8
0
        public async Task SendAsync()
        {
            ClientConnector client = new ClientConnector(port, address);

            client.Connect();
            Message message = new Message("Abdulmueez", "Hello", DateTime.Now);

            client.SendAsync(message);
            Message value = await CheckClientOfListener();

            Assert.AreEqual(value.Msg, message.Msg);
            Assert.AreEqual(value.UserName, message.UserName);
            Assert.AreEqual(value.time, message.time);
            message.Msg += "2";
            client.SendAsync(message);
            value = await CheckClientOfListener();

            Assert.AreEqual(value.Msg, message.Msg);
            Assert.AreEqual(value.UserName, message.UserName);
            Assert.AreEqual(value.time, message.time);
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("TcpConnectors.TestClient");


            _clientConnector = new ClientConnector(new ClientConnectorSettings()
            {
                PacketsMap        = SendPacketsUtils.GetServer2ClientMapping(Assembly.GetAssembly(typeof(LoginResponsePacket))),
                ServerAddressList = new List <Tuple <string, int> >()
                {
                    new Tuple <string, int>("127.0.0.1", 1111)
                }
            });

            _clientConnector.OnPacket     += ClientConnector_OnPacket;
            _clientConnector.OnConnect    += ClientConnector_OnConnect;
            _clientConnector.OnDisconnect += ClientConnector_OnDisconnect;
            _clientConnector.OnException  += ClientConnector_OnException;
            _clientConnector.OnDebugLog   += ClientConnector_OnDebugLog;;

            _clientConnector.Connect();

            Thread.Sleep(-1);
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            Console.WriteLine("TestRequestMultiResponseClient");

            var serverIp = "127.0.0.1";

            //var serverIp = "83.151.199.77";

            _clientConnector = new ClientConnector(new ClientConnectorSettings()
            {
                PacketsMap = new Dictionary <Tuple <int, int>, Type>()
                {
                    { new Tuple <int, int>(1, 1), typeof(GetListResponsePacket) },
                    { new Tuple <int, int>(1, 2), typeof(GetListResponsePacket) },
                },
                ServerAddressList = new List <Tuple <string, int> >()
                {
                    new Tuple <string, int>(serverIp, 1112)
                }
            });

            _clientConnector.OnPacket     += ClientConnector_OnPacket;
            _clientConnector.OnConnect    += ClientConnector_OnConnect;
            _clientConnector.OnDisconnect += ClientConnector_OnDisconnect;
            _clientConnector.OnException  += ClientConnector_OnException;
            _clientConnector.OnDebugLog   += ClientConnector_OnDebugLog;

            _clientConnector.Connect();

            while (true)
            {
                try
                {
                    Console.WriteLine("Enter Input (r-request, m-request multi response, x - test RequestMultiResponsesHandler)");
                    var inputLine = Console.ReadLine();

                    if (inputLine == "r")
                    {
                        Console.WriteLine("Perform Request (large single response)");
                        var resPacket = _clientConnector.SendRequest(1, 1, new GetListRequestPacket()) as GetListResponsePacket;
                        //Console.WriteLine($"response packet:{JsonConvert.SerializeObject(resPacket)}");
                        Console.WriteLine($"response packet count:{resPacket.Records.Count}");
                    }
                    else if (inputLine == "m")
                    {
                        Console.WriteLine("Perform Request - multi responses");

                        for (int i = 0; i < 10; i++)
                        {
                            _clientConnector.SendRequestMultiResponses(
                                1,
                                2,
                                new GetListRequestPacket(),
                                MultiResponseCallback);
                        }

                        //var resPacket = _clientConnector.SendRequest(1, 1, new GetListRequestPacket()) as GetListResponsePacket;
                        //Console.WriteLine($"response packet:{JsonConvert.SerializeObject(resPacket)}");
                        //Console.WriteLine($"response packet count:{resPacket.List.Count}");
                    }
                    else if (inputLine == "x")
                    {
                        Console.WriteLine("TestRequestMultiResponsesHandler");
                        var test = new TestRequestMultiResponsesHandler();
                        test.Run();
                    }
                    else
                    {
                        Console.WriteLine("input incorrect.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception:" + ex.ToString());
                }
            }
        }
Ejemplo n.º 11
0
        public void Test()
        {
            ClientConnector client = new ClientConnector(port, address);

            client.Connect();
        }
Ejemplo n.º 12
0
 public void Connect(System.Net.IPAddress ip, int port)
 {
     ClientConnector.Connect(ip, port);
 }