private StubSocket GetSocket(SocketPurpose socketPurpose)
        {
            StubSocket socket = null;
            var retries = socketWaitRetries;
            Func<bool> socketIsMissing = () => socket == null;

            while (retries-- > 0 && socketIsMissing())
            {
                socket = sockets.FirstOrDefault(s => s.Purpose == socketPurpose)?.Socket;
                Wait(socketIsMissing);
            }
            return socket;
        }
Example #2
0
        private string SocketName(SocketPurpose Purpose)
        {
            if(Purpose == SocketPurpose.Debug)
                return "Debug";
            if(Purpose == SocketPurpose.Control)
                return "Control";

            return "";
        }
Example #3
0
        private void OnSocketException(SocketPurpose Purpose, Socket NetworkSocket, SocketException e)
        {
            if (!NetworkSocket.Connected)
            {
                Console.WriteLine("{0} connection closed (errcode: {1})", SocketName(Purpose), e.SocketErrorCode);
            }
            else
            {
                Console.WriteLine("Error on {0} connection, closing (errcode: {1})", SocketName(Purpose), e.SocketErrorCode);
            }

            NetworkSocket.Close();

            DeviceInUse = false;
            StartListen();
        }
Example #4
0
        private void ProcessFinish(ProcessData Processor, SocketPurpose Purpose, Socket NetworkSocket, IAsyncResult ar)
        {
            try
            {
                byte[] writeData = Processor.EndInvoke(ar);

                if (writeData != null)
                {
                    NetworkSocket.BeginSend(writeData, 0, writeData.Length, SocketFlags.None,
                        delegate(IAsyncResult AsR) { NetworkWrite(Processor, Purpose, NetworkSocket, writeData, 0, AsR); },
                        null);
                }
                else
                {
                    NetworkSocket.BeginReceive(networkBuf, 0, 1024, SocketFlags.None,
                            delegate(IAsyncResult AsR) { NetworkReceive(Purpose, NetworkSocket, AsR); },
                            null);
                }
            }
            catch (JTAGException e)
            {
                Console.WriteLine("JTAG Error while procesing.  {0}", e.Message);

                Disconnect();
                DeviceInUse = false;

                OnError();
            }
            catch (SocketException e)
            {
                OnSocketException(Purpose, NetworkSocket, e);
            }
            catch (ObjectDisposedException)
            {
                OnObjectDisposedException(Purpose, NetworkSocket);
            }
        }
Example #5
0
        private void OnObjectDisposedException(SocketPurpose Purpose, Socket NetworkSocket)
        {
            Console.WriteLine("Error on {0} connection, the connection was closed", SocketName(Purpose));

            NetworkSocket.Close();

            DeviceInUse = false;
            StartListen();
        }
Example #6
0
        private void NetworkWrite(ProcessData Processor, SocketPurpose Purpose, Socket NetworkSocket, byte[] Data, int offset, IAsyncResult ar)
        {
            try
            {
                int length = NetworkSocket.EndSend(ar);

                if (length + offset < Data.Length)
                {
                    NetworkSocket.BeginSend(Data, length, Data.Length - (length + offset), SocketFlags.None,
                        delegate(IAsyncResult AsR) { NetworkWrite(Processor, Purpose, NetworkSocket, Data, length + offset, AsR); },
                        null);
                }
                else
                {
                    NetworkSocket.BeginReceive(networkBuf, 0, 1024, SocketFlags.None,
                            delegate(IAsyncResult AsR) { NetworkReceive(Purpose, NetworkSocket, AsR); },
                            null);
                }
            }
            catch (SocketException e)
            {
                OnSocketException(Purpose, NetworkSocket, e);
            }
            catch (ObjectDisposedException)
            {
                OnObjectDisposedException(Purpose, NetworkSocket);
            }
        }
Example #7
0
        private void NetworkReceive(SocketPurpose Purpose, Socket NetworkSocket, IAsyncResult ar)
        {
            try
            {
                int length = NetworkSocket.EndReceive(ar);
                if (length > 0)
                {
                    ProcessData Processor = null;

                    if (Purpose == SocketPurpose.Debug)
                        Processor = debugProcessor.Process;
                    else if (Purpose == SocketPurpose.Control)
                        Processor = controlProcessor.Process;

                    if (Processor != null)
                    {
                        Processor.BeginInvoke(networkBuf, length,
                            delegate(IAsyncResult AsR) { ProcessFinish(Processor, Purpose, NetworkSocket, AsR); },
                            null);
                    }
                    else
                    {
                        throw new Exception("In network receive with invalid purpose, this should not happen");
                    }

                }
                else
                {
                    Console.WriteLine("{0} connection closed", SocketName(Purpose));

                    DeviceInUse = false;
                    StartListen();
                }
            }
            catch (SocketException e)
            {
                OnSocketException(Purpose, NetworkSocket, e);
            }
            catch (ObjectDisposedException)
            {
                OnObjectDisposedException(Purpose, NetworkSocket);
            }
        }
Example #8
0
        private void ConnectionAccept(SocketPurpose Purpose, Socket ListenSocket, IAsyncResult ar)
        {
            bool SetDeviceInUse = false;
            Socket NetworkSocket = null;

            try
            {
                NetworkSocket = ListenSocket.EndAccept(ar);
                IPEndPoint RemoteEP = (IPEndPoint)NetworkSocket.RemoteEndPoint;
                System.Console.WriteLine("{0} connected from {1}:{2}", SocketName(Purpose),
                    RemoteEP.Address, RemoteEP.Port);

                lock (this)
                {
                    if (DeviceInUse) //Something already using the device
                    {
                        System.Console.WriteLine("Device in use, closing {0} connection", SocketName(Purpose));

                        NetworkSocket.Close();
                        NetworkSocket = null;

                        CloseListenSocket(Purpose);

                        return;
                    }
                    else //Otherwise set the flag as we're using the device
                    {
                        DeviceInUse = true;
                        SetDeviceInUse = true;

                        CurrentNetworkSocket = NetworkSocket;

                        CloseListenSocket(Purpose);
                    }
                }

                NetworkSocket.BeginReceive(networkBuf, 0, 1024, SocketFlags.None,
                    delegate(IAsyncResult AsR){NetworkReceive(Purpose, NetworkSocket, AsR);},
                    null);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Error accepting connection from {0} (errcode: {1})", SocketName(Purpose), e.SocketErrorCode);
                CloseListenSocket(Purpose);

                if (NetworkSocket != null)
                    NetworkSocket.Close();

                if (SetDeviceInUse)
                    DeviceInUse = false;

                StartListen();
            }
            catch (ObjectDisposedException)
            {
                Console.WriteLine("Error accepting connection from {0}, the socket was closed", SocketName(Purpose));
                CloseListenSocket(Purpose);

                if (NetworkSocket != null)
                    NetworkSocket.Close();

                if (SetDeviceInUse)
                    DeviceInUse = false;

                StartListen();
            }
        }
Example #9
0
 private void CloseListenSocket(SocketPurpose Purpose)
 {
     if (Purpose == SocketPurpose.Debug)
         CloseDebug();
     else if (Purpose == SocketPurpose.Control)
         CloseControl();
 }