Example #1
0
        public virtual void GetResponse(FDFSResponse response)
        {
            if(this._connection == null)
                this._connection = ConnectionManager.GetTrackerConnection();
            _connection.Open();
            try
            {
                NetworkStream stream = this._connection.GetStream();
                this.SendRequest(stream);
                
                
                FDFSHeader header = new FDFSHeader(stream);
                if (header.Status != 0)
                    throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));

                if (response != null)
                    response.ReceiveResponse(stream, header.Length);
                _connection.Close();
            }
            catch(Exception ex)
            {
                _connection.Release();
                throw ex;//可以看Storage节点的log看
                //22    -〉下载字节数超过文件长度 invalid download file bytes: 10 > file remain bytes: 4
                //      -> 或者 pkg length is not correct
                //2     -〉没有此文件 error info: No such file or directory.
            }            
        }
Example #2
0
        public virtual void GetResponse(FDFSResponse response)
        {
            if (this._connection == null)
            {
                this._connection = ConnectionManager.GetTrackerConnection();
            }
            _connection.Open();
            try
            {
                NetworkStream stream = this._connection.GetStream();
                this.SendRequest(stream);


                FDFSHeader header = new FDFSHeader(stream);
                if (header.Status != 0)
                {
                    throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));
                }

                if (response != null)
                {
                    response.ReceiveResponse(stream, header.Length);
                }
                _connection.Close();
            }
            catch (Exception ex)
            {
                _connection.Release();
                throw ex;//可以看Storage节点的log看
                //22    -〉下载字节数超过文件长度 invalid download file bytes: 10 > file remain bytes: 4
                //      -> 或者 pkg length is not correct
                //2     -〉没有此文件 error info: No such file or directory.
            }
        }
Example #3
0
        public static byte[] GetResponse(FDFSRequest request)
        {
            Connection conn    = null;
            IPEndPoint address = null;

            try
            {
                try
                {
                    if (request.Connection == null)
                    {
                        address = locator.GetTrackerAddress();
                        conn    = GetTrackerConnection(address);
                    }
                    else
                    {
                        conn = request.Connection;
                    }
                    //打开
                    conn.OpenConnection();
                    locator.SetActive(address);
                }
                catch (SocketException ex)
                {
                    locator.SetInActive(address);
                    throw new FdfsConnectException("connect failed", ex);
                }

                var stream = conn.GetStream();

                var headerBuffer = request.Header.ToByte();

                stream.Write(headerBuffer, 0, headerBuffer.Length);
                stream.Write(request.Body, 0, request.Body.Length);

                var header = new FDFSHeader(stream);

                if (header.Status != 0)
                {
                    throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));
                }

                var body = new byte[header.Length];
                if (header.Length != 0)
                {
                    stream.Read(body, 0, (int)header.Length);
                }

                return(body);
            }
            finally
            {
                //关闭
                //Connection.Close();
                if (conn != null)
                {
                    conn.ReleaseConnection();
                }
            }
        }
        protected override async Task <T> ParseResponseInfo <T>(FDFSHeader responseHeader)
        {
            var buff = ArrayPool <byte> .Shared.Rent(DEFAULT_BUFFER_SIZE);

            try
            {
                var remainBytes = responseHeader.Length;
                while (remainBytes > 0)
                {
                    var bytes = await _connection.ReceiveExAsync(buff, remainBytes > buff.Length?buff.Length : (int)remainBytes);

                    int result;
                    if ((result = await Callback.ReceiveAsync(responseHeader.Length, buff, bytes)) != 0)
                    {
                        throw new FDFSStatusException(responseHeader.Status, $"Callback Receive Error:{result}");
                    }
                    remainBytes -= bytes;
                }
                return(new T());
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buff);

                responseHeader.Dispose();
            }
        }
Example #5
0
        public IEnumerator <object> GetEnumerator()
        {
            /*
             * 组装协议Header. header共10字节,格式如下:
             *       8 bytes body length
             *       1 byte command
             *       1 byte status
             */
            var header = new FDFSHeader(15 + _fileStream.Length, ProtoCommon.StorageProtoCmdUploadFile, 0);

            /*
             * 组装Body
             *  0       StorePathIndex
             *  1-8     文件长度
             *  9-14    扩展名
             *  15 +    文件
             */

            var extBuffer  = Util.StringToByte(_fileExt);
            var fileHeader = new byte[15]; // 文件头

            fileHeader[0] = _storageNode.StorePathIndex;

            var lengthBuffer = Util.LongToBuffer(_fileStream.Length);

            Array.Copy(lengthBuffer, 0, fileHeader, 1, lengthBuffer.Length);
            Array.Copy(extBuffer, 0, fileHeader, 9, extBuffer.Length);

            yield return(header.ToByte());

            yield return(fileHeader);

            yield return(_fileStream);
        }
Example #6
0
        protected virtual async Task <byte[]> ReceiveAsync()
        {
            byte[] numArray0 = new byte[10];
            if (await Connection.ReceiveExAsync(numArray0) == 0)
            {
                throw new FDFSException("Init Header Exeption : Cann't Read Stream");
            }

            var length  = Util.BufferToLong(numArray0, 0);
            var command = numArray0[8];
            var status  = numArray0[9];

            var fDFSHeader = new FDFSHeader(length, command, status);

            if (fDFSHeader.Status != 0)
            {
                throw new FDFSStatusException(fDFSHeader.Status, $"Get Response Error,Error Code:{fDFSHeader.Status}");
            }
            byte[] numArray = new byte[fDFSHeader.Length];
            if (fDFSHeader.Length != (long)0)
            {
                await Connection.ReceiveExAsync(numArray);
            }
            return(numArray);
        }
Example #7
0
        private async Task <T> GetResponseInfo <T>(FDFSHeader responseHeader) where T : IFDFSResponse, new()
        {
            if (responseHeader.Status != 0)
            {
                if (Header.Command == FDFSConstants.STORAGE_PROTO_CMD_QUERY_FILE_INFO)
                {
                    return(default(T));
                }
                throw new FDFSStatusException(responseHeader.Status, $"Get Response Error, Error Code:{(FDFSConstants.ErrorCode)responseHeader.Status}");
            }

            if (responseHeader.Length <= 0)
            {
                return(default(T));
            }

            int resLen    = (int)responseHeader.Length;
            var resBuffer = ArrayPool <byte> .Shared.Rent(resLen);

            try
            {
                await _connection.ReceiveExAsync(resBuffer, resLen);

                var response = new T();
                response.ParseBuffer(resBuffer, resLen);
                return(response);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(resBuffer);

                responseHeader.Dispose();
            }
        }
Example #8
0
        private async Task CloseSocketAsync()
        {
            try
            {
                byte[] buffer0 = new FDFSHeader(0L, 0x52, 0).ToByte();
                await _socket.SendExAsync(buffer0);

                _socket.Close();
                _socket.Dispose();
            }
            catch
            {
                // ignored
            }
            _socket = null;
        }
        public IEnumerator <object> GetEnumerator()
        {
            byte[] masterFileBuffer = Util.StringToByte(_masterFileName);

            long bodyLength = 2 * ProtoCommon.FdfsProtoPkgLenSize
                              + ProtoCommon.FdfsFilePrefixMaxLen
                              + ProtoCommon.FdfsFileExtNameMaxLen
                              + masterFileBuffer.Length
                              + _stream.Length;

            var header = new FDFSHeader(bodyLength, ProtoCommon.StorageProtoCmdUploadSlaveFile, 0).ToByte();

            var package = new byte[header.Length + bodyLength - _stream.Length];

            Array.Copy(header, 0, package, 0, header.Length);
            long offset = header.Length;

            var masterFileLengthBuffer = Util.LongToBuffer(_masterFileName.Length);

            Array.Copy(masterFileLengthBuffer, 0, package, offset, masterFileLengthBuffer.Length);
            offset += masterFileLengthBuffer.Length;

            var indexBuffer = Util.LongToBuffer(_stream.Length);

            Array.Copy(indexBuffer, 0, package, offset, indexBuffer.Length);
            offset += indexBuffer.Length;

            //byte[] indexBuffer = Util.LongToBuffer(_storageNode.StorePathIndex);
            //Array.Copy(indexBuffer, 0, body, offset, indexBuffer.Length);
            //offset += indexBuffer.Length;

            byte[] prefixBuffer = Util.StringToByte(_prefix);
            Array.Copy(prefixBuffer, 0, package, offset, prefixBuffer.Length);
            offset += ProtoCommon.FdfsFilePrefixMaxLen;

            var extBuffer = Util.StringToByte(_fileExt);

            Array.Copy(extBuffer, 0, package, offset, extBuffer.Length);
            offset += ProtoCommon.FdfsFileExtNameMaxLen;

            Array.Copy(masterFileBuffer, 0, package, offset, masterFileBuffer.Length);

            yield return(package);

            yield return(_stream);
        }
Example #10
0
        private async Task <T> GetResponseInfo <T>(FDFSHeader responseHeader) where T : IFDFSResponse, new()
        {
            if (responseHeader.Status != 0)
            {
                if (Header.Command == FDFSConstants.STORAGE_PROTO_CMD_QUERY_FILE_INFO)
                {
                    return(default(T));
                }
                throw new FDFSStatusException(responseHeader.Status, $"Get Response Error, Error Code:{(FDFSConstants.ErrorCode)responseHeader.Status}");
            }

            if (responseHeader.Length <= 0)
            {
                return(default(T));
            }

            return(await ParseResponseInfo <T>(responseHeader));
        }
Example #11
0
        protected virtual async Task <T> ParseResponseInfo <T>(FDFSHeader responseHeader) where T : IFDFSResponse, new()
        {
            int resLen    = (int)responseHeader.Length;
            var resBuffer = ArrayPool <byte> .Shared.Rent(resLen);

            try
            {
                await _connection.ReceiveExAsync(resBuffer, resLen);

                var response = new T();
                response.ParseBuffer(resBuffer, resLen);
                return(response);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(resBuffer);

                responseHeader.Dispose();
            }
        }
Example #12
0
        public IEnumerator <object> GetEnumerator()
        {
            long length  = ProtoCommon.FdfsGroupNameMaxLen + _fileName.Length;
            var  header  = new FDFSHeader(length, 12, 0).ToByte();
            var  package = new byte[header.Length + length];

            Array.Copy(header, 0, package, 0, header.Length);
            long offset = header.Length;

            var groupNameBuffer = Util.StringToByte(_groupName);

            Array.Copy(groupNameBuffer, 0, package, offset, groupNameBuffer.Length);
            offset += ProtoCommon.FdfsGroupNameMaxLen;


            var fileNameBuffer = Util.StringToByte(_fileName);

            Array.Copy(fileNameBuffer, 0, package, offset, fileNameBuffer.Length);

            yield return(package);
        }
 public void ReleaseConnection(Connection conn)
 {
     if (!conn.InUse)
     {
         try
         {
             FDFSHeader header = new FDFSHeader(0, Consts.FDFS_PROTO_CMD_QUIT, 0);
             byte[] buffer = header.ToByte();
             conn.GetStream().Write(buffer, 0, buffer.Length);
             conn.GetStream().Close();
         }
         catch
         {
         }
     }
     conn.Close();            
     lock ((inUse as ICollection).SyncRoot)
     {
         inUse.Remove(conn);
     }
     autoEvent.Set();
 }
Example #14
0
 public void ReleaseConnection(Connection conn)
 {
     if (!conn.InUse)
     {
         try
         {
             FDFSHeader header = new FDFSHeader(0, Consts.FDFS_PROTO_CMD_QUIT, 0);
             byte[]     buffer = header.ToByte();
             conn.GetStream().Write(buffer, 0, buffer.Length);
             conn.GetStream().Close();
         }
         catch
         {
         }
     }
     conn.Close();
     lock ((inUse as ICollection).SyncRoot)
     {
         inUse.Remove(conn);
     }
     autoEvent.Set();
 }
Example #15
0
        public virtual byte[] GetResponse()
        {
            if (this._connection == null)
            {
                this._connection = ConnectionManager.GetTrackerConnection();
            }
            _connection.Open();
            try
            {
                NetworkStream stream       = this._connection.GetStream();
                byte[]        headerBuffer = this._header.ToByte();
                stream.Write(headerBuffer, 0, headerBuffer.Length);
                stream.Write(this._body, 0, this._body.Length);

                FDFSHeader header = new FDFSHeader(stream);
                if (header.Status != 0)
                {
                    throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));
                }
                byte[] body = new byte[header.Length];
                if (header.Length != 0)
                {
                    stream.Read(body, 0, (int)header.Length);
                }

                _connection.Close();
                return(body);
            }
            catch (Exception ex)
            {
                _connection.Release();
                throw ex;//可以看Storage节点的log看
                //22    -〉下载字节数超过文件长度 invalid download file bytes: 10 > file remain bytes: 4
                //      -> 或者 pkg length is not correct
                //2     -〉没有此文件 error info: No such file or directory.
            }
        }