public virtual void WriteCommand(INetCommand command, INetSession session)
        {
            Byte[] rdata = null;
            if (command != null)
            {
                rdata = command.CommandName;
                if (rdata != null && rdata.Length > 0)
                {
                    session.WriteBytes(rdata, 0, rdata.Length);
                    WriteCommandNameEndBytes(command, session);
                }

                if (command.Parameters != null)
                {
                    foreach (Byte[] pData in command.Parameters)
                    {
                        if (pData != null && pData.Length > 0)
                        {
                            session.WriteBytes(pData, 0, pData.Length);
                        }
                        WriteCommandParameterSplitBytes(command, session);
                    }
                }
            }

            WriteFrameEndBytes(command, session);
        }
Exemplo n.º 2
0
 public override void WriteCommand(INetCommand command, INetSession session)
 {
     if (command is HandshakeResponseCommand)
     {
         HandshakeResponseCommand response = command as HandshakeResponseCommand;
         Byte[] responseData = response.GetResponseData(session);
         session.WriteBytes(responseData, 0, responseData.Length);
     }
     else if (command is FrameCommandBase)
     {
         FrameStreamWriter writer = new FrameStreamWriter(command as FrameCommandBase);
         writer.Write(session);
     }
 }
 protected override void WriteFrameEndBytes(INetCommand command, INetSession session)
 {
     session.WriteBytes(splitCommandBytes, 0, splitCommandBytes.Length);
 }
 protected override void WriteCommandParameterSplitBytes(INetCommand command, INetSession session)
 {
     session.WriteBytes(new byte[] { 0xff }, 0, 1);
 }
 protected override void WriteCommandNameEndBytes(INetCommand command, INetSession session)
 {
     session.WriteBytes(new byte[] { 0xff }, 0, 1);
 }
        public bool Write(INetSession session)
        {
            //第一字节
            MemoryStream stream = new MemoryStream();
            BinaryWriter bw     = new BinaryWriter(stream);

            Byte fin = 0;

            fin = (byte)(fin | 0x80);

            Byte opcode = (Byte)command.Opcode;

            opcode = (byte)(opcode & 0x7f);
            fin    = (byte)(fin | opcode);

            bw.Write(fin);

            int frameMask = 0;

            if (isMask)
            {
                frameMask = frameMask | 0x80;
            }

            Byte[] responseData = command.GetResponseData(session);
            long   length       = 0;

            if (responseData != null)
            {
                length = responseData.Length;
            }

            if (length >= 0 && length <= 0x7d)
            {
                frameMask = frameMask | (byte)length;
                bw.Write((byte)frameMask);
            }
            else if (length > 0x7d && length <= 0xffff)
            {
                frameMask = frameMask | 0x7e;
                bw.Write((byte)frameMask);



                bw.Write(BitConverter.GetBytes((UInt16)length).Reverse().ToArray());
            }
            else
            {
                frameMask = frameMask | 0x7f;
                bw.Write((byte)frameMask);
                bw.Write(BitConverter.GetBytes((UInt64)length).Reverse().ToArray());
            }



            if (isMask)
            {
                //四个随机字节
                Random r            = new Random();
                Byte[] maskingBytes = new Byte[4];
                r.NextBytes(maskingBytes);
                bw.Write(maskingBytes);

                if (length > 0)
                {
                    responseData.EecodeMask(maskingBytes, 0, responseData.Length);
                }
            }

            if (length > 0)
            {
                bw.Write(responseData);
            }

            stream.Flush();

            stream.Position = 0;
            BinaryReader br = new BinaryReader(stream);

            Byte[] frameData = br.ReadBytes((int)stream.Length);
            session.WriteBytes(frameData, 0, frameData.Length);

            bw.Close();
            return(true);
        }