A base class for all network senders. Supports one-way sending of messages over various protocols.
Inheritance: IDisposable
Beispiel #1
0
        private void ChunkedSend(NetworkSender sender, byte[] buffer, AsyncContinuation continuation)
        {
            int tosend = buffer.Length;
            int pos = 0;

            AsyncContinuation sendNextChunk = null;

            sendNextChunk = ex =>
                {
                    if (ex != null)
                    {
                        continuation(ex);
                        return;
                    }

                    if (tosend <= 0)
                    {
                        continuation(null);
                        return;
                    }

                    int chunksize = tosend;
                    if (chunksize > this.MaxMessageSize)
                    {
                        if (this.OnOverflow == NetworkTargetOverflowAction.Discard)
                        {
                            continuation(null);
                            return;
                        }

                        if (this.OnOverflow == NetworkTargetOverflowAction.Error)
                        {
                            continuation(new OverflowException("Attempted to send a message larger than MaxMessageSize (" + this.MaxMessageSize + "). Actual size was: " + buffer.Length + ". Adjust OnOverflow and MaxMessageSize parameters accordingly."));
                            return;
                        }

                        chunksize = this.MaxMessageSize;
                    }

                    int pos0 = pos;
                    tosend -= chunksize;
                    pos += chunksize;

                    sender.Send(buffer, pos0, chunksize, sendNextChunk);
                };

            sendNextChunk(null);
        }
Beispiel #2
0
        private void ChunkedSend(NetworkSender sender, byte[] buffer)
        {
            int tosend = buffer.Length;
            int pos = 0;

            while (tosend > 0)
            {
                int chunksize = tosend;
                if (chunksize > MaxMessageSize)
                {
                    if (OnOverflow == OverflowAction.Discard)
                        return;

                    if (OnOverflow == OverflowAction.Error)
                        throw new OverflowException("Attempted to send a message larger than MaxMessageSize(" + MaxMessageSize + "). Actual size was: " + buffer.Length + ". Adjust OnOverflow and MaxMessageSize parameters accordingly.");

                    chunksize = MaxMessageSize;
                }
                sender.Send(buffer, pos, chunksize);
                tosend -= chunksize;
                pos += chunksize;
            }
        }
Beispiel #3
0
        private void ReleaseCachedConnection(NetworkSender sender)
        {
            lock (this.currentSenderCache)
            {
                lock (this.openNetworkSenders)
                {
                    if (this.openNetworkSenders.Remove(sender))
                    {
                        // only remove it once
                        sender.Close(ex => { });
                    }
                }

                NetworkSender sender2;

                // make sure the current sender for this address is the one we want to remove
                if (this.currentSenderCache.TryGetValue(sender.Address, out sender2))
                {
                    if (ReferenceEquals(sender, sender2))
                    {
                        this.currentSenderCache.Remove(sender.Address);
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Sends the provided text to the specified address.
        /// </summary>
        /// <param name="address">The address. Can be tcp://host:port, udp://host:port, http://host:port</param>
        /// <param name="bytes">The bytes to be sent.</param>
        protected virtual void NetworkSend(string address, byte[] bytes)
        {
            lock (this)
            {
                if (KeepConnection)
                {
                    if (_sender != null)
                    {
                        if (_sender.Address != address)
                        {
                            _sender.Close();
                            _sender = null;
                        }
                    };
                    if (_sender == null)
                    {
                        _sender = NetworkSender.Create(address);
                    }

                    try
                    {
                        ChunkedSend(_sender, bytes);
                    }
                    catch (Exception ex)
                    {
                        InternalLogger.Error("Error when sending {0}", ex);
                        _sender.Close();
                        _sender = null;
                        throw;
                    }
                }
                else
                {
                    NetworkSender sender = NetworkSender.Create(address);

                    try
                    {
                        ChunkedSend(sender, bytes);
                    }
                    finally
                    {
                        sender.Close();
                    }
                }
            }
        }