0

I'm creating a smart home server and I want to support Google Smart Home actions. The server app is written in C# using HttpListener and I'm using Mono 5.18 (5.20 version have problems with httpcfg) to run it on Debian 10 server. The server app is working correctly but 1 out of 5 queries isn't received on program. Tcpdump is showing some type of traffic but app doesn't get any.

I tried reinstalling Debian 2 times, using different mono version, changing port, running it on Windows 10, disabling the part of the code that it provides MQTT and MySQL support, disabling firewall and nothing happen. The main problem is that Google Server isn't sending any packets after only one fail and I must disconnect and reconnect my devices in Google Home App.

There is my code with HttpListner:

static HttpListener listener;
//...

static void Main(string[] args)
{
        //...
        HttpServiceMain();
}

//...

private static void HttpServiceMain()
{

    listener = new HttpListener();
    listener.Prefixes.Add("https://*:2030/");
    listener.Start();

    while (true)
    {
        ProcessRequest();                               
    }
}

static void ProcessRequest()
{
    var result = listener.BeginGetContext(ListenerCallback, listener);
    var startNew = Stopwatch.StartNew();
    result.AsyncWaitHandle.WaitOne();
    startNew.Stop();
    Console.WriteLine("Elapsed miliseconds: " + startNew.ElapsedMilliseconds);
}

static void ListenerCallback(IAsyncResult ar)
{
    Console.WriteLine("Listening...");
    HttpListenerContext context = listener.EndGetContext(ar);
    HttpListenerRequest request = context.Request;

    string documentContents;

    using (Stream receiveStream = request.InputStream)
    {
        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
        {
            documentContents = readStream.ReadToEnd();
        }
    }

    string responseString = "{}";

    //Creating response and exporting it to 'responseString'
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    HttpListenerResponse httpResponse = context.Response;
    httpResponse.StatusCode = 200;
    httpResponse.StatusDescription = "OK";
    httpResponse.ContentLength64 = buffer.Length;
    System.IO.Stream output = httpResponse.OutputStream;
    output.Write(buffer, 0, buffer.Length);
    httpResponse.Close();
}

That I said, everything is working fine in 4 out of 5 times, but after some requests server didn't get query and I must reconnect Google Home App with my service. It is bug in HttpListener, in my code or in Google Server? Have you any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

The fact that you didn’t get all the results while implementing the QUERY intent means that there is some problem while communicating with the server. You can try to troubleshoot following the Troubleshooting Guide, to see if we are sending requests to your server.

When you don’t get requests as you expected, the other culprit might also be the OAuth implementation, as you found out. If Google does not get valid Access Tokens for users, it might not send all the requests to your server.

Anish Yadav
  • 141
  • 4