Ejemplo n.º 1
0
        private string SendMessage(string msg, UpDevice target, bool waitForResponse = true)
        {
            UpNetworkInterface netInt         = GetAppropriateInterface(target);
            string             networkAddress = netInt.networkAddress;
            string             networkType    = netInt.netType;

            ClientConnection con = OpenActiveConnection(networkAddress, networkType);

            if (con == null)
            {
                logger.LogWarning("Not possible to stablish a connection with '" + networkAddress + "' of type '" + networkType + "'.");
                return(null);
            }

            try
            {
                byte[] data = Encoding.UTF8.GetBytes(msg);
                con.Write(data, 0, data.Length);


                // Gets response.
                string response = null;
                if (waitForResponse)
                {
                    data = con.Read();
                    if (data != null)
                    {
                        response = Encoding.UTF8.GetString(data);
                        if (response.Trim().Length == 0)
                        {
                            response = null;
                        }
                    }
                }
                con.Close();
                return(response);
            }
            catch (System.Exception)
            {
                if (con.connected)
                {
                    con.Close();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
            private void ConnectionThread()
            {
                ClientConnection con = null;

                while (gatewayServer.running)
                {
                    try
                    {
                        // This blocks the thread until some client connects...
                        con = gatewayServer.gateway.OpenPassiveConnection(device.networkDeviceName, device.networkDeviceType);
                        if (con != null)
                        {
                            gatewayServer.PushLog(
                                "Connection received from an ubiquitos-client device: '" +
                                con.clientDevice.networkDeviceName + "' on '" + con.clientDevice.networkDeviceType + "'.");
                        }
                        else
                        {
                            throw new System.Exception("Couldn't establish connection to client.");
                        }

                        if (con.connected)
                        {
                            StringBuilder builder = new StringBuilder();
                            byte[]        data    = con.Read();
                            if ((data != null) && (data.Length > 0))
                            {
                                int      read     = data.Length;
                                string[] msgs     = Encoding.UTF8.GetString(data, 0, read).Split('\n');
                                int      last     = msgs.Length - 1;
                                int      i        = 0;
                                byte     lastByte = data[read - 1];

                                // Were we waiting for the rest of a message?
                                if (builder.Length > 0)
                                {
                                    builder.Append(msgs[i++]);
                                    if ((msgs.Length > 1) || (lastByte == '\n'))
                                    {
                                        gatewayServer.HandleMessage(builder.ToString(), con);
                                        builder.Length = 0;
                                    }
                                }

                                // Processes intermediate messages...
                                for (; i < last; ++i)
                                {
                                    gatewayServer.HandleMessage(msgs[i], con);
                                }

                                // Processes the last chunk...
                                if (i == last)
                                {
                                    builder.Append(msgs[i]);
                                    if (lastByte == '\n')
                                    {
                                        gatewayServer.HandleMessage(builder.ToString(), con);
                                        builder.Length = 0;
                                    }
                                }
                            }
                            con.Close();
                        }
                    }
                    catch (System.Threading.ThreadAbortException)
                    {
                        if ((con != null) && (con.connected))
                        {
                            con.Close();
                        }
                        return;
                    }
                    catch (System.Exception e)
                    {
                        if ((con != null) && (con.connected))
                        {
                            con.Close();
                        }
                        gatewayServer.PushLog("Failed to handle ubiquitos-smartspace connection. ", e);
                    }
                }
            }