Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #3030: Parse search requet when read byte is zero #3036

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions cobalt/network/dial/dial_udp_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,37 @@ void DialUdpServer::DidClose(net::UDPSocket* server) {}

void DialUdpServer::DidRead(int bytes_read) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// If M-Search request was valid, send response. Else, keep quiet.
if (!socket_ || !read_buf_.get() || !read_buf_->data()) {
LOG(INFO) << "Dial server socket read error: no socket or buffer";
} else if (bytes_read <= 0) {
LOG(WARNING) << "Dial server socket read error: " << bytes_read;
} else if (ParseSearchRequest(std::string(read_buf_->data()))) {
auto response = std::make_unique<std::string>();
*response = std::move(ConstructSearchResponse());
// Using the fake IOBuffer to avoid another copy.
scoped_refptr<net::WrappedIOBuffer> fake_buffer =
new net::WrappedIOBuffer(response->data());
// After optimization, some compiler will dereference and get response size
// later than passing response.
auto response_size = response->size();
int result = socket_->SendTo(
fake_buffer.get(), response_size, client_address_,
base::Bind(&DialUdpServer::WriteComplete, base::Unretained(this),
fake_buffer, base::Passed(&response)));
if (result == net::ERR_IO_PENDING) {
// WriteComplete is responsible for posting the next callback to accept
// connection.
return;
} else if (result < 0) {
LOG(ERROR) << "UDPSocket SendTo error: " << result;
} else {
if (bytes_read <= 0) {
LOG(WARNING) << "Dial server socket reads no bytes: " << bytes_read;
}

// ParseSearchRequest can be triggered when read bytes is zero, this will
// prompt a response that updates the device picker.
if (ParseSearchRequest(std::string(read_buf_->data()))) {
LOG(INFO) << "Dial server socket parses search request with "
<< bytes_read << " bytes read";
auto response = std::make_unique<std::string>();
*response = std::move(ConstructSearchResponse());
// Using the fake IOBuffer to avoid another copy.
scoped_refptr<net::WrappedIOBuffer> fake_buffer =
new net::WrappedIOBuffer(response->data());
// After optimization, some compiler will dereference and get response
// size later than passing response.
auto response_size = response->size();
int result = socket_->SendTo(
fake_buffer.get(), response_size, client_address_,
base::Bind(&DialUdpServer::WriteComplete, base::Unretained(this),
fake_buffer, base::Passed(&response)));
if (result == net::ERR_IO_PENDING) {
// WriteComplete is responsible for posting the next callback to accept
// connection.
return;
} else if (result < 0) {
LOG(ERROR) << "UDPSocket SendTo error: " << result;
}
}
}

Expand Down
Loading