예제 #1
0
파일: SpCodec.cs 프로젝트: ZhHong/sproto-cs
    public bool Encode (SpType type, SpObject obj, SpStream stream) {
        if (type == null || obj == null || stream == null)
            return false;

        mStream = stream;
		bool success = EncodeInternal (type, obj);
		return (success && stream.IsOverflow () == false);
    }
예제 #2
0
    public bool Encode(SpType type, SpObject obj, SpStream stream)
    {
        if (type == null || obj == null || stream == null)
        {
            return(false);
        }

        mStream = stream;
        bool success = EncodeInternal(type, obj);

        return(success && stream.IsOverflow() == false);
    }
예제 #3
0
파일: SpCodec.cs 프로젝트: ZhHong/sproto-cs
	public SpStream Encode (SpType type, SpObject obj) {
		SpStream stream = new SpStream ();
		
		if (Encode (type, obj, stream) == false) {
			if (stream.IsOverflow ()) {
				if (stream.Position > MAX_SIZE)
					return null;
				
				int size = stream.Position;
				size = ((size + 7) / 8) * 8;
				stream = new SpStream (size);
				if (Encode (type, obj, stream) == false)
					return null;
			}
			else {
				return null;
			}
		}
		
		return stream;
	}
예제 #4
0
    public static bool Pack (SpStream input, SpStream output) {
		int src_size = input.Length;
		if (src_size % 8 != 0) {
			int new_size = ((src_size + 7) / 8) * 8;
			if (input.Capacity < new_size) {
				SpStream new_input = new SpStream (new_size);
				Array.Copy (input.Buffer, input.Offset, new_input.Buffer, new_input.Offset, input.Length);
				new_input.Position = new_input.Offset;
				input = new_input;
				src_size = new_size;
			}
			else {
				int pos = input.Position;
				input.Position = input.Tail;
				for (int i = src_size; i < new_size; i++) {
					input.Write ((byte)0);
				}
				input.Position = pos;
			}
		}
		
		int src_start = input.Offset;
		int src_offset = input.Offset;
		byte[] src = input.Buffer;

        int ff_n = 0;
        int ff_src_start = 0;
        int ff_dest_start = 0;

        for (; src_offset < src_size; src_offset += 8) {
			int pos = output.Position;
			int n = PackSeg (src, src_offset, output, ff_n);

            if (n == 10) {
                ff_src_start = src_offset;
				ff_dest_start = pos;
                ff_n = 1;
            }
            else if (n == 8 && ff_n > 0) {
                ff_n++;
                if (ff_n == 256) {
					output.Position = ff_dest_start;
                    WriteFF (src, ff_src_start, output, 256 * 8, n);
                    ff_n = 0;
                }
            }
            else {
				if (ff_n > 0) {
					output.Position = ff_dest_start;
                    WriteFF (src, ff_src_start, output, ff_n * 8, n);
                    ff_n = 0;
                }
            }

			output.Position = pos + n;
        }

		if (ff_n == 1) {
			output.Position = ff_dest_start;
			WriteFF (src, ff_src_start, output, 8, 0);
		}
		else if (ff_n > 1) {
			output.Position = ff_dest_start;
			WriteFF (src, ff_src_start, output, src_size - (ff_src_start - src_start), 0);
		}

		return (output.IsOverflow () == false);
    }
예제 #5
0
    public static bool Unpack (SpStream input, SpStream output) {
		int src_size = input.Tail;
		int src_offset = input.Offset;
		byte[] src = input.Buffer;

		while (src_offset < src_size) {
			byte header = src[src_offset];
			src_offset++;

			if (header == 0xff) {
				if (src_offset > src_size)
					return false;

				int n = (src[src_offset] + 1) * 8;
				if (src_size - src_offset < n + 1)
					return false;

				src_offset++;
				output.Write (src, src_offset, n);
				src_offset += n;
			}
			else {
				for (int i = 0; i < 8; i++) {
					int nz = (header >> i) & 1;
					if (nz != 0) {
						if (src_offset > src_size)
							return false;

						output.Write (src[src_offset]);
						src_offset++;
					}
					else {
						output.Write ((byte)0);
					}
				}
			}
		}
		
		return (output.IsOverflow () == false);
    }
예제 #6
0
    private SpStream EncodeRequest(string proto, SpObject args, int session)
    {
        if (mAttachTypeManager == null || mHostTypeManager == null || mHeaderType == null)
        {
            return(null);
        }

        SpProtocol protocol = mAttachTypeManager.GetProtocolByName(proto);

        if (protocol == null)
        {
            return(null);
        }

        SpObject header = new SpObject();

        header.Insert("type", protocol.Tag);
        if (session != 0)
        {
            header.Insert("session", session);
        }

        SpStream stream = mHostTypeManager.Codec.Encode(mHeaderType, header);

        if (stream == null)
        {
            return(null);
        }

        if (args != null)
        {
            if (mAttachTypeManager.Codec.Encode(protocol.Request, args, stream) == false)
            {
                if (stream.IsOverflow())
                {
                    if (stream.Position > SpCodec.MAX_SIZE)
                    {
                        return(null);
                    }

                    int size = stream.Position;
                    size   = ((size + 7) / 8) * 8;
                    stream = new SpStream(size);
                    if (mAttachTypeManager.Codec.Encode(protocol.Request, args, stream) == false)
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
        }

        if (session != 0)
        {
            mSessions[session] = protocol;
        }

        return(stream);
    }
예제 #7
0
    public static bool Pack(SpStream input, SpStream output)
    {
        int src_size = input.Length;

        if (src_size % 8 != 0)
        {
            int new_size = ((src_size + 7) / 8) * 8;
            if (input.Capacity < new_size)
            {
                SpStream new_input = new SpStream(new_size);
                Array.Copy(input.Buffer, input.Offset, new_input.Buffer, new_input.Offset, input.Length);
                new_input.Position = new_input.Offset;
                input    = new_input;
                src_size = new_size;
            }
            else
            {
                int pos = input.Position;
                input.Position = input.Tail;
                for (int i = src_size; i < new_size; i++)
                {
                    input.Write((byte)0);
                }
                input.Position = pos;
            }
        }

        int src_start  = input.Offset;
        int src_offset = input.Offset;

        byte[] src = input.Buffer;

        int ff_n          = 0;
        int ff_src_start  = 0;
        int ff_dest_start = 0;

        for (; src_offset < src_size; src_offset += 8)
        {
            int pos = output.Position;
            int n   = PackSeg(src, src_offset, output, ff_n);

            if (n == 10)
            {
                ff_src_start  = src_offset;
                ff_dest_start = pos;
                ff_n          = 1;
            }
            else if (n == 8 && ff_n > 0)
            {
                ff_n++;
                if (ff_n == 256)
                {
                    output.Position = ff_dest_start;
                    WriteFF(src, ff_src_start, output, 256 * 8, n);
                    ff_n = 0;
                }
            }
            else
            {
                if (ff_n > 0)
                {
                    output.Position = ff_dest_start;
                    WriteFF(src, ff_src_start, output, ff_n * 8, n);
                    ff_n = 0;
                }
            }

            output.Position = pos + n;
        }

        if (ff_n == 1)
        {
            output.Position = ff_dest_start;
            WriteFF(src, ff_src_start, output, 8, 0);
        }
        else if (ff_n > 1)
        {
            output.Position = ff_dest_start;
            WriteFF(src, ff_src_start, output, src_size - (ff_src_start - src_start), 0);
        }

        return(output.IsOverflow() == false);
    }