Advertisement
Guest User

libev-basedserver

a guest
Apr 4th, 2013
189
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <netinet/in.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/un.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ev.h>
  11. #include <pthread.h>
  12. #include <mysql.h>
  13. #include <unistd.h>
  14. #include <syslog.h>
  15. #include <signal.h>
  16. #include <fcntl.h>
  17.  
  18. int sd;
  19. int total_clients = 0;
  20. int exit_dsn_flag = 0;
  21.  
  22. int setnonblock(int fd);
  23. void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents);
  24. int open_evsocket(int *sd);
  25. void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents);
  26. void *socket_thread_start(void *x);
  27.  
  28. int setnonblock(int fd) {
  29.     int flags;
  30.  
  31.     flags = fcntl(fd, F_GETFL);
  32.     flags |= O_NONBLOCK;
  33.     return fcntl(fd, F_SETFL, flags);
  34. }
  35.  
  36. /* Accept client requests */
  37. void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
  38.     struct sockaddr_in client_addr;
  39.     socklen_t client_len = sizeof (client_addr);
  40.     int client_sd;
  41.     //struct ev_io watch_client
  42.     struct ev_io *w_client = (struct ev_io*) malloc(sizeof (struct ev_io));
  43.  
  44.     if (EV_ERROR & revents) {
  45.         perror("got invalid event");
  46.         return;
  47.     }
  48.  
  49.     // Accept client request
  50.     client_sd = accept(watcher->fd, (struct sockaddr *) & client_addr, &client_len);
  51.  
  52.     if (client_sd < 0) {
  53.         perror("accept error");
  54.         return;
  55.     }
  56.  
  57.     total_clients++; // Increment total_clients count
  58.     send(client_sd, "1\r\n", 3, 0);
  59.  
  60.     // Initialize and start watcher to read client requests
  61.     ev_io_init(w_client, read_cb, client_sd, EV_READ);
  62.     ev_io_start(loop, w_client);
  63. }
  64.  
  65. int open_evsocket(int *sd) {
  66.     struct sockaddr_un socket_un;
  67.     struct ev_loop *loop = ev_default_loop(0);
  68.     struct ev_io w_accept;
  69.     if ((*sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  70.         perror("socket error");
  71.         return -1;
  72.     }
  73.  
  74.     if (-1 == setnonblock(*sd)) {
  75.         perror("echo server socket nonblock");
  76.         exit(EXIT_FAILURE);
  77.     }
  78.  
  79.     // Set it as unix socket
  80.     socket_un.sun_family = AF_UNIX;
  81.     strcpy(socket_un.sun_path, "/tmp/test.sock");
  82.  
  83.     if (-1 == bind(*sd, (struct sockaddr*) & socket_un, sizeof (socket_un.sun_family) + strlen(socket_un.sun_path))) {
  84.         perror("echo server bind");
  85.         exit(EXIT_FAILURE);
  86.     }
  87.  
  88.     if (-1 == listen(*sd, 50)) {
  89.         perror("listen");
  90.         exit(EXIT_FAILURE);
  91.     }
  92.     ev_io_init(&w_accept, accept_cb, *sd, EV_READ);
  93.     ev_io_start(loop, &w_accept);
  94.  
  95.     // Start infinite loop
  96.     while (exit_dsn_flag == 0) {
  97.         ev_loop(loop, 0);
  98.     }
  99.     pthread_exit(0);
  100.  
  101.     return (0);
  102. }
  103.  
  104. void *socket_thread_start(void *x) {
  105.     if (0 != open_evsocket(&sd)) {
  106.         perror("Socket failed");
  107.     }
  108.     return (NULL);
  109. }
  110.  
  111. /* Read client message */
  112. void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
  113.     char buffer[1024];
  114.     ssize_t readlen;
  115.     bzero(buffer, 1024);
  116.  
  117.     if (EV_ERROR & revents) {
  118.         perror("got invalid event");
  119.         return;
  120.     }
  121.  
  122.     // Receive message from client socket
  123.  
  124.     readlen = recv(watcher->fd, buffer, 1024, 0);
  125.     syslog(LOG_INFO, "Received from socket <%d> <%s>", readlen, buffer);
  126.     if (readlen < 1) {
  127.         // Stop and free watchet if client socket is closing
  128.         ev_io_stop(loop, watcher);
  129.         free(watcher);
  130.         total_clients--; // Decrement total_clients count
  131.         return;
  132.     } else {
  133.  
  134.         // The problem is here , sometimes even if send return 3 .. the communication to the socket is lost
  135.         syslog(LOG_INFO, "Sending 1 to socket  buffer=<%s> status=%d ", buffer, send(watcher->fd, "1\r\n", 3, 0));
  136.     }
  137. }
  138.  
  139. int main(int argc, char **argv) {
  140.      pthread_t sock_thread;
  141.     openlog(argv[0], LOG_PID | LOG_CONS, LOG_MAIL);
  142.     pthread_create(&sock_thread, NULL, socket_thread_start, NULL);
  143.      pthread_join(sock_thread, NULL);
  144.     return 0;
  145. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement