private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false) { int port; Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port)) { // Send a few packets, in case they aren't delivered reliably. for (int i = 0; i < Configuration.UDPRedundancy; i++) { using (ManualResetEvent waitHandle = new ManualResetEvent(false)) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.RemoteEndPoint = new IPEndPoint(connectTo, port); args.SetBuffer(new byte[1], 0, 1); args.UserToken = waitHandle; args.Completed += AsyncCompleted; bool async = client.SendToAsync(args); if (async) { Assert.True(waitHandle.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection"); } Assert.Equal(1, args.BytesTransferred); if (args.SocketError != SocketError.Success) { throw new SocketException((int)args.SocketError); } } } bool success = server.WaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }
private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer) { int port; ManualResetEvent waitHandle = new ManualResetEvent(false); Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port)) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.RemoteEndPoint = new IPEndPoint(connectTo, port); args.SetBuffer(new byte[1], 0, 1); args.UserToken = waitHandle; args.Completed += AsyncCompleted; bool async = client.SendToAsync(args); if (async) { Assert.True(waitHandle.WaitOne(5000), "Timeout while waiting for connection"); } Assert.Equal(1, args.BytesTransferred); if (args.SocketError != SocketError.Success) { throw new SocketException((int)args.SocketError); } bool success = server.WaitHandle.WaitOne(Configuration.FailingTestTimeout); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }
private void init(string ip, int port) { // udp_Server = new SocketUdpServer(new IPEndPoint( IPAddress.Parse( "192.168.10.225"), 8888)); Debug.Log($"udpserver:{ip}:{port}"); try { udp_Server = new SocketUdpServer(new IPEndPoint(IPAddress.Parse(ip), port)); udp_Server.Start(); } catch (Exception e) { Debug.Log($"udpserver启动失败:{e.Message} "); } }
private void DualModeBeginSendTo_EndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false) { int port; Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port)) { // Send a few packets, in case they aren't delivered reliably. for (int i = 0; i < Configuration.UDPRedundancy; i++) { IAsyncResult async = client.BeginSendTo(new byte[1], 0, 1, SocketFlags.None, new IPEndPoint(connectTo, port), null, null); int sent = client.EndSendTo(async); Assert.Equal(1, sent); } bool success = server.WaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }
private void DualModeSendTo_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false) { int port; Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(listenOn, dualModeServer, out port)) { int sent = client.SendTo(new byte[1], new IPEndPoint(connectTo, port)); Assert.Equal(1, sent); bool success = server.WaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }
private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer) { int port; ManualResetEvent waitHandle = new ManualResetEvent(false); Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(listenOn, dualModeServer, out port)) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.RemoteEndPoint = new IPEndPoint(connectTo, port); args.SetBuffer(new byte[1], 0, 1); args.UserToken = waitHandle; args.Completed += AsyncCompleted; bool async = client.SendToAsync(args); if (async) { Assert.True(waitHandle.WaitOne(5000), "Timeout while waiting for connection"); } Assert.Equal(1, args.BytesTransferred); if (args.SocketError != SocketError.Success) { throw new SocketException((int)args.SocketError); } bool success = server.WaitHandle.WaitOne(100); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }
private void DualModeBeginSendTo_EndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer) { int port; Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(listenOn, dualModeServer, out port)) { IAsyncResult async = client.BeginSendTo(new byte[1], 0, 1, SocketFlags.None, new IPEndPoint(connectTo, port), null, null); int sent = client.EndSendTo(async); Assert.Equal(1, sent); bool success = server.WaitHandle.WaitOne(100); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }
private void OnDisable() { udp_Server.Stop(); udp_Server = null; }
private void DualModeSendTo_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false) { int port; Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp); using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port)) { // Send a few packets, in case they aren't delivered reliably. for (int i = 0; i < (expectedToTimeout ? 1 : TestSettings.UDPRedundancy); i++) { int sent = client.SendTo(new byte[1], new IPEndPoint(connectTo, port)); Assert.Equal(1, sent); } bool success = server.WaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout); // Make sure the bytes were received if (!success) { throw new TimeoutException(); } } }