Exemplo n.º 1
0
        public void Constructor4()
        {
            Socket     s;
            IPEndPoint localEP;
            IPEndPoint clientEP;

            clientEP = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort());
            using (MyUdpClient client = new MyUdpClient(clientEP))
            {
                s = client.Client;
                Assert.IsNotNull(s, "#A:Client");
                Assert.AreEqual(AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
                Assert.IsFalse(s.Connected, "#A:Client:Connected");
                Assert.IsTrue(s.IsBound, "#A:Client:IsBound");
                Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
                Assert.AreEqual(SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
                Assert.IsFalse(client.Active, "#A:Active");
                Assert.IsFalse(client.DontFragment, "#A:DontFragment");
                Assert.IsFalse(client.EnableBroadcast, "#A:EnableBroadcast");
                //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
                Assert.IsTrue(client.MulticastLoopback, "#A:MulticastLoopback");
                //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
                localEP = s.LocalEndPoint as IPEndPoint;
                Assert.IsNotNull(localEP, "#A:Client:LocalEndpoint");
                Assert.IsFalse(object.ReferenceEquals(clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
                Assert.AreEqual(clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
                Assert.AreEqual(clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
                Assert.AreEqual(clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
            }
        }
Exemplo n.º 2
0
        public void Constructor5_IPv6()
        {
            MyUdpClient client;
            Socket      s;
            IPEndPoint  localEP;

            client = new MyUdpClient(IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
            s      = client.Client;
            Assert.IsNotNull(s, "#B:Client");
            Assert.AreEqual(AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
            Assert.IsFalse(s.Connected, "#B:Client:Connected");
#if NET_2_0
            Assert.IsTrue(s.IsBound, "#B:Client:IsBound");
#endif
            Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
            Assert.AreEqual(SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
            Assert.IsFalse(client.Active, "#B:Active");
#if NET_2_0
            //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
            Assert.IsFalse(client.EnableBroadcast, "#B:EnableBroadcast");
            //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
            Assert.IsTrue(client.MulticastLoopback, "#B:MulticastLoopback");
            //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
            localEP = s.LocalEndPoint as IPEndPoint;
            Assert.IsNotNull(localEP, "#B:Client:LocalEndpoint");
            Assert.AreEqual(IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
            Assert.AreEqual(AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
            Assert.AreEqual(IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
        }
Exemplo n.º 3
0
        [Test]         // .ctor ()
        public void Constructor1()
        {
            MyUdpClient client;
            Socket      s;

            client = new MyUdpClient();
            s      = client.Client;
            Assert.IsNotNull(s, "Client");
            Assert.AreEqual(AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
            Assert.IsFalse(s.Connected, "Client:Connected");
#if NET_2_0
            Assert.IsFalse(s.IsBound, "#A:Client:IsBound");
#endif
            Assert.IsNull(s.LocalEndPoint, "Client:LocalEndPoint");
            Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
            Assert.IsNull(s.RemoteEndPoint, "Client:RemoteEndPoint");
            Assert.AreEqual(SocketType.Dgram, s.SocketType, "Client:SocketType");
            Assert.IsFalse(client.Active, "Active");
#if NET_2_0
            Assert.IsFalse(client.DontFragment, "DontFragment");
            Assert.IsFalse(client.EnableBroadcast, "EnableBroadcast");
            //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
            Assert.IsTrue(client.MulticastLoopback, "MulticastLoopback");
            //Assert.AreEqual (32, client.Ttl, "Ttl");
#endif
            client.Close();
        }
Exemplo n.º 4
0
		[Test] // .ctor ()
		public void Constructor1 ()
		{
			MyUdpClient client;
			Socket s;

			client = new MyUdpClient ();
			s = client.Client;
			Assert.IsNotNull (s, "Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
			Assert.IsFalse (s.Connected, "Client:Connected");
#if NET_2_0
			Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
			Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
			Assert.IsFalse (client.Active, "Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "Ttl");
#endif
			client.Close ();
		}
 public void Start(ServerDatas serverDatas, ServerDatas sdatas, Func <ClientGameMaster> createGameMaster, bool debug = false)
 {
     Client     = new MyUdpClient();
     this.debug = debug;
     Client.Start(serverDatas.address, serverDatas.port, GetReady, debug);
     _gameMaster = createGameMaster();
     _gameMaster.Init(this);
 }
Exemplo n.º 6
0
 public void CloseClient()
 {
     if (this.client != null)
     {
         this.client.Close(this.client.IsConnected);
         this.client = null;
     }
     this.hasExit = true;
 }
Exemplo n.º 7
0
        [Test]         // .ctor (String, Int32)
        public void Constructor6()
        {
            Socket     s;
            IPEndPoint localEP;

            // Bug #5503
            // UDP port 0 doesn't seem to be valid.
            using (MyUdpClient client = new MyUdpClient("127.0.0.1", 53))
            {
                s = client.Client;
                Assert.IsNotNull(s, "#A:Client");
                Assert.AreEqual(AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
                Assert.IsTrue(s.Connected, "#A:Client:Connected");
#if NET_2_0
                Assert.IsTrue(s.IsBound, "#A:Client:IsBound");
#endif
                Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
                Assert.AreEqual(SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
                Assert.IsTrue(client.Active, "#A:Active");
#if NET_2_0
                Assert.IsFalse(client.DontFragment, "#A:DontFragment");
                Assert.IsFalse(client.EnableBroadcast, "#A:EnableBroadcast");
                //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
                //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
                //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
                localEP = s.LocalEndPoint as IPEndPoint;
                Assert.IsNotNull(localEP, "#A:Client:LocalEndpoint");
                Assert.AreEqual(IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
                Assert.AreEqual(AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
            }
            using (MyUdpClient client = new MyUdpClient("127.0.0.1", IPEndPoint.MaxPort))
            {
                s = client.Client;
                Assert.IsNotNull(s, "#B:Client");
                Assert.AreEqual(AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
                Assert.IsTrue(s.Connected, "#B:Client:Connected");
#if NET_2_0
                Assert.IsTrue(s.IsBound, "#B:Client:IsBound");
#endif
                Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
                Assert.AreEqual(SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
                Assert.IsTrue(client.Active, "#B:Active");
#if NET_2_0
                Assert.IsFalse(client.DontFragment, "#B:DontFragment");
                Assert.IsFalse(client.EnableBroadcast, "#B:EnableBroadcast");
                //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
                //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
                //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
                localEP = s.LocalEndPoint as IPEndPoint;
                Assert.IsNotNull(localEP, "#B:Client:LocalEndpoint");
                Assert.AreEqual(IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
                Assert.AreEqual(AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
            }
        }
Exemplo n.º 8
0
 public static void smethod_0()
 {
     myUdpClient_0 = new MyUdpClient("192.168.5.126", 0x8f0, 0xe10);
     myUdpClient_0.LoginOKEvent    += new MyUdpClient.LoginOKDelegate(Class1.smethod_2);
     myUdpClient_0.ReceiveMsgEvent += new MyUdpClient.ReceiveMsgDelegate(Class1.smethod_3);
     myUdpClient_0.StopEvent       += new MyUdpClient.StopDelegate(Class1.smethod_4);
     myUdpClient_0.Connect("myname2", "1234", "0");
     Console.ReadLine();
     myUdpClient_0.Close(true);
 }
Exemplo n.º 9
0
        [Test]         // .ctor (AddressFamily)
        public void Constructor2()
        {
            MyUdpClient client;
            Socket      s;

            client = new MyUdpClient(AddressFamily.InterNetwork);
            s      = client.Client;
            Assert.IsNotNull(s, "#A:Client");
            Assert.AreEqual(AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
            Assert.IsFalse(s.Connected, "#A:Client:Connected");
#if NET_2_0
            Assert.IsFalse(s.IsBound, "#A:Client:IsBound");
#endif
            Assert.IsNull(s.LocalEndPoint, "#A:Client:LocalEndPoint");
            Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
            Assert.IsNull(s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
            Assert.AreEqual(SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
            Assert.IsFalse(client.Active, "#A:Active");
#if NET_2_0
            //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
            Assert.IsFalse(client.EnableBroadcast, "#A:EnableBroadcast");
            //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
            Assert.IsTrue(client.MulticastLoopback, "#A:MulticastLoopback");
            //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif

#if NET_2_0
            if (!Socket.OSSupportsIPv6)
#else
            if (!Socket.SupportsIPv6)
#endif
            { Assert.Ignore("IPv6 not enabled."); }

            client = new MyUdpClient(AddressFamily.InterNetworkV6);
            s      = client.Client;
            Assert.IsNotNull(s, "#B:Client");
            Assert.AreEqual(AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
            Assert.IsFalse(s.Connected, "#B:Client:Connected");
#if NET_2_0
            Assert.IsFalse(s.IsBound, "#A:Client:IsBound");
#endif
            Assert.IsNull(s.LocalEndPoint, "#B:Client:LocalEndPoint");
            Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
            Assert.IsNull(s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
            Assert.AreEqual(SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
            Assert.IsFalse(client.Active, "#B:Active");
#if NET_2_0
            //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
            Assert.IsFalse(client.EnableBroadcast, "#B:EnableBroadcast");
            //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
            Assert.IsTrue(client.MulticastLoopback, "#B:MulticastLoopback");
            //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
            client.Close();
        }
Exemplo n.º 10
0
		[Test] // .ctor (AddressFamily)
		public void Constructor2 ()
		{
			MyUdpClient client;
			Socket s;

			client = new MyUdpClient (AddressFamily.InterNetwork);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
			//Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif

#if NET_2_0
			if (!Socket.OSSupportsIPv6)
#else
			if (!Socket.SupportsIPv6)
#endif
				Assert.Ignore ("IPv6 not enabled.");

			client = new MyUdpClient (AddressFamily.InterNetworkV6);
			s = client.Client;
			Assert.IsNotNull (s, "#B:Client");
			Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#B:Client:Connected");
#if NET_2_0
			Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
			Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
			Assert.IsFalse (client.Active, "#B:Active");
#if NET_2_0
			//Assert.IsFalse (client.DontFragment, "#B:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
			client.Close ();
		}
Exemplo n.º 11
0
 public MyDataComunication OpenCommunication(string ipAddress, Action <object> callbackDataForImageProcessing)
 {
     ImageServer = new MyTcpServer(NetworkConstants.LocalLisenerConnection, 27001);
     ImageClient = new MyTcpClient(ipAddress, 27001);
     VoiceClient = new MyUdpClient(ipAddress, 27000);
     VoiceServer = new MyUdpServer(NetworkConstants.LocalLisenerConnection, 27000);
     ReciveImageAndProcess(callbackDataForImageProcessing);
     GrabImageAndSend();
     RecordSoundAndSend();
     ReceiveAndPlaySound();
     return(this);
 }
Exemplo n.º 12
0
        public void Constructor5()
        {
            Socket     s;
            IPEndPoint localEP;

            using (MyUdpClient client = new MyUdpClient(IPEndPoint.MinPort, AddressFamily.InterNetwork))
            {
                s = client.Client;
                Assert.IsNotNull(s, "#A:Client");
                Assert.AreEqual(AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
                Assert.IsFalse(s.Connected, "#A:Client:Connected");
                Assert.IsTrue(s.IsBound, "#A:Client:IsBound");
                Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
                Assert.AreEqual(SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
                Assert.IsFalse(client.Active, "#A:Active");
                //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
                Assert.IsFalse(client.EnableBroadcast, "#A:EnableBroadcast");
                //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
                Assert.IsTrue(client.MulticastLoopback, "#A:MulticastLoopback");
                //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
                localEP = s.LocalEndPoint as IPEndPoint;
                Assert.IsNotNull(localEP, "#A:Client:LocalEndpoint");
                Assert.AreEqual(IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
                Assert.AreEqual(AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
            }

            if (!Socket.OSSupportsIPv6)
            {
                Assert.Ignore("IPv6 not enabled.");
            }

            using (MyUdpClient client = new MyUdpClient(IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
            {
                s = client.Client;
                Assert.IsNotNull(s, "#B:Client");
                Assert.AreEqual(AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
                Assert.IsFalse(s.Connected, "#B:Client:Connected");
                Assert.IsTrue(s.IsBound, "#B:Client:IsBound");
                Assert.AreEqual(ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
                Assert.AreEqual(SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
                Assert.IsFalse(client.Active, "#B:Active");
                //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
                Assert.IsFalse(client.EnableBroadcast, "#B:EnableBroadcast");
                //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
                Assert.IsTrue(client.MulticastLoopback, "#B:MulticastLoopback");
                //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
                localEP = s.LocalEndPoint as IPEndPoint;
                Assert.IsNotNull(localEP, "#B:Client:LocalEndpoint");
                Assert.AreEqual(IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
                Assert.AreEqual(AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
                Assert.AreEqual(IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
            }
        }
Exemplo n.º 13
0
 internal void ConnectServer()
 {
     try
     {
         if (((this.client == null) || !this.client.ServerPoint.Address.ToString().Equals(Config.serverIP)) || (this.client.ServerPoint.Port != int.Parse(Config.serverPort)))
         {
             this.client = new MyUdpClient(Config.serverIP, int.Parse(Config.serverPort), int.Parse(Config.clientPort));
             this.client.MaxConnWaitTime  = int.Parse(Config.maxConnWaitTime);
             this.client.LoginOKEvent    += Login;
             this.client.ReceiveMsgEvent += getMess;
             this.client.StopEvent       += stopMess;
             this.client.ReConnectEvent  += ReConnectServer;
         }
         if (!this.client.IsConnected)
         {
             this.client.Connect(this.userName, this.userID + "." + MD5_Crypt.GetHashString32(this.userID + "saxiTAO"), Config.maxMessID);
         }
     }
     catch (Exception exception)
     {
         this.log.ErrorFormat("登录公告服务器异常:{0}", exception.ToString());
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// Creates a "channel" to the given address.
        /// This won't send any message unlike TCP.
        /// </summary>
        /// <param name="broadcast">Sets this channel to be able to broadcast.</param>
        /// <param name="handlers">The handlers to be added to the channel's pipeline.</param>
        /// <returns>The created channel or null, if the channel could not be created.</returns>
        public MyUdpClient CreateUdp(bool broadcast, IDictionary <string, IChannelHandler> handlers)
        {
            _readWriteLockUdp.EnterReadLock();
            try
            {
                if (_shutdownUdp)
                {
                    return(null);
                }
                // try to aquire resources for the channel
                if (_semaphoreUdp == null || !_semaphoreUdp.TryAcquire())
                {
                    const string errorMsg = "Tried to acquire more resources (UDP) than announced.";
                    Logger.Error(errorMsg);
                    throw new SystemException(errorMsg);
                }

                // create and bind
                var pipeline         = new Pipeline(handlers);
                var filteredPipeline = _channelClientConfiguration.PipelineFilter.Filter(pipeline, false, true);

                var udpClient = new MyUdpClient(_externalBindings.WildcardSocket(), filteredPipeline);
                _recipients.Add(udpClient);
                SetupCloseListener(udpClient, _semaphoreUdp);

                if (broadcast)
                {
                    udpClient.Socket.EnableBroadcast = true;
                }

                return(udpClient);
            }
            finally
            {
                _readWriteLockUdp.ExitReadLock();
            }
        }
 private void Start()
 {
     _udpClient = new MyUdpClient();
     _udpClient.OnReceived.Subscribe(x => Debug.Log(x.Length));
     _udpClient.AddTo(this);
 }
Exemplo n.º 16
0
        /// <summary>
        /// Sends a message via UDP.
        /// </summary>
        /// <param name="handler">The handler to deal with the response message.</param>
        /// <param name="tcsResponse">The TCS for the response message. (FutureResponse equivalent.)</param>
        /// <param name="message">The message to send.</param>
        /// <param name="channelCreator">The channel creator for the UDP channel.</param>
        /// <param name="idleUdpSeconds">The idle time of a message until fail.</param>
        /// <param name="broadcast">True, if the message is to be sent via layer 2 broadcast.</param>
        /// <returns>The response message or null, if it is fire-and-forget or a failure occurred.</returns>
        public async Task SendUdpAsync(IInboundHandler handler, TaskCompletionSource <Message.Message> tcsResponse, Message.Message message, ChannelCreator channelCreator, int idleUdpSeconds, bool broadcast)
        {
            // no need to continue if already finished
            if (tcsResponse.Task.IsCompleted)
            {
                return;
            }
            RemovePeerIfFailed(tcsResponse, message);

            bool isFireAndForget = handler == null;

            // relay options
            if (message.Sender.IsRelayed)
            {
                message.SetPeerSocketAddresses(message.Sender.PeerSocketAddresses);

                IList <PeerSocketAddress> relayAddresses = new List <PeerSocketAddress>(message.Recipient.PeerSocketAddresses);
                Logger.Debug("Send neighbor request to random relay peer {0}.", relayAddresses);
                if (relayAddresses.Count > 0)
                {
                    var relayAddress = relayAddresses[_random.NextInt(relayAddresses.Count)];
                    message.SetRecipientRelay(message.Recipient
                                              .ChangePeerSocketAddress(relayAddress)
                                              .ChangeIsRelayed(true));
                }
                else
                {
                    const string msg = "Peer is relayed, but no relay is given.";
                    Logger.Error(msg);
                    tcsResponse.SetException(new TaskFailedException(msg));
                    return;
                }
            }

            // check for invalid UDP connection to unreachable peers
            if (message.Recipient.IsRelayed && message.Command != Rpc.Rpc.Commands.Neighbor.GetNr() &&
                message.Command != Rpc.Rpc.Commands.Ping.GetNr())
            {
                string msg =
                    String.Format(
                        "Tried to send a UDP message to unreachable peers. Only TCP messages can be sent to unreachable peers: {0}.",
                        message);
                Logger.Warn(msg);
                tcsResponse.SetException(new TaskFailedException(msg));
                return;
            }

            // pipeline handler setup
            TimeoutFactory timeoutFactory = CreateTimeoutHandler(tcsResponse, idleUdpSeconds, isFireAndForget);
            var            handlers       = new Dictionary <string, IChannelHandler>();

            if (!isFireAndForget)
            {
                handlers.Add("timeout0", timeoutFactory.CreateIdleStateHandlerTomP2P());
                handlers.Add("timeout1", timeoutFactory.CreateTimeHandler());
            }
            handlers.Add("decoder", new TomP2PSinglePacketUdp(ChannelClientConfiguration.SignatureFactory));
            handlers.Add("encoder", new TomP2POutbound(false, ChannelClientConfiguration.SignatureFactory));
            if (!isFireAndForget)
            {
                handlers.Add("handler", handler);
            }

            // create UDP channel
            MyUdpClient udpClient = null;

            try
            {
                udpClient = channelCreator.CreateUdp(broadcast, handlers);
            }
            catch (Exception ex)
            {
                string msg = "Channel creation failed. " + ex;
                Logger.Debug(msg);
                tcsResponse.SetException(ex);
                // may have been closed by the other side
                // or it may have been canceled from this side
            }

            // "afterConnect"
            // check if channel could be created (due to shutdown)
            if (udpClient == null)
            {
                const string msg = "Could not create a UDP socket. (Due to shutdown.)";
                Logger.Warn(msg);
                tcsResponse.SetException(new TaskFailedException(msg));
                return;
            }
            Logger.Debug("About to connect to {0} with channel {1}, ff = {2}.", message.Recipient, udpClient, isFireAndForget);

            // send request message
            // processes client-side outbound pipeline
            // (await for possible exception re-throw, does not block)
            await udpClient.SendMessageAsync(message);

            // if not fire-and-forget, receive response
            if (isFireAndForget)
            {
                Logger.Debug("Fire and forget message {0} sent. Close channel {1} now.", message, udpClient);
                tcsResponse.SetResult(null); // set FF result
            }
            else
            {
                // receive response message
                // processes client-side inbound pipeline
                await udpClient.ReceiveMessageAsync();
            }
            udpClient.Close();
        }
Exemplo n.º 17
0
		[Test] // .ctor (IPEndPoint)
		public void Constructor4 ()
		{
			MyUdpClient client;
			Socket s;
			IPEndPoint localEP;
			IPEndPoint clientEP;

			clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
			client = new MyUdpClient (clientEP);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
			Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
			Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
			Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
			Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
		}
Exemplo n.º 18
0
		[Test] // .ctor (Int32, AddressFamily)
		public void Constructor5 ()
		{
			Socket s;
			IPEndPoint localEP;

			using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
			{
				s = client.Client;
				Assert.IsNotNull (s, "#A:Client");
				Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
				Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
				Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
				Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
				Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
				Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
				//Assert.IsFalse (client.DontFragment, "#A:DontFragment");
				Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
				//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
				Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
				//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
				localEP = s.LocalEndPoint as IPEndPoint;
				Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
				Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
				Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
			}

			using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
			{
				s = client.Client;
				Assert.IsNotNull (s, "#B:Client");
				Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
				Assert.IsFalse (s.Connected, "#B:Client:Connected");
#if NET_2_0
				Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
#endif
				Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
				Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
				Assert.IsFalse (client.Active, "#B:Active");
#if NET_2_0
				//Assert.IsFalse (client.DontFragment, "#B:DontFragment");
				Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
				//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
				Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
				//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
				localEP = s.LocalEndPoint as IPEndPoint;
				Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
				Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
				Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
				Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
			}
		}
Exemplo n.º 19
0
		[Test] // .ctor (String, Int32)
		public void Constructor6 ()
		{
			Socket s;
			IPEndPoint localEP;

			// Bug #5503
			// UDP port 0 doesn't seem to be valid.
			using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
			{
				s = client.Client;
				Assert.IsNotNull (s, "#A:Client");
				Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
				Assert.IsTrue (s.Connected, "#A:Client:Connected");
#if NET_2_0
				Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
				Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
				Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
				Assert.IsTrue (client.Active, "#A:Active");
#if NET_2_0
				Assert.IsFalse (client.DontFragment, "#A:DontFragment");
				Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
				//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
				//Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
				//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
				localEP = s.LocalEndPoint as IPEndPoint;
				Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
				Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
				Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
			}
			using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
			{
				s = client.Client;
				Assert.IsNotNull (s, "#B:Client");
				Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
				Assert.IsTrue (s.Connected, "#B:Client:Connected");
#if NET_2_0
				Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
#endif
				Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
				Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
				Assert.IsTrue (client.Active, "#B:Active");
#if NET_2_0
				Assert.IsFalse (client.DontFragment, "#B:DontFragment");
				Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
				//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
				//Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
				//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
				localEP = s.LocalEndPoint as IPEndPoint;
				Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
				Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
				Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
			}
		}