/// <summary> /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. /// </summary> /// <returns> /// The unsigned byte cast to an Int32, or -1 if at the end of the stream. /// </returns> public override int ReadByte() { if (recvDataCache == null || !recvDataCache.IsReadable()) { recvDataCache = streamSocket.Receive(); } return(recvDataCache.ReadByte()); }
/// <summary> /// Sends data to this socket with a ByteBuf object that contains data to be sent. /// </summary> /// <param name="data">A ByteBuf object that contains data to be sent</param> public void Send(ByteBuf data) { EnsureAccessible(); if (!isConnected) { throw new InvalidOperationException("The operation is not allowed on non-connected sockets."); } if (!data.IsReadable()) { throw new ArgumentException("The parameter {0} must contain one or more elements.", "data"); } var context = new RequestContext(SocketOperation.Send, data); DoSend(GenerateUniqueKey(), context); var status = sendStatusQueue.Take(); if (status == (int)SocketError.Success) { return; } // throw a SocketException if theres is an error. Dispose(true); var socketException = new SocketException(status); throw socketException; }
/// <summary> /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. /// </summary> /// <returns> /// The unsigned byte cast to an Int32, or -1 if at the end of the stream. /// </returns> public override int ReadByte() { if (recvDataCache == null || !recvDataCache.IsReadable()) { recvDataCache = streamSocket.Receive(); } var v = recvDataCache.ReadByte(); if (recvDataCache.IsReadable()) { return(v); } // Release cache if it is not readable. If we do not reset the cache here, // the cache will be used in next Read() that caused 0 bytes return. recvDataCache.Release(); recvDataCache = null; return(v); }
/// <summary> /// Flushes data in send cache to the stream. /// </summary> public override void Flush() { if (sendDataCache != null && sendDataCache.IsReadable()) { try { streamSocket.Send(sendDataCache); sendDataCache = null; } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } // some sort of error occurred on the socked call, // set the SocketException as InnerException and throw throw new IOException(string.Format("Unable to write data to the transport connection: {0}.", e.Message), e); } } }
/// <summary> /// Sends data to this socket with a ByteBuf object that contains data to be sent. /// </summary> /// <param name="data">A ByteBuf object that contains data to be sent</param> public void Send(ByteBuf data) { EnsureAccessible(); if (!isConnected) { throw new InvalidOperationException("The operation is not allowed on non-connected sockets."); } if (!data.IsReadable()) { throw new ArgumentException("The parameter {0} must contain one or more elements.", "data"); } var dataToken = new SockDataToken(this, data); if (parent != null) { parent.DoSend(dataToken); } else { DoSend(dataToken); } var status = sendStatusQueue.Take(); if (status == (int)SocketError.Success) { return; } // throw a SocketException if theres is an error. dataToken.Reset(); Dispose(true); var socketException = new SocketException(status); throw socketException; }
private void WriteReadByteBufTest(ByteBuf byteBuf) { var initWriteIndex = byteBuf.WriterIndex; var initReadIndex = byteBuf.WriterIndex; Assert.AreEqual(initWriteIndex, initReadIndex); Assert.AreEqual(byteBuf.Capacity, byteBuf.WritableBytes); Assert.AreEqual(0, byteBuf.ReadableBytes); Assert.IsFalse(byteBuf.IsReadable()); Assert.IsTrue(byteBuf.IsWritable()); // Verify WriteBytes() function const string writeStr = "Write bytes to ByteBuf."; var writeBytes = Encoding.UTF8.GetBytes(writeStr); byteBuf.WriteBytes(writeBytes, 0, writeBytes.Length); Assert.AreEqual(initWriteIndex + writeBytes.Length, byteBuf.WriterIndex); Assert.AreEqual(byteBuf.Capacity - writeBytes.Length, byteBuf.WritableBytes); Assert.AreEqual(writeBytes.Length, byteBuf.ReadableBytes); Assert.AreEqual(initReadIndex, byteBuf.ReaderIndex); Assert.IsTrue(byteBuf.IsReadable()); // Verify ReadBytes() function var readBytes = new byte[writeBytes.Length]; var ret = byteBuf.ReadBytes(readBytes, 0, readBytes.Length); Assert.AreEqual(writeBytes.Length, ret); var readStr = Encoding.UTF8.GetString(readBytes, 0, ret); Assert.AreEqual(writeStr, readStr); Assert.AreEqual(initWriteIndex + writeBytes.Length, byteBuf.WriterIndex); Assert.AreEqual(byteBuf.Capacity - writeBytes.Length, byteBuf.WritableBytes); Assert.AreEqual(0, byteBuf.ReadableBytes); Assert.AreEqual(initReadIndex + ret, byteBuf.ReaderIndex); // Verify ReadByte() function byteBuf.WriteBytes(writeBytes, 0, 1); var retByte = byteBuf.ReadByte(); Assert.AreEqual(writeBytes[0], retByte); // Verify clear() function byteBuf.Clear(); Assert.AreEqual(0, byteBuf.ReaderIndex); Assert.AreEqual(0, byteBuf.WriterIndex); }
/// <summary> /// Sends data to this socket with a ByteBuf object that contains data to be sent. /// </summary> /// <param name="data">A ByteBuf object that contains data to be sent</param> public void Send(ByteBuf data) { EnsureAccessible(); if (!isConnected) { throw new InvalidOperationException("The operation is not allowed on non-connected sockets."); } if (!data.IsReadable()) { throw new ArgumentException("The parameter {0} must contain one or more elements.", "data"); } var dataToken = new SockDataToken(this, data); if (parent != null) { parent.DoSend(dataToken); } else { DoSend(dataToken); } var status = sendStatusQueue.Take(); if (status == (int) SocketError.Success) return; // throw a SocketException if theres is an error. dataToken.Reset(); Dispose(true); var socketException = new SocketException(status); throw socketException; }
/// <summary> /// Sends data to this socket with a ByteBuf object that contains data to be sent. /// </summary> /// <param name="data">A ByteBuf object that contains data to be sent</param> public void Send(ByteBuf data) { EnsureAccessible(); if (!isConnected) { throw new InvalidOperationException("The operation is not allowed on non-connected sockets."); } if (!data.IsReadable()) { throw new ArgumentException("The parameter {0} must contain one or more elements.", "data"); } var context = new RequestContext(SocketOperation.Send, data); DoSend(GenerateUniqueKey(), context); var status = sendStatusQueue.Take(); if (status == (int)SocketError.Success) return; // throw a SocketException if theres is an error. Dispose(true); var socketException = new SocketException(status); throw socketException; }