コード例 #1
0
        /// <summary>
        /// 根据缓存Key获取其所存储的服务器信息
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>Key所存储的服务器信息</returns>
        public static string GetHostInfoByCacheKey(string key)
        {
            string hostInfo = "";

            SockIO sock = null;

            try
            {
                sock = SockIOPool.GetInstance(SocketPoolName).GetSock(key);

                if (sock != null)
                {
                    hostInfo = sock.Host;
                }
            }
            finally
            {
                if (sock != null)
                {
                    sock.Close();
                }
            }

            return(hostInfo);
        }
コード例 #2
0
ファイル: SockIO.cs プロジェクト: yanh19930226/yunExpress
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">hostname:port</param>
        /// <param name="timeout">read timeout value for connected socket</param>
        /// <param name="connectTimeout">timeout for initial connections</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
            {
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));
            }

            _pool = pool;


            String[] ip = host.Split(':');

            // get socket: default is to use non-blocking connect
            if (connectTimeout > 0)
            {
                _socket = GetSocket(ip[0], int.Parse(ip[1], new System.Globalization.NumberFormatInfo()), connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(ip[0]), int.Parse(ip[1], new System.Globalization.NumberFormatInfo())));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host;
        }
コード例 #3
0
ファイル: SockIO.cs プロジェクト: yanh19930226/yunExpress
        /// <summary>
        /// creates a new SockIO object wrapping a socket
        /// connection to host:port, and its input and output streams
        /// </summary>
        /// <param name="pool">Pool this object is tied to</param>
        /// <param name="host">host to connect to</param>
        /// <param name="port">port to connect to</param>
        /// <param name="timeout">int ms to block on data for read</param>
        /// <param name="connectTimeout">timeout (in ms) for initial connection</param>
        /// <param name="noDelay">TCP NODELAY option?</param>
        public SockIO(SockIOPool pool, String host, int port, int timeout, int connectTimeout, bool noDelay)
            : this()
        {
            if (host == null || host.Length == 0)
            {
                throw new ArgumentNullException(GetLocalizedString("host"), GetLocalizedString("null host"));
            }

            _pool = pool;


            if (connectTimeout > 0)
            {
                _socket = GetSocket(host, port, connectTimeout);
            }
            else
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(new IPEndPoint(IPAddress.Parse(host), port));
            }

            _networkStream = new BufferedStream(new NetworkStreamIgnoreSeek(_socket));

            _host = host + ":" + port;
        }
コード例 #4
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        public static SockIOPool GetInstance(String poolName)
        {
            if (Pools.ContainsKey(poolName))
            {
                return((SockIOPool)Pools[poolName]);
            }

            SockIOPool pool = new SockIOPool();

            Pools[poolName] = pool;

            return(pool);
        }
コード例 #5
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
 public MaintenanceThread(SockIOPool pool)
 {
     _thread = new Thread(new ThreadStart(Maintain));
     _pool   = pool;
 }
コード例 #6
0
        /// <summary>
        /// 初始化相关配置项
        /// </summary>
        static void Initialization()
        {
            #region 加载配置文件

            MemcachedConfigSection memcachedConfig = (MemcachedConfigSection)ConfigurationManager.GetSection("memcached");

            if (memcachedConfig == null)
            {
                throw new ConfigurationErrorsException("未找到memcached对应的配置节点,请检查配置文件!");
            }

            IsMemcachedEnabled = memcachedConfig.Enabled;

            if (memcachedConfig.Servers == null || memcachedConfig.Servers.Count < 1)
            {
                throw new ConfigurationErrorsException("未找到任何服务器配置信息,请检查配置文件!");
            }

            ServerList = new string[memcachedConfig.Servers.Count];

            for (int i = 0; i < memcachedConfig.Servers.Count; i++)
            {
                ServerList[i] = string.Concat(memcachedConfig.Servers[i].ServerIP, ":", memcachedConfig.Servers[i].Port);
            }

            if (memcachedConfig.SocketPool == null)
            {
                throw new ConfigurationErrorsException("未找到Socket连接串配置信息,请检查配置文件!");
            }

            SocketPoolName = memcachedConfig.SocketPool.PoolName;

            #endregion

            #region 初始化Socket连接串

            pool = SockIOPool.GetInstance(SocketPoolName);

            pool.SetServers(ServerList);

            // 初始化链接数
            pool.InitConnections = memcachedConfig.SocketPool.MinConnections;

            // 最少链接数
            pool.MinConnections = memcachedConfig.SocketPool.MinConnections;

            // 最大连接数
            pool.MaxConnections = memcachedConfig.SocketPool.MaxConnections;

            // Socket链接超时时间
            pool.SocketConnectTimeout = memcachedConfig.SocketPool.SocketConnectTimeout;

            // Socket超时时间
            pool.SocketTimeout = memcachedConfig.SocketPool.SocketTimeout;

            // 维护线程休眠时间
            pool.MaintenanceSleep = memcachedConfig.SocketPool.MaintenanceSleep;

            // 失效转移(一种备份操作模式)
            pool.Failover = memcachedConfig.SocketPool.Failover;

            // 是否用nagle算法启动socket
            pool.Nagle = memcachedConfig.SocketPool.Nagle;

            // 散列算法
            pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash;

            pool.Initialize();

            #endregion

            #region 初始化Memcached客户端

            client = new MemcachedClient();

            client.PoolName = SocketPoolName;

            // 是否启用压缩
            client.EnableCompression = false;

            #endregion
        }