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