protected override void OnDispose(bool disposing) { if (disposing) { _bufferWriter.Dispose(); } }
public void BufferWriter_Writing_text() { var pool = new TestPool(); var writer = new BufferWriter(ArrayPool <char> .Shared); writer.Write("text1".ToCharArray(), 0, "text1".Length); writer.Write("something".ToCharArray(), 2, 3); Assert.AreEqual(8, writer.Count); Assert.AreEqual("text1met", new string(writer.WrittenSegment.Array, writer.WrittenSegment.Offset, writer.WrittenSegment.Count)); writer.Dispose(); }
public void BufferWriter_Pool_Handing() { var pool = new TestPool(); var writer = new BufferWriter(pool); Assert.AreEqual(0, writer.Count); writer.Write(new char[10], 0, 1); Assert.AreEqual(1, writer.Count); Assert.AreEqual(0, pool.currentBufferIndex); writer.Write(new char[10], 0, 2); Assert.AreEqual(3, writer.Count); Assert.AreEqual(1, pool.currentBufferIndex); writer.Dispose(); //check if all buffers were returned Assert.True(Array.TrueForAll(pool.buffers, b => b == null)); }
public void WriteAndFlush(int msgId, byte[] buf) { BufferWriter outBuf = new BufferWriter(); this.mershaEncoder.Encoder(this, msgId, buf, outBuf); byte[] buffer = outBuf.GetBuffer(); try { if (!this.IsDispose) { //封包 int size = this._Socket.Send(buffer, 0, buffer.Length, SocketFlags.None, out SenderError); if (log.IsDebugEnabled()) { log.Debug("发送消息Length:" + buf.Length + " flush:" + size); } CheckSocketError(SenderError); LastRecvTime = Utils.TimeUtil.CurrentTimeMillis(); } } catch (System.ObjectDisposedException) { this.Close("链接已经被关闭"); } catch (System.Net.Sockets.SocketException) { this.Close("链接已经被关闭"); } catch (Exception ex) { if (isServer) { NettyPool.SessionHandler.ExceptionCaught(this, ex); } else { NettyPool.ClientSessionHandler.ExceptionCaught(this, ex); } } finally { outBuf.Close(); outBuf.Dispose(); } }