Beispiel #1
0
        /// <summary>
        /// Sends the specified slice.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">slice</exception>
        /// <exception cref="System.InvalidOperationException">Socket as not been Assign():ed.</exception>
        /// <seealso cref="StreamSocketWriterJob"/>
        /// <seealso cref="SliceSocketWriterJob"/>
        public void Send(ISocketWriterJob job)
        {
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }
            if (this.socket == null || !this.socket.Connected)
            {
                throw new InvalidOperationException("Socket is not connected.");
            }

            lock (this)
            {
                if (this.currentJob != null)
                {
                    this.logger.Debug(this.writeArgs.GetHashCode() + ": Enqueueing ");
                    this.writeQueue.Enqueue(job);
                    return;
                }

                this.logger.Debug(this.writeArgs.GetHashCode() + ": sending directly ");
                this.currentJob = job;
            }

            this.currentJob.Write(this.writeArgs);

            var isPending = this.socket.SendAsync(this.writeArgs);

            if (!isPending)
            {
                this.HandleWriteCompleted(this.writeArgs.SocketError, this.writeArgs.BytesTransferred);
            }
        }
        /// <summary>
        /// Sends the specified slice.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">slice</exception>
        /// <exception cref="System.InvalidOperationException">Socket as not been Assign():ed.</exception>
        /// <seealso cref="StreamSocketWriterJob"/>
        /// <seealso cref="SliceSocketWriterJob"/>
        public void Send(ISocketWriterJob job)
        {
            if (job == null) throw new ArgumentNullException("job");
            if (_socket == null || !_socket.Connected)
                throw new InvalidOperationException("Socket is not connected.");

            lock (this)
            {
                if (_currentJob != null)
                {
                    _logger.Debug(_writeArgs.GetHashCode() + ": Enqueueing ");
                    _writeQueue.Enqueue(job);
                    return;
                }

                _logger.Debug(_writeArgs.GetHashCode() + ": sending directly ");
                _currentJob = job;
            }

            _currentJob.Write(_writeArgs);

            var isPending = _socket.SendAsync(_writeArgs);
            if (!isPending)
                HandleWriteCompleted(_writeArgs.SocketError, _writeArgs.BytesTransferred);
        }
    public void Send(ISocketWriterJob job)
    {
        if (Interlocked.CompareExchange(ref _currentJob, job, null) != null)
        {
            _writeQueue.Enqueue(job);
            return;
        }
        _currentJob.Write(_writeArgs);

        // The job is invoked asynchronously here
    }
    private void HandleWriteCompleted(SocketError error, int bytesTransferred)
    {
        // error checks etc removed for this sample.

        if (_currentJob.WriteCompleted(bytesTransferred))
        {
            _currentJob.Dispose();

            bool spin = true;
            while (spin)
            {
                switch (_state)
                {
                case State.Active:
                    if (Interlocked.CompareExchange(ref _state, State.Dequeue, State.Active) == State.Active)
                    {
                        if (!_writeQueue.TryDequeue(out _currentJob))
                        {
                            _currentJob = null;
                            Interlocked.Exchange(_state, State.Idle);
                            return;
                        }
                        else
                        {
                            Interlocked.Exchange(_state, State.Active);
                        }
                    }
                    // else consider new state
                    break;

                case State.Enqueue:
                case State.Dequeue:
                    // spin to wait for new state
                    Thread.Yield();
                    break;

                case State.Idle:     // impossible state
                    break;
                }
            }
        }

        _logger.Debug(_writeArgs.GetHashCode() + ": writing more ");
        _currentJob.Write(_writeArgs);

        // the job is invoked asycnhronously here.
    }
Beispiel #5
0
        /// <summary>
        /// Reset writer.
        /// </summary>
        public void Reset()
        {
            if (this.currentJob != null)
            {
                this.currentJob.Dispose();
            }
            this.currentJob = null;


            ISocketWriterJob job;

            while (this.writeQueue.TryDequeue(out job))
            {
                job.Dispose();
            }

            this.socket = null;
        }
Beispiel #6
0
        /// <summary>
        /// Reset writer.
        /// </summary>
        public void Reset()
        {
            if (_currentJob != null)
            {
                _currentJob.Dispose();
            }
            _currentJob = null;


            ISocketWriterJob job;

            while (_writeQueue.TryDequeue(out job))
            {
                job.Dispose();
            }

            _socket = null;
        }
    private void HandleWriteCompleted(SocketError error, int bytesTransferred)
    {
        // error checks etc removed for this sample.

        if (_currentJob.WriteCompleted(bytesTransferred))
        {
            _currentJob.Dispose();
            _currentJob = null;     // maybe some barrier is needed here?...
            if (!_writeQueue.TryDequeue(out _currentJob))
            {
                return;
            }
        }

        _logger.Debug(_writeArgs.GetHashCode() + ": writing more ");
        _currentJob.Write(_writeArgs);

        // the job is invoked asycnhronously here.
    }
Beispiel #8
0
    public void Send(ISocketWriterJob job)
    {
        bool spin = true;

        while (spin)
        {
            switch (_state)
            {
            case State.Idle:
                if (Interlocked.CompareExchange(ref _state, State.Active, State.Idle) == State.Idle)
                {
                    spin = false;
                }
                // else consider new state
                break;

            case State.Active:
                if (Interlocked.CompareExchange(ref _state, State.Enqueue, State.Active) == State.Active)
                {
                    _writeQueue.Enqueue(job);
                    _state = State.Active;
                    return;
                }
                // else consider new state
                break;

            case State.Enqueue:
            case State.Dequeue:
                // spin to wait for new state
                Thread.Yield();
                break;
            }
        }

        _currentJob = job;
        _currentJob.Write(_writeArgs);

        // The job is invoked asynchronously here
    }
Beispiel #9
0
        private void HandleWriteCompleted(SocketError error, int bytesTransferred)
        {
            if (this.currentJob == null || this.socket == null || !this.socket.Connected)
            {
                return; // got disconnected
            }
            if (error == SocketError.Success && bytesTransferred > 0)
            {
                lock (this)
                {
                    if (this.currentJob.WriteCompleted(bytesTransferred))
                    {
                        this.currentJob.Dispose();
                        if (!this.writeQueue.TryDequeue(out this.currentJob))
                        {
                            this.logger.Debug(this.writeArgs.GetHashCode() + ": no new job ");
                            this.currentJob = null;
                            return;
                        }
                    }
                }

                this.currentJob.Write(this.writeArgs);
                var isPending = this.socket.SendAsync(this.writeArgs);
                if (!isPending)
                {
                    this.HandleWriteCompleted(this.writeArgs.SocketError, this.writeArgs.BytesTransferred);
                }
            }
            else
            {
                if (error == SocketError.Success)
                {
                    error = SocketError.ConnectionReset;
                }
                this.HandleDisconnect(error);
            }
        }
        private void HandleWriteCompleted(SocketError error, int bytesTransferred)
        {
            if (_currentJob == null || _socket == null || !_socket.Connected)
                return; // got disconnected

            if (error == SocketError.Success && bytesTransferred > 0)
            {
                lock (this)
                {
                    if (_currentJob.WriteCompleted(bytesTransferred))
                    {
                        _currentJob.Dispose();
                        if (!_writeQueue.TryDequeue(out _currentJob))
                        {
                            _logger.Debug(_writeArgs.GetHashCode() + ": no new job ");
                            _currentJob = null;
                            return;
                        }
                    }
                }

                _currentJob.Write(_writeArgs);
                var isPending = _socket.SendAsync(_writeArgs);
                if (!isPending)
                    HandleWriteCompleted(_writeArgs.SocketError, _writeArgs.BytesTransferred);
            }
            else
            {
                if (error == SocketError.Success)
                    error = SocketError.ConnectionReset;
                HandleDisconnect(error);
            }
        }
        /// <summary>
        /// Reset writer.
        /// </summary>
        public void Reset()
        {
            if (_currentJob != null)
                _currentJob.Dispose();
            _currentJob = null;


            ISocketWriterJob job;
            while (_writeQueue.TryDequeue(out job))
            {
                job.Dispose();
            }

            _socket = null;
        }