int create_epoll(int s_fd, int MAXEVENTS) { int e_fd = -1; struct epoll_event e_event; memset(&e_event, 0, sizeof(e_event)); if (os_listen(s_fd, 10) < 0) { //perror("ERROR: LISTEN SERVER"); os_close(s_fd); return -1; } e_fd = os_epoll_create(MAXEVENTS + 1); if (e_fd < 0) { //perror("ERROR: EPOLL_CREATE"); os_close(s_fd); return -1; } e_event.events = EPOLLIN; e_event.data.fd = s_fd; if (os_epoll_ctl(e_fd, EPOLL_CTL_ADD, s_fd, &e_event) < 0) { //perror("ERROR: EPOLL_CTL"); os_close(e_fd); os_close(s_fd); return -1; } return e_fd; }
void receive_run(const char* channel, const char* file) { int socket = os_listen(channel); int log = os_flash_open(file, WRITE); while (true) { uint8_t buffer[10]; size_t len; while(tc_receive(socket, buffer, sizeof(buffer), &len) != SUCCESS); len -= len % sizeof(int32_t); log_to(log, buffer, len); } }
ut_setup() { int len; struct in_addr addr; int reuse; UT_ASSERT(!os_net_init()); /* create sockets */ server_sock = os_socket(AF_INET, SOCK_STREAM, 0); UT_ASSERT(server_sock >= 0); reuse = 1; UT_ASSERT(os_setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) >= 0); client_sock = os_socket(AF_INET, SOCK_STREAM, 0); UT_ASSERT(client_sock >= 0); /* fill in server information */ server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); server_addr.sin_port = htons(DATA_PORT); /* bind sockets */ UT_ASSERT(os_bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) >= 0); UT_ASSERT(os_listen(server_sock, BACKLOG) >= 0); /* connect to server */ os_inet_aton("127.0.0.1", &addr); memset(&client_addr, 0, sizeof(client_addr)); client_addr.sin_family = AF_INET; client_addr.sin_addr.s_addr = addr.s_addr; client_addr.sin_port = htons(DATA_PORT); if (os_connect(client_sock, (struct sockaddr *)&client_addr, sizeof(client_addr)) < 0) UT_FAIL(); len = sizeof(struct sockaddr_in); accept_sock = os_accept(server_sock, (struct sockaddr *)&server_addr, &len); UT_ASSERT(accept_sock >= 0); }
/** \brief Return a descritor on a socket that allow us to wait GUI/CLI Request * * \param[in] port: The IP port to wait on * \return Return a descritor on a socket if > 0 or a negative error code */ static int listen_socket_port(int port) { int sd; struct sockaddr_in sin; int autorisation; int retval; /* get an internet domain socket */ if ((sd = os_socket (AF_INET, SOCK_STREAM, 0)) < 0) return sd; /* Change socket options to be able to restart the daemon right * after it has been stopped -- otherwise, bind() would block for * 60 secs because of TCP */ autorisation = 1; os_setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &autorisation, sizeof(int)); /* complete the socket structure */ memset(&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons (port); /* bind the socket to the port number */ retval = os_bind(sd, (struct sockaddr *) &sin, sizeof (sin)); if (retval < 0) return retval; /* show that we are willing to listen */ retval = os_listen (sd, 5); if (retval < 0) return retval; return sd; }
/** * init_plugin initialise internal data * * if we are server, try to bind and launch a thread to accept * launch the thread of receive data * * @param net_plugin Info on the new instance that we will fill * * @return EXA_SUCCESS or error */ int algopr_init_plugin(exa_nodeid_t node_id, int max_buffer_size) { struct sockaddr_in serv_addr; int retval = -NBD_ERR_MALLOC_FAILED; int i; EXA_ASSERT(EXA_NODEID_VALID(node_id)); this_node_id = node_id; init_peers(); eth.max_buffer_size = max_buffer_size + max_buffer_size; wq_init(ð.wq_send); nbd_init_root(MAX_BIG_RECV_ELT, eth.max_buffer_size, ð.root_list_big_recv); nbd_init_root(MAX_SEND_ELT, sizeof(payload_t), ð.root_list_send); for (i = 0; i < EXA_MAX_NODES_NUMBER; i++) nbd_init_list(ð.root_list_send, ð.send_list[i]); algopr_run = true; if (!exathread_create_named(ð.receive_thread, NBD_THREAD_STACK_SIZE + MIN_THREAD_STACK_SIZE_OF_THIS_PLUGIN, algopr_receive_thread, NULL, "AlgoPrRcv")) return -NBD_ERR_THREAD_CREATION; if (!exathread_create_named(ð.send_thread, NBD_THREAD_STACK_SIZE + MIN_THREAD_STACK_SIZE_OF_THIS_PLUGIN, algopr_send_thread, NULL, "AlgoPrSnd")) return -NBD_ERR_THREAD_CREATION; eth.accept_sock = os_socket(PF_INET, SOCK_STREAM, 0); if (eth.accept_sock < 0) return -EXA_ERR_CREATE_SOCKET; /* bind a socket to SERVERD_DATA_PORT port and make it listen for incoming * connections */ serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(algopr_network_port); retval = internal_setsock_opt(eth.accept_sock, SOCK_LISTEN_FLAGS); if (retval != EXA_SUCCESS) return retval; if (os_bind(eth.accept_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) return -EXA_ERR_CREATE_SOCKET; if (os_listen(eth.accept_sock, EXA_MAX_NODES_NUMBER) < 0) return -EXA_ERR_CREATE_SOCKET; if (!exathread_create_named(ð.accept_thread, NBD_THREAD_STACK_SIZE + MIN_THREAD_STACK_SIZE_OF_THIS_PLUGIN, accept_thread, NULL, "servEthAccPlugin")) return -NBD_ERR_THREAD_CREATION; return EXA_SUCCESS; }