Skip to content

Commit

Permalink
Backport of vanilla bugfixes and enhancements to network objects.
Browse files Browse the repository at this point in the history
In particular, this adds IPv6 support. This was added already in Pd 0.51
by Dan Wilcox, Christof Ressi, and IOhannes Zmoelnig, thanks!

Also thanks to Ico for backporting these to pd-l2ork, which is where I
grabbed this changeset. See:
pd-l2ork/pd-l2ork@9b2c6ec4
  • Loading branch information
agraef committed Sep 29, 2024
1 parent 90dbb79 commit e069f89
Show file tree
Hide file tree
Showing 9 changed files with 1,543 additions and 424 deletions.
10 changes: 5 additions & 5 deletions pd/src/makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ OPT_SAFE_SRC = g_canvas.c g_graph.c g_text.c g_rtext.c g_array.c g_template.c g_
m_pd.c m_class.c m_obj.c m_atom.c m_memory.c m_binbuf.c \
m_conf.c m_glob.c m_sched.c \
s_main.c s_inter.c s_file.c s_print.c \
s_loader.c s_path.c s_entry.c s_audio.c s_midi.c \
s_loader.c s_path.c s_entry.c s_audio.c s_midi.c s_net.c \
s_utf8.c \
d_ugen.c d_arithmetic.c d_dac.c d_misc.c \
d_fft.c d_global.c \
Expand Down Expand Up @@ -142,11 +142,11 @@ NOTHING = $(shell mkdir -p $(BIN_DIR))
$(BIN_DIR)/pd-watchdog: s_watchdog.c
$(CC) $(CFLAGS) $(STRIPFLAG) -o $(BIN_DIR)/pd-watchdog s_watchdog.c

$(BIN_DIR)/pdsend: u_pdsend.c
$(CC) $(CFLAGS) $(STRIPFLAG) -o $(BIN_DIR)/pdsend u_pdsend.c
$(BIN_DIR)/pdsend: u_pdsend.c s_net.c
$(CC) $(CFLAGS) $(STRIPFLAG) -o $(BIN_DIR)/pdsend u_pdsend.c s_net.c

$(BIN_DIR)/pdreceive: u_pdreceive.c $(BIN_DIR)
$(CC) $(CFLAGS) $(STRIPFLAG) -o $(BIN_DIR)/pdreceive u_pdreceive.c
$(BIN_DIR)/pdreceive: u_pdreceive.c s_net.c $(BIN_DIR)
$(CC) $(CFLAGS) $(STRIPFLAG) -o $(BIN_DIR)/pdreceive u_pdreceive.c s_net.c

$(PDEXEC): $(OBJ)
cd ../obj; $(CC) $(LDFLAGS) $(DBG_CFLAGS) -o $(PDEXEC) $(OBJ) $(LIB)
Expand Down
16 changes: 8 additions & 8 deletions pd/src/makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ INCLUDE = -I$(pd_src)/src
GINCLUDE = -I/usr/local/include $(INCLUDE)

LDFLAGS =
LIBS = -lm -lwsock32 -lwinmm -lole32 -lpthread

LIBS = -lm -lws2_32 -lwinmm -lole32 -lpthread
# used to have -lwsock32 instead of -lws2_32
OPT_CFLAGS =

WARN_CFLAGS = -Wall -W -Wstrict-prototypes -Wno-unused \
Expand Down Expand Up @@ -120,7 +120,7 @@ SRC = g_canvas.c g_graph.c g_text.c g_rtext.c g_array.c g_template.c g_io.c \
g_all_guis.c g_bang.c g_radio.c g_mycanvas.c g_numbox.c g_toggle.c \
g_vumeter.c m_pd.c m_class.c m_obj.c m_atom.c m_memory.c \
m_binbuf.c m_conf.c m_glob.c m_sched.c s_main.c s_inter.c s_file.c \
s_print.c s_loader.c s_path.c s_entry.c s_audio.c s_midi.c \
s_print.c s_loader.c s_path.c s_entry.c s_audio.c s_midi.c s_net.c \
d_ugen.c d_ctl.c d_arithmetic.c d_osc.c d_filter.c d_dac.c d_misc.c \
d_math.c d_fft.c d_fft_mayer.c d_fftroutine.c d_array.c d_global.c \
d_delay.c d_resample.c x_arithmetic.c x_connective.c x_interface.c \
Expand All @@ -129,7 +129,7 @@ SRC = g_canvas.c g_graph.c g_text.c g_rtext.c g_array.c g_template.c g_io.c \
x_scalar.c x_vexp.c x_vexp_if.c x_vexp_fun.c \
g_undo.c s_utf8.c x_preset.c g_magicglass.c

SRSRC = u_pdsend.c u_pdreceive.c
SRSRC = u_pdsend.c u_pdreceive.c s_net.c

OBJ = $(SRC:.c=.o)
GOBJ = $(GSRC:.c=.o)
Expand Down Expand Up @@ -194,12 +194,12 @@ $(PMOBJ) : %.o : %.c
$(VOBJ) : %.o : %.c
$(CC) $(CFLAGS) $(GFLAGS) $(INCLUDE) -c -o $*.o $*.c

pdsend.exe: u_pdsend.o
$(CC) $(CFLAGS) $(LDFLAGS) -o pdsend.exe u_pdsend.o $(LIBS)
pdsend.exe: u_pdsend.o s_net.o
$(CC) $(CFLAGS) $(LDFLAGS) -o pdsend.exe u_pdsend.o s_net.o $(LIBS)
$(STRIP) pdsend.exe

pdreceive.exe: u_pdreceive.o
$(CC) $(CFLAGS) $(LDFLAGS) -o pdreceive.exe u_pdreceive.o $(LIBS)
pdreceive.exe: u_pdreceive.o s_net.o
$(CC) $(CFLAGS) $(LDFLAGS) -o pdreceive.exe u_pdreceive.o s_net.o $(LIBS)
$(STRIP) pdreceive.exe

$(PDEXEC): s_entry.o pd.res
Expand Down
112 changes: 86 additions & 26 deletions pd/src/s_inter.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ that didn't really belong anywhere. */
#include "s_stuff.h"
#include "m_imp.h"
#include "g_canvas.h" /* for GUI queueing stuff */
#include "s_net.h"

/* Use this if you want to point the guidir at a local copy of the
* repo while developing. Then recompile and copy the pd-l2ork binary
Expand Down Expand Up @@ -129,8 +130,10 @@ struct _socketreceiver
int sr_intail;
void *sr_owner;
int sr_udp;
struct sockaddr_storage *sr_fromaddr; /* optional */
t_socketnotifier sr_notifier;
t_socketreceivefn sr_socketreceivefn;
t_socketfromaddrfn sr_fromaddrfn; /* optional */
};

extern char *pd_version;
Expand Down Expand Up @@ -532,36 +535,68 @@ static int socketreceiver_doread(t_socketreceiver *x)
static void socketreceiver_getudp(t_socketreceiver *x, int fd)
{
char buf[INBUFSIZE+1];
int ret = recv(fd, buf, INBUFSIZE, 0);
if (ret < 0)
{
sys_sockerror("recv");
sys_rmpollfn(fd);
sys_closesocket(fd);
}
else if (ret > 0)
socklen_t fromaddrlen = sizeof(struct sockaddr_storage);
int ret, readbytes = 0;
while (1)
{
buf[ret] = 0;
#if 0
post("%s", buf);
#endif
if (buf[ret-1] != '\n')
ret = (int)recvfrom(fd, buf, INBUFSIZE, 0,
(struct sockaddr *)x->sr_fromaddr, (x->sr_fromaddr ? &fromaddrlen : 0));
if (ret < 0)
{
#if 0
buf[ret] = 0;
error("dropped bad buffer %s\n", buf);
#endif
/* socket_errno_udp() ignores some error codes */
if (socket_errno_udp())
{
sys_sockerror("recv (udp)");
/* only notify and shutdown a UDP sender! */
if (x->sr_notifier)
{
(*x->sr_notifier)(x->sr_owner, fd);
sys_rmpollfn(fd);
sys_closesocket(fd);
}
}
return;
}
else
else if (ret > 0)
{
char *semi = strchr(buf, ';');
if (semi)
*semi = 0;
binbuf_text(inbinbuf, buf, strlen(buf));
outlet_setstacklim();
if (x->sr_socketreceivefn)
(*x->sr_socketreceivefn)(x->sr_owner, inbinbuf);
else bug("socketreceiver_getudp");
/* handle too large UDP packets */
if (ret > INBUFSIZE)
{
post("warning: incoming UDP packet truncated from %d to %d bytes.",
ret, INBUFSIZE);
ret = INBUFSIZE;
}
buf[ret] = 0;
#if 0
post("%s", buf);
#endif
if (buf[ret-1] != '\n')
{
#if 0
error("dropped bad buffer %s\n", buf);
#endif
}
else
{
char *semi = strchr(buf, ';');
if (semi)
*semi = 0;
if (x->sr_fromaddrfn)
(*x->sr_fromaddrfn)(x->sr_owner, (const void *)x->sr_fromaddr);
binbuf_text(inbinbuf, buf, strlen(buf));
outlet_setstacklim();
if (x->sr_socketreceivefn)
(*x->sr_socketreceivefn)(x->sr_owner,
inbinbuf);
else bug("socketreceiver_getudp");
}
readbytes += ret;
/* throttle */
if (readbytes >= INBUFSIZE)
return;
/* check for pending UDP packets */
if (socket_bytes_available(fd) <= 0)
return;
}
}
}
Expand Down Expand Up @@ -622,6 +657,15 @@ void socketreceiver_read(t_socketreceiver *x, int fd)
if (x->sr_inhead >= INBUFSIZE) x->sr_inhead = 0;
while (socketreceiver_doread(x))
{
if (x->sr_fromaddrfn)
{
socklen_t fromaddrlen = sizeof(struct sockaddr_storage);
if(!getpeername(fd,
(struct sockaddr *)x->sr_fromaddr,
&fromaddrlen))
(*x->sr_fromaddrfn)(x->sr_owner,
(const void *)x->sr_fromaddr);
}
outlet_setstacklim();
if (x->sr_socketreceivefn)
(*x->sr_socketreceivefn)(x->sr_owner, inbinbuf);
Expand All @@ -634,6 +678,22 @@ void socketreceiver_read(t_socketreceiver *x, int fd)
}
}

void socketreceiver_set_fromaddrfn(t_socketreceiver *x,
t_socketfromaddrfn fromaddrfn)
{
x->sr_fromaddrfn = fromaddrfn;
if (fromaddrfn)
{
if (!x->sr_fromaddr)
x->sr_fromaddr = malloc(sizeof(struct sockaddr_storage));
}
else if (x->sr_fromaddr)
{
free(x->sr_fromaddr);
x->sr_fromaddr = NULL;
}
}

void sys_closesocket(int fd)
{
#ifdef HAVE_UNISTD_H
Expand Down
Loading

0 comments on commit e069f89

Please sign in to comment.