Skip to content

Commit

Permalink
Cherry pick PR #3030: Parse search requet when read byte is zero (#3037)
Browse files Browse the repository at this point in the history
Refer to the original PR: #3030

b/268088112
Test-On-Device: true

Change-Id: Ic16b1ae5a9e5393489f1994bbfc768bd4288d2ee

Co-authored-by: Hao <[email protected]>
  • Loading branch information
1 parent e2a9143 commit 40ef2bd
Showing 1 changed file with 29 additions and 22 deletions.
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 @@ -141,30 +141,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

0 comments on commit 40ef2bd

Please sign in to comment.