Esempio n. 1
0
        /// <summary>
        /// Starts the receive process.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Thrown if the <see cref="NetworkSession"/> instance does not have a socket attached to it or
        /// if the instance is already active.
        /// </exception>
        public void Start()
        {
            if (_socket == null)
            {
                throw new InvalidOperationException(CommonStrings.NoSocketAttached);
            }

            if (!_isActive.FlipIf(false))
            {
                throw new InvalidOperationException(CommonStrings.SessionAlreadyActive);
            }

            _receiveDescriptor.StartReceive();
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public void Start()
        {
            if (!_isConfigured)
            {
                throw GetServerIsNotConfiguredException();
            }

            if (!_isRunning.FlipIf(false))
            {
                throw GetServerAlreadyRunningException();
            }

            _acceptor.Start();
            _logger.Info(@"Now listening on port {0}.", _acceptor.Endpoint.Port);
        }
Esempio n. 3
0
        /// <summary>
        /// Starts the asynchronous packet sending process.
        /// </summary>
        public void Push()
        {
            if (PacketProcessing == null)
            {
                throw new InvalidOperationException(ServerStrings.PacketProcessingEventHasNoSubscriber);
            }

            if (_isPushing.FlipIf(false))
            {
                StartPushing();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Writes a byte array to the stream.
        /// </summary>
        /// <param name="data">The data to write.</param>
        /// <exception cref="InvalidOperationException">
        /// Thrown if this session is not open.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="data"/> is <see langword="null"/>.
        /// </exception>
        public void Write(byte[] data)
        {
            if (!Container.IsActive)
            {
                throw new InvalidOperationException(CommonStrings.SessionIsNotActive);
            }

            Guard.NotNull(() => data, data);

            _queue.Enqueue(data);

            // If the sending operations are already in progress,
            // they'll get to the packet we just queued eventually...
            if (_isSending.FlipIf(false))
            {
                // Otherwise, we do it ourselves.
                _sentBytes = 0;
                BeginSend();
            }
        }