예제 #1
0
        public static TcpClient CreateSocket(IPEndPoint endpoint, DistCacheConfigBase config)
        {
            TcpClient Connection = new TcpClient();

            if (Connection.ReceiveBufferSize != 1 << 15)
            {
                Connection.ReceiveBufferSize = 1 << 15;
            }

            if (Connection.SendBufferSize != 1 << 15)
            {
                Connection.SendBufferSize = 1 << 15;
            }
            if (Connection.ReceiveTimeout != config.SocketReadTimeout)
            {
                Connection.ReceiveTimeout = config.SocketReadTimeout;
            }

            if (Connection.SendTimeout != config.SocketWriteTimeout)
            {
                Connection.SendTimeout = config.SocketWriteTimeout;
            }
            if (Connection.Client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive) as int? == 1)
            {
                Connection.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            }
            Connection.Connect(endpoint);

            return(Connection);
        }
예제 #2
0
 protected SocketHandler(TcpClient tcp, DistCacheConfigBase config)
 {
     this.lockMe          = new object();
     this.Config          = config;
     this._connection     = tcp;
     this._messagesToSend = new ConcurrentQueue <MessageTriplete>();
     this._memoryStream   = new MemoryStream();
 }
예제 #3
0
 protected SocketHandler(SocketHandler other)
 {
     this.lockMe = other.lockMe;
     lock (this.lockMe)
     {
         this.Config              = Config;
         this._connection         = other._connection;
         other._connection        = null;
         this._memoryStream       = other._memoryStream;
         other._memoryStream      = null;
         this.Config              = other.Config;
         this._messagesToSend     = other._messagesToSend;
         this._posistionReadUntil = other._posistionReadUntil;
     }
 }
예제 #4
0
        public void HandShakeAcceptMoar()
        {
            var dic = new Dictionary <string, string>();

            string pass = DistCacheConfigBase.GenerateRandomPassword();

            var serverConfig = new DistCacheServerConfig()
            {
                Password = pass
            };

            int toAdd = 16;
            int iters = 2;

            using (var srv = new CacheServer(serverConfig))
            {
                var clientscon = new ConcurrentBag <DistCacheClient>();

                for (int k = 0; k < iters; ++k)
                {
                    List <Task <DistCacheClient> > clients = new List <Task <DistCacheClient> >();
                    var ls = new List <DistCacheClient>();

                    for (int i = 0; i < toAdd; ++i)
                    {
                        clients.Add(new Task <DistCacheClient>(() =>
                        {
                            var o = DistCacheClient.Create(new DistCacheClientConfig()
                            {
                                Password = pass
                            });
                            clientscon.Add(o);
                            return(o);
                        }));
                        clients.Last().Start();
                    }

                    Task.WaitAll(clients.ToArray());
                }
                while (srv.PendingClientsCount > 0)
                {
                    Thread.Sleep(500);
                }
                Assert.AreEqual(toAdd * iters, srv.ConnectedClientsCount);
            }
        }
예제 #5
0
        public static void Main(string[] args)
        {
            var dic = new Dictionary <string, string>();

            string pass = DistCacheConfigBase.GenerateRandomPassword();

            var serverConfig = new DistCacheServerConfig()
            {
                Password = pass
            };

            using (var srv = new CacheServer(serverConfig))
            {
                int clients = 1;
                int msgs    = 50000;

                var ls = new ConcurrentBag <DistCacheClient>();

                for (int k = 0; k < clients; ++k)
                {
                    var client = DistCacheClient.Create(new DistCacheClientConfig()
                    {
                        Password = pass
                    });
                    ls.Add(client);
                }

                Stopwatch sw  = Stopwatch.StartNew();
                long      cnt = 0;
                for (int i = 0; i < msgs; ++i)
                {
                    foreach (var cl in ls)
                    {
                        string s = cl.GetMessage($"{Interlocked.Increment(ref cnt)}").Result;
                    }
                }
                string ss = "";
                while (ss != "q")
                {
                    Console.WriteLine($"got {Interlocked.Read(ref cnt)} {sw.Elapsed}");
                    ss = Console.ReadLine();
                }
            }
        }
예제 #6
0
        public void HandShakeAccept()
        {
            var dic = new Dictionary <string, string>();

            string pass = DistCacheConfigBase.GenerateRandomPassword();

            var serverConfig = new DistCacheServerConfig()
            {
                Password = pass
            };

            using (var srv = new CacheServer(serverConfig))
            {
                var ls     = new List <DistCacheClient>();
                var client = DistCacheClient.Create(new DistCacheClientConfig()
                {
                    Password = pass
                });
                Thread.Sleep(1000);
                Assert.IsTrue(srv.ConnectedClientsCount == 1);
            }
        }
예제 #7
0
 public ClientHandShakeHandler(TcpClient tcp, DistCacheConfigBase config, Guid clientUID) : base(tcp, config)
 {
     this.clientUID = clientUID;
 }