Esempio n. 1
0
        public static ReceivedPackage PackPackage(string fileBlockKey,ParsePackageView view,Stream stream, byte cmd, long bodyLen)
        {
            ReceivedHeader header = PackHeader(fileBlockKey,view, stream, cmd, bodyLen);

            if (header.ErrorCode != 0)
            {
                return new ReceivedPackage(header.ErrorCode, null);
            }

            byte[] body = new byte[(int)header.BodyLength];
            int totalBytes = 0;
            int remainBytes = (int)header.BodyLength;
            int bytes;

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

                totalBytes += bytes;
                remainBytes -= bytes;
            }

            if (totalBytes != header.BodyLength)
            {
                throw new IOException("receive package size " + totalBytes + " != " + header.BodyLength);
            }

            return new ReceivedPackage((byte)0, body);
        }
Esempio n. 2
0
        public static ReceivedHeader PackHeader(string blockKey,ParsePackageView view,Stream stream, byte cmd, long bodyLen)
        {
            byte[] header = new byte[TRACKER_PROTO_PKG_LEN_SIZE + 2];
            int bytes;
            long pkg_len;

            if ((bytes = stream.Read(header, 0, header.Length)) != header.Length)
            {
                throw new IOException("receive package size " + bytes + " != " + header.Length);
            }

            if (header[PROTO_HEADER_CMD_INDEX] != cmd)
            {
                throw new IOException("receive cmd: " + header[PROTO_HEADER_CMD_INDEX] + " is not correct, expect cmd: " + cmd);
            }

            if (header[PROTO_HEADER_STATUS_INDEX] != 0)
            {
                return new ReceivedHeader(header[PROTO_HEADER_STATUS_INDEX], 0);
            }

            pkg_len = ConvertBuffToLong(header, 0);

            //if(ConfigManager.Instance.DumpObjEnable)
            //_tracing.InfoFmt("cmd:{0};bodyLen:{1};pkg_len:{2};blockKey:{3};view:{4};datat:{5}", cmd, bodyLen, pkg_len, blockKey, view,ObjectHelper.DumpObject(header));

            if (pkg_len < 0)
            {
                throw new IOException("receive body length: " + pkg_len + " < 0!");
            }

            if (bodyLen >= 0 && pkg_len != bodyLen)
            {
                throw new IOException("receive body length: " + pkg_len + " is not correct, expect length: " + bodyLen);
            }

            return new ReceivedHeader((byte)0, pkg_len);
        }