public void TFBasicLineTimeout() { NamedPortEventRaiser.VirtualPortEvent += VirtualPortEventRaiser_VirtualPortEvent; try { keepServerRunning = true; socketServer = SocketServerHelper.Start(tcpPort); Task.Delay(TimeSpan.FromSeconds(1)).Wait(); using (var tcpIpClient = new VaiTcpIpClient()) { var ipAddress = "127.0.0.1"; Assert.True(tcpIpClient.ConnectAsync(IPAddress.Parse(ipAddress), tcpPort).Result); int timeoutLenSec = 2; Task.Run(() => CheckForClientsAsync(timeoutLenSec)); // wait 1 extra second to be sure Task.Delay(TimeSpan.FromSeconds((timeoutLenSec + 1))).Wait(); Assert.True(timeoutHasTakenPlace); } } finally { keepServerRunning = false; SocketServerHelper.Stop(cancellation, socketServer); Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\tTest Finished."); } }
public void WriteToSocketServer() { const int tcpPort = 10097; TcpListener socketServer = null; try { socketServer = SocketServerHelper.Start(tcpPort); var ipAddress = "127.0.0.1"; SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "Testing."); for (var testRun = 0; testRun < 2; testRun++) { using (var tcpIpClient = new VaiTcpIpClient()) { Assert.True(tcpIpClient.ConnectAsync(IPAddress.Parse(ipAddress), tcpPort).Result); for (var i = 0; i < numSentMessages; i++) { var message = "Sample data " + i; Assert.True(tcpIpClient.Write(message)); Task.Delay(TimeSpan.FromMilliseconds(100)).Wait(); } Assert.True(tcpIpClient.Disconnect()); } } } finally { SocketServerHelper.Stop(null, socketServer); } }
/// <summary> /// Send some messages at even intervals using new tcpClient /// </summary> private async void ConnectWithNewTcpClientAsync() { IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); using (var tcpIpClient = new VaiTcpIpClient()) { bool ok = await tcpIpClient.ConnectAsync(ipAddress, tcpPort); Debug.WriteLine("Client connected."); } }
/// <summary> /// Send some messages at even intervals using new tcpClient /// </summary> private async void SendTestDataWithNewTcpClientAsync(int tcpPort) { IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // DnsWrapper.GetPrimaryIp; using (var tcpIpClient = new VaiTcpIpClient()) { bool ok = await tcpIpClient.ConnectAsync(ipAddress, tcpPort); Debug.Assert(ok, "Failed to connect to server"); for (var i = 0; i < numSentMessages; i++) { var msg = i + "ABC"; ok = tcpIpClient.Write(msg); Debug.Assert(ok, "Failed to send message:" + msg); Debug.WriteLine("Sending:" + msg); Task.Delay(TimeSpan.FromMilliseconds(500)).Wait(); } Debug.WriteLine("End send data."); } }
public void TestAppendAndParse() { NamedPortEventRaiser.VirtualPortEvent += VirtualPortEventRaiser_VirtualPortEvent; var stopWatch2 = new Stopwatch(); var tcpIpClient = new VaiTcpIpClient(); var portDataParser = new PortDataParser(tcpIpClient); stopWatch2.Start(); RunNonCheckSumTests(portDataParser, portDataParser.AppendAndParse); stopWatch2.Stop(); Debug.WriteLine("Stopwatch2:" + stopWatch2.ElapsedMilliseconds); portDataParser.UseCrc32 = true; stopWatch2.Start(); RunCheckSumTests(portDataParser, portDataParser.AppendAndParse); stopWatch2.Stop(); Debug.WriteLine("Stopwatch2:" + stopWatch2.ElapsedMilliseconds); }
/// <summary> /// Start listening to incoming connections with timeout = 'timeoutLenSec' /// Timeout event should take place after 'timeoutLenSec' seconds /// </summary> /// <returns></returns> private async Task <bool> CheckForClientsAsync(int timeoutLenSec) { TcpClient tcpClient = null; VaiListener listener = null; try { while (keepServerRunning && !cancellation.IsCancellationRequested) { // If you don't check for pending messages, // and there have been at least one active connection, // AcceptTcpClientAsync will throw a "cannot used disposed object exception" // when tcpServer stops. if (!socketServer.Pending()) { Thread.Sleep(0); continue; } //Accept the pending client connection and return a TcpClient object initialized for communication. tcpClient = await socketServer.AcceptTcpClientAsync().WithWaitCancellation(cancellation.Token); var tcpIpClient = new VaiTcpIpClient(tcpClient); var portDataParser = new PortDataParser(tcpIpClient); listener = new VaiListener(tcpIpClient, portDataParser, timeoutLenSec); var t1 = new Task(() => listener.StartListening()); t1.Start(); } return(true); } // this exception may occur if there have been open connections catch (ObjectDisposedException ex) { Debug.WriteLine("Exception in CheckForClient." + ex.Message); Debug.Assert(true, ex.Message); return(false); } // this exception may occur if there have been open connections catch (OperationCanceledException ex) { Debug.Assert(true, ex.Message); Debug.WriteLine("Exception in CheckForClient." + ex.Message); return(false); } catch (Exception ex) { Debug.WriteLine("Exception in CheckForClient." + ex.Message); Debug.Assert(false, ex.Message); return(false); } finally { listener?.Dispose(); if (!keepServerRunning) { Debug.WriteLine("CheckForClient finally. keepServerRunning:" + keepServerRunning); } } }