You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
1.8 KiB

  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://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #if defined HAVE_CONFIG_H
  15. # include "config.h"
  16. #endif
  17. #include <stdio.h> /* BUFSIZ */
  18. #include <string.h> /* strncmp() */
  19. #include "mini-neercs.h"
  20. #include "mini-socket.h"
  21. static nrx_socket_t *insock, *outsock;
  22. void server_init(void)
  23. {
  24. while (!insock)
  25. insock = socket_open("/tmp/neercs.sock", 1);
  26. }
  27. int server_step(void)
  28. {
  29. char buf[BUFSIZ];
  30. ssize_t bytes;
  31. int ret;
  32. if (outsock)
  33. {
  34. ret = socket_select(outsock, 1000);
  35. if (ret <= 0)
  36. goto nothing;
  37. bytes = socket_read(outsock, buf, BUFSIZ);
  38. if (bytes <= 0)
  39. goto nothing;
  40. }
  41. nothing:
  42. ret = socket_select(insock, 1000);
  43. if (ret <= 0)
  44. return 1;
  45. bytes = socket_read(insock, buf, BUFSIZ);
  46. if (bytes <= 0)
  47. return 1;
  48. /* Parse message */
  49. if (!strncmp(buf, "CONNECT ", strlen("CONNECT ")))
  50. {
  51. outsock = socket_open(buf + strlen("CONNECT "), 0);
  52. socket_puts(outsock, "OK");
  53. }
  54. else if (!strncmp(buf, "QUIT ", strlen("QUIT ")))
  55. {
  56. return 0;
  57. }
  58. return 1;
  59. }
  60. void server_fini(void)
  61. {
  62. if (insock)
  63. socket_close(insock);
  64. if (outsock)
  65. socket_close(outsock);
  66. }