public StorageClientEx(TrackerServer trackerServer, StorageServer storageServer) : base(trackerServer, storageServer) { }
/// <summary> /// query storage servers to upload file /// </summary> /// <param name="trackerServer">the tracker server</param> /// <param name="groupName">the group name to upload file to, can be empty</param> /// <returns>storage servers, return null if fail</returns> public StorageServer[] getStoreStorages(TrackerServer trackerServer, string groupName) { byte[] header; string ip_addr; int port; byte cmd; int out_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 { if (groupName == null || groupName.Length == 0) { cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL; out_len = 0; } else { cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL; out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; } header = ProtoCommon.packHeader(cmd, out_len, (byte)0); output.Write(header, 0, header.Length); if (groupName != null && groupName.Length > 0) { byte[] bGroupName; byte[] bs; int group_len; 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) { group_len = bs.Length; } else { group_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, group_len); output.Write(bGroupName, 0, bGroupName.Length); } ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.GetStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); this.errno = pkgInfo.errno; if (pkgInfo.errno != 0) { return(null); } if (pkgInfo.body.Length < ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN) { this.errno = ProtoCommon.ERR_NO_EINVAL; return(null); } int ipPortLen = pkgInfo.body.Length - (ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1); int recordLength = ProtoCommon.FDFS_IPADDR_SIZE - 1 + ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; if (ipPortLen % recordLength != 0) { this.errno = ProtoCommon.ERR_NO_EINVAL; return(null); } int serverCount = ipPortLen / recordLength; if (serverCount > 16) { this.errno = ProtoCommon.ERR_NO_ENOSPC; return(null); } StorageServer[] results = new StorageServer[serverCount]; byte store_path = pkgInfo.body[pkgInfo.body.Length - 1]; int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; for (int i = 0; i < serverCount; i++) { ip_addr = Encoding.GetEncoding(ClientGlobal.g_charset).GetString(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).Replace("\0", "").Replace("\0", "").Trim(); offset += ProtoCommon.FDFS_IPADDR_SIZE - 1; port = (int)ProtoCommon.buff2long(pkgInfo.body, offset); offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; results[i] = new StorageServer(ip_addr, port, store_path); } return(results); } catch (IOException ex) { if (!bNewConnection) { try { trackerServer.close(); } catch (IOException ex1) { } } throw ex; } finally { if (bNewConnection) { try { trackerServer.close(); } catch (IOException ex1) { } } } }