[ekg2-commit] r4204 - in trunk: ekg plugins/icq: trunk/ekg/plugins.c trunk/ekg/plugins.h trunk/ekg/stuff.c trunk/ekg/stuff.h trunk/plugins/icq/icq.c
SVN commit
svn w toxygen.net
Czw, 7 Sie 2008, 10:39:37 CEST
Author: darkjames
Date: 2008-08-07 10:39:37 +0200 (Thu, 07 Aug 2008)
New Revision: 4204
Modified:
trunk/ekg/plugins.c
trunk/ekg/plugins.h
trunk/ekg/stuff.c
trunk/ekg/stuff.h
trunk/plugins/icq/icq.c
Log:
use ekg_close(fd) -> instead close(fd) ; when you know you have watches.
use ekg_write(fd, ...) -> instead write(fd, ...); when you send a lot of data, and don't want f**k with WATCH_WRITE
later we'll have ekg_writef(fd, format, ...)
idea of:
j->send_watch = watch_write(....) imho sucks. why developer should looking for pointer, and fd. isn't fd enough?
calling watch_free(j->send_watch) sucks also.
Modified: trunk/ekg/plugins.c
===================================================================
--- trunk/ekg/plugins.c 2008-08-07 08:19:54 UTC (rev 4203)
+++ trunk/ekg/plugins.c 2008-08-07 08:39:37 UTC (rev 4204)
@@ -936,7 +936,7 @@
return;
}
- if (w->type == WATCH_WRITE && w->buf && !w->handler) {
+ if (w->type == WATCH_WRITE && w->buf && !w->handler && w->plugin) { /* XXX */
debug_error("[INTERNAL_DEBUG] WATCH_LINE_WRITE must be removed by plugin, manually (settype to WATCH_NONE and than call watch_free()\n");
return;
}
@@ -1083,11 +1083,25 @@
return res;
}
-int watch_write(watch_t *w, const char *format, ...) {
+int watch_write_data(watch_t *w, const char *buf, int len) { /* XXX, refactory: watch_write() */
+ int was_empty;
+
+ if (!w || !buf || len <= 0)
+ return -1;
+
+ was_empty = !w->buf->len;
+ string_append_raw(w->buf, buf, len);
+
+ if (was_empty)
+ return watch_handle_write(w); /* let's try to write somethink ? */
+ return 0;
+}
+
+int watch_write(watch_t *w, const char *format, ...) { /* XXX, refactory: watch_writef() */
char *text;
- int was_empty;
int textlen;
va_list ap;
+ int res;
if (!w || !format)
return -1;
@@ -1099,17 +1113,17 @@
textlen = xstrlen(text);
debug_io("[watch]_send: %s\n", text ? textlen ? text: "[0LENGTH]":"[FAILED]");
- if (!text) return -1;
- was_empty = !w->buf->len;
- string_append_n(w->buf, text, textlen);
+ if (!text)
+ return -1;
+ res = watch_write_data(w, text, textlen);
+
xfree(text);
-
- if (was_empty) return watch_handle_write(w); /* let's try to write somethink ? */
- return 0;
+ return res;
}
+
/**
* watch_handle()
*
Modified: trunk/ekg/plugins.h
===================================================================
--- trunk/ekg/plugins.h 2008-08-07 08:19:54 UTC (rev 4203)
+++ trunk/ekg/plugins.h 2008-08-07 08:39:37 UTC (rev 4204)
@@ -186,6 +186,7 @@
#else
int watch_write(watch_t *w, const char *format, ...);
#endif
+int watch_write_data(watch_t *w, const char *buf, int len);
watch_t *watch_find(plugin_t *plugin, int fd, watch_type_t type);
void watch_free(watch_t *w);
Modified: trunk/ekg/stuff.c
===================================================================
--- trunk/ekg/stuff.c 2008-08-07 08:19:54 UTC (rev 4203)
+++ trunk/ekg/stuff.c 2008-08-07 08:39:37 UTC (rev 4204)
@@ -79,6 +79,8 @@
#include "vars.h"
#include "windows.h"
#include "xmalloc.h"
+#include "plugins.h"
+#include "sessions.h"
#include "dynstuff_inline.h"
#include "queries.h"
@@ -3130,6 +3132,77 @@
}
/**
+ * ekg_write()
+ *
+ * write data to given fd, if it cannot be done [because system buffer is too small. it'll create watch, and write as soon as possible]
+ * XXX, for now it'll always create watch.
+ * (You can be notified about state of buffer when you call ekg_write(fd, NULL, -1))
+ *
+ * @note
+ * This _should_ be used as replacement for write()
+ */
+
+int ekg_write(int fd, const char *buf, int len) {
+ watch_t *wl = NULL;
+ list_t l;
+
+ if (fd == -1)
+ return -1;
+
+ /* first check if we have watch for this fd */
+ for (l = watches; l; l = l->next) {
+ watch_t *w = l->data;
+
+ if (w && w->fd == fd && w->type == WATCH_WRITE && w->buf) {
+ wl = w;
+ break;
+ }
+ }
+
+ if (wl) {
+ if (!buf && len == -1) /* smells stupid, but do it */
+ return wl->buf->len;
+ } else {
+ /* if we have no watch, let's create it. */ /* XXX, first try write() ? */
+ wl = watch_add(NULL, fd, WATCH_WRITE_LINE, NULL, NULL);
+ }
+
+ return watch_write_data(wl, buf, len);
+}
+
+/* XXX, int ekg_writef(int fd, const char *format, ...); */
+
+/**
+ * ekg_close()
+ *
+ * close fd and all watches associated with that fd
+ *
+ * @note
+ * This _should_ be used as replacement for close() (especially in protocol plugins)
+ */
+
+int ekg_close(int fd) {
+ list_t l;
+
+ if (fd == -1)
+ return -1;
+
+ for (l = watches; l; l = l->next) {
+ watch_t *w = l->data;
+
+ if (w && w->fd == fd) {
+ debug("ekg_close(%d) w->plugin: %s w->session: %s w->type: %d w->buf: %d\n",
+ fd, w->plugin ? w->plugin->name : "-",
+ w->is_session ? ((session_t *) w->data)->uid : "-",
+ w->type, !!w->buf);
+
+ watch_free(w);
+ }
+ }
+ return close(fd);
+}
+
+/**
* password_input()
*
* Try to get password through UI_PASSWORD_INPUT, printing error messages if needed.
Modified: trunk/ekg/stuff.h
===================================================================
--- trunk/ekg/stuff.h 2008-08-07 08:19:54 UTC (rev 4203)
+++ trunk/ekg/stuff.h 2008-08-07 08:39:37 UTC (rev 4204)
@@ -406,6 +406,9 @@
watch_t *ekg_resolver2(plugin_t *plugin, const char *server, watcher_handler_func_t async, void *data);
watch_t *ekg_resolver3(plugin_t *plugin, const char *server, watcher_handler_func_t async, void *data);
+int ekg_close(int fd);
+int ekg_write(int fd, const char *buf, int len);
+
#endif
#endif /* __EKG_STUFF_H */
Modified: trunk/plugins/icq/icq.c
===================================================================
--- trunk/plugins/icq/icq.c 2008-08-07 08:19:54 UTC (rev 4203)
+++ trunk/plugins/icq/icq.c 2008-08-07 08:39:37 UTC (rev 4204)
@@ -66,28 +66,7 @@
debug_io("icq_send_pkt(%s) fd: %d len: %d\n", s->uid, fd, buf->len);
icq_hexdump(DEBUG_IO, (unsigned char *) buf->str, buf->len);
-
- for (l = watches; l; l = l->next) {
- watch_t *w = l->data;
-
- if (w && w->plugin == &icq_plugin && w->is_session && w->data == s && w->type == WATCH_WRITE && w->buf && w->fd == fd) {
- int ready = (w->buf->len == 0);
-
- string_append_raw(w->buf, buf->str, buf->len);
- string_free(buf, 1);
-
- if (ready)
- watch_handle_write(w);
- return !ready;
- }
- }
-
- w = watch_add_session(s, fd, WATCH_WRITE_LINE, NULL);
-
- string_append_raw(w->buf, buf->str, buf->len);
- string_free(buf, 1);
-
- watch_handle_write(w);
+ ekg_write(fd, buf->str, buf->len);
return 0;
}
@@ -473,15 +452,13 @@
timer_remove_session(s, "ping");
protocol_disconnected_emit(s, reason, type);
- /* XXX, watch_free() */
-
if (j->fd != -1) {
- close(j->fd);
+ ekg_close(j->fd);
j->fd = -1;
}
if (j->fd2 != -1) {
- close(j->fd2);
+ ekg_close(j->fd2);
j->fd2 = -1;
}
}
@@ -572,7 +549,7 @@
}
if (j->fd2 != -1) { /* fd changed */
- close(j->fd);
+ ekg_close(j->fd);
j->fd = j->fd2;
j->fd2 = -1;
Więcej informacji o liście dyskusyjnej ekg2-commit