Пример #1
0
            public SendAsyncResult(UdpOutputChannel channel, Message message, AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.channel = channel;

                //obtain the transaction propagation token from the TransactionFlowProperty on the message
                byte[] txPropToken = TransactionFlowProperty.Get(message);

                this.messageBuffer = channel.EncodeMessage(message);

                txmsgBuffer = TransactionMessageBuffer.WriteTransactionMessageBuffer(txPropToken, messageBuffer);
                if ((long)txmsgBuffer.Length > channel.Factory.MaxPacketSize)
                {
                    throw new CommunicationException("The output packet size is greater than the maximum size supported.");
                }

                try
                {
                    IAsyncResult result = null;
                    try
                    {
                        result = channel.socket.BeginSendTo(txmsgBuffer, 0, txmsgBuffer.Length,
                                                            SocketFlags.None, channel.remoteEndPoint, new AsyncCallback(OnSend), this);
                    }
                    catch (SocketException socketException)
                    {
                        throw UdpChannelHelpers.ConvertTransferException(socketException);
                    }

                    if (!result.CompletedSynchronously)
                    {
                        return;
                    }

                    CompleteSend(result, true);
                }
                catch
                {
                    CleanupBuffer();
                    throw;
                }
            }
Пример #2
0
        public void Send(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            base.ThrowIfDisposedOrNotOpen();

            //obtain the transaction propagation token from the TransactionFlowProperty on the message
            byte[] txPropToken = TransactionFlowProperty.Get(message);

            ArraySegment <byte> messageBuffer = EncodeMessage(message);

            byte[] txmsgBuffer = TransactionMessageBuffer.WriteTransactionMessageBuffer(txPropToken, messageBuffer);
            if ((long)txmsgBuffer.Length > this.Factory.MaxPacketSize)
            {
                throw new CommunicationException("The output packet size is greater than the maximum size supported.");
            }

            try
            {
                int bytesSent = this.socket.SendTo(txmsgBuffer, 0, txmsgBuffer.Length,
                                                   SocketFlags.None, this.remoteEndPoint);

                if (bytesSent != txmsgBuffer.Length)
                {
                    throw new CommunicationException(string.Format(CultureInfo.CurrentCulture,
                                                                   "A Udp error occurred sending a message to {0}.", this.remoteEndPoint));
                }
            }
            catch (SocketException socketException)
            {
                throw UdpChannelHelpers.ConvertTransferException(socketException);
            }
            finally
            {
                // we need to make sure buffers are always returned to the BufferManager
                parent.BufferManager.ReturnBuffer(messageBuffer.Array);
            }
        }