Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ev.h>
- #include <pthread.h>
- #include <mysql.h>
- #include <unistd.h>
- #include <syslog.h>
- #include <signal.h>
- #include <fcntl.h>
- int sd;
- int total_clients = 0;
- int exit_dsn_flag = 0;
- int setnonblock(int fd);
- void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents);
- int open_evsocket(int *sd);
- void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents);
- void *socket_thread_start(void *x);
- int setnonblock(int fd) {
- int flags;
- flags = fcntl(fd, F_GETFL);
- flags |= O_NONBLOCK;
- return fcntl(fd, F_SETFL, flags);
- }
- /* Accept client requests */
- void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
- struct sockaddr_in client_addr;
- socklen_t client_len = sizeof (client_addr);
- int client_sd;
- //struct ev_io watch_client
- struct ev_io *w_client = (struct ev_io*) malloc(sizeof (struct ev_io));
- if (EV_ERROR & revents) {
- perror("got invalid event");
- return;
- }
- // Accept client request
- client_sd = accept(watcher->fd, (struct sockaddr *) & client_addr, &client_len);
- if (client_sd < 0) {
- perror("accept error");
- return;
- }
- total_clients++; // Increment total_clients count
- send(client_sd, "1\r\n", 3, 0);
- // Initialize and start watcher to read client requests
- ev_io_init(w_client, read_cb, client_sd, EV_READ);
- ev_io_start(loop, w_client);
- }
- int open_evsocket(int *sd) {
- struct sockaddr_un socket_un;
- struct ev_loop *loop = ev_default_loop(0);
- struct ev_io w_accept;
- if ((*sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
- perror("socket error");
- return -1;
- }
- if (-1 == setnonblock(*sd)) {
- perror("echo server socket nonblock");
- exit(EXIT_FAILURE);
- }
- // Set it as unix socket
- socket_un.sun_family = AF_UNIX;
- strcpy(socket_un.sun_path, "/tmp/test.sock");
- if (-1 == bind(*sd, (struct sockaddr*) & socket_un, sizeof (socket_un.sun_family) + strlen(socket_un.sun_path))) {
- perror("echo server bind");
- exit(EXIT_FAILURE);
- }
- if (-1 == listen(*sd, 50)) {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- ev_io_init(&w_accept, accept_cb, *sd, EV_READ);
- ev_io_start(loop, &w_accept);
- // Start infinite loop
- while (exit_dsn_flag == 0) {
- ev_loop(loop, 0);
- }
- pthread_exit(0);
- return (0);
- }
- void *socket_thread_start(void *x) {
- if (0 != open_evsocket(&sd)) {
- perror("Socket failed");
- }
- return (NULL);
- }
- /* Read client message */
- void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
- char buffer[1024];
- ssize_t readlen;
- bzero(buffer, 1024);
- if (EV_ERROR & revents) {
- perror("got invalid event");
- return;
- }
- // Receive message from client socket
- readlen = recv(watcher->fd, buffer, 1024, 0);
- syslog(LOG_INFO, "Received from socket <%d> <%s>", readlen, buffer);
- if (readlen < 1) {
- // Stop and free watchet if client socket is closing
- ev_io_stop(loop, watcher);
- free(watcher);
- total_clients--; // Decrement total_clients count
- return;
- } else {
- // The problem is here , sometimes even if send return 3 .. the communication to the socket is lost
- syslog(LOG_INFO, "Sending 1 to socket buffer=<%s> status=%d ", buffer, send(watcher->fd, "1\r\n", 3, 0));
- }
- }
- int main(int argc, char **argv) {
- pthread_t sock_thread;
- openlog(argv[0], LOG_PID | LOG_CONS, LOG_MAIL);
- pthread_create(&sock_thread, NULL, socket_thread_start, NULL);
- pthread_join(sock_thread, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement