//TCP 송신. public static void SendByte(byte[] send_data) { //tcpChannel 의 상태를 점검하여, 송신할 상황이 아니라면 종료. if (tcpChannel == null || tcpChannel.Active != true || tcpChannel.IsWritable != true) { return; } try { //IByteBuffer 는, 자바 Netty 의 ByteBuf 와 같은 역할. //보낼 데이터릐 길이만큼, alloc 에서 DirectBuffer를 할당받아서 가져옴. IByteBuffer data = alloc.DirectBuffer(send_data.Length); //받아온 byte[] 데이터를, 할당받아온 IByteBuffer에 기록함. data.WriteBytes(send_data); //tcpChannel 를 통해 해당 데이터를 송신! tcpChannel.WriteAndFlushAsync(data); } catch (Exception e) { Debug.Log("SendByte() Error:" + e.ToString()); } }
public void GlobalSetup() { PooledByteBufferAllocator allocator = PooledByteBufferAllocator.Default; // Use buffer sizes that will also allow to write UTF-8 without grow the buffer this.buffer = allocator.DirectBuffer(512); this.wrapped = Unpooled.UnreleasableBuffer(allocator.DirectBuffer(512)); var asciiSequence = new StringBuilder(128); for (int i = 0; i < 128; i++) { asciiSequence.Append('a'); } this.ascii = asciiSequence.ToString(); // Generate some mixed UTF-8 String for benchmark var utf8Sequence = new StringBuilder(128); char[] chars = "Some UTF-8 like äÄ∏ŒŒ".ToCharArray(); for (int i = 0; i < 128; i++) { utf8Sequence.Append(chars[i % chars.Length]); } this.utf8 = utf8Sequence.ToString(); byte[] bytes = Encoding.ASCII.GetBytes(this.ascii); this.asciiLength = bytes.Length; this.asciiBuffer = allocator.DirectBuffer(this.asciiLength); this.asciiBuffer.WriteBytes(bytes); bytes = Encoding.UTF8.GetBytes(this.utf8); this.utf8Length = bytes.Length; this.utf8Buffer = allocator.DirectBuffer(bytes.Length); this.utf8Buffer.WriteBytes(bytes); }
/// <summary> /// /// </summary> /// <param name="values"></param> /// <param name="fun"></param> public void Write(byte[] values, byte fun) { try { var buffer = pooledByteBufAllocator.DirectBuffer(values.Length + 1); buffer.WriteByte(fun); Marshal.Copy(values, 0, buffer.AddressOfPinnedMemory() + 1, values.Length); buffer.SetWriterIndex(values.Length + 1); mContext?.WriteAndFlushAsync(buffer); } catch { } }
/// <summary> /// /// </summary> /// <param name="size"></param> /// <returns></returns> public IByteBuffer Allocate(int size) { return(pooledByteBufAllocator.DirectBuffer(size)); }