ref: b402639434e9360c99fc8b9cb12d51614abfbeea
parent: b579d46f62e61c4fbb97ed9edd024ddb26def34d
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Jul 19 15:45:06 EDT 2025
adds async read for replies and filesystem stub
--- a/fns.h
+++ b/fns.h
@@ -6,6 +6,7 @@
void clearrequest(Request);
void reply(Reply, ...);
+int hasreplies(char*);
int Rfmt(Fmt *f);
char *getversion(void);
--- a/ircd.c
+++ b/ircd.c
@@ -1,5 +1,9 @@
#include <u.h>
#include <libc.h>
+#include <auth.h>
+#include <fcall.h>
+#include <thread.h>
+#include <9p.h>
#include <bio.h>
#include "dat.h"
#include "fns.h"
@@ -42,9 +46,94 @@
}
}
+char *mtpt = "/mnt/ircd";
+char *srvname = "ircd";
+Reqqueue *reqqueue = nil;
+
+// TODO: implement hasdata for client. put all replies in a bulk buffer.
+int hasdata = 0;
+QLock datalock;
+
+static void
+readproc(Req *r)
+{
+ while (1) {
+ qlock(&datalock);
+ if (hasdata) {
+ readstr(r, "hasdata\n");
+ fprint(2, "responded with hasdata\n");
+ hasdata = 0;
+ qunlock(&datalock);
+ respond(r, nil);
+ return;
+ }
+ qunlock(&datalock);
+ }
+}
+
+static void
+fsread(Req *r)
+{
+ reqqueuepush(reqqueue, r, readproc);
+}
+
+static void
+fswrite(Req *r)
+{
+ char line[512];
+ char *c;
+ Request parsed;
+
+ if (r->ifcall.count > 512) {
+ respond(r, "too many bytes");
+ return;
+ }
+ memcpy(line, r->ifcall.data, r->ifcall.count);
+ r->ofcall.count = r->ifcall.count;
+ line[r->ifcall.count-1] = 0; /* last char is either \n or \r */
+
+ c = strchr(line, ' ');
+ if (c)
+ *c = 0;
+
+ parsed = parseline(line);
+ if (debug > 1)
+ fprint(2, "request:%R", parsed);
+ execrequest(parsed);
+
+ qlock(&datalock);
+ hasdata = 1;
+ qunlock(&datalock);
+
+ respond(r, nil);
+}
+
+static void
+fsflush(Req *r)
+{
+ reqqueueflush(reqqueue, r->oldreq);
+ respond(r, nil);
+}
+
+Srv fs = {
+ .read = fsread,
+ .write = fswrite,
+ .flush = fsflush,
+};
+
void
-main(int argc, char **argv)
+populatetree(File *root)
{
+ File *f;
+
+ if ((f = createfile(root, "irc", nil, 0666, nil)) != nil) {
+ f->length = 0;
+ }
+}
+
+void
+threadmain(int argc, char **argv)
+{
ARGBEGIN{
case 'h':
usage();
@@ -52,17 +141,29 @@
case 'v':
fprint(2, "version: %s\n", getversion());
exits(nil);
+ case 's':
+ srvname = EARGF(usage());
+ break;
+ case 'm':
+ mtpt = EARGF(usage());
+ break;
case 'd':
debug++;
break;
+ case 'D':
+ chatty9p++;
+ break;
}ARGEND;
fmtinstall('R', Rfmt);
sysnameb = sysname();
+ reqqueue = reqqueuecreate();
- bio = Bfdopen(0, OREAD);
- parselines();
+ fs.tree = alloctree(nil, nil, DMDIR|0777, nil);
+ populatetree(fs.tree->root);
+ threadpostmountsrv(&fs, srvname, mtpt, MREPL|MCREATE);
+ exits(nil);
}
void
--
⑨