Example #1
0
        public void Send(byte flag, ushort opcode, byte[] bytes)
        {
            if (this.IsDisposed)
            {
                throw new Exception("session已经被Dispose了");
            }

            this.byteses[0][0] = flag;
            this.byteses[1]    = BytesHelper.GetBytes(opcode);
            this.byteses[2]    = bytes;

#if SERVER
            // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈
            if (this.Network.AppType == AppType.AllServer)
            {
                Session session = this.Network.Entity.GetComponent <NetInnerComponent>().Get(this.RemoteAddress);
                this.pkt.Length = 0;
                ushort index = 0;
                foreach (var byts in byteses)
                {
                    Array.Copy(byts, 0, this.pkt.Bytes, index, byts.Length);
                    index += (ushort)byts.Length;
                }

                this.pkt.Length = index;
                session.Run(this.pkt);
                return;
            }
#endif

            channel.Send(this.byteses);
        }
Example #2
0
 public override void Send(byte[] buffer, int index, int length)
 {
     if (this.IsDisposed)
     {
         throw new Exception("TChannel已经被Dispose, 不能发送消息");
     }
     byte[] size = BytesHelper.GetBytes((ushort)buffer.Length);
     this.sendBuffer.Write(size, 0, size.Length);
     this.sendBuffer.Write(buffer, index, length);
     if (this.isConnected)
     {
         this.StartSend();
     }
 }
Example #3
0
        public bool Parse()
        {
            if (this.isOK)
            {
                return(true);
            }

            bool finish = false;

            while (!finish)
            {
                switch (this.state)
                {
                case ParserState.PacketSize:
                    if (this.buffer.Length < 2)
                    {
                        finish = true;
                    }
                    else
                    {
                        this.buffer.Read(this.packet.Bytes, 0, 2);
                        this.packetSize = BytesHelper.ToUInt16(this.packet.Bytes, 0);
                        if (packetSize > 60000)
                        {
                            throw new Exception($"packet too large, size: {this.packetSize}");
                        }
                        this.state = ParserState.PacketBody;
                    }
                    break;

                case ParserState.PacketBody:
                    if (this.buffer.Length < this.packetSize)
                    {
                        finish = true;
                    }
                    else
                    {
                        this.buffer.Read(this.packet.Bytes, 0, this.packetSize);
                        this.packet.Length = this.packetSize;
                        this.isOK          = true;
                        this.state         = ParserState.PacketSize;
                        finish             = true;
                    }
                    break;
                }
            }
            return(this.isOK);
        }
Example #4
0
        public override void Send(List <byte[]> buffers)
        {
            if (this.IsDisposed)
            {
                throw new Exception("TChannel已经被Dispose, 不能发送消息");
            }
            ushort size = (ushort)buffers.Select(b => b.Length).Sum();

            byte[] sizeBuffer = BytesHelper.GetBytes(size);
            this.sendBuffer.Write(sizeBuffer, 0, sizeBuffer.Length);
            foreach (byte[] buffer in buffers)
            {
                this.sendBuffer.Write(buffer, 0, buffer.Length);
            }
            if (this.isConnected)
            {
                this.StartSend();
            }
        }