void proxy<arp_frame>::do_handle_frame(const_helper<ethernet_frame> ethernet_helper, const_helper<arp_frame> arp_helper) { if (arp_helper.operation() == ARP_REQUEST_OPERATION) { entry_map_type::const_iterator entry_it = m_entry_map.find(arp_helper.target_logical_address()); ethernet_address_type eth_addr; bool should_answer = false; if (entry_it != m_entry_map.end()) { eth_addr = entry_it->second; should_answer = true; } else { if (m_arp_request_callback) { should_answer = m_arp_request_callback(arp_helper.target_logical_address(), eth_addr); } } if (should_answer) { size_t payload_size; builder<arp_frame> arp_builder(response_buffer()); payload_size = arp_builder.write( ARP_REPLY_OPERATION, boost::asio::buffer(eth_addr.data()), arp_helper.target_logical_address(), arp_helper.sender_hardware_address(), arp_helper.sender_logical_address() ); builder<ethernet_frame> ethernet_builder(response_buffer(), payload_size); payload_size = ethernet_builder.write( ethernet_helper.sender(), ethernet_helper.target(), ethernet_helper.protocol() ); data_available(get_truncated_response_buffer(payload_size)); } } }
std::wstring IEDriverServer::SendCommandToManager(const std::wstring& session_id, const std::wstring& serialized_command) { // Sending a command consists of four actions: // 1. Setting the command to be executed // 2. Executing the command // 3. Waiting for the response to be populated // 4. Retrieving the response std::map<std::wstring, HWND>::iterator it = this->sessions_.find(session_id); if (it == this->sessions_.end()) { // Hand-code the response for an invalid session id return L"{ status : 404, sessionId : \"" + session_id + L"\", value : \"session " + session_id + L" does not exist\" }"; } HWND manager_window_handle = it->second; ::SendMessage(manager_window_handle, WD_SET_COMMAND, NULL, (LPARAM)serialized_command.c_str()); ::PostMessage(manager_window_handle, WD_EXEC_COMMAND, NULL, NULL); int response_length = (int)::SendMessage(manager_window_handle, WD_GET_RESPONSE_LENGTH, NULL, NULL); while (response_length == 0) { // Sleep a short time to prevent thread starvation on single-core machines. ::Sleep(10); response_length = (int)::SendMessage(manager_window_handle, WD_GET_RESPONSE_LENGTH, NULL, NULL); } // Must add one to the length to handle the terminating character. std::vector<TCHAR> response_buffer(response_length + 1); ::SendMessage(manager_window_handle, WD_GET_RESPONSE, NULL, (LPARAM)&response_buffer[0]); std::wstring serialized_response(&response_buffer[0]); response_buffer.clear(); return serialized_response; }
void ExtraHID::SendHIDStatus() { if (is_device_reload_pending.exchange(false)) LoadInputDevices(); constexpr int C_STICK_CENTER = 0x800; // TODO(wwylele): this value is not accurately measured. We currently assume that the axis can // take values in the whole range of a 12-bit integer. constexpr int C_STICK_RADIUS = 0x7FF; float x, y; std::tie(x, y) = c_stick->GetStatus(); ExtraHIDResponse response; response.c_stick.header.Assign(static_cast<u8>(ResponseID::PollHID)); response.c_stick.c_stick_x.Assign(static_cast<u32>(C_STICK_CENTER + C_STICK_RADIUS * x)); response.c_stick.c_stick_y.Assign(static_cast<u32>(C_STICK_CENTER + C_STICK_RADIUS * y)); response.buttons.battery_level.Assign(0x1F); response.buttons.zl_not_held.Assign(!zl->GetStatus()); response.buttons.zr_not_held.Assign(!zr->GetStatus()); response.buttons.r_not_held.Assign(1); response.unknown = 0; Core::Movie::GetInstance().HandleExtraHidResponse(response); std::vector<u8> response_buffer(sizeof(response)); memcpy(response_buffer.data(), &response, sizeof(response)); Send(response_buffer); }
/** * Opens a connection to the window server. */ g_ui_open_status g_ui::open() { // check if already open if (g_ui_initialized) { return G_UI_OPEN_STATUS_EXISTING; } // get window managers id g_tid window_mgr = g_task_get_id(G_UI_REGISTRATION_THREAD_IDENTIFIER); if (window_mgr == -1) { klog("failed to retrieve task id of window server with identifier '%s'", G_UI_REGISTRATION_THREAD_IDENTIFIER); return G_UI_OPEN_STATUS_COMMUNICATION_FAILED; } // start event dispatcher g_ui_event_dispatcher_tid = g_create_thread((void*) &event_dispatch_thread); // send initialization request g_message_transaction init_tx = g_get_message_tx_id(); g_ui_initialize_request request; request.header.id = G_UI_PROTOCOL_INITIALIZATION; g_send_message_t(window_mgr, &request, sizeof(g_ui_initialize_request), init_tx); // receive initialization response uint32_t response_buffer_size = sizeof(g_message_header) + sizeof(g_ui_initialize_response); g_local<uint8_t> response_buffer(new uint8_t[response_buffer_size]); if (g_receive_message_t(response_buffer(), response_buffer_size, init_tx) != G_MESSAGE_RECEIVE_STATUS_SUCCESSFUL) { klog("failed to communicate with the window server"); return G_UI_OPEN_STATUS_COMMUNICATION_FAILED; } // check response g_ui_initialize_response* response = (g_ui_initialize_response*) G_MESSAGE_CONTENT(response_buffer()); if (response->status != G_UI_PROTOCOL_SUCCESS) { klog("failed to open UI"); return G_UI_OPEN_STATUS_FAILED; } // mark UI as ready g_ui_initialized = true; g_ui_delegate_tid = response->window_server_delegate_thread; return G_UI_OPEN_STATUS_SUCCESSFUL; }
bool IESession::ExecuteCommand(const std::string& serialized_command, std::string* serialized_response) { LOG(TRACE) << "Entering IESession::ExecuteCommand"; // Sending a command consists of five actions: // 1. Setting the command to be executed // 2. Executing the command // 3. Waiting for the response to be populated // 4. Retrieving the response // 5. Retrieving whether the command sent caused the session to be ready for shutdown ::SendMessage(this->executor_window_handle_, WD_SET_COMMAND, NULL, reinterpret_cast<LPARAM>(serialized_command.c_str())); ::PostMessage(this->executor_window_handle_, WD_EXEC_COMMAND, NULL, NULL); int response_length = static_cast<int>(::SendMessage(this->executor_window_handle_, WD_GET_RESPONSE_LENGTH, NULL, NULL)); LOG(TRACE) << "Beginning wait for response length to be not zero"; while (response_length == 0) { // Sleep a short time to prevent thread starvation on single-core machines. ::Sleep(10); response_length = static_cast<int>(::SendMessage(this->executor_window_handle_, WD_GET_RESPONSE_LENGTH, NULL, NULL)); } LOG(TRACE) << "Found non-zero response length"; // Must add one to the length to handle the terminating character. std::vector<char> response_buffer(response_length + 1); ::SendMessage(this->executor_window_handle_, WD_GET_RESPONSE, NULL, reinterpret_cast<LPARAM>(&response_buffer[0])); *serialized_response = &response_buffer[0]; bool session_is_valid = ::SendMessage(this->executor_window_handle_, WD_IS_SESSION_VALID, NULL, NULL) != 0; return session_is_valid; }