2020-09-22 22:41:06 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-09-25 23:06:24 +08:00
|
|
|
#include "callbacks.h" // NOLINT(build/include)
|
2020-09-22 22:41:06 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-09-25 23:06:24 +08:00
|
|
|
size_t g_iterations = 0;
|
2020-09-22 22:41:06 +08:00
|
|
|
size_t constexpr kMaxIterations = 1'000'000;
|
2020-09-25 23:06:24 +08:00
|
|
|
static char g_buffer[1024];
|
|
|
|
static uv_buf_t g_iov;
|
2020-09-22 22:41:06 +08:00
|
|
|
|
|
|
|
// Stop the handle if the methods was called kMaxIterations times
|
|
|
|
void IdleCallback(uv_idle_t* handle) {
|
2020-09-25 23:06:24 +08:00
|
|
|
++g_iterations;
|
|
|
|
if (g_iterations > kMaxIterations) {
|
2020-09-22 22:41:06 +08:00
|
|
|
std::cout << "IdleCallback was called " << kMaxIterations << " times"
|
|
|
|
<< std::endl;
|
|
|
|
uv_idle_stop(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called after some chars have been written
|
|
|
|
// As soon as writing of these bytes is completed, read more
|
|
|
|
void OnWrite(uv_fs_t* req) {
|
|
|
|
if (req->result < 0) {
|
|
|
|
std::cerr << "Write error: " << uv_strerror(static_cast<int>(req->result))
|
|
|
|
<< std::endl;
|
2020-09-25 23:06:24 +08:00
|
|
|
return;
|
2020-09-22 22:41:06 +08:00
|
|
|
}
|
2020-09-25 23:06:24 +08:00
|
|
|
// Start reading more after writing these bytes
|
|
|
|
uv_fs_read(uv_default_loop(), &read_req, open_req.result, &g_iov, 1, -1,
|
|
|
|
OnRead);
|
2020-09-22 22:41:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called after some chars have been read
|
|
|
|
// As soon as reading of these bytes is completed, write them
|
|
|
|
void OnRead(uv_fs_t* req) {
|
|
|
|
if (req->result < 0) {
|
|
|
|
std::cerr << "Read error: " << uv_strerror(req->result) << std::endl;
|
2020-09-25 23:06:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req->result == 0) {
|
2020-09-22 22:41:06 +08:00
|
|
|
// No more bytes left, close the loop
|
|
|
|
uv_fs_t close_req;
|
|
|
|
uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
|
|
|
|
} else if (req->result > 0) {
|
|
|
|
// Start writing after reading some bytes
|
2020-09-25 23:06:24 +08:00
|
|
|
g_iov.len = req->result;
|
|
|
|
uv_fs_write(uv_default_loop(), &write_req, 1, &g_iov, 1, -1, OnWrite);
|
2020-09-22 22:41:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called after the file has been opened
|
|
|
|
// As soon as opening is completed, read the file
|
|
|
|
void OnOpen(uv_fs_t* req) {
|
|
|
|
if (req != &open_req) {
|
|
|
|
std::cerr << "Open error: req != &open_req" << std::endl;
|
2020-09-25 23:06:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req->result < 0) {
|
2020-09-22 22:41:06 +08:00
|
|
|
std::cerr << "Open error: " << uv_strerror(static_cast<int>(req->result))
|
|
|
|
<< std::endl;
|
2020-09-25 23:06:24 +08:00
|
|
|
return;
|
2020-09-22 22:41:06 +08:00
|
|
|
}
|
2020-09-25 23:06:24 +08:00
|
|
|
// Initialize uv_buf_t g_buffer
|
|
|
|
g_iov = uv_buf_init(g_buffer, sizeof(g_buffer));
|
|
|
|
// Start reading after opening
|
|
|
|
uv_fs_read(uv_default_loop(), &read_req, req->result, &g_iov, 1, -1, OnRead);
|
2020-09-22 22:41:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the integer pointed by handle->data and increment it by one
|
|
|
|
// Then close the handle
|
|
|
|
void TimerCallback(uv_timer_t* handle) {
|
|
|
|
int* data = static_cast<int*>(
|
|
|
|
uv_handle_get_data(reinterpret_cast<uv_handle_t*>(handle)));
|
2020-09-25 23:06:24 +08:00
|
|
|
++(*data);
|
2020-09-22 22:41:06 +08:00
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(handle), nullptr);
|
|
|
|
}
|