Addons updated to new doc format
[io.git] / addons / Socket / source / IoEvent.c
blobbb6bf22b1c3fb06525de3e121d3d09fd98d7594a
1 //metadoc Event copyright Steve Dekorte, 2004
2 //metadoc Event license BSD revised
3 //metadoc Event description Networking Event.
4 //metadoc category Networking
6 #include "IoEvent.h"
7 #include "IoState.h"
8 #include "IoNumber.h"
9 #include "IoSeq.h"
11 #define EVENT(self) ((struct event *)IoObject_dataPointer(self))
13 IoEvent *IoMessage_locals_eventArgAt_(IoMessage *self, IoObject *locals, int n)
15 IoObject *v = IoMessage_locals_valueArgAt_(self, locals, n);
17 if (!ISEVENT(v))
19 IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "Event");
22 return v;
25 IoTag *IoEvent_newTag(void *state)
27 IoTag *tag = IoTag_newWithName_("Event");
28 IoTag_state_(tag, state);
29 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoEvent_rawClone);
30 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoEvent_free);
31 return tag;
34 IoEvent *IoEvent_proto(void *state)
36 IoObject *self = IoObject_new(state);
38 IoObject_tag_(self, IoEvent_newTag(state));
39 IoObject_setDataPointer_(self, (struct event *)calloc(1, sizeof(struct event)));
41 IoState_registerProtoWithFunc_((IoState *)state, self, IoEvent_proto);
44 IoMethodTable methodTable[] = {
45 {"isPending", IoEvent_isPending},
46 {"isValid", IoEvent_isValid},
47 {NULL, NULL},
50 IoObject_addMethodTable_(self, methodTable);
53 IoObject_setSlot_to_(self, IOSYMBOL("EV_READ"), IONUMBER(EV_READ));
54 IoObject_setSlot_to_(self, IOSYMBOL("EV_WRITE"), IONUMBER(EV_WRITE));
55 IoObject_setSlot_to_(self, IOSYMBOL("EV_SIGNAL"), IONUMBER(EV_SIGNAL));
56 //IoObject_setSlot_to_(self, IOSYMBOL("EV_TIMEOUT"), IONUMBER(EV_TIMEOUT));
57 //IoObject_setSlot_to_(self, IOSYMBOL("EV_PERSIST"), IONUMBER(EV_PERSIST));
59 return self;
62 IoEvent *IoEvent_rawClone(IoEvent *proto)
64 IoObject *self = IoObject_rawClonePrimitive(proto);
65 IoObject_setDataPointer_(self, (struct event *)calloc(1, sizeof(struct event)));
66 return self;
69 IoEvent *IoEvent_new(void *state)
71 IoObject *proto = IoState_protoWithInitFunction_((IoState *)state, IoEvent_proto);
72 return IOCLONE(proto);
75 void IoEvent_free(IoEvent *self)
77 // this check ensures that libevent is never holding a referenced
78 // to an IoEvent that has been collected
80 if (event_initialized(EVENT(self)) && event_pending(EVENT(self), 0, NULL))
82 event_del(EVENT(self));
85 free(EVENT(self));
88 struct event *IoEvent_rawEvent(IoEvent *self)
90 return EVENT(self);
93 IoObject *IoEvent_isPending(IoEvent *self, IoObject *locals, IoMessage *m)
95 int result = event_pending(EVENT(self), 0, NULL);
96 return IOBOOL(self, result);
99 IoObject *IoEvent_isValid(IoEvent *self, IoObject *locals, IoMessage *m)
101 return RawDescriptor_isValid(EVENT(self)->ev_fd) ? IOTRUE(self) : IOFALSE(self);