Exemplo n.º 1
0
        private void SendMessage(object message)
        {
            //Log.Debug($"send: {MongoHelper.ToJson(message)}");
            Opcode opcode = this.network.Parent.GetComponent <OpcodeTypeComponent>().GetOpcode(message.GetType());
            ushort op     = (ushort)opcode;

            byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);

#if SERVER
            // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈
            if (this.network.AppType == AppType.AllServer)
            {
                Session session = this.network.Parent.GetComponent <NetInnerComponent>().Get(this.RemoteAddress);
                session.RunDecompressedBytes(op, messageBytes, 0, messageBytes.Length);
                return;
            }
#endif

            byte[] opcodeBytes = BitConverter.GetBytes(op);

            this.byteses[0] = opcodeBytes;
            this.byteses[1] = messageBytes;

            channel.Send(this.byteses);
        }
Exemplo n.º 2
0
        private void SendMessage(uint rpcId, object message, bool isCall = true)
        {
            ushort opcode = this.network.Owner.GetComponent <MessageDispatherComponent>().GetOpcode(message.GetType());

            byte[] opcodeBytes = BitConverter.GetBytes(opcode);
            if (!isCall)
            {
                rpcId = rpcId | 0x40000000;
            }

            byte[] messageBytes = message.ToBson();
            if (messageBytes.Length > 100)
            {
                byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
                if (newMessageBytes.Length < messageBytes.Length)
                {
                    messageBytes = newMessageBytes;
                    rpcId        = rpcId | 0x80000000;
                }
            }

            byte[] seqBytes = BitConverter.GetBytes(rpcId);

            channel.Send(new List <byte[]> {
                opcodeBytes, seqBytes, messageBytes
            });
        }
Exemplo n.º 3
0
        private void SendMessage(object message)
        {
            //Log.Debug($"send: {message.ToJson()}");
            ushort opcode = this.network.Owner.GetComponent <OpcodeTypeComponent>().GetOpcode(message.GetType());

            opcode = NetworkHelper.HostToNetworkOrder(opcode);
            byte[] opcodeBytes = BitConverter.GetBytes(opcode);

            byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
            byte   flag         = 0;

            if (messageBytes.Length > 100)
            {
                byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
                if (newMessageBytes.Length < messageBytes.Length)
                {
                    messageBytes = newMessageBytes;
                    flag        |= 0x80;
                }
            }

            byte[] flagBytes = { flag };

            channel.Send(new List <byte[]> {
                opcodeBytes, flagBytes, messageBytes
            });
        }
Exemplo n.º 4
0
        private void SendMessage(byte flag, ushort opcode, uint rpcId, byte[] bytes)
        {
            this.flagBytes[0] = flag;

            List <byte[]> bb;

            if (rpcId == 0)
            {
                bb    = this.byteses;
                bb[0] = flagBytes;
                bb[1] = BitConverter.GetBytes(opcode);
                bb[2] = bytes;
            }
            else
            {
                bb    = this.rpcByteses;
                bb[0] = flagBytes;
                bb[1] = BitConverter.GetBytes(opcode);
                bb[2] = BitConverter.GetBytes(rpcId);
                bb[3] = 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 bb)
                {
                    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(bb);
        }
Exemplo n.º 5
0
        private void SendMessage(object message)
        {
            //Log.Debug($"send: {MongoHelper.ToJson(message)}");
            ushort opcode = this.network.Entity.GetComponent <OpcodeTypeComponent>().GetOpcode(message.GetType());

            byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
            if (messageBytes.Length > 100)
            {
                byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
                if (newMessageBytes.Length < messageBytes.Length)
                {
                    messageBytes = newMessageBytes;
                    opcode      |= 0x8000;
                }
            }

            byte[] opcodeBytes = BitConverter.GetBytes(opcode);

            this.byteses[0] = opcodeBytes;
            this.byteses[1] = messageBytes;
            channel.Send(this.byteses);
        }
Exemplo n.º 6
0
        private void SendMessage(object rMessage)
        {
            //Log.Debug($"send: {MongoHelper.ToJson(message)}");
            ushort nOpcode = NetworkOpcodeType.Instance.GetOpcode(rMessage.GetType());

            byte[] rMessageBytes = this.mNetwork.MessagePacker.SerializeToByteArray(rMessage);
            if (rMessageBytes.Length > 100)
            {
                byte[] rNewMessageBytes = ZipHelper.Compress(rMessageBytes);
                if (rNewMessageBytes.Length < rMessageBytes.Length)
                {
                    rMessageBytes = rNewMessageBytes;
                    nOpcode      |= 0x8000;
                }
            }

            byte[] rOpcodeBytes = BitConverter.GetBytes(nOpcode);

            this.mByteses[0] = rOpcodeBytes;
            this.mByteses[1] = rMessageBytes;

            mChannel.Send(this.mByteses);
        }
Exemplo n.º 7
0
        private void SendMessage(byte flag, ushort opcode, uint rpcId, byte[] bytes)
        {
            Header header = new Header
            {
                Opcode = opcode,
                RpcId  = rpcId,
                Flag   = flag
            };

            byte[] headerBytes  = this.Network.MessagePacker.SerializeToByteArray(header);
            byte[] headerLength = BitConverter.GetBytes((ushort)headerBytes.Length);

            this.byteses[0] = headerLength;
            this.byteses[1] = headerBytes;
            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.packet.Length = 0;
                ushort index = 0;
                foreach (var byts in this.byteses)
                {
                    Array.Copy(byts, 0, this.packet.Bytes, index, byts.Length);
                    index += (ushort)byts.Length;
                }

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

            channel.Send(this.byteses);
        }
Exemplo n.º 8
0
 public void Send(MemoryStream stream)
 {
     channel.Send(stream);
 }