SHARE
TWEET

libuv

a guest Aug 24th, 2016 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <uv.h>
  6.  
  7. #include "s7/s7.h"
  8.  
  9. #define DEFAULT_PORT 7771
  10. #define DEFAULT_BACKLOG 128
  11.  
  12.  
  13. uv_loop_t *loop;
  14. FILE *memstream;
  15.  
  16.  
  17. static void s7Print(s7_scheme *sc, unsigned char c, s7_pointer port) {
  18.   fputc(c, memstream);
  19. }
  20.  
  21. void read_cb(uv_stream_t * stream, ssize_t nread, const uv_buf_t *buf) {
  22.   /* dynamically allocate memory for a new write task */
  23.   uv_write_t * req = (uv_write_t *) malloc(sizeof(uv_write_t));
  24.    
  25.   /* if read bytes counter -1 there is an error or EOF */
  26.   if (nread == -1) {
  27.     fprintf(stderr, "Error on reading client stream.\n");
  28.     uv_close((uv_handle_t *) stream, NULL);
  29.   }
  30.  
  31.   uv_buf_t outBuffer;
  32.   memstream = open_memstream(&(outBuffer.base), &(outBuffer.len));
  33.  
  34.   s7_scheme *s7;
  35.   s7 = s7_init();
  36.   s7_set_current_output_port(s7, s7_open_output_function(s7, s7Print));
  37.   s7_eval_c_string(s7, buf->base);
  38.  
  39.   /* Write sync the output buffer to the socket */
  40.   fflush(memstream);
  41.   int r = uv_write(req, stream, &outBuffer, 1, NULL);
  42.  
  43.   if (r < 0)
  44.     fprintf(stderr, "Error on writing client stream.\n");
  45.  
  46.   /* free the remaining memory */
  47.   free(buf->base);
  48. }
  49.  
  50. void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
  51.     buf->base = malloc(suggested_size);
  52.     buf->len = suggested_size;
  53. }
  54.  
  55. void connection_cb(uv_stream_t * stream, int status) {
  56.   if (status < 0) {
  57.     fprintf(stderr, "New connection error %s\n", uv_strerror(status));
  58.     return;
  59.   }
  60.  
  61.   uv_tcp_t client;
  62.   uv_tcp_init(loop, &client);
  63.   if (uv_accept(stream, (uv_stream_t*)&client) == 0) {
  64.     uv_read_start((uv_stream_t*)&client, alloc_cb, read_cb);
  65.     uv_write_t request;
  66.     uv_buf_t prompt = { .base = "sonodi>", .len = 7 };
  67.  
  68.     if (uv_is_writable(stream))
  69.       uv_write(&request, stream, &prompt, 1, NULL);
  70.   }
  71.   else {
  72.     uv_close((uv_handle_t*)&client, NULL);
  73.   }
  74. }
  75.  
  76. int listenForConnections() {
  77.   loop = uv_default_loop();
  78.  
  79.   uv_tcp_t server;
  80.   uv_tcp_init(loop, &server);
  81.  
  82.   struct sockaddr_in addr;
  83.   uv_ip4_addr("127.0.0.1", DEFAULT_PORT, &addr);
  84.  
  85.   uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
  86.   int r = uv_listen((uv_stream_t*) &server, DEFAULT_BACKLOG, connection_cb);
  87.   if (r) {
  88.     fprintf(stderr, "Listen error %s\n", uv_strerror(r));
  89.     return 1;
  90.   }
  91.   return uv_run(loop, UV_RUN_DEFAULT);
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top