public async Task <bool> Send <T>(ComNode node, short channelId, T data) { if (!nodeMap.ContainsKey(node.IP)) { return(false); } if (!dataChannelMap.ContainsKey(channelId)) { return(false); } IDataChannel channel = dataChannelMap[channelId]; bool isRent = true; int bufSize = channel.GetDataSize(data); byte[] buf = arrayPool.Rent(bufSize + 6); if (buf == null) { isRent = false; buf = new byte[bufSize + 6]; } BytePacker packer = new BytePacker(buf); packer.Write((short)bufSize); #if DISABLE_CHANNEL_VARINT packer.Write(channelId); #else int s = 0; VarintBitConverter.SerializeShort(channelId, packer, out s); #endif channel.ToStream(data, ref packer); int maxpos = (int)packer.Position; if (channel.Qos == QosType.Reliable) { await node.Connection.Send(maxpos, buf); } else if (channel.Qos == QosType.Unreliable) { await udpSender.Send(node.IP, maxpos, buf); } if (isRent) { arrayPool.Return(buf); } return(true); }
void BuildBuffer <T>( IDataChannel channel, T data, ref byte[] buffer, ref int bufferSize, ref bool isRent, IEncrypter encrypter ) { isRent = true; if (channel.Encryption == Encryption.Rsa) { throw new InvalidOperationException("Server cant send data via RSA channel."); } int bufSize = channel.GetDataSize(data); int lz4ext = 0; if (channel.Compression == Compression.LZ4) { lz4ext = 4; } buffer = arrayPool.Rent(bufSize + 6 + lz4ext); if (buffer == null) { isRent = false; buffer = new byte[bufSize + 6 + lz4ext]; } BytePacker packer = new BytePacker(buffer); packer.Write((short)bufSize); #if DISABLE_CHANNEL_VARINT packer.Write(channel.ChannelID); #else int s = 0; VarintBitConverter.SerializeShort(channel.ChannelID, packer, out s); #endif int start = packer.Position; channel.ToStream(data, ref packer, encrypter); bufferSize = (int)packer.Position; packer.Position = 0; packer.Write((short)(bufferSize - start)); }
public void BuildBuffer <T>(IDataChannel channel, T data, ref byte[] buffer, ref int bufferSize, ref bool isRent) { isRent = true; int bufSize = channel.GetDataSize(data); int lz4ext = 0; if (channel.Compression == Compression.LZ4) { lz4ext = 4; } buffer = arrayPool.Rent(bufSize + 6 + lz4ext); if (buffer == null) { isRent = false; buffer = new byte[bufSize + 6 + lz4ext]; } BytePacker packer = new BytePacker(buffer); packer.Write((short)bufSize); #if DISABLE_CHANNEL_VARINT packer.Write(channelId); #else int s = 0; VarintBitConverter.SerializeShort(channel.ChannelID, packer, out s); #endif int start = packer.Position; channel.ToStream(data, ref packer); bufferSize = (int)packer.Position; packer.Position = 0; packer.Write((short)(bufferSize - start)); }