[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(); }
[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 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); }
[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(); }
[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"); Assert.IsFalse (s.IsBound, "#A:Client:IsBound"); 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"); //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"); if (!Socket.OSSupportsIPv6) 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"); Assert.IsFalse (s.IsBound, "#A:Client:IsBound"); 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"); //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"); client.Close (); }
/// <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(); }