/// <summary> /// 得到tracker链接 /// </summary> /// <returns></returns> public static TcpConnection GetTrackerConnection(string groupName) { bool isGetTcpConnection = true; do { IPEndPoint tracker; if (string.IsNullOrEmpty(groupName))//没有限制组的时候,负载均衡 { _trackerServerIndex++; if (_trackerServerIndex >= TcpConnectionPoolManager.TrackerServers.Length) { _trackerServerIndex = 0; } tracker = TcpConnectionPoolManager.TrackerServers[_trackerServerIndex]; } else { IList <IPEndPoint> list = TcpConnectionPoolManager.GroupServer[groupName]; int index = sort.ContainsKey(groupName) ? sort[groupName] + 1 : 0; if (index >= list.Count) { index = 0; } tracker = list[index]; sort[groupName] = index; } IPAddress ip = tracker.Address; int port = tracker.Port; IObjectPool <TcpConnection> pool = TcpConnectionPoolManager.GetPool(ip.ToString(), port, true, false); try { if (null != _logger) { _logger.InfoFormat("Tracker可用连接数:{0}", pool.NumIdle); } TcpConnection tcp = pool.GetObject(ip.ToString(), port); if (null != tcp && tcp.Connected) { return(tcp); } isGetTcpConnection = false; } catch (Exception exc) { if (null != _logger) { _logger.ErrorFormat("连接追踪器服务器时发生异常,异常信息为:{0}", exc.Message); } isGetTcpConnection = false; } } while (!isGetTcpConnection); return(null); }
/// <summary> /// 释放此 <see cref="T:System.Net.Sockets.TcpClient"/> 实例,而不关闭基础连接。 /// </summary> /// <param name="isTracker">if set to <c>true</c> [is tracker].</param> /// <param name="isStorage">if set to <c>true</c> [is storage].</param> public void Close(bool isTracker, bool isStorage) { if (!_isFromPool) { byte[] headerBuffer = Util.PackHeader(Protocol.FDFS_PROTO_CMD_QUIT, 0, 0); GetStream().Write(headerBuffer, 0, headerBuffer.Length); GetStream().Close(); Close(); } TcpConnectionPoolManager.GetPool(_ipAddress, _port, isTracker, isStorage).ReturnObject(this); }
public static TcpConnection GetStorageConnection(string groupName) { StorageServerInfo storageServerInfo = TrackerClient.GetStoreStorage(groupName); IObjectPool <TcpConnection> pool = TcpConnectionPoolManager.GetPool(storageServerInfo.IpAddress, storageServerInfo.Port, false, true); try { TcpConnection storageConnection = pool.GetObject(storageServerInfo.IpAddress, storageServerInfo.Port); storageConnection.Index = storageServerInfo.StorePathIndex; if (null != _logger) { _logger.InfoFormat("Storage可用连接数为:{0}", pool.NumIdle); } return(storageConnection); } catch (Exception exc) { if (null != _logger) { _logger.WarnFormat("连接Storage服务器时发生异常,异常信息为:{0}", exc.Message); } throw; } }