Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s_list<SOCKET, 1024> socket_arr = zero;
- while(true) {
- WSAPOLLFD poll_arr[1025] = zero;
- poll_arr[0].fd = server_socket;
- poll_arr[0].events = POLLIN;
- for(int socket_i = 0; socket_i < socket_arr.count; socket_i += 1) {
- poll_arr[socket_i + 1].fd = socket_arr[socket_i];
- poll_arr[socket_i + 1].events = POLLIN;
- }
- int hot_socket_count = WSAPoll(poll_arr, socket_arr.count + 1, -1);
- if(hot_socket_count < 0) {
- error();
- }
- int handled_count = 0;
- int i = 0;
- while(handled_count < hot_socket_count && i < 1025) {
- SOCKET s = poll_arr[i].fd;
- int flags = poll_arr[i].revents;
- bool can_read = (flags & POLLRDNORM) || (flags & POLLRDBAND);
- bool has_error = (flags & POLLERR) || (flags & POLLNVAL);
- bool disconnected = flags & POLLHUP;
- check(!has_error);
- bool delete_socket = false;
- if(disconnected) {
- printf("connection %i closed\n", i);
- check(s != server_socket);
- handled_count += 1;
- delete_socket = true;
- }
- else if(can_read) {
- handled_count += 1;
- if(s == server_socket) {
- SOCKET new_socket = accept(server_socket, null, null);
- check(new_socket != INVALID_SOCKET);
- socket_arr.add(new_socket);
- }
- else {
- char buffer[65535 + 1];
- int bytes_read = recv(s, buffer, sizeof(buffer), 0);
- if(bytes_read > 0) {
- }
- else if(bytes_read == 0) {
- printf("connection clossed\n");
- delete_socket = true;
- }
- else {
- printf("recv failed\n");
- delete_socket = true;
- }
- }
- }
- if(delete_socket) {
- shutdown(s, SD_BOTH);
- closesocket(s);
- socket_arr.remove_and_swap(i - 1);
- }
- i += 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement