/// <summary>
        /// receive whole pack
        /// </summary>
        /// <param name="in">input stream</param>
        /// <param name="expect_cmd">expect response command</param>
        /// <param name="expect_body_len">expect response package body length</param>
        /// <returns> RecvPackageInfo: errno and reponse body(byte buff)</returns>
        public static RecvPackageInfo recvPackage(Stream stream, byte expect_cmd, long expect_body_len)
        {
            RecvHeaderInfo header = recvHeader(stream, expect_cmd, expect_body_len);

            if (header.errno != 0)
            {
                return(new RecvPackageInfo(header.errno, null));
            }
            byte[] body        = new byte[(int)header.body_len];
            int    totalBytes  = 0;
            int    remainBytes = (int)header.body_len;
            int    bytes;

            while (totalBytes < header.body_len)
            {
                if ((bytes = stream.Read(body, totalBytes, remainBytes)) < 0)
                {
                    break;
                }
                totalBytes  += bytes;
                remainBytes -= bytes;
            }
            if (totalBytes != header.body_len)
            {
                throw new IOException("recv package size " + totalBytes + " != " + header.body_len);
            }
            return(new RecvPackageInfo(0, body));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// receive whole pack
        /// </summary>
        /// <param name="input">input stream</param>
        /// <param name="expectCmd">response command</param>
        /// <param name="expectBodyLen">response package body length</param>
        /// <returns>errno and reponse body(byte buff)</returns>
        public static RecvPackageInfo RecvPackage(Stream input, byte expectCmd, long expectBodyLen)
        {
            RecvHeaderInfo header = RecvHeader(input, expectCmd, expectBodyLen);

            if (header.Errno != 0)
            {
                return(new RecvPackageInfo(header.Errno, null));
            }
            byte[] body        = new byte[(int)header.BodyLen];
            int    totalBytes  = 0;
            int    remainBytes = (int)header.BodyLen;
            int    bytes;

            while (totalBytes < header.BodyLen)
            {
                if ((bytes = input.Read(body, totalBytes, remainBytes)) < 0)
                {
                    break;
                }

                totalBytes  += bytes;
                remainBytes -= bytes;
            }
            if (totalBytes != header.BodyLen)
            {
                throw new IOException("recv package size " + totalBytes + " != " + header.BodyLen);
            }
            return(new RecvPackageInfo((byte)0, body));
        }
        /// <summary>
        /// send ACTIVE_TEST command to server, test if network is ok and the server is alive
        /// </summary>
        /// <param name="sock">the JavaSocket object</param>
        public static bool activeTest(JavaSocket sock)
        {
            byte[] header;
            header = packHeader(FDFS_PROTO_CMD_ACTIVE_TEST, 0, 0);
            sock.GetStream().Write(header, 0, header.Length);
            RecvHeaderInfo headerInfo = recvHeader(sock.GetStream(), TRACKER_PROTO_CMD_RESP, 0);

            return(headerInfo.errno == 0 ? true : false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// send ACTIVE_TEST command to server, test if network is ok and the server is alive
        /// </summary>
        /// <param name="sock">the Socket object</param>
        /// <returns></returns>
        public static bool ActiveTest(TcpClient sock)
        {
            byte[] header;
            header = PackHeader(FdfsProtoCmdActiveTest, 0, (byte)0);
            sock.GetStream().Write(header, 0, header.Length);

            RecvHeaderInfo headerInfo = RecvHeader(sock.GetStream(), TrackerProtoCmdResp, 0);

            return(headerInfo.Errno == 0 ? true : false);
        }