/// <summary> /// Implements Stream.WriteAsync /// </summary> /// <exception cref="DisconnectedException">catched a SocketException</exception> /// <exception cref="OutputStreamException">current mode is KnownLength, and position is out of range</exception> public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { try { if (count == 0) { return; } haveWrote += count; WRITE: if (CurrentMode == Mode.Buffering) { if (haveWrote <= BufferSize) { if (this.bufferHandle == null) { this.bufferHandle = bufferPool.Get(); } this.buffer.Write(buffer, offset, count); } else { if (needKeepAlive) { await SwitchToChunkedModeAsync().CAF(); } else { await SwitchToNoKeepAliveModeAsync().CAF(); } goto WRITE; } } else if (CurrentMode == Mode.Chunked) { await writeChunkSizeAsync(baseStream, count).CAF(); await baseStream.WriteAsync(buffer, offset, count, cancellationToken).CAF(); await writeBytesToStreamAsync(baseStream, CRLF).CAF(); } else if (CurrentMode == Mode.KnownLength) { if (haveWrote > lengthToWrite) { throw new OutputStreamException("position > content-length"); } await baseStream.WriteAsync(buffer, offset, count, cancellationToken).CAF(); } else if (CurrentMode == Mode.NoKeepAlive) { await baseStream.WriteAsync(buffer, offset, count, cancellationToken).CAF(); } } catch (IOException e) when(e.InnerException is SocketException) { throw new DisconnectedException(e.InnerException.Message); } catch (SocketException se) { throw new DisconnectedException(se.Message); } Actived?.Invoke(this); }
/// <summary> /// Implements Stream.Write /// </summary> /// <exception cref="DisconnectedException">catched a SocketException</exception> /// <exception cref="OutputStreamException">current mode is KnownLength, and position is out of range</exception> public override void Write(byte[] buffer, int offset, int count) { try { _write(buffer, offset, count); } catch (IOException e) when(e.InnerException is SocketException) { throw new DisconnectedException(e.InnerException.Message); } Actived?.Invoke(this); }
protected override void OnMouseUp(MouseButtonEventArgs e) { IsActive = true; Actived?.Invoke(this); base.OnMouseUp(e); }