Пример #1
0
        public byte[] GetResponse()
        {
            if (Connection == null)
            {
                Connection = ConnectionManager.GetTrackerConnection();
            }

            try
            {
                //打开
                Connection.OpenConnection();
                var stream       = Connection.GetStream();
                var headerBuffer = Header.ToByte();

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

                var header = new FdfsHeader(stream);
                if (header.Status != 0)
                {
                    var fdfsEx = new FdfsException($"Get Response Error,Error Code:{header.Status}")
                    {
                        ErrorCode = header.Status
                    };
                    throw fdfsEx;
                }

                var length = (int)header.Length;
                var body   = new byte[length];
                if (length == 0)
                {
                    return(body);
                }

                var offset = stream.Read(body, 0, length);
                while (offset < length)
                {
                    offset += stream.Read(body, offset, length - offset);
                }

                return(body);
            }
            finally
            {
                //关闭
                //Connection.Close();
                Connection.ReleaseConnection();
            }
        }
Пример #2
0
        public void ReleaseConnection(Connection conn)
        {
            if (!conn.InUse)
            {
                var header = new FdfsHeader(0, Consts.FdfsProtoCmdQuit, 0);
                var buffer = header.ToByte();
                conn.GetStream().Write(buffer, 0, buffer.Length);
                conn.GetStream().Close();
            }

            conn.Close();

            lock ((_inUse as ICollection).SyncRoot)
                _inUse.Remove(conn);
            _autoEvent.Set();
        }