关于GLib.Application和Soup.Server的良好实践

时间:2015-06-18 18:54:14

标签: vala

我尝试使用libsoup在vala中创建一个简单的服务器。

我想知道这是从Soup.Server开始GLib.Application的好方法。由于不推荐同步使用它(run不推荐),我发现保持活着的唯一方法是保留默认应用程序。

public class Simple.Server : Soup.Server
{

    public Server () {
        Application.get_default ().hold ();
        add_handler(null, null_handler);
    }

    private void null_handler (Soup.Server server, Soup.Message message,
                               string path, HashTable<string,string>? query,
                               Soup.ClientContext client) {
        GLib.message ("path: %s", path);
        message.status_code = 404;
        message.set_response ("text/plain", Soup.MemoryUse.COPY, "".data);
    }
}

public class Simple.App : Application 
{
    private Simple.Server server;

    App () {
        Object (application_id: "org.dev.simple-server",
               flags: ApplicationFlags.FLAGS_NONE);
    }

    protected override void activate () {
        base.activate ();

        server = new Simple.Server();
        try {
            server.listen_all(8080, 0);
        }
        catch (Error e) {
            GLib.message ("Error n°%u: %s", e.code, e.message);
        }
    }

    protected override void shutdown () {
        base.shutdown ();
        server.disconnect ();
    }

    static int main (string[] args) {
        App app = new  Simple.App();
        return app.run (args);
    }
}

这模仿了我的代码。

所以这是一个问题,启动服务器是一个很好的做法,仍然使用GLib.Application,或者我应该使用(如示例所示)仅服务器,手动启动/停止MainLoop

感谢。

0 个答案:

没有答案
相关问题