/// <summary> /// Serialize message and sent it add it to the buffer /// </summary> /// <param name="args">Socket buffer</param> public void Send(ISocketBuffer args) { if (_bytesTransferred < _bytesEnqueued) { //TODO: Is this faster than moving the bytes to the beginning of the buffer and append more bytes? args.SetBuffer(_bufferSlice.Buffer, _bufferSlice.Offset + _bytesTransferred, _bytesEnqueued - _bytesTransferred); return; } if (!_headerIsSent) { var headerLength = CreateHeader(); var bytesToWrite = (int)Math.Min(_bufferSlice.Capacity - headerLength, _bodyStream.Length); _bodyStream.Read(_bufferSlice.Buffer, _bufferSlice.Offset + headerLength, bytesToWrite); args.SetBuffer(_bufferSlice.Buffer, _bufferSlice.Offset, bytesToWrite + headerLength); _bytesEnqueued = headerLength + bytesToWrite; _bytesLeftToSend = headerLength + (int)_bodyStream.Length; } else { _bytesEnqueued = Math.Min(_bufferSlice.Capacity, _bytesLeftToSend); _bodyStream.Read(_bufferSlice.Buffer, _bufferSlice.Offset, _bytesEnqueued); args.SetBuffer(_bufferSlice.Buffer, _bufferSlice.Offset, _bytesEnqueued); } }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { // last send operation did not send all bytes enqueued in the buffer // so let's just continue until doing next message if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend); return; } // continuing with the message body if (_isHeaderSent) { var bytes = Math.Min(_totalAmountToSend, _buffer.Length); _message.Body.Read(_buffer, 0, bytes); _bytesToSend = bytes; buffer.SetBuffer(_buffer, 0, bytes); return; } _writer.WriteLine(_message.StatusLine); foreach (var header in _message.Headers) { _writer.Write("{0}: {1}\r\n", header.Key, header.Value); } _writer.Write("\r\n"); _writer.Flush(); _isHeaderSent = true; buffer.UserToken = _message; if (_message.Body == null || _message.ContentLength == 0) { _bytesToSend = (int) _stream.Length; _totalAmountToSend = _bytesToSend; buffer.SetBuffer(_buffer, 0, (int) _stream.Length); return; } else { } var bytesLeft = _buffer.Length - _stream.Length; var bytesToSend = Math.Min(_message.ContentLength, (int) bytesLeft); var offset = (int) _stream.Position; _message.Body.Read(_buffer, offset, bytesToSend); _bytesToSend = (int) _stream.Length + bytesToSend; _totalAmountToSend = (int) _stream.Length + _message.ContentLength; buffer.SetBuffer(_buffer, 0, _bytesToSend); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { // last send operation did not send all bytes enqueued in the buffer // so let's just continue until doing next message if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend); return; } // continuing with the message body if (_isHeaderSent) { var bytes = Math.Min(_totalAmountToSend, _buffer.Length); _message.Body.Read(_buffer, 0, bytes); _bytesToSend = bytes; buffer.SetBuffer(_buffer, 0, bytes); return; } _writer.WriteLine(_message.StatusLine); foreach (var header in _message.Headers) { _writer.Write("{0}: {1}\r\n", header.Key, header.Value); } _writer.Write("\r\n"); _writer.Flush(); _isHeaderSent = true; buffer.UserToken = _message; if (_message.Body == null || _message.ContentLength == 0) { _bytesToSend = (int)_stream.Length; _totalAmountToSend = _bytesToSend; buffer.SetBuffer(_buffer, 0, (int)_stream.Length); return; } else { } var bytesLeft = _buffer.Length - _stream.Length; var bytesToSend = Math.Min(_message.ContentLength, (int)bytesLeft); var offset = (int)_stream.Position; _message.Body.Read(_buffer, offset, bytesToSend); _bytesToSend = (int)_stream.Length + bytesToSend; _totalAmountToSend = (int)_stream.Length + _message.ContentLength; buffer.SetBuffer(_buffer, 0, _bytesToSend); }
public void Send(ISocketBuffer args) { if (_bytesTransferred < _bytesEnqueued) { //TODO: Is this faster than moving the bytes to the beginning of the buffer and append more bytes? args.SetBuffer(_bufferSlice.Buffer, _bufferSlice.Offset + _bytesTransferred, _bytesEnqueued - _bytesTransferred); return; } if (!_headerIsSent) { var headerLength = CreateHeader(); var bytesToWrite = (int)Math.Min(_bufferSlice.Capacity - headerLength, _bodyStream.Length); _bodyStream.Read(_bufferSlice.Buffer, _bufferSlice.Offset + headerLength, bytesToWrite); args.SetBuffer(_bufferSlice.Buffer, _bufferSlice.Offset, bytesToWrite + headerLength); _bytesEnqueued = headerLength + bytesToWrite; _bytesLeftToSend = headerLength + (int)_bodyStream.Length; } else { _bytesEnqueued = (int)Math.Min(_bufferSlice.Capacity, _bytesLeftToSend); _bodyStream.Write(_bufferSlice.Buffer, _bufferSlice.Offset, _bytesEnqueued); args.SetBuffer(_bufferSlice.Buffer, _bufferSlice.Offset, _bytesEnqueued); } }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend); return; } if (_isHeaderSent) { var bytes = Math.Min(_totalAmountToSend, _buffer.Length); _message.Body.Read(_buffer, 0, bytes); _bytesToSend = bytes; buffer.SetBuffer(_buffer, 0, bytes); return; } _writer.WriteLine(_message.StatusLine); foreach (var header in _message.Headers) { _writer.Write("{0}:{1}\r\n", header.Key, header.Value); } _writer.Write("\r\n"); _writer.Flush(); _isHeaderSent = true; if (_message.Body == null || _message.ContentLength == 0) { _bytesToSend = (int) _stream.Length; _totalAmountToSend = _bytesToSend; buffer.SetBuffer(_buffer, 0, (int) _stream.Length); return; } var bytesLeft = _buffer.Length - _stream.Length; var bytesToSend = Math.Min(_message.ContentLength, (int) bytesLeft); var offset = (int) _stream.Position; _message.Body.Read(_buffer, offset, bytesToSend); _bytesToSend = (int) _stream.Length + bytesToSend; _totalAmountToSend = (int) _stream.Length + _message.ContentLength; buffer.SetBuffer(_buffer, 0, _totalAmountToSend); }
public void Send(ISocketBuffer buffer) { this.stream.Position = 0; this.stream.SetLength(0); if (!this.isHeaderSent) { this.writer.WriteLine(this.response.StatusLine); foreach (var header in this.response.Headers) { if (string.IsNullOrEmpty(header.Key)) { continue; } this.writer.Write("{0}: {1}\r\n", header.Key, header.Value); } this.writer.Write("\r\n"); this.isHeaderSent = true; buffer.UserToken = this.response; this.writer.Flush(); } //stopwatch.Stop(); //Debug.WriteLine("Network: " + stopwatch.ElapsedMilliseconds); //stopwatch.Restart(); this.nextFrameAvailable = this.response.StreamSource.WriteNextFrame(this.multipartStream).Result; //stopwatch.Stop(); //Debug.WriteLine("Encoding: " + stopwatch.ElapsedMilliseconds); //stopwatch.Restart(); // Send buffer.SetBuffer(this.buffer, 0, (int)this.stream.Length); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer" />. This choice was made /// to prevent unnecessary copy operations. /// </param> /// <remarks> /// The <c>buffer</c> variable is typically a wrapper around <see cref="SocketAsyncEventArgs" />, but may be something /// else if required. /// </remarks> public void Send(ISocketBuffer buffer) { buffer.SetBuffer(_buffer, _offset, _bytesLeft); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { if (_message == null) { _httpMessageEncoder.Send(buffer); } else { // last send operation did not send all bytes enqueued in the buffer // so let's just continue until doing next message if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend); return; } var offset = (int)_message.Payload.Position; var length = (int)_message.Payload.Length; var frameLength = length - offset; var fin = WebSocketFin.Final; if (frameLength > WebSocketFrame.FragmentLength) { frameLength = WebSocketFrame.FragmentLength; fin = WebSocketFin.More; } var opcode = WebSocketOpcode.Continuation; if (offset == 0) // first frame { opcode = _message.Opcode; } var buff = new byte[frameLength]; _message.Payload.Read(buff, 0, buff.Length); var payload = new MemoryStream(buff); WebSocketFrame frame = new WebSocketFrame(fin, opcode, (_handshake is IHttpRequest) ? WebSocketMask.Mask : WebSocketMask.Unmask, payload); using (var stream = new MemoryStream()) { var header = (int)frame.Fin; header = (header << 1) + (int)frame.Rsv1; header = (header << 1) + (int)frame.Rsv2; header = (header << 1) + (int)frame.Rsv3; header = (header << 4) + (int)frame.Opcode; header = (header << 1) + (int)frame.Mask; header = (header << 7) + (int)frame.PayloadLength; stream.Write(WebSocketUtils.GetBigEndianBytes((ushort)header), 0, 2); if (frame.PayloadLength > 125) { stream.Write(frame.ExtPayloadLength, 0, frame.ExtPayloadLength.Length); } if (frame.Mask == WebSocketMask.Mask) { stream.Write(frame.MaskingKey, 0, frame.MaskingKey.Length); frame.Unmask(); } _totalAmountToSend += (int)stream.Length; if (frame.PayloadLength > 0) { frame.Payload.CopyTo(stream); } buffer.UserToken = _message; _buffer = stream.ToArray(); _bytesToSend = _buffer.Length; buffer.SetBuffer(_buffer, 0, _bytesToSend); } } }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend, _bytesToSend); return; } if (_isHeaderSent) { var bytes = Math.Min(_totalAmountToSend, _buffer.Length); _frame.Body.Read(_buffer, 0, bytes); _bytesToSend = bytes; // include null char as message delimter if (bytes < _buffer.Length) { buffer.Buffer[_bytesToSend] = 0; bytes++; } buffer.SetBuffer(_buffer, 0, bytes, bytes); return; } if (_frame.Name == "NoOp") { _bytesToSend = 0; buffer.Buffer[0] = 10; buffer.SetBuffer(0, 1); return; } _writer.Write("{0}\n", _frame.Name); foreach (var header in _frame.Headers) { _writer.Write("{0}:{1}\n", header.Key, header.Value); } _writer.Write("\n"); _writer.Flush(); _isHeaderSent = true; if (_frame.Body == null || _frame.ContentLength == 0) { _stream.Write(new byte[] {0}, 0, 1); _bytesToSend = (int) _stream.Length; _totalAmountToSend = _bytesToSend; buffer.SetBuffer(_buffer, 0, (int) _stream.Length); return; } var bytesLeft = _buffer.Length - _stream.Length; var bytesToSend = Math.Min(_frame.ContentLength, (int) bytesLeft); var offset = (int) _stream.Position; _frame.Body.Read(_buffer, offset, bytesToSend); _bytesToSend = (int) _stream.Length + bytesToSend; _totalAmountToSend = (int) _stream.Length + _frame.ContentLength + 1; // everything is done in the first run // so add the message delimiter. if (_totalAmountToSend < buffer.Count) { _totalAmountToSend++; _bytesToSend++; _buffer[_bytesToSend] = 0; } buffer.SetBuffer(_buffer, 0, _totalAmountToSend); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer">Do note that there are not buffer attached to the structure, you have to assign one yourself using <see cref="ISocketBuffer.SetBuffer"/>. This choice was made /// to prevent unnecessary copy operations.</param> public void Send(ISocketBuffer buffer) { buffer.SetBuffer(Buffer, Offset, Count); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { if (_message == null) { _httpMessageEncoder.Send(buffer); } else { // last send operation did not send all bytes enqueued in the buffer // so let's just continue until doing next message if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend); return; } var offset = (int)_message.Payload.Position; var length = (int)_message.Payload.Length; var frameLength = length - offset; var fin = WebSocketFin.Final; if (frameLength > WebSocketFrame.FragmentLength) { frameLength = WebSocketFrame.FragmentLength; fin = WebSocketFin.More; } var opcode = WebSocketOpcode.Continuation; if (offset == 0) // first frame { opcode = _message.Opcode; } var buff = new byte[frameLength]; _message.Payload.Read(buff, 0, buff.Length); var payload = new MemoryStream(buff); WebSocketFrame frame = new WebSocketFrame(fin, opcode, (_handshake is IHttpRequest) ? WebSocketMask.Mask : WebSocketMask.Unmask, payload); using (var stream = new MemoryStream()) { var header = (int)frame.Fin; header = (header << 1) + (int)frame.Rsv1; header = (header << 1) + (int)frame.Rsv2; header = (header << 1) + (int)frame.Rsv3; header = (header << 4) + (int)frame.Opcode; header = (header << 1) + (int)frame.Mask; header = (header << 7) + (int)frame.PayloadLength; stream.Write(WebSocketUtils.GetBigEndianBytes((ushort)header), 0, 2); if (frame.PayloadLength > 125) { stream.Write(frame.ExtPayloadLength, 0, frame.ExtPayloadLength.Length); } if (frame.Mask == WebSocketMask.Mask) { stream.Write(frame.MaskingKey, 0, frame.MaskingKey.Length); frame.Unmask(); } _totalAmountToSend += (int)stream.Length; if (frame.PayloadLength > 0) { frame.Payload.CopyTo(stream); } buffer.UserToken = _message; _buffer = stream.ToArray(); _bytesToSend = _buffer.Length; buffer.SetBuffer(_buffer, 0, _bytesToSend); } } }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> public void Send(ISocketBuffer buffer) { if (_bytesToSend > 0) { buffer.SetBuffer(_buffer, _offset, _bytesToSend, _bytesToSend); return; } if (_isHeaderSent) { var bytes = Math.Min(_totalAmountToSend, _buffer.Length); _frame.Body.Read(_buffer, 0, bytes); _bytesToSend = bytes; // include null char as message delimter if (bytes < _buffer.Length) { buffer.Buffer[_bytesToSend] = 0; bytes++; } buffer.SetBuffer(_buffer, 0, bytes, bytes); return; } if (_frame.Name == "NoOp") { _bytesToSend = 0; buffer.Buffer[0] = 10; buffer.SetBuffer(0, 1); return; } _writer.Write("{0}\n", _frame.Name); foreach (var header in _frame.Headers) { _writer.Write("{0}:{1}\n", header.Key, header.Value); } _writer.Write("\n"); _writer.Flush(); _isHeaderSent = true; if (_frame.Body == null || _frame.ContentLength == 0) { _stream.Write(new byte[] { 0 }, 0, 1); _bytesToSend = (int)_stream.Length; _totalAmountToSend = _bytesToSend; buffer.SetBuffer(_buffer, 0, (int)_stream.Length); return; } var bytesLeft = _buffer.Length - _stream.Length; var bytesToSend = Math.Min(_frame.ContentLength, (int)bytesLeft); var offset = (int)_stream.Position; _frame.Body.Read(_buffer, offset, bytesToSend); _bytesToSend = (int)_stream.Length + bytesToSend; _totalAmountToSend = (int)_stream.Length + _frame.ContentLength + 1; // everything is done in the first run // so add the message delimiter. if (_totalAmountToSend < buffer.Count) { _totalAmountToSend++; _bytesToSend++; _buffer[_bytesToSend] = 0; } buffer.SetBuffer(_buffer, 0, _totalAmountToSend); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer"> /// Do note that there are not buffer attached to the structure, you have to assign one yourself using /// <see cref="ISocketBuffer.SetBuffer(int,int)" />. This choice was made /// to prevent unnecessary copy operations. /// </param> /// <remarks> /// The <c>buffer</c> variable is typically a wrapper around <see cref="SocketAsyncEventArgs" />, but may be something /// else if required. /// </remarks> public void Send(ISocketBuffer buffer) { buffer.SetBuffer(_buffer, _offset, _bytesLeft); }
public void Send(ISocketBuffer buffer) { // Continue from where the last send operation stopped. buffer.SetBuffer(_memoryStream.GetBuffer(), _offset, _bytesLeft); }
/// <summary> /// Buffer structure used for socket send operations. /// </summary> /// <param name="buffer">Do note that there are not buffer attached to the structure, you have to assign one yourself using <see cref="ISocketBuffer.SetBuffer"/>. This choice was made /// to prevent unnecessary copy operations.</param> public void Send(ISocketBuffer buffer) { buffer.SetBuffer(Buffer, Offset, Count); }
public void Send(ISocketBuffer buffer) { // Continue from where the last send operation stopped. buffer.SetBuffer(_memoryStream.GetBuffer(), _offset, _bytesLeft); }