예제 #1
0
        public void SetupTestException()
        {
            CameraSocket testSocket = new CameraSocket
            {
                DataSocket = new WSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
                Config     = new CameraConfiguration()
            };

            bool threw = false;

            try
            {
                testSocket.Setup();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.IsTrue(e.Message.Equals("Configuration address not configured"));
                threw = true;
            }

            Assert.IsTrue(threw);
            threw = false;

            IPAddress address = NetworkHelpers.GrabIpv4();

            testSocket.Config.Address = address.Address;
            try
            {
                testSocket.Setup();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.IsTrue(e.Message.Equals("Configuration port not configured"));
                threw = true;
            }
            Assert.IsTrue(threw);

            testSocket.Config.Port = 700;
            try
            {
                Assert.IsFalse(testSocket.Setup());
            }
            catch (Exception)
            {
                Assert.Fail("Shouldn't throw an exception");
            }
        }
예제 #2
0
        //[Test]
        public void CameraThreadConnectionTest()
        {
            Thread camServer = new Thread(new Listener().StartListening);

            camServer.Name = "CameraServer";
            camServer.Start();
            CameraConfiguration testConfig = new CameraConfiguration
            {
#pragma warning disable 618
                Address = NetworkHelpers.GrabIpv4(Dns.GetHostEntry(Dns.GetHostName())).Address,
#pragma warning restore 618
                Port            = 11003,
                CamFileIdentity = "testCam",
                Id = -1
            };

            CameraSocket testSocket = new CameraSocket
            {
                DataSocket = new WSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
                Config     = testConfig
            };

            //start initialising the camera thread
            Assert.IsTrue(testSocket.Setup());
            CameraThread threadTask = new CameraThread(testSocket);
            Thread       camThread  = new Thread(threadTask.Start);

            camThread.Name = "CameraThread";
            camThread.Start();

            //Tell the camera to send a test image
            threadTask.Request = CameraRequest.SendTestImage;
            Thread.Sleep(32);
            while (threadTask.Request != CameraRequest.Alive)
            {
                Thread.Sleep(10);
            }

            camThread.Join();
            camServer.Join();
        }
예제 #3
0
        public void StartListening()
        {
            // Data buffer for incoming data.
            byte[] bytes = new byte[Constants.CameraBufferSize];

            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = NetworkHelpers.GrabIpv4(ipHostInfo);
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, int.Parse(CameraSettings.GetSetting("port", "11003")));

            Console.WriteLine("IP address = " + ipAddress);
#pragma warning disable CS0618 // Type or member is obsolete
            Console.WriteLine("IP long address = " + ipAddress.Address);
#pragma warning restore CS0618 // Type or member is obsolete

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket handler  = null;

            // Bind the socket to the local endpoint and
            // listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    // Program is suspended while waiting for an incoming connection.
                    handler = listener.Accept();
                    data    = null;
                    Console.WriteLine("Connected!!");

                    while (Connected(handler))
                    {
                        RequestProcess process = new RequestProcess(handler);
                        bytes = new byte[Constants.CameraBufferSize];
                        int bytesRec = handler.Receive(bytes);
                        data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                        if (data.IndexOf(Constants.EndOfMessage) > -1)
                        {
                            //process data
                            process.ProcessRequest(bytes);

                            // Show the data on the console.
                            Console.WriteLine("Data received : {0}", data);
                            data = "";
                        }
                        Console.WriteLine("Waiting for next request...");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                if (handler != null)
                {
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }

            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();
        }