ref: 2663fd26c74a163a30328f9fecbe8b5f46c977f6
parent: ae2b94dda420756baca88a6ef97a1b8ad904670e
author: qwx <qwx@sciops.net>
date: Sun Oct 27 16:51:41 EDT 2024
doom-udpip: use libthread instead of rfork, fixes breakage
--- a/doom-udpip
+++ b/doom-udpip
@@ -1,43 +1,7 @@
-port udp/ip code from release.
-this works, but parts of the code suck and need to be altered. what sucks
-is that the doom networking protocol is shit. it's a peer-to-peer thing
-where each node needs to send frame information (ticcmd's) to every other
-node, and receive ticcmd's from every other node. any node that has a
-slower connection or is a slower machine will bring the game down for
-everyone. also, high bandwidth required.
-
-shitfuck n2: no port uses this protocol, since it sucks, and everyone
-implements their own incompatible crap, so no one port can talk to another,
-besides maybe the ports back to DOS. the code in i_net.c is unlikely to
-change too much though, unless this implementation is amended.
-one idea is to ask for chocolate-doom's networking protocol specs.
-chocolate-doom works fairly well, and supports a bunch of different OS',
-including openbsd, and being able to connect with 9front doom to another
-OS' shit is advanteageous, at least because live servers with bunches of
-players will be accessible. 9front doom and chocolate-doom _should_ be
-compatible, but idk.
-as it is, sdldoom might be able to connect to 9front doom with this patch,
-but good luck making sdldoom work.
-
-player 0 is special, and sets the game settings, sending them to the other
-nodes.
-testing this is more difficult without other patches because of the issues
-in the sound code, so try with doom-sndmus.patch and doom-sleep.patch.
-
-syntax: doom [-srv port] [-pn 0-3] [-net host!port...]
-defaults: udp port 666, player number 0
-
-example for a 3 player game, two nodes being on the same host (for shits):
-u14% games/doom -altdeath -nomonsters -warp7 -net u6!666 u14!667
-u6% games/doom -pn 1 -net u14!666 u14!667
-u14-1% games/doom -pn 2 -srv 667 -net u14!666 u6!666
-
-TODO: cf FIXME's in the code, conreq shit, more testing(?)
-
-diff -Naur a/sys/src/games/doom/i_net.c b/sys/src/games/doom/i_net.c
---- a/sys/src/games/doom/i_net.c Wed Jan 18 01:13:24 2012
-+++ b/sys/src/games/doom/i_net.c Wed Sep 16 06:45:07 2015
-@@ -20,142 +20,298 @@
+diff 790a516884e45ee2a3a11b915f5e125a0ccb02ca uncommitted
+--- a/sys/src/games/doom/i_net.c
++++ b/sys/src/games/doom/i_net.c
+@@ -20,142 +20,296 @@
//
//-----------------------------------------------------------------------------
@@ -53,6 +17,7 @@
+#include <bio.h>
+#include <ndb.h>
+#include <ip.h>
++#include <thread.h>
#include "m_argv.h"
-
+#include "i_system.h"
@@ -66,19 +31,12 @@
-// I_InitNetwork
-//
-void I_InitNetwork (void)
--{
--printf("PORTME i_net.c I_InitNetwork (use 9P)\n");
+enum{
+ HDRSZ = 16+16+16+2+2 /* sizeof Udphdr w/o padding */
+};
-
-- doomcom = malloc (sizeof(*doomcom));
-- memset (doomcom, 0, sizeof(*doomcom));
++
+static char lsrv[6] = "666";
-
-- /* set up for network */
-- doomcom->ticdup = 1;
-- doomcom->extratics = 0;
++
+struct Addr{
+ Udphdr h;
+ char srv[6]; /* convenience */
@@ -86,33 +44,36 @@
+ long called;
+};
+static Addr raddr[MAXNETNODES];
-
--// netsend = PacketSend;
--// netget = PacketGet;
--// netgame = true;
++
+static int ucfd;
+static int udfd;
+static int upfd[2];
+static int upid;
-
-- /* parse player number and host list */
--// doomcom->consoleplayer = myargv[i+1][0]-'1';
-
-- doomcom->numnodes = 1; // this node for sure
++
++
+static void
+conreq(doomdata_t *d)
-+{
+ {
+-printf("PORTME i_net.c I_InitNetwork (use 9P)\n");
+ int fd;
+ long t;
+ char ip[64];
+ Addr *p;
-+
+
+- doomcom = malloc (sizeof(*doomcom));
+- memset (doomcom, 0, sizeof(*doomcom));
+ p = &raddr[doomcom->remotenode];
-+
+
+- /* set up for network */
+- doomcom->ticdup = 1;
+- doomcom->extratics = 0;
+ t = time(nil);
+ if(t - p->called < 1)
+ return;
-+
+
+-// netsend = PacketSend;
+-// netget = PacketGet;
+-// netgame = true;
+ snprint(ip, sizeof ip, "%I", p->h.raddr);
+ if((fd = dial(netmkaddr(ip, "udp", p->srv), lsrv, nil, nil)) < 0)
+ sysfatal("dial: %r");
@@ -122,8 +83,8 @@
+ p->called = t;
+}
-- doomcom->id = DOOMCOM_ID;
-- doomcom->numplayers = doomcom->numnodes;
+- /* parse player number and host list */
+-// doomcom->consoleplayer = myargv[i+1][0]-'1';
+static void
+dsend(void)
+{
@@ -130,13 +91,16 @@
+ int i;
+ uchar buf[HDRSZ+sizeof(doomdata_t)];
+ doomdata_t d;
-+
+
+- doomcom->numnodes = 1; // this node for sure
+ hnputl(&d.checksum, netbuffer->checksum);
+ d.player = netbuffer->player;
+ d.retransmitfrom = netbuffer->retransmitfrom;
+ d.starttic = netbuffer->starttic;
+ d.numtics = netbuffer->numtics;
-+
+
+- doomcom->id = DOOMCOM_ID;
+- doomcom->numplayers = doomcom->numnodes;
+ for(i = 0; i < netbuffer->numtics; i++){
+ d.cmds[i].forwardmove = netbuffer->cmds[i].forwardmove;
+ d.cmds[i].sidemove = netbuffer->cmds[i].sidemove;
@@ -193,14 +157,30 @@
- doomcom->consoleplayer = 0;
- return;
- }
--
++ if(!raddr[doomcom->remotenode].ready){
++ conreq(&d);
++ return;
++ }
++ memcpy(buf, &raddr[doomcom->remotenode].h, HDRSZ);
++ memcpy(buf+HDRSZ, &d, sizeof d);
+
- netsend = PacketSend;
- netget = PacketGet;
- netgame = true;
--
++ i = doomcom->datalength + HDRSZ;
++ if(write(udfd, buf, i) != i)
++ sysfatal("dsend: %r");
++}
+
- // parse player number and host list
- doomcom->consoleplayer = myargv[i+1][0]-'1';
--
++static void
++drecv(void)
++{
++ int n;
++ ushort i;
++ doomdata_t d;
+
- doomcom->numnodes = 1; // this node for sure
-
- i++;
@@ -212,29 +192,17 @@
- {
- sendaddress[doomcom->numnodes].sin_addr.s_addr
- = inet_addr (myargv[i]+1);
-+ if(!raddr[doomcom->remotenode].ready){
-+ conreq(&d);
-+ return;
- }
-+ memcpy(buf, &raddr[doomcom->remotenode].h, HDRSZ);
-+ memcpy(buf+HDRSZ, &d, sizeof d);
-+
-+ i = doomcom->datalength + HDRSZ;
-+ if(write(udfd, buf, i) != i)
-+ sysfatal("dsend: %r");
-+}
-+
-+static void
-+drecv(void)
-+{
-+ int n;
-+ ushort i;
-+ doomdata_t d;
-+
+ if(filelength(upfd[1]) < 1){
+ doomcom->remotenode = -1;
+ return;
-+ }
+ }
+- else
+- {
+- hostentry = gethostbyname (myargv[i]);
+- if (!hostentry)
+- I_Error ("gethostbyname: couldn't find %s", myargv[i]);
+- sendaddress[doomcom->numnodes].sin_addr.s_addr
+- = *(int *)hostentry->h_addr_list[0];
+ if((n = read(upfd[1], &d, sizeof d)) <= 0
+ || read(upfd[1], &i, sizeof i) <= 0)
+ sysfatal("drecv: %r");
@@ -255,17 +223,25 @@
+ netbuffer->cmds[i].consistancy = nhgets(&d.cmds[i].consistancy);
+ netbuffer->cmds[i].chatchar = d.cmds[i].chatchar;
+ netbuffer->cmds[i].buttons = d.cmds[i].buttons;
-+ }
-+}
-+
+ }
+- doomcom->numnodes++;
+- }
+-
+- doomcom->id = DOOMCOM_ID;
+- doomcom->numplayers = doomcom->numnodes;
+-*/
+ }
+
+static void
-+uproc(void)
++uproc(void*)
+{
+ int n;
+ ushort i;
+ uchar buf[HDRSZ+sizeof(doomdata_t)];
+ Udphdr h;
-+
+
+-void I_NetCmd (void)
++ upid = getpid();
+ for(;;){
+ if((n = read(udfd, buf, sizeof buf)) <= 0)
+ break;
@@ -290,25 +266,24 @@
+
+void
+I_NetCmd(void)
-+{
+ {
+-/*
+- if (doomcom->command == CMD_SEND)
+- {
+- netsend ();
+- }
+- else if (doomcom->command == CMD_GET)
+- {
+- netget ();
+- }
+- else
+- I_Error ("Bad net cmd: %i\n",doomcom->command);
+-*/
+ if(doomcom->command == CMD_SEND)
+ dsend();
+ else if(doomcom->command == CMD_GET)
+ drecv();
- else
-- {
-- hostentry = gethostbyname (myargv[i]);
-- if (!hostentry)
-- I_Error ("gethostbyname: couldn't find %s", myargv[i]);
-- sendaddress[doomcom->numnodes].sin_addr.s_addr
-- = *(int *)hostentry->h_addr_list[0];
-- }
-- doomcom->numnodes++;
-- }
--
-- doomcom->id = DOOMCOM_ID;
-- doomcom->numplayers = doomcom->numnodes;
--*/
++ else
+ I_Error("invalid netcmd %d", doomcom->command);
+}
+
@@ -325,7 +300,6 @@
+static void
+initudp(void)
+{
-+ int pid;
+ char data[64], adir[40];
+
+ /* FIXME */
@@ -342,11 +316,8 @@
+
+ if(pipe(upfd) < 0)
+ sysfatal("pipe: %r");
-+ if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
-+ uproc();
-+ exits(nil);
-+ }
-+ upid = pid;
++ if(procrfork(uproc, nil, mainstacksize, RFFDG) < 0)
++ sysfatal("procrfork: %r");
+}
+
+static void
@@ -420,8 +391,8 @@
+ }
+
+ return 0;
- }
-
++}
++
+void
+I_InitNetwork(void)
+{
@@ -439,26 +410,11 @@
+
+ fmtinstall('I', eipfmt);
+ initudp();
-
--void I_NetCmd (void)
--{
--/*
-- if (doomcom->command == CMD_SEND)
-- {
-- netsend ();
-- }
-- else if (doomcom->command == CMD_GET)
-- {
-- netget ();
-- }
-- else
-- I_Error ("Bad net cmd: %i\n",doomcom->command);
--*/
++
+ netgame = true;
}
-diff -Naur a/sys/src/games/doom/i_net.h b/sys/src/games/doom/i_net.h
---- a/sys/src/games/doom/i_net.h Wed Jan 18 01:13:23 2012
-+++ b/sys/src/games/doom/i_net.h Wed Sep 16 06:36:09 2015
+--- a/sys/src/games/doom/i_net.h
++++ b/sys/src/games/doom/i_net.h
@@ -35,6 +35,7 @@
void I_InitNetwork (void);
@@ -467,9 +423,8 @@
#endif
-diff -Naur a/sys/src/games/doom/i_system.c b/sys/src/games/doom/i_system.c
---- a/sys/src/games/doom/i_system.c Wed Feb 5 08:58:15 2014
-+++ b/sys/src/games/doom/i_system.c Wed Sep 16 06:36:09 2015
+--- a/sys/src/games/doom/i_system.c
++++ b/sys/src/games/doom/i_system.c
@@ -6,6 +6,7 @@
#include "i_system.h"
#include "i_sound.h"
@@ -476,9 +431,9 @@
#include "i_video.h"
+#include "i_net.h"
+ #include "d_main.h"
#include "d_net.h"
- #include "g_game.h"
-@@ -41,6 +42,7 @@
+@@ -42,6 +43,7 @@
void I_Quit (void)
{
D_QuitNetGame ();
@@ -486,7 +441,7 @@
I_ShutdownSound();
I_ShutdownMusic();
M_SaveDefaults ();
-@@ -85,6 +87,7 @@
+@@ -86,6 +88,7 @@
G_CheckDemoStatus();
D_QuitNetGame ();
@@ -494,14 +449,14 @@
I_ShutdownGraphics();
exits("I_Error");
-diff -Naur a/sys/src/games/doom/w_wad.h b/sys/src/games/doom/w_wad.h
---- a/sys/src/games/doom/w_wad.h Wed Jan 18 01:13:24 2012
-+++ b/sys/src/games/doom/w_wad.h Wed Sep 16 06:38:33 2015
-@@ -67,6 +67,7 @@
+--- a/sys/src/games/doom/w_wad.h
++++ b/sys/src/games/doom/w_wad.h
+@@ -67,6 +67,8 @@
void* W_CacheLumpNum (int lump, int tag);
void* W_CacheLumpName (char* name, int tag);
+vlong filelength(int);
-
-
-
++
+ #endif
+ //-----------------------------------------------------------------------------
+ //