Example #1
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
            });
        }
Example #2
0
        public override void Send(List <byte[]> buffers, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
        {
            if (this.Id == 0)
            {
                throw new Exception("TChannel已经被Dispose, 不能发送消息");
            }
            ushort size = (ushort)buffers.Select(b => b.Length).Sum();

            size = NetworkHelper.HostToNetworkOrder(size);
            byte[] sizeBuffer = BitConverter.GetBytes(size);
            this.sendBuffer.SendTo(sizeBuffer);
            foreach (byte[] buffer in buffers)
            {
                this.sendBuffer.SendTo(buffer);
            }
            if (this.isConnected)
            {
                this.StartSend();
            }
        }