EchoServer(int port) { printf("Mendengarkan pada port %d\n", port); struct sockaddr_in addr; s = socket(PF_INET, SOCK_STREAM, 0); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) != 0) { perror("bind"); } fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK); listen(s, 5); io.set<EchoServer, &EchoServer::io_accept>(this); io.start(s, ev::READ); sio.set<&EchoServer::signal_cb>(); sio.start(SIGINT); }
server (int fd) { read_ev.set <server, &server::read_cb> (this); log_cb.set <server, &server::log_msg> (this); getfd_cb.set<server, &server::getfd> (this); this->fd = fd; fcntl (fd, F_SETFD, FD_CLOEXEC); fcntl (fd, F_SETFL, 0); read_ev.start (fd, ev::READ); }
void setupNode(ev::io& node) { int reuseaddr_on = 1; int node_fd = socket(AF_INET, SOCK_STREAM, 0); if (node_fd < 0) err(1, "listen failed"); if (setsockopt(node_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)) == -1) err(1, "setsockopt failed"); sockaddr_in node_addr; memset(&node_addr, 0, sizeof(node_addr)); node_addr.sin_family = AF_INET; node_addr.sin_addr.s_addr = INADDR_ANY; node_addr.sin_port = htons(COMM_NODE_PORT); if (bind(node_fd, reinterpret_cast<sockaddr*>(&node_addr), sizeof(node_addr)) < 0) err(1, "bind failed"); if (listen(node_fd, 5) < 0) err(1, "listen failed"); if (setnonblock(node_fd) < 0) err(1, "failed to set server socket to non-blocking"); node.set<nodeAccept>(); node.start(node_fd, ev::READ); }
void setupHTTP(ev::io& http) { int reuseaddr_on = 1; int http_fd = socket(AF_INET, SOCK_STREAM, 0); if (http_fd < 0) err(1, "listen failed"); if (setsockopt(http_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)) == -1) err(1, "setsockopt failed"); sockaddr_in http_addr; memset(&http_addr, 0, sizeof(http_addr)); http_addr.sin_family = AF_INET; http_addr.sin_addr.s_addr = INADDR_ANY; http_addr.sin_port = htons(COMM_HTTP_PORT); if (bind(http_fd, reinterpret_cast<sockaddr*>(&http_addr), sizeof(http_addr)) < 0) err(1, "bind failed"); if (listen(http_fd, 5) < 0) err(1, "listen failed"); if (setnonblock(http_fd) < 0) err(1, "failed to set server socket to non-blocking"); http.set<httpAccept>(); http.start(http_fd, ev::READ); }
EchoInstance(int s) : sfd(s) { fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK); printf("Client terkoneksi\n"); io.set<EchoInstance, &EchoInstance::callback>(this); io.start(s, ev::READ); }