Esempio n. 1
0
        private void ReleaseCachedConnection(NetworkSender sender)
        {
            lock (currentSenderCache)
            {
                lock (openNetworkSenders)
                {
                    if (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 (currentSenderCache.TryGetValue(sender.Address, out sender2))
                {
                    if (ReferenceEquals(sender, sender2))
                    {
                        currentSenderCache.Remove(sender.Address);
                    }
                }
            }
        }
Esempio n. 2
0
        private void ChunkedSend(NetworkSender sender, byte[] buffer, AsyncContinuation continuation)
        {
            var tosend = buffer.Length;
            var pos = 0;

            AsyncContinuation sendNextChunk = null;

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

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

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

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

                                        chunksize = MaxMessageSize;
                                    }

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

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

            sendNextChunk(null);
        }