Add a fd_readable(int fd) helper function
Use it instead of direct calls to select scatered around the code base.
This commit is contained in:
parent
d0a29511d2
commit
8b02bb749d
|
@ -4,9 +4,6 @@
|
|||
#include "event_manager.hh"
|
||||
#include "file.hh"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#define st_mtim st_mtimespec
|
||||
#endif
|
||||
|
@ -120,8 +117,6 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
|
|||
size_t loops = 16;
|
||||
char data[buffer_size];
|
||||
const int fifo = watcher.fd();
|
||||
timeval tv{ 0, 0 };
|
||||
fd_set rfds;
|
||||
ssize_t count = 0;
|
||||
do
|
||||
{
|
||||
|
@ -142,12 +137,8 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
|
|||
if (data[count-1] == '\n')
|
||||
buffer->insert(buffer->end_coord(), "\n");
|
||||
}
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fifo, &rfds);
|
||||
}
|
||||
while (--loops and count > 0 and
|
||||
select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1);
|
||||
while (--loops and count > 0 and fd_readable(fifo));
|
||||
|
||||
buffer->run_hook_in_own_context("BufReadFifo", buffer->name());
|
||||
|
||||
|
|
11
src/file.cc
11
src/file.cc
|
@ -13,6 +13,7 @@
|
|||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
#include <sys/sysctl.h>
|
||||
|
@ -123,6 +124,16 @@ String compact_path(StringView filename)
|
|||
return filename.str();
|
||||
}
|
||||
|
||||
bool fd_readable(int fd)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
|
||||
timeval tv{0,0};
|
||||
return select(fd+1, &rfds, nullptr, nullptr, &tv) == 1;
|
||||
}
|
||||
|
||||
String read_fd(int fd, bool text)
|
||||
{
|
||||
String content;
|
||||
|
|
|
@ -40,6 +40,7 @@ std::pair<StringView, StringView> split_path(StringView path);
|
|||
|
||||
String get_kak_binary_path();
|
||||
|
||||
bool fd_readable(int fd);
|
||||
String read_fd(int fd, bool text = false);
|
||||
String read_file(StringView filename, bool text = false);
|
||||
void write(int fd, StringView data);
|
||||
|
|
|
@ -399,24 +399,11 @@ void JsonUI::eval_json(const Value& json)
|
|||
throw runtime_error("unknown method");
|
||||
}
|
||||
|
||||
static bool stdin_ready()
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(0, &rfds);
|
||||
|
||||
timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
return select(1, &rfds, nullptr, nullptr, &tv) == 1;
|
||||
}
|
||||
|
||||
void JsonUI::parse_requests(EventMode mode)
|
||||
{
|
||||
constexpr size_t bufsize = 1024;
|
||||
char buf[bufsize];
|
||||
while (stdin_ready())
|
||||
while (fd_readable(0))
|
||||
{
|
||||
ssize_t size = ::read(0, buf, bufsize);
|
||||
if (size == -1 or size == 0)
|
||||
|
|
|
@ -372,17 +372,7 @@ void RemoteUI::set_ui_options(const Options& options)
|
|||
|
||||
bool RemoteUI::is_key_available()
|
||||
{
|
||||
timeval tv;
|
||||
fd_set rfds;
|
||||
|
||||
int sock = m_socket_watcher.fd();
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(sock, &rfds);
|
||||
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
int res = select(sock+1, &rfds, nullptr, nullptr, &tv);
|
||||
return res == 1;
|
||||
return fd_readable(m_socket_watcher.fd());
|
||||
}
|
||||
|
||||
Key RemoteUI::get_key()
|
||||
|
@ -466,15 +456,9 @@ RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&&
|
|||
void RemoteClient::process_available_messages()
|
||||
{
|
||||
int socket = m_socket_watcher->fd();
|
||||
timeval tv{ 0, 0 };
|
||||
fd_set rfds;
|
||||
|
||||
do {
|
||||
process_next_message();
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(socket, &rfds);
|
||||
} while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1);
|
||||
} while (fd_readable(socket));
|
||||
}
|
||||
|
||||
void RemoteClient::process_next_message()
|
||||
|
@ -573,9 +557,7 @@ public:
|
|||
private:
|
||||
void handle_available_input()
|
||||
{
|
||||
int socket = m_socket_watcher.fd();
|
||||
timeval tv{ 0, 0 };
|
||||
fd_set rfds;
|
||||
const int socket = m_socket_watcher.fd();
|
||||
do
|
||||
{
|
||||
char c;
|
||||
|
@ -610,11 +592,8 @@ private:
|
|||
}
|
||||
else
|
||||
m_buffer += c;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(socket, &rfds);
|
||||
}
|
||||
while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1);
|
||||
while (fd_readable(socket));
|
||||
}
|
||||
|
||||
String m_buffer;
|
||||
|
|
Loading…
Reference in New Issue
Block a user