Exemple #1
0
        protected virtual void OnSendComplete(SendEventArgs arg)
        {
            if (arg.SocketError != System.Net.Sockets.SocketError.Success)
            {
                //Release back to the pool
                arg.Socket = null;
                _sendPool.PushBack(arg);
                sending = false;

                Close();
            }
            else if (arg.BytesTransferred == 0)
            {
                //Release back to the pool
                arg.Socket = null;
                _sendPool.PushBack(arg);
                sending = false;

                Close();
            }
            else
            {
                foreach (var msg in arg.Confirmations)
                {
                    msg.callback(msg.state);
                }

                if (SendData(arg) == SendResult.NotQueued)
                {
                    sending = false;
                }
            }
        }
        public void OnSendComplete(SendEventArgs args)
        {
            if (args.SocketError != System.Net.Sockets.SocketError.Success)
            {
                //Release back to the pool
                args.Socket = null;
                _sendPool.PushBack(args);

                Close();
            }
            else
            {
                // give to terraria
                args.SendCallback(args.UserToken);

                args.Socket = null;
                _sendPool.PushBack(args);
            }
        }
Exemple #3
0
        protected SendResult SendData(SendEventArgs args)
        {
            if (args == null)
            {
                args = _sendPool.PopFront();
                if (args.Socket != null)
                {
                    throw new InvalidOperationException($"{nameof(ReceiveEventArgs)} was not released correctly");
                }

                args.Socket = this;

                //Create the buffer list that we use to store our queued data.
                if (args.BufferList == null)
                {
                    args.BufferList = new List <ArraySegment <byte> >();
                }
            }

            //Ensure our operation collections are reset
            if (args.BufferList.Count > 0)
            {
                args.BufferList.Clear();
            }
            if (args.Confirmations.Count > 0)
            {
                args.Confirmations.Clear();
            }

            try
            {
                lock (_txSyncRoot)
                {
                    if (_txQueue.Count == 0)
                    {
                        args.Socket = null;
                        _sendPool.PushBack(args);
                        return(SendResult.NotQueued);
                    }

                    //Let our data list be sent out
                    args.BufferList = _txQueue;
                    _txQueue        = new List <ArraySegment <byte> >();

                    //Place all callbacks into this operation
                    while (_sendQueue.Count > 0)
                    {
                        args.Confirmations.Add(_sendQueue.Dequeue());
                    }

                    if (!sending)
                    {
                        sending = true;
                    }
                    var sent = _socket.SendAsync(args);
                    if (!sent)
                    {
                        OnSendComplete(args);
                    }

                    if (sent)
                    {
                        return(SendResult.Queued);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception in {this.GetType().FullName}.{nameof(SendData)}\n{ex}");
            }

            return(SendResult.NotQueued);
        }