spice client: Transfer all channels run loop from EventsLoop class to ProcessLoop class

This commit is contained in:
Yonit Halperin 2009-10-20 11:18:56 +02:00 committed by Yaniv Kamay
parent 8d5b738ba1
commit 49f5e5973c
26 changed files with 58 additions and 759 deletions

View File

@ -27,7 +27,6 @@ RED_COMMON_SRCS = \
debug.h \
display_channel.cpp \
display_channel.h \
events_loop.h \
red_gl_canvas.cpp \
red_gl_canvas.h \
gl_canvas.cpp \

View File

@ -73,10 +73,10 @@ private:
virtual void on_connect();
virtual void add_evnet_sorce(EventsLoop::File& evnet_sorce);
virtual void remove_evnet_sorce(EventsLoop::File& evnet_sorce);
virtual void add_evnet_sorce(EventsLoop::Trigger& evnet_sorce);
virtual void remove_evnet_sorce(EventsLoop::Trigger& evnet_sorce);
virtual void add_event_source(EventSources::File& event_source);
virtual void remove_event_source(EventSources::File& event_source);
virtual void add_event_source(EventSources::Trigger& event_source);
virtual void remove_event_source(EventSources::Trigger& event_source);
virtual void push_frame(uint8_t *frame);
void send_start_mark();

View File

@ -26,12 +26,12 @@
#include "utils.h"
ClientNetSocket::ClientNetSocket(uint16_t id, const struct in_addr& dst_addr, uint16_t dst_port,
EventsLoop& events_loop, EventHandler& event_handler)
ProcessLoop& process_loop, EventHandler& event_handler)
: _id (id)
, _local_addr (dst_addr)
, _local_port (dst_port)
, _peer (INVALID_SOCKET)
, _events_loop (events_loop)
, _process_loop (process_loop)
, _event_handler (event_handler)
, _num_recv_tokens (0)
, _send_message (NULL)
@ -82,7 +82,7 @@ bool ClientNetSocket::connect(uint32_t recv_tokens)
return false;
}
_events_loop.add_socket(*this);
_process_loop.add_socket(*this);
_status = SOCKET_STATUS_OPEN;
_num_recv_tokens = recv_tokens;
return true;
@ -374,7 +374,7 @@ void ClientNetSocket::close_and_tell()
void ClientNetSocket::apply_disconnect()
{
if (_peer != INVALID_SOCKET) {
_events_loop.remove_socket(*this);
_process_loop.remove_socket(*this);
closesocket(_peer);
_peer = INVALID_SOCKET;
LOG_INFO("closing connection_id=%d", _id);

View File

@ -23,7 +23,7 @@
#include "platform_utils.h"
#include "common.h"
#include "events_loop.h"
#include "process_loop.h"
/* intterface for connenctions inside client LAN */
@ -34,7 +34,7 @@ typedef enum {
SOCKET_STATUS_CLOSED,
} SocketStatus;
class ClientNetSocket: public EventsLoop::Socket {
class ClientNetSocket: public EventSources::Socket {
public:
class ReceiveBuffer;
class SendBuffer;
@ -46,7 +46,7 @@ public:
class ShutdownExcpetion {};
ClientNetSocket(uint16_t id, const struct in_addr& dst_addr, uint16_t dst_port,
EventsLoop& events_loop, ClientNetSocket::EventHandler& event_handler);
ProcessLoop& process_loop, ClientNetSocket::EventHandler& event_handler);
virtual ~ClientNetSocket();
bool connect(uint32_t recv_tokens);
@ -61,7 +61,7 @@ public:
inline const struct in_addr& local_addr() {return _local_addr;}
inline uint16_t local_port() {return _local_port;}
/* EventsLoop::Socket interface */
/* EventSources::Socket interface */
void on_event();
int get_socket() {return _peer;}
@ -98,7 +98,7 @@ private:
SOCKET _peer;
EventsLoop& _events_loop;
ProcessLoop& _process_loop;
EventHandler& _event_handler;

View File

@ -524,7 +524,7 @@ CursorChannel::CursorChannel(RedClient& client, uint32_t id)
sizeof(RedInvalOne));
handler->set_handler(RED_CURSOR_INVAL_ALL, &CursorChannel::handle_inval_all, 0);
get_events_loop().add_trigger(_cursor_trigger);
get_process_loop().add_trigger(_cursor_trigger);
}
CursorChannel::~CursorChannel()

View File

@ -43,7 +43,7 @@ public:
typedef Cache<CursorData, CursorCacheTreat, 1024> CursorCache;
class CursorModeTrigger: public EventsLoop::Trigger {
class CursorModeTrigger: public EventSources::Trigger {
public:
CursorModeTrigger(CursorChannel& channel);
virtual void on_event();

View File

@ -571,7 +571,7 @@ GLInterruptRecreate::GLInterruptRecreate(DisplayChannel& channel)
void GLInterruptRecreate::trigger()
{
Lock lock(_lock);
EventsLoop::Trigger::trigger();
EventSources::Trigger::trigger();
_cond.wait(lock);
}
@ -654,11 +654,11 @@ DisplayChannel::DisplayChannel(RedClient& client, uint32_t id,
handler->set_handler(RED_DISPLAY_STREAM_DESTROY_ALL,
&DisplayChannel::handle_stream_destroy_all, 0);
get_events_loop().add_trigger(_streams_trigger);
get_process_loop().add_trigger(_streams_trigger);
#ifdef USE_OGL
get_events_loop().add_trigger(_gl_interrupt_recreate);
get_process_loop().add_trigger(_gl_interrupt_recreate);
#endif
get_events_loop().add_trigger(_interrupt_update);
get_process_loop().add_trigger(_interrupt_update);
}
DisplayChannel::~DisplayChannel()

View File

@ -25,7 +25,7 @@
#include "cairo.h"
#include "cache.hpp"
#include "screen_layer.h"
#include "events_loop.h"
#include "process_loop.h"
#ifdef USE_OGL
#include "red_pixmap_gl.h"
#endif
@ -36,7 +36,7 @@ class ChannelFactory;
class VideoStream;
class DisplayChannel;
class StreamsTrigger: public EventsLoop::Trigger {
class StreamsTrigger: public EventSources::Trigger {
public:
StreamsTrigger(DisplayChannel& channel);
@ -47,7 +47,7 @@ private:
};
#ifdef USE_OGL
class GLInterruptRecreate: public EventsLoop::Trigger {
class GLInterruptRecreate: public EventSources::Trigger {
public:
GLInterruptRecreate(DisplayChannel& channel);
virtual void trigger();
@ -60,7 +60,7 @@ private:
};
#endif
class InterruptUpdate: public EventsLoop::Trigger {
class InterruptUpdate: public EventSources::Trigger {
public:
InterruptUpdate(DisplayChannel& channel);

View File

@ -1,86 +0,0 @@
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _H_EVENTS_LOOP
#define _H_EVENTS_LOOP
#include "common.h"
#include "events_loop_p.h"
class EventSourceOld;
class EventsLoop: public EventsLoop_p {
public:
class Trigger;
class Socket;
class File;
EventsLoop();
virtual ~EventsLoop();
void add_trigger(Trigger& trigger);
void remove_trigger(Trigger& trigger);
void add_socket(Socket& socket);
void remove_socket(Socket& socket);
void add_file(File& file);
void remove_file(File& file);
void run();
// FIXME: temporary - need to adjust the loop for the main thread
void run_once(int timeout_milli = INFINITE);
};
class EventSourceOld {
public:
virtual ~EventSourceOld() {}
virtual void on_event() = 0;
private:
virtual void action() {on_event();}
friend class EventsLoop;
};
class EventsLoop::Trigger: public EventSourceOld, private EventsLoop_p::Trigger_p {
public:
Trigger();
virtual ~Trigger();
virtual void trigger();
virtual void reset();
private:
virtual void action();
friend class EventsLoop;
};
class EventsLoop::Socket: public EventSourceOld {
protected:
virtual int get_socket() = 0;
friend class EventsLoop;
};
class EventsLoop::File: public EventSourceOld {
protected:
virtual int get_fd() = 0;
friend class EventsLoop;
};
#endif

View File

@ -21,7 +21,7 @@
#include "cursor.h"
#include "process_loop.h"
#include "event_sources.h"
#include "events_loop.h"
#include "process_loop.h"
class WaveRecordAbstract;
class WavePlaybackAbstract;
@ -118,10 +118,10 @@ public:
class Platform::RecordClinet {
public:
virtual ~RecordClinet() {}
virtual void add_evnet_sorce(EventsLoop::File& evnet_sorce) = 0;
virtual void remove_evnet_sorce(EventsLoop::File& evnet_sorce) = 0;
virtual void add_evnet_sorce(EventsLoop::Trigger& evnet_sorce) = 0;
virtual void remove_evnet_sorce(EventsLoop::Trigger& evnet_sorce) = 0;
virtual void add_event_source(EventSources::File& evnet_source) = 0;
virtual void remove_event_source(EventSources::File& evnet_source) = 0;
virtual void add_event_source(EventSources::Trigger& evnet_source) = 0;
virtual void remove_event_source(EventSources::Trigger& evnet_source) = 0;
virtual void push_frame(uint8_t *frame) = 0;
};

View File

@ -209,24 +209,24 @@ void RecordChannel::release_message(RecordSamplesMessage *message)
_messages.push_front(message);
}
void RecordChannel::add_evnet_sorce(EventsLoop::File& evnet_sorce)
void RecordChannel::add_event_source(EventSources::File& event_source)
{
get_events_loop().add_file(evnet_sorce);
get_process_loop().add_file(event_source);
}
void RecordChannel::remove_evnet_sorce(EventsLoop::File& evnet_sorce)
void RecordChannel::remove_event_source(EventSources::File& event_source)
{
get_events_loop().remove_file(evnet_sorce);
get_process_loop().remove_file(event_source);
}
void RecordChannel::add_evnet_sorce(EventsLoop::Trigger& evnet_sorce)
void RecordChannel::add_event_source(EventSources::Trigger& event_source)
{
get_events_loop().add_trigger(evnet_sorce);
get_process_loop().add_trigger(event_source);
}
void RecordChannel::remove_evnet_sorce(EventsLoop::Trigger& evnet_sorce)
void RecordChannel::remove_event_source(EventSources::Trigger& event_source)
{
get_events_loop().remove_trigger(evnet_sorce);
get_process_loop().remove_trigger(event_source);
}
#define FRAME_SIZE 256

View File

@ -254,6 +254,7 @@ RedChannel::RedChannel(RedClient& client, uint8_t type, uint8_t id,
, _incomming_message (NULL)
, _message_ack_count (0)
, _message_ack_window (0)
, _loop (this)
, _send_trigger (*this)
, _disconnect_stamp (0)
, _disconnect_reason (RED_ERR_OK)

View File

@ -23,7 +23,7 @@
#include "threads.h"
#include "red_peer.h"
#include "platform.h"
#include "events_loop.h"
#include "process_loop.h"
enum {
PASSIVE_STATE,
@ -85,7 +85,7 @@ private:
ChannelCaps _remote_caps;
};
class SendTrigger: public EventsLoop::Trigger {
class SendTrigger: public EventSources::Trigger {
public:
SendTrigger(RedChannel& channel);
@ -95,7 +95,7 @@ private:
RedChannel& _channel;
};
class AbortTrigger: public EventsLoop::Trigger {
class AbortTrigger: public EventSources::Trigger {
public:
virtual void on_event();
};
@ -129,7 +129,7 @@ public:
protected:
RedClient& get_client() { return _client;}
EventsLoop& get_events_loop() { return _loop;}
ProcessLoop& get_process_loop() { return _loop;}
MessageHandler* get_message_handler() { return _message_handler.get();}
virtual void on_connecting() {}
virtual void on_connect() {}
@ -188,7 +188,7 @@ private:
uint32_t _message_ack_count;
uint32_t _message_ack_window;
EventsLoop _loop;
ProcessLoop _loop;
SendTrigger _send_trigger;
AbortTrigger _abort_trigger;

View File

@ -23,11 +23,11 @@
#include "common.h"
#include "red.h"
#include "events_loop.h"
#include "process_loop.h"
#include "threads.h"
#include "platform_utils.h"
class RedPeer: protected EventsLoop::Socket {
class RedPeer: protected EventSources::Socket {
public:
RedPeer();
virtual ~RedPeer();

View File

@ -751,7 +751,7 @@ bool RedScreen::need_recreate_context_gl()
#endif
void RedScreen::set_update_interrupt_trigger(EventsLoop::Trigger *trigger)
void RedScreen::set_update_interrupt_trigger(EventSources::Trigger *trigger)
{
_update_interrupt_trigger = trigger;
}

View File

@ -25,7 +25,7 @@
#include "red_window.h"
#include "platform.h"
#include "events_loop.h"
#include "process_loop.h"
#include "threads.h"
class Application;
@ -78,7 +78,7 @@ public:
void untouch_context();
bool need_recreate_context_gl();
#endif
void set_update_interrupt_trigger(EventsLoop::Trigger *trigger);
void set_update_interrupt_trigger(EventSources::Trigger *trigger);
bool update_by_interrupt();
void interrupt_update();
void set_type_gl();
@ -174,7 +174,7 @@ private:
};
PointerLocation _pointer_location;
int _pixel_format_index;
EventsLoop::Trigger *_update_interrupt_trigger;
EventSources::Trigger *_update_interrupt_trigger;
};
#endif

View File

@ -185,7 +185,7 @@ struct TunnelService {
class TunnelChannel::TunnelSocket: public ClientNetSocket {
public:
TunnelSocket(uint16_t id, TunnelService & dst_service, EventsLoop & events_loop,
TunnelSocket(uint16_t id, TunnelService& dst_service, ProcessLoop& process_loop,
EventHandler & event_handler);
virtual ~TunnelSocket() {}
@ -208,10 +208,10 @@ private:
};
TunnelChannel::TunnelSocket::TunnelSocket(uint16_t id, TunnelService& dst_service,
EventsLoop& events_loop,
ProcessLoop& process_loop,
ClientNetSocket::EventHandler& event_handler)
: ClientNetSocket(id, dst_service.ip, htons((uint16_t)dst_service.port),
events_loop, event_handler)
process_loop, event_handler)
, _num_tokens (0)
, _server_num_tokens (0)
, _service_id (dst_service.id)
@ -356,7 +356,7 @@ void TunnelChannel::handle_socket_open(RedPeer::InMessage* message)
open_msg->service_id);
}
sckt = new TunnelSocket(open_msg->connection_id, *service, get_events_loop(), *this);
sckt = new TunnelSocket(open_msg->connection_id, *service, get_process_loop(), *this);
if (sckt->connect(open_msg->tokens)) {
_sockets[open_msg->connection_id] = sckt;

View File

@ -1,158 +0,0 @@
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "events_loop.h"
#include "debug.h"
#include "utils.h"
EventsLoop::EventsLoop()
{
}
EventsLoop::~EventsLoop()
{
}
void EventsLoop::run()
{
for (;;) {
run_once();
}
}
void EventsLoop::run_once(int timeout_milli)
{
DWORD wait_res = WaitForMultipleObjects(_handles.size(), &_handles[0], FALSE, timeout_milli);
if (wait_res == WAIT_FAILED) {
THROW("wait failed");
}
int event_index = wait_res - WAIT_OBJECT_0;
if (event_index < 0 || event_index >= (int)_events.size()) {
THROW("invalid event id");
}
_events[event_index]->action();
}
void EventsLoop::add_socket(Socket& socket)
{
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!event) {
THROW("create event failed");
}
if (WSAEventSelect(socket.get_socket(), event, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR) {
CloseHandle(event);
THROW("event select failed");
}
int size = _events.size();
_events.resize(size + 1);
_handles.resize(size + 1);
_events[size] = &socket;
_handles[size] = event;
}
void EventsLoop::remove_socket(Socket& socket)
{
int size = _events.size();
for (int i = 0; i < size; i++) {
if (_events[i] == &socket) {
if (WSAEventSelect(socket.get_socket(), NULL, 0) == SOCKET_ERROR) {
THROW("event select failed");
}
u_long arg = 0;
if (ioctlsocket(socket.get_socket(), FIONBIO, &arg) == SOCKET_ERROR) {
THROW("set blocking mode failed");
}
CloseHandle(_handles[i]);
for (i++; i < size; i++) {
_events[i - 1] = _events[i];
_handles[i - 1] = _handles[i];
}
_events.resize(size - 1);
_handles.resize(size - 1);
return;
}
}
THROW("socket not found");
}
void EventsLoop::add_trigger(Trigger& trigger)
{
int size = _events.size();
_events.resize(size + 1);
_handles.resize(size + 1);
_events[size] = &trigger;
_handles[size] = trigger.get_handle();
}
void EventsLoop::remove_trigger(Trigger& trigger)
{
int size = _events.size();
for (int i = 0; i < size; i++) {
if (_events[i] == &trigger) {
for (i++; i < size; i++) {
_events[i - 1] = _events[i];
_handles[i - 1] = _handles[i];
}
_events.resize(size - 1);
_handles.resize(size - 1);
return;
}
}
THROW("trigger not found");
}
EventsLoop::Trigger::Trigger()
{
if (!(event = CreateEvent(NULL, FALSE, FALSE, NULL))) {
THROW("create event failed");
}
}
EventsLoop::Trigger::~Trigger()
{
CloseHandle(event);
}
void EventsLoop::Trigger::trigger()
{
if (!SetEvent(event)) {
THROW("set event failed");
}
}
void EventsLoop::Trigger::reset()
{
if (!ResetEvent(event)) {
THROW("set event failed");
}
}
void EventsLoop::Trigger::action()
{
on_event();
}
void EventsLoop::add_file(File& file)
{
}
void EventsLoop::remove_file(File& file)
{
}

View File

@ -1,44 +0,0 @@
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _H_EVENTS_LOOP_P
#define _H_EVENTS_LOOP_P
#include "common.h"
#include <vector>
class EventSourceOld;
class EventsLoop_p {
public:
class Trigger_p;
public:
std::vector<EventSourceOld*> _events;
std::vector<HANDLE> _handles;
};
class EventsLoop_p::Trigger_p {
public:
HANDLE get_handle() { return event;}
public:
HANDLE event;
};
#endif

View File

@ -61,7 +61,7 @@ WaveRecorder::WaveRecorder(Platform::RecordClinet& client, uint32_t sampels_per_
_frame_pos = _frame;
_frame_end = _frame + frame_bytes;
init_ring(sampels_per_sec, frame_bytes, frame_align);
_client.add_evnet_sorce(*this);
_client.add_event_source(*this);
} catch (...) {
delete[] _ring;
delete[] _frame;
@ -74,7 +74,7 @@ WaveRecorder::~WaveRecorder()
{
waveInReset(_wave_in);
reclaim();
_client.remove_evnet_sorce(*this);
_client.remove_event_source(*this);
waveInClose(_wave_in);
delete[] _ring;
delete[] _frame;

View File

@ -21,7 +21,7 @@
#include "audio_devices.h"
#include "platform.h"
class WaveRecorder: public WaveRecordAbstract, public EventsLoop::Trigger {
class WaveRecorder: public WaveRecordAbstract, public EventSources::Trigger {
public:
WaveRecorder(Platform::RecordClinet& client, uint32_t sampels_per_sec,
uint32_t bits_per_sample, uint32_t channels);

View File

@ -228,10 +228,6 @@
RelativePath=".\event_sources_p.cpp"
>
</File>
<File
RelativePath=".\events_loop_p.cpp"
>
</File>
<File
RelativePath="..\gdi_canvas.cpp"
>
@ -452,14 +448,6 @@
RelativePath=".\event_sources_p.h"
>
</File>
<File
RelativePath="..\events_loop.h"
>
</File>
<File
RelativePath=".\events_loop_p.h"
>
</File>
<File
RelativePath="..\..\common\gdi_canvas.h"
>

View File

@ -50,7 +50,6 @@ RED_COMMON_SRCS = \
$(top_srcdir)/client/debug.h \
$(top_srcdir)/client/display_channel.cpp \
$(top_srcdir)/client/display_channel.h \
$(top_srcdir)/client/events_loop.h \
$(top_srcdir)/client/red_gl_canvas.cpp \
$(top_srcdir)/client/red_gl_canvas.h \
$(top_srcdir)/client/gl_canvas.cpp \
@ -109,8 +108,6 @@ bin_PROGRAMS = spicec
spicec_SOURCES = \
atomic_count.h \
events_loop_p.cpp \
events_loop_p.h \
event_sources_p.cpp \
main.cpp \
named_pipe.h \

View File

@ -1,337 +0,0 @@
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/epoll.h>
#include <sys/fcntl.h>
#include "events_loop.h"
#include "debug.h"
#include "utils.h"
#ifdef USING_EVENT_FD
#include <sys/eventfd.h>
#endif
#define NUM_EPOLL_EVENTS 10
#ifdef USING_EVENT_FD
#define WRITE_FD _event_fd
#define EVENT_DATA_TYPE eventfd_t
#else
#define WRITE_FD _event_write_fd
#define EVENT_DATA_TYPE uint8_t
#endif
class EventWrapper {
public:
EventWrapper(EventsLoop& owner, EventSourceOld& event)
: _owner (owner)
, _event (&event)
, _refs (1)
{
}
EventWrapper* ref()
{
_refs++;
return this;
}
void unref()
{
if (!--_refs) {
_owner.remove_wrapper(this);
delete this;
}
}
EventSourceOld* get_event()
{
return _event;
}
void invalidate()
{
_event = NULL;
}
private:
EventsLoop& _owner;
EventSourceOld* _event;
int _refs;
};
EventsLoop::EventsLoop()
{
_epoll = epoll_create(NUM_EPOLL_EVENTS);
if (_epoll == -1) {
THROW("create epool failed");
}
}
EventsLoop::~EventsLoop()
{
Events::iterator iter = _events.begin();
for (; iter != _events.end(); iter++) {
delete *iter;
}
close(_epoll);
}
void EventsLoop::run()
{
for (;;) {
run_once();
}
}
void EventsLoop::run_once(int timeout_milli)
{
struct epoll_event events[NUM_EPOLL_EVENTS];
int num_events = epoll_wait(_epoll, events, NUM_EPOLL_EVENTS, timeout_milli);
if (num_events == -1) {
if (errno == EINTR) {
return;
}
THROW("wait error eventfd failed");
}
for (int i = 0; i < num_events; i++) {
((EventWrapper*)events[i].data.ptr)->ref();
}
for (int i = 0; i < num_events; i++) {
EventWrapper* wrapper;
EventSourceOld* event;
wrapper = (EventWrapper *)events[i].data.ptr;
if ((event = wrapper->get_event())) {
event->action();
}
wrapper->unref();
}
}
void EventsLoop::add_trigger(Trigger& trigger)
{
int fd = trigger.get_fd();
EventWrapper* wrapper = new EventWrapper(*this, trigger);
struct epoll_event event;
event.data.ptr = wrapper;
event.events = EPOLLIN;
if (epoll_ctl(_epoll, EPOLL_CTL_ADD, fd, &event) == -1) {
THROW("epoll add failed");
}
_events.push_back(wrapper);
}
void EventsLoop_p::remove_wrapper(EventWrapper* wrapper)
{
Events::iterator iter = _events.begin();
for (;; iter++) {
if (iter == _events.end()) {
THROW("wrapper not found");
}
if ((*iter) == wrapper) {
_events.erase(iter);
return;
}
}
}
void EventsLoop::remove_trigger(Trigger& trigger)
{
Events::iterator iter = _events.begin();
for (;; iter++) {
if (iter == _events.end()) {
THROW("trigger not found");
}
if ((*iter)->get_event() == &trigger) {
(*iter)->invalidate();
(*iter)->unref();
break;
}
}
int fd = trigger.get_fd();
if (epoll_ctl(_epoll, EPOLL_CTL_DEL, fd, NULL) == -1) {
THROW("epoll remove failed");
}
}
EventsLoop::Trigger::Trigger()
{
#ifdef USING_EVENT_FD
_event_fd = eventfd(0, 0);
if (_event_fd == -1) {
THROW("create eventfd failed");
}
#else
int fd[2];
if (pipe(fd) == -1) {
THROW("create pipe failed");
}
_event_fd = fd[0];
_event_write_fd = fd[1];
#endif
int flags;
if ((flags = fcntl(_event_fd, F_GETFL)) == -1) {
THROW("failed to set eventfd non block: %s", strerror(errno));
}
if (fcntl(_event_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
THROW("failed to set eventfd non block: %s", strerror(errno));
}
}
EventsLoop::Trigger::~Trigger()
{
close(_event_fd);
#ifndef USING_EVENT_FD
close(_event_write_fd);
#endif
}
void EventsLoop::Trigger::trigger()
{
Lock lock(_lock);
if (_pending_int) {
return;
}
_pending_int = true;
static const EVENT_DATA_TYPE val = 1;
if (::write(WRITE_FD, &val, sizeof(val)) != sizeof(val)) {
THROW("write event failed");
}
}
bool EventsLoop_p::Trigger_p::reset_event()
{
Lock lock(_lock);
if (!_pending_int) {
return false;
}
EVENT_DATA_TYPE val;
if (read(_event_fd, &val, sizeof(val)) != sizeof(val)) {
THROW("event read error");
}
_pending_int = false;
return true;
}
void EventsLoop::Trigger::reset()
{
reset_event();
}
void EventsLoop::Trigger::action()
{
if (reset_event()) {
on_event();
}
}
static void set_non_blocking(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1) {
THROW("failed to set socket non block: %s", strerror(errno));
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
THROW("failed to set socket non block: %s", strerror(errno));
}
}
static void set_blocking(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1) {
THROW("failed to clear socket non block: %s", strerror(errno));
}
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1) {
THROW("failed to clear socket non block: %s", strerror(errno));
}
}
static void add_to_poll(int fd, int epoll, EventWrapper* wrapper)
{
struct epoll_event event;
event.data.ptr = wrapper;
event.events = EPOLLIN | EPOLLOUT | EPOLLET;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, fd, &event) == -1) {
THROW("epoll add failed");
}
}
void EventsLoop::add_socket(Socket& socket)
{
int fd = socket.get_socket();
set_non_blocking(fd);
EventWrapper* wrapper = new EventWrapper(*this, socket);
add_to_poll(fd, _epoll, wrapper);
_events.push_back(wrapper);
}
static bool remove_event(EventsLoop_p::Events& events, EventSourceOld& event)
{
EventsLoop_p::Events::iterator iter = events.begin();
for (;; iter++) {
if (iter == events.end()) {
return false;
}
if ((*iter)->get_event() == &event) {
(*iter)->invalidate();
(*iter)->unref();
return true;
}
}
}
void EventsLoop::remove_socket(Socket& socket)
{
if (!remove_event(_events, socket)) {
THROW("socket not found");
}
int fd = socket.get_socket();
if (epoll_ctl(_epoll, EPOLL_CTL_DEL, fd, NULL) == -1) {
THROW("epoll remove failed");
}
set_blocking(fd);
}
void EventsLoop::add_file(File& file)
{
int fd = file.get_fd();
set_non_blocking(fd);
EventWrapper* wrapper = new EventWrapper(*this, file);
add_to_poll(fd, _epoll, wrapper);
_events.push_back(wrapper);
}
void EventsLoop::remove_file(File& file)
{
if (!remove_event(_events, file)) {
THROW("file not found");
}
int fd = file.get_fd();
if (epoll_ctl(_epoll, EPOLL_CTL_DEL, fd, NULL) == -1) {
THROW("epoll remove failed");
}
set_blocking(fd);
}

View File

@ -1,61 +0,0 @@
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _H_EVENTS_LOOP_P
#define _H_EVENTS_LOOP_P
#include "common.h"
#include "threads.h"
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8)
#define USING_EVENT_FD
#endif
#define INFINITE -1
class EventWrapper;
class EventsLoop_p {
public:
class Trigger_p;
void remove_wrapper(EventWrapper*);
public:
int _epoll;
typedef std::list<EventWrapper*> Events;
Events _events;
friend class EventWrapper;
};
class EventsLoop_p::Trigger_p {
public:
Trigger_p() : _pending_int (false) {}
int get_fd() { return _event_fd;}
bool reset_event();
public:
int _event_fd;
#ifndef USING_EVENT_FD
int _event_write_fd;
#endif
bool _pending_int;
Mutex _lock;
};
#endif

View File

@ -20,7 +20,7 @@
#include "debug.h"
class WaveRecorder::EventTrigger: public EventsLoop::File {
class WaveRecorder::EventTrigger: public EventSources::File {
public:
EventTrigger(WaveRecorder& recorder, int fd);
virtual void on_event();
@ -71,7 +71,7 @@ WaveRecorder::~WaveRecorder()
void WaveRecorder::cleanup()
{
if (_event_trigger) {
_client.remove_evnet_sorce(*_event_trigger);
_client.remove_event_source(*_event_trigger);
delete _event_trigger;
}
@ -194,7 +194,7 @@ bool WaveRecorder::init(uint32_t sampels_per_sec,
return false;
}
_event_trigger = new WaveRecorder::EventTrigger(*this, pfd.fd);
_client.add_evnet_sorce(*_event_trigger);
_client.add_event_source(*_event_trigger);
return true;
}