Ejemplo n.º 1
0
        public void Send(Message message)
        {
            base.ThrowIfDisposedOrNotOpen();

            ArraySegment <byte> messageBuffer = EncodeMessage(message);

            // Adding the framing header
            messageBuffer = FramingCodec.Encode(this.remoteAddress.Uri,
                                                messageBuffer, parent.BufferManager);

            try
            {
                int bytesSent = this.socket.SendTo(messageBuffer.Array, messageBuffer.Offset, messageBuffer.Count,
                                                   SocketFlags.None, this.remoteEndPoint);

                if (bytesSent != messageBuffer.Count)
                {
                    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
            {
                // Must make sure buffers are always returned to the BufferManager
                parent.BufferManager.ReturnBuffer(messageBuffer.Array);
            }
        }