Esempio n. 1
0
        private void RunCheckSumTests(PortDataParser portDataParser, Func <string, bool> testMethod)
        {
            expectedData.Clear();
            expectedData.Add(string.Empty);
            expectedData.Add(string.Empty);


            expectedData[0] = "(test message)12345678\r\n";
            var ok = testMethod(expectedData[0]);

            Assert.True(ok);

            ok = testMethod(expectedData[0]);
            Assert.True(ok);

            expectedData[0] = "(test message)12345678\r\n";
            expectedData[1] = ")";
            ok = testMethod(")(test message)12345678\r\n");
            Assert.True(ok);

            expectedData[0] = "";
            expectedData[1] = "";

            expectedData[0] = "(test message)12345678\r\n";
            ok = testMethod("(test mess");
            ok = testMethod("age)12345678\r\n sdfsdf ( te");
            Assert.True(ok);

            expectedData[0] = " sdfsdf ( test message 2)12345678\r\n";
            ok = testMethod("st message 2)12345678\r\nsdffsadfsda");
            Assert.True(ok);

            portDataParser.ClearDataReceptionBuffer();

            expectedData[0] = ")12345678\r\n";
            expectedData[1] = "(2134)12345678\r\n";
            ok = testMethod(")12345678\r\n");
            Assert.True(ok);
            ok = testMethod("(2");
            Assert.True(ok);
            ok = testMethod("134)12345678\r\n");
            Assert.True(ok);
        }
 void MultiClienTCPPort_DataReadEvent(object sender, byte[] data)
 {
     try
     {
         string         message = ASCIIEncoding.ASCII.GetString(data);
         PortDataParser parser  = new PortDataParser();
         if (parser.GetRequestType(message) == RequestType.HTTPGet)
         {
             message = parser.ParseGetMethodData(message);
             int dataIndex = message.IndexOf("?");
             if (dataIndex >= 0)
             {
                 dataIndex++;
             }
             if (dataIndex > 0)
             {
                 message = message.Substring(dataIndex);
             }
             if (!message.Equals("/favicon.ico"))
             {
                 message = message.Replace("%20", " ");
             }
             TcpClient     client  = sender as TcpClient;
             NetworkStream stream  = client.GetStream();
             byte[]        respons = ASCIIEncoding.ASCII.GetBytes("+OK");
             stream.Write(respons, 0, respons.Length);
             stream.Close();
         }
         if (message != null && !message.Equals("/favicon.ico"))
         {
             logger.Info("Data received : " + message);
         }
         PortServiceHelper.UpdateCallDetails(message);
     }
     catch (Exception ex)
     {
         logger.Error("Error Occurred as " + ex.Message);
     }
 }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        /// <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);
                }
            }
        }