Пример #1
0
        /// <summary>
        /// query storage server stat info of the group
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <param name="groupName">the group name of storage server</param>
        /// <param name="storageIpAddr">the storage server ip address, can be null or empty</param>
        /// <returns>storage server stat array, return null if fail</returns>
        public StructStorageStat[] listStorages(TrackerServer trackerServer, string groupName, string storageIpAddr)
        {
            byte[]    header;
            byte[]    bGroupName;
            byte[]    bs;
            int       len;
            bool      bNewConnection;
            TcpClient trackerSocket;

            if (trackerServer == null)
            {
                trackerServer = getConnection();
                if (trackerServer == null)
                {
                    return(null);
                }
                bNewConnection = true;
            }
            else
            {
                bNewConnection = false;
            }
            trackerSocket = trackerServer.getSocket();
            Stream output = trackerSocket.GetStream();

            try
            {
                bs         = Encoding.GetEncoding(ClientGlobal.g_charset).GetBytes(groupName);
                bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];

                if (bs.Length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
                {
                    len = bs.Length;
                }
                else
                {
                    len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                }
                for (int i = 0; i < bGroupName.Length; i++)
                {
                    bGroupName[i] = (byte)0;
                }
                Array.Copy(bs, 0, bGroupName, 0, len);

                int    ipAddrLen;
                byte[] bIpAddr;
                if (storageIpAddr != null && storageIpAddr.Length > 0)
                {
                    bIpAddr = Encoding.GetEncoding(ClientGlobal.g_charset).GetBytes(storageIpAddr);
                    if (bIpAddr.Length < ProtoCommon.FDFS_IPADDR_SIZE)
                    {
                        ipAddrLen = bIpAddr.Length;
                    }
                    else
                    {
                        ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1;
                    }
                }
                else
                {
                    bIpAddr   = null;
                    ipAddrLen = 0;
                }

                header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte)0);
                byte[] wholePkg = new byte[header.Length + bGroupName.Length + ipAddrLen];
                Array.Copy(header, 0, wholePkg, 0, header.Length);
                Array.Copy(bGroupName, 0, wholePkg, header.Length, bGroupName.Length);
                if (ipAddrLen > 0)
                {
                    Array.Copy(bIpAddr, 0, wholePkg, header.Length + bGroupName.Length, ipAddrLen);
                }
                output.Write(wholePkg, 0, wholePkg.Length);

                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.GetStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }

                ProtoStructDecoder decoder = new ProtoStructDecoder();
                return(decoder.decode <StructStorageStat>(pkgInfo.body, StructStorageStat.getFieldsTotalSize()));
            }
            catch (IOException ex)
            {
                if (!bNewConnection)
                {
                    try
                    {
                        trackerServer.close();
                    }
                    catch (IOException ex1)
                    {
                    }
                }

                throw ex;
            }
            catch (Exception ex)
            {
                this.errno = ProtoCommon.ERR_NO_EINVAL;
                return(null);
            }
            finally
            {
                if (bNewConnection)
                {
                    try
                    {
                        trackerServer.close();
                    }
                    catch (IOException ex1)
                    {
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// list groups
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <returns>group stat array, return null if fail</returns>
        public StructGroupStat[] listGroups(TrackerServer trackerServer)
        {
            byte[]    header;
            string    ip_addr;
            int       port;
            byte      cmd;
            int       out_len;
            bool      bNewConnection;
            byte      store_path;
            TcpClient trackerSocket;

            if (trackerServer == null)
            {
                trackerServer = getConnection();
                if (trackerServer == null)
                {
                    return(null);
                }
                bNewConnection = true;
            }
            else
            {
                bNewConnection = false;
            }

            trackerSocket = trackerServer.getSocket();
            Stream output = trackerSocket.GetStream();

            try
            {
                header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_GROUP, 0, (byte)0);
                output.Write(header, 0, header.Length);

                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.GetStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }

                ProtoStructDecoder decoder = new ProtoStructDecoder();
                return(decoder.decode <StructGroupStat>(pkgInfo.body, StructGroupStat.getFieldsTotalSize()));
            }
            catch (IOException ex)
            {
                if (!bNewConnection)
                {
                    try
                    {
                        trackerServer.close();
                    }
                    catch (IOException ex1)
                    {
                    }
                }

                throw ex;
            }
            catch (Exception ex)
            {
                this.errno = ProtoCommon.ERR_NO_EINVAL;
                return(null);
            }
            finally
            {
                if (bNewConnection)
                {
                    try
                    {
                        trackerServer.close();
                    }
                    catch (IOException ex1)
                    {
                    }
                }
            }
        }