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

            ArraySegment <byte> messageBuffer = EncodeMessage(message);

            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
            {
                // we need to make sure buffers are always returned to the BufferManager
                parent.BufferManager.ReturnBuffer(messageBuffer.Array);
            }
        }
Ejemplo n.º 2
0
            public SendAsyncResult(UdpOutputChannel channel, Message message, AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.channel       = channel;
                this.messageBuffer = channel.EncodeMessage(message);
                try
                {
                    IAsyncResult result = null;
                    try
                    {
                        result = channel.socket.BeginSendTo(messageBuffer.Array, messageBuffer.Offset, messageBuffer.Count,
                                                            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;
                }
            }
Ejemplo n.º 3
0
        public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
        {
            //Console.Out.WriteLine("ChannelBeginTryReceive");
            //StackTrace st = new StackTrace();
            //string s = st.ToString();
            //Console.WriteLine(s);

            UdpChannelHelpers.ValidateTimeout(timeout);
            return(this.messageQueue.BeginDequeue(timeout, callback, state));
        }
Ejemplo n.º 4
0
        protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
        {
            //Console.Out.WriteLine("OnBeginAcceptChannel");
            UdpChannelHelpers.ValidateTimeout(timeout);
            if (!this.IsDisposed)
            {
                this.EnsureChannelAvailable();
            }

            return(this.channelQueue.BeginDequeue(timeout, callback, state));
        }
Ejemplo n.º 5
0
        //Synchronously returns a channel that is attached to this listener.
        protected override IInputChannel OnAcceptChannel(TimeSpan timeout)
        {
            //Console.Out.WriteLine("OnAcceptChannel");
            UdpChannelHelpers.ValidateTimeout(timeout);
            if (!this.IsDisposed)
            {
                this.EnsureChannelAvailable();
            }

            IInputChannel channel;

            if (this.channelQueue.Dequeue(timeout, out channel))
            {
                return(channel);
            }
            else
            {
                throw CreateAcceptTimeoutException(timeout);
            }
        }
Ejemplo n.º 6
0
            void CompleteSend(IAsyncResult result, bool synchronous)
            {
                try
                {
                    int bytesSent = channel.socket.EndSendTo(result);

                    if (bytesSent != messageBuffer.Count)
                    {
                        throw new CommunicationException(string.Format(CultureInfo.CurrentCulture,
                                                                       "A Udp error occurred sending a message to {0}.", channel.remoteEndPoint));
                    }
                }
                catch (SocketException socketException)
                {
                    throw UdpChannelHelpers.ConvertTransferException(socketException);
                }
                finally
                {
                    CleanupBuffer();
                }

                base.Complete(synchronous);
            }
Ejemplo n.º 7
0
 public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state)
 {
     //Console.WriteLine("BeginWaitForMessage");
     UdpChannelHelpers.ValidateTimeout(timeout);
     return(this.messageQueue.BeginWaitForItem(timeout, callback, state));
 }
Ejemplo n.º 8
0
 public bool WaitForMessage(TimeSpan timeout)
 {
     //Console.WriteLine("WaitForMessage");
     UdpChannelHelpers.ValidateTimeout(timeout);
     return(this.messageQueue.WaitForItem(timeout));
 }
Ejemplo n.º 9
0
 public bool TryReceive(TimeSpan timeout, out Message message)
 {
     UdpChannelHelpers.ValidateTimeout(timeout);
     return(this.messageQueue.Dequeue(timeout, out message));
 }
Ejemplo n.º 10
0
        internal UdpOutputChannel(UdpChannelFactory factory, EndpointAddress remoteAddress, Uri via, MessageEncoder encoder)
            : base(factory)
        {
            // validate addressing arguments
            if (!string.Equals(via.Scheme, UdpConstants.Scheme, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          "The scheme {0} specified in address is not supported.", via.Scheme), "via");
            }

            if (via.IsDefaultPort)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          "The address {0} must specify a remote port.", via), "via");
            }

            // convert the Uri host into an IP Address
            IPAddress remoteIP = null;

            switch (via.HostNameType)
            {
            default:
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          "Cannot determine the remote host address from {0}.",
                                                          this.via.ToString()), "via");

            case UriHostNameType.IPv4:
            case UriHostNameType.IPv6:
                remoteIP = IPAddress.Parse(via.Host);
                break;

            case UriHostNameType.Basic:
            case UriHostNameType.Dns:
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(via.Host);
                if (hostEntry.AddressList.Length > 0)
                {
                    remoteIP = hostEntry.AddressList[0];
                }
                else
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              "Failed to resolve remote host: {0}.", via.Host),
                                                "via");
                }
                break;
            }
            }

            if (factory.Multicast && !UdpChannelHelpers.IsInMulticastRange(remoteIP))
            {
                throw new ArgumentOutOfRangeException("remoteEndPoint", "Via must be in the valid multicast range.");
            }

            this.parent         = factory;
            this.remoteAddress  = remoteAddress;
            this.via            = via;
            this.encoder        = encoder;
            this.remoteEndPoint = new IPEndPoint(remoteIP, via.Port);
            this.socket         = new Socket(this.remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
            this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            if (parent.SendPort != 0)
            {
                this.socket.Bind(new IPEndPoint(IPAddress.Any, parent.SendPort));
            }
            //if (parent.Multicast)
            //{
            //this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            //this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(remoteIP));
            //socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 50);//貌似不需要这么设置,自动支持组播

#if LATER // Support outgoing interface
            if (this.remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
            {
                this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder((int)interfaceIndex));
            }
            else     //  we are IPv6
            {
                this.sendSocketV6.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, (int)interfaceIndex);
            }
#endif
            //}
        }
Ejemplo n.º 11
0
 protected override IAsyncResult OnBeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state)
 {
     //Console.WriteLine("OnBeginWaitForChannel");
     UdpChannelHelpers.ValidateTimeout(timeout);
     return(this.channelQueue.BeginWaitForItem(timeout, callback, state));
 }
Ejemplo n.º 12
0
 protected override bool OnWaitForChannel(TimeSpan timeout)
 {
     //Console.WriteLine("OnWaitForChannel");
     UdpChannelHelpers.ValidateTimeout(timeout);
     return(this.channelQueue.WaitForItem(timeout));
 }