Skip to content

Commit

Permalink
EventLoop: Add pullDown().
Browse files Browse the repository at this point in the history
Calling EventLoop_Destroy() will also delete entries registered
by other classes.
  • Loading branch information
tindzk committed Aug 19, 2011
1 parent 49d3dc6 commit 9f16702
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
24 changes: 18 additions & 6 deletions src/EventLoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ def(void, Destroy) {
LinkedList_OnDestroy_For(this, ref(_DestroyEntry)));
}

def(ref(Entry) *, AddChannel, Channel *ch, ref(OnInput) onInput, ref(OnOutput) onOutput, ref(OnDestroy) onDestroy) {
def(void, pullDown, void *object) {
DoublyLinkedList_safeEach(&this->entries, node) {
if (node->object == object) {
DoublyLinkedList_Remove(&this->entries, node);
call(_DestroyEntry, node);
}
}
}

def(ref(Entry) *, AddChannel, void *object, Channel *ch, ref(OnInput) onInput, ref(OnOutput) onOutput, ref(OnDestroy) onDestroy) {
ref(Entry) *entry = Memory_New(sizeof(ref(Entry)) + sizeof(ref(ChannelEntry)));

entry->type = ref(EntryType_Channel);
entry->type = ref(EntryType_Channel);
entry->object = object;

ref(ChannelEntry) *data = (void *) entry->data;

Expand Down Expand Up @@ -91,10 +101,11 @@ def(void, DetachChannel, ref(Entry) *entry, bool watcher) {
}

/* Listens for incoming connections. */
def(void, AttachSocket, SocketServer *socket, ref(OnConnection) onConnection) {
def(void, AttachSocket, void *object, SocketServer *socket, ref(OnConnection) onConnection) {
ref(Entry) *entry = Memory_New(sizeof(ref(Entry)) + sizeof(ref(SocketEntry)));

entry->type = ref(EntryType_Socket);
entry->type = ref(EntryType_Socket);
entry->object = object;

ref(SocketEntry) *data = (void *) entry->data;

Expand All @@ -111,7 +122,7 @@ def(void, AttachSocket, SocketServer *socket, ref(OnConnection) onConnection) {
}

/* Accepts incoming connection and listens for data. */
def(ref(ClientEntry) *, AcceptClient, SocketServer *socket, bool edgeTriggered, ref(Client) client) {
def(ref(ClientEntry) *, AcceptClient, void *object, SocketServer *socket, bool edgeTriggered, ref(Client) client) {
int flags = 0;

if (edgeTriggered) {
Expand Down Expand Up @@ -139,7 +150,8 @@ def(ref(ClientEntry) *, AcceptClient, SocketServer *socket, bool edgeTriggered,
sizeof(ref(ClientEntry)) +
delegate(client, getSize));

entry->type = ref(EntryType_Client);
entry->type = ref(EntryType_Client);
entry->object = object;

ref(ClientEntry) *data = (void *) entry->data;
data->client = client;
Expand Down
8 changes: 5 additions & 3 deletions src/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(ref(EntryType)) {

record(ref(Entry)) {
DoublyLinkedList_DeclareRef(ref(Entry));
void *object;
ref(EntryType) type;
char data[];
};
Expand All @@ -63,10 +64,11 @@ class {

rsdef(self, New);
def(void, Destroy);
def(ref(Entry) *, AddChannel, Channel *ch, ref(OnInput) onInput, ref(OnOutput) onOutput, ref(OnDestroy) onDestroy);
def(void, pullDown, void *object);
def(ref(Entry) *, AddChannel, void *object, Channel *ch, ref(OnInput) onInput, ref(OnOutput) onOutput, ref(OnDestroy) onDestroy);
def(void, DetachChannel, ref(Entry) *entry, bool watcher);
def(void, AttachSocket, SocketServer *socket, ref(OnConnection) onConnection);
def(ref(ClientEntry) *, AcceptClient, SocketServer *socket, bool edgeTriggered, ref(Client) client);
def(void, AttachSocket, void *object, SocketServer *socket, ref(OnConnection) onConnection);
def(ref(ClientEntry) *, AcceptClient, void *object, SocketServer *socket, bool edgeTriggered, ref(Client) client);
def(void, DetachClient, void *addr);
def(void, Enqueue, void *addr, int events);
def(void, ClientEnqueue, void *addr, int events);
Expand Down
18 changes: 10 additions & 8 deletions src/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ rsdef(self, New, ConnectionInterface *conn, Logger *logger) {
def(void, Destroy) {
SocketServer_Destroy(&this->socket);

/* The EventLoop may still have pointers to this instance. Therefore, we
* need to destroy it now before the object gets ivnalidated.
/* The EventLoop may still have pointers to this instance.
* Therefore, we need to destroy it now before the object gets
* invalidated.
* Only delete those entries that were registered by this class.
*/
EventLoop_Destroy(EventLoop_GetInstance());
EventLoop_pullDown(EventLoop_GetInstance(), this);
}

def(void, SetEdgeTriggered, bool value) {
this->edgeTriggered = value;
}

static def(void, OnDestroy, ref(ClientDynInst) inst) {
assert(this->conn->destroy != NULL);
assert(this->conn->destroy != null);
this->conn->destroy(inst.addr->object);
}

static def(void, OnPull, ref(ClientDynInst) inst) {
assert(this->conn->pull != NULL);
assert(this->conn->pull != null);
this->conn->pull(inst.addr->object);
}

static def(void, OnPush, ref(ClientDynInst) inst) {
assert(this->conn->push != NULL);
assert(this->conn->push != null);
this->conn->push(inst.addr->object);
}

Expand All @@ -49,7 +51,7 @@ static def(size_t, GetSize) {

static def(void, OnConnection, SocketServer *socket) {
EventLoop_ClientEntry *entry =
EventLoop_AcceptClient(EventLoop_GetInstance(),
EventLoop_AcceptClient(EventLoop_GetInstance(), this,
socket, this->edgeTriggered, call(AsEventLoop_Client));

ref(Client) *client = (void *) entry->data;
Expand All @@ -64,7 +66,7 @@ def(void, Listen, unsigned short port) {
SocketServer_Bind(&this->socket, port);
SocketServer_Listen(&this->socket, ref(ConnectionLimit));

EventLoop_AttachSocket(EventLoop_GetInstance(), &this->socket,
EventLoop_AttachSocket(EventLoop_GetInstance(), this, &this->socket,
EventLoop_OnConnection_For(this, ref(OnConnection)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def(void, listen) {
assert(this->evLoop == NULL);

this->evLoop =
EventLoop_AddChannel(EventLoop_GetInstance(), &this->ch,
EventLoop_AddChannel(EventLoop_GetInstance(), this, &this->ch,
EventLoop_OnInput_For(this, ref(onSignal)),
EventLoop_OnOutput_Empty(),
EventLoop_OnDestroy_Empty());
Expand Down

0 comments on commit 9f16702

Please sign in to comment.