int editor_type::command (command_type& ct) { static const std::wstring onemore (L"cdijkmnpstw"); std::wstring doc; if (1 == ct.naddr) line2 = line1; if (line1 > line2 || buffer.dollar () < line1 || buffer.dollar () < line2) return '?'; if (line1 == 0 && onemore.find (ct.command) != std::wstring::npos) return '?'; switch (ct.command) { default: return '?'; case 'a': ct.command = cmd_a (ct); break; case 'c': ct.command = cmd_c (ct); break; case 'd': ct.command = cmd_d (ct); break; case 'e': ct.command = cmd_e (ct); break; case 'f': ct.command = cmd_f (ct); break; case 'i': ct.command = cmd_i (ct); break; case 'j': ct.command = cmd_j (ct); break; case 'k': ct.command = cmd_k (ct); break; case 'm': ct.command = cmd_m (ct); break; case 'n': ct.command = cmd_n (ct); break; case 'p': ct.command = cmd_p (ct); break; case 'r': ct.command = cmd_r (ct); break; case 's': ct.command = cmd_s (ct); break; case 't': ct.command = cmd_t (ct); break; case 'w': ct.command = cmd_w (ct); break; case '=': ct.command = cmd_equal (ct); break; case '\n': ct.command = cmd_p (ct); break; } print_dot (ct); return ct.command; }
//----------------------------------------------------------------------------- void *handle_request (void *data) { bool done; struct conn_info *info; char msg_buf[MSG_MAX]; char *cmd, *port, *p; struct response_node response_head; struct recv_buf recv_buf = { .pos = 0, .len = 0 }; info = data; done = false; while (!done) { // go to cleanup if connection was closed if (!read_msg (info->sock, &recv_buf, msg_buf)) break; // parse message cmd = strtok_r (msg_buf, " \r\n", &p); port = strtok_r (NULL, " \r\n", &p); // construct response response_head.next = NULL; if (!cmd) { response_bad (&response_head); } else if (cmd_equal (cmd, "CONNECT", 7)) { if (!port || add_client (info->addr, port)) { response_bad (&response_head); } else { response_ok (&response_head); #ifdef P2PSERV_LOG printf (ANSI_GREEN "+ %s %s\n" ANSI_RESET, info->addr, port); #endif } } else if (cmd_equal (cmd, "DISCONNECT", 10)) { if (!port || remove_client (info->addr, port)) { response_bad (&response_head); } else { response_ok (&response_head); #ifdef P2PSERV_LOG printf (ANSI_RED "- %s %s\n" ANSI_RESET, info->addr, port); #endif } } else if (cmd_equal (cmd, "LIST", 4)) { struct response_node *jlist; if (!port || clients_to_json (&jlist, info->addr, port)) { response_bad (&response_head); } else { response_head.next = jlist; #ifdef P2PSERV_LOG printf (ANSI_YELLOW "L %s %s\n" ANSI_RESET, info->addr, port); #endif } } else if (cmd_equal (cmd, "EXIT", 4)) { response_ok (&response_head); done = true; } else { response_bad (&response_head); } // send response //send_msg (info->sock, response.str, response.len); //free (response.str); send_response (info->sock, response_head.next); free_response (response_head.next); } #ifdef P2PSERV_LOG printf ("D %s\n", info->addr); fflush (stdout); #endif close (info->sock); free (info); pthread_mutex_lock (&num_threads_lock); num_threads--; pthread_mutex_unlock (&num_threads_lock); pthread_exit (NULL); }