Esempio n. 1
0
        public async virtual Task <byte[]> GetResponseAsync()
        {
            if (Connection == null)
            {
                Connection = ConnectionManager.GetTrackerConnection();
            }
            Connection.Open();
            try
            {
                NetworkStream stream       = Connection.GetStream();
                byte[]        headerBuffer = Header.ToByte();
                await stream.WriteAsync(headerBuffer, 0, headerBuffer.Length);

                await stream.WriteAsync(Body, 0, Body.Length);

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

                int    count  = (int)header.Length;
                byte[] body   = new byte[count];
                var    offset = 0;
                while (count > 0)
                {
                    var readCount = await stream.ReadAsync(body, offset, count);

                    if (readCount <= 0)
                    {
                        throw new FDFSException("Get Response Error,readCount <= 0");
                    }

                    offset += readCount;
                    count  -= readCount;
                }

                Connection.Close();
                return(body);
            }
            catch (Exception ex)
            {
                Connection.Release();
                throw ex;
                //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.
            }
        }
Esempio n. 2
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();
        }