Ejemplo n.º 1
0
        public async Task<long> WriteAsync(Stream input, TcpResponseHeader header = null)
        {
            var stream = new NetworkStream(_socket);

            var len = input.Length - input.Position;

            if (header == null) header = new TcpResponseHeader(() => len);

            var head = header.GetBytes();

            await stream.WriteAsync(head, 0, head.Length);

            await input.CopyToAsync(stream, _writeBuffer.Length);

            return len + head.Length;
        }
Ejemplo n.º 2
0
        public int Write(Stream input, TcpResponseHeader header = null)
        {
            var len = input.Length - input.Position;

            if (header == null) header = new TcpResponseHeader(() => len);

            var head = header.GetBytes();

            Array.Copy(head, _writeBuffer, head.Length);

            var offset = head.Length;

            int read = 0;
            int sent = 0;
            int sending = 0;

            DebugOutput.Log("Sending {0} bytes", len);

            _socket.SendBufferSize = _writeBuffer.Length;

            while (input.Position < len || offset > 0)
            {
                if (len > 0)
                {
                    read = input.Read(_writeBuffer, offset, _writeBuffer.Length - offset);

                    if (read == 0 && offset == 0) break;
                }

                sending = read + offset;

                if (sending < _socket.SendBufferSize)
                {
                    _socket.SendBufferSize = sending;
                }

                _socket.Send(_writeBuffer, sending, SocketFlags.None);

                offset = 0;
                sent += read;
            }

            return sent;
        }