示例#1
0
 /// <summary>
 /// close conn
 /// </summary>
 public void CloseConnection()
 {
     if (this.Connected)
     {
         // 断开连接
         try
         {
             var header = new FDFSHeader(0, Consts.FDFS_PROTO_CMD_QUIT, 0);
             var buffer = header.ToByte();
             this.GetStream().Write(buffer, 0, buffer.Length);
             this.GetStream().Close();
         }
         catch
         {
         }
         // 关闭连接
         try
         {
             this.Close();
         }
         catch
         {
         }
     }
 }
示例#2
0
        public virtual 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)
                {
                    throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));
                }

                var body = new byte[header.Length];
                if (header.Length != 0)
                {
                    byte[] myReadBuffer      = new byte[1024 * 2];
                    int    numberOfBytesRead = 0;
                    int    pos   = 0;
                    int    tatal = (int)header.Length;
                    while (tatal - pos > 0)
                    {
                        numberOfBytesRead = stream.Read(myReadBuffer, 0, Math.Min(myReadBuffer.Length, tatal - pos));
                        Array.Copy(myReadBuffer, 0, body, pos, numberOfBytesRead);
                        pos += numberOfBytesRead;
                    }
                }
                return(body);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //关闭
                //Connection.Close();
                Connection.ReleaseConnection();
            }
        }
示例#3
0
        public virtual 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)
                    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);

                int offset = 0, bytesread = 0;
                while (offset < header.Length)
                {
                    bytesread = stream.Read(body, offset, (int)header.Length - offset);
                    if (bytesread == 0)
                        throw new Exception("网络异常断开,数据读取不完整。");
                    else
                        offset += bytesread;
                }
                return body;

                //return body;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //关闭
                //Connection.Close();
                Connection.ReleaseConnection();
            }
        }
示例#4
0
        public void ReleaseConnection(Connection conn)
        {
            if (!conn.InUse)
            {
                var header = new FDFSHeader(0, Consts.FDFS_PROTO_CMD_QUIT, 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();
        }
示例#5
0
        public virtual 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)
                {
                    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);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //关闭
                //Connection.Close();
                Connection.ReleaseConnection();
            }
        }
示例#6
0
        /// <summary>
        /// 获取响应
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        private byte[] GetResponse(Connection connection)
        {
            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)
                {
                    throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));
                }

                var body = new byte[header.Length];
                if (header.Length != 0)
                {
                    Task <int> task = stream.ReadAsync(body, 0, (int)header.Length);
                    task.Wait();
                    if (task.Result == 0)
                    {
                        body = null;
                    }
                }
                return(body);
            }
            catch (Exception ex)
            {
                //connection.Close();
                throw ex;
            }
            finally
            {
            }
        }
示例#7
0
文件: Pool.cs 项目: daimeiquan/test
        public void ReleaseConnection(Connection conn)
        {
            if (!conn.InUse)
            {
                var header = new FDFSHeader(0, Consts.FDFS_PROTO_CMD_QUIT, 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();
        }