예제 #1
0
        public static Handshake Deserialize(byte[] data)
        {
            Handshake hs = new Handshake();
            using (var ms = new MemoryStream(data))
            {
                using (var r = new BinaryReader(ms))
                {
                    try
                    {
                        hs.MessageId = (HandshakeType)r.ReadByte();
                        hs.MajorVersion = r.ReadByte();
                        hs.MinorVersion = r.ReadByte();
                        hs.PayloadSize = r.ReadUInt16(true);
                    }
                    catch (Exception e)
                    {
                        throw new SerializationException(
                            "The specified byte array contains " +
                            "invalid data.",
                            e);
                    }
                }
            }

            if (hs.MajorVersion != Majorversion || hs.MinorVersion != Minorversion)
            {
                throw new SerializationException("Unexpected handshake version: " +
                    hs.MajorVersion + "." + hs.MinorVersion);
            }

            if (hs.MessageId == HandshakeType.HandshakeError && hs.PayloadSize != 8)
            {
                throw new SerializationException("Unexpected payload size. Expected " +
                    "8, but was: " + hs.PayloadSize);
            }

            return hs;
        }
예제 #2
0
        private void ReadServerResponse()
        {
            string base64 = this.ReadLine(this.innerStream);
            HandshakeType type = HandshakeType.HandshakeInProgress;
            byte[] decoded;

            try
            {
                base64 = Regex.Replace(base64, @"^(\+|334)\s", string.Empty);
                decoded = Convert.FromBase64String(base64);
            }
            catch (FormatException)
            {
                type = HandshakeType.HandshakeError;
                decoded = ErrorCode;
            }

            Handshake hs = new Handshake(type, (ushort)decoded.Length);
            this.receivedData = new ByteBuilder()
                .Append(hs.Serialize())
                .Append(decoded)
                .ToArray();
            this.receivedConsumed = 0;
        }
예제 #3
0
        private bool ReadHandshake(byte[] buffer, int offset, int count)
        {
            int read = Math.Min(count, 5 - handshakeData.Length);
            this.handshakeData.Append(buffer, offset, read);
            if (this.handshakeData.Length == 5)
            {
                this.state = FilterStreamState.ReadingPayload;

                this.handshake = Handshake.Deserialize(this.handshakeData.ToArray());
                this.handshakeData.Clear();

                this.payloadData.Append(buffer, offset + read, count - read);

                return true;
            }

            return false;
        }