source: trunk/tools/neercs/old/mini-server.c @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net>
4 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                2008-2010 Pascal Terjan <pterjan@linuxfr.org>
6 *                All Rights Reserved
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://www.wtfpl.net/ for more details.
13 */
14
15#if defined HAVE_CONFIG_H
16#   include "config.h"
17#endif
18
19#include <stdio.h> /* BUFSIZ */
20#include <string.h> /* strncmp() */
21
22#include "mini-neercs.h"
23#include "mini-socket.h"
24
25static nrx_socket_t *insock, *outsock;
26
27void server_init(void)
28{
29while (!insock)
30    insock = socket_open("/tmp/neercs.sock", 1);
31}
32
33int server_step(void)
34{
35    char buf[BUFSIZ];
36    ssize_t bytes;
37    int ret;
38
39    if (outsock)
40    {
41        ret = socket_select(outsock, 1000);
42        if (ret <= 0)
43            goto nothing;
44
45        bytes = socket_read(outsock, buf, BUFSIZ);
46        if (bytes <= 0)
47            goto nothing;
48    }
49nothing:
50
51    ret = socket_select(insock, 1000);
52    if (ret <= 0)
53        return 1;
54
55    bytes = socket_read(insock, buf, BUFSIZ);
56    if (bytes <= 0)
57        return 1;
58
59    /* Parse message */
60    if (!strncmp(buf, "CONNECT ", strlen("CONNECT ")))
61    {
62        outsock = socket_open(buf + strlen("CONNECT "), 0);
63        socket_puts(outsock, "OK");
64    }
65    else if (!strncmp(buf, "QUIT ", strlen("QUIT ")))
66    {
67        return 0;
68    }
69
70    return 1;
71}
72
73void server_fini(void)
74{
75    if (insock)
76        socket_close(insock);
77    if (outsock)
78        socket_close(outsock);
79}
80
Note: See TracBrowser for help on using the repository browser.