Exemplo n.º 1
0
        protected override void SocketListenerThreadStart()
        {
            int size = 0;

            Byte[] byteBuffer = new Byte[1024];

            try { m_clientSocket.ReceiveTimeout = 500; } catch (Exception) { }
            while (!m_stopClient)
            {
                try
                {
                    size = m_clientSocket.Receive(byteBuffer);
                    if (size == 0)
                    {
                        // Indicates the remote side closed the connection
                        break;
                    }
                }
                catch (SocketException e)
                {
                    if (e.ErrorCode == WSAETIMEDOUT)
                    {
                        // Timeout while waiting for shutdown
                        continue;
                    }
                    else
                    {
                        ORTLog.LogS(string.Format("ORTCommand Exception in Receive {0}", e.ToString()));
                        break;
                    }
                }
                catch (Exception e)
                {
                    ORTLog.LogS(string.Format("ORTCommand Exception in Receive {0}", e.ToString()));
                    break;
                }

                // Get a string representation from the socket buffer
                string data = Encoding.ASCII.GetString(byteBuffer, 0, size);
                if (!IsValidIpsData(data))
                {
                    ORTLog.LogS(String.Format("ORTCommand: Invalid data={0}", CleanString(data)));
                    break;
                }

                string customer = "";
                string device   = "";
                string command  = "";
                try
                {
                    // Parse the customer+device
                    customer = data.Split(null)[1];
                    device   = data.Split(null)[2];

                    // Parse the command - hacky :/
                    int i = data.IndexOf(" ", data.IndexOf(" ", data.IndexOf(" ") + 1) + 1) + 1;
                    command = data.Substring(i);

                    // Remove the carriage return and/or line feed
                    command = Regex.Replace(command, @"\r\n?|\n", "");
                }
                catch (Exception)
                {
                    ORTLog.LogS(String.Format("ORTCommand: Invalid data={0}", CleanString(data)));
                    break;
                }

                ORTLog.LogS(string.Format("ORTCommand: customer={0} device={1} command={2}", customer, device, command));
                string key = GetKey(customer, device);

                // Get the cooresponding socket and send the command
                DeviceListener d = SharedMem.Get(key);
                if (d != null)
                {
                    try
                    {
                        if (d.Send(Encoding.ASCII.GetBytes(command)) == 0)
                        {
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        ORTLog.LogS(string.Format("ORTCommand Exception {0}", e.ToString()));
                        break;
                    }
                }
                else
                {
                    ORTLog.LogS(string.Format("ORTCommand: customer device combination not found: {0}", key));
                }
            }

            ORTLog.LogS(String.Format("ORTCommand: Connection dropped {0}", this.ToString()));

            try { m_clientSocket.Shutdown(SocketShutdown.Both); } catch (Exception) { }
            m_clientSocket.Close();
            m_markedForDeletion = true;
        }
Exemplo n.º 2
0
        protected override void SocketListenerThreadStart()
        {
            Byte[] byteBuffer = new Byte[1024];
            int    size       = 0;

            try
            {
                size = m_clientSocket.Receive(byteBuffer);
            }
            catch (Exception e)
            {
                ORTLog.LogS(string.Format("ORTDevice Exception in Receive {0}", e.ToString()));
                m_clientSocket.Close();
                m_markedForDeletion = true;
                return;
            }

            string data = Encoding.ASCII.GetString(byteBuffer, 0, size);

            if (!IsValidIpsData(data))
            {
                ORTLog.LogS(String.Format("ORTDevice: Invalid data={0}", CleanString(data)));
                ORTLog.LogS(String.Format("ORTDevice: Connection dropped {0}", this.ToString()));
                try { m_clientSocket.Shutdown(SocketShutdown.Both); } catch (Exception) { }
                m_clientSocket.Close();
                m_markedForDeletion = true;
                return;
            }

            string customer = "";
            string device   = "";
            string ipAddr   = "";

            try
            {
                // Parse the customer+device
                customer = data.Split(null)[1];
                device   = data.Split(null)[2];
                ipAddr   = data.Split(null)[3];
            }
            catch (Exception)
            {
                ORTLog.LogS(String.Format("ORTDevice: Invalid data={0}", CleanString(data)));
                ORTLog.LogS(String.Format("ORTDevice: Connection dropped {0}", this.ToString()));
                try { m_clientSocket.Shutdown(SocketShutdown.Both); } catch (Exception) { }
                m_clientSocket.Close();
                m_markedForDeletion = true;
                return;
            }

            string key = GetKey(customer, device);

            // Check if this key already exists
            DeviceListener d = SharedMem.Get(key);

            if (d != null)
            {
                // Detected duplicate
                ORTLog.LogS(String.Format("ORTDevice: Detected duplicate customer={0} device={1}", customer, device));

                // Remove existing key
                ORTLog.LogS(String.Format("ORTDevice: Remove listener for customer={0} device={1}", customer, device));
                SharedMem.Remove(key);

                // Shutdown the existing (zombie) listener
                d.StopSocketListener();
            }

            ORTLog.LogS(String.Format("ORTDevice: Add listener for customer={0} device={1} localIpAddr={2}", customer, device, ipAddr));
            if (!SharedMem.Add(key, this))
            {
                ORTLog.LogS(String.Format("ORTDevice: Unable to add key={0}", key));
                ORTLog.LogS(String.Format("ORTDevice: Connection dropped {0}", this.ToString()));
                try { m_clientSocket.Shutdown(SocketShutdown.Both); } catch (Exception) { }
                m_clientSocket.Close();
                m_markedForDeletion = true;
                return;
            }

            // Block forever waiting for the connection to terminate
            while (!m_stopClient)
            {
                bool poll = false;
                try
                {
                    poll = m_clientSocket.Poll(100000, SelectMode.SelectRead); // 100ms
                }
                catch (Exception e)
                {
                    ORTLog.LogS(string.Format("ORTDevice Exception in Poll {0}", e.ToString()));
                    break;
                }

                if (poll)
                {
                    // Connection has been closed, reset, or terminated
                    break;
                }
            }

            if (SharedMem.Remove(key))
            {
                ORTLog.LogS(String.Format("ORTDevice: Removed listener for customer={0} device={1}", customer, device));
            }

            ORTLog.LogS(String.Format("ORTDevice: Connection dropped {0}", this.ToString()));

            try { m_clientSocket.Shutdown(SocketShutdown.Both); } catch (Exception) { }
            m_clientSocket.Close();
            m_markedForDeletion = true;
        }