From 02a5595973b61d2cf789b9074167ed73b928d157 Mon Sep 17 00:00:00 2001 From: Guenter Geiger Date: Wed, 13 Nov 2002 08:38:02 +0000 Subject: This commit was generated by cvs2svn to compensate for changes in r206, which included commits to RCS files with non-trunk default branches. svn path=/trunk/; revision=207 --- xgui/netserver/help-netserver.pd | 48 ++++ xgui/netserver/makefile | 88 ++++++ xgui/netserver/netserver.c | 563 +++++++++++++++++++++++++++++++++++++++ xgui/netserver/netserver.dll | Bin 0 -> 36864 bytes xgui/netserver/netserver.exp | Bin 0 -> 594 bytes xgui/netserver/netserver.lib | Bin 0 -> 1986 bytes xgui/netserver/netserver.zip | Bin 0 -> 31494 bytes 7 files changed, 699 insertions(+) create mode 100644 xgui/netserver/help-netserver.pd create mode 100644 xgui/netserver/makefile create mode 100644 xgui/netserver/netserver.c create mode 100644 xgui/netserver/netserver.dll create mode 100644 xgui/netserver/netserver.exp create mode 100644 xgui/netserver/netserver.lib create mode 100644 xgui/netserver/netserver.zip (limited to 'xgui/netserver') diff --git a/xgui/netserver/help-netserver.pd b/xgui/netserver/help-netserver.pd new file mode 100644 index 00000000..10688163 --- /dev/null +++ b/xgui/netserver/help-netserver.pd @@ -0,0 +1,48 @@ +#N canvas 106 97 754 476 12; +#X floatatom 49 333 5 0 0; +#X floatatom 87 298 5 0 0; +#X symbolatom 164 251 10 0 0; +#X text 102 332 received data; +#X text 140 298 number of connections; +#X msg 49 54 print; +#X floatatom 125 272 5 0 0; +#X text 183 276 socket number; +#X msg 103 179 broadcast hallo world!; +#X text 288 179 send to all clients; +#X text 144 33 written by Olaf Matthes ; +#X obj 49 223 netserver 3000; +#X text 137 120 send message to client no. 1; +#X text 256 251 client's IP address; +#X msg 498 116 connect localhost 3000; +#X msg 511 143 disconnect; +#X msg 477 84 send 23; +#X floatatom 544 218 5 0 0; +#X msg 83 91 send 380 17.3; +#X floatatom 477 296 5 0 0; +#X obj 600 281 print anything; +#X obj 538 309 print list; +#X obj 477 245 route float list; +#X msg 98 142 client 1 23; +#X text 204 91 "send "; +#X text 110 70 send message on specified socket; +#X text 200 143 "client "; +#X text 48 379 This example demonstrates how to set up a client/server +connection. Data sent by the client get's received and displayed by +the server imediately. Or just try it the other way round...; +#X text 38 15 netclient :: simple client that connects to netserver +; +#X obj 480 189 netsend; +#X connect 5 0 11 0; +#X connect 8 0 11 0; +#X connect 11 0 0 0; +#X connect 11 1 1 0; +#X connect 11 2 6 0; +#X connect 11 3 2 0; +#X connect 14 0 29 0; +#X connect 16 0 29 0; +#X connect 18 0 11 0; +#X connect 22 0 19 0; +#X connect 22 1 21 0; +#X connect 22 2 20 0; +#X connect 23 0 11 0; +#X connect 29 0 22 0; diff --git a/xgui/netserver/makefile b/xgui/netserver/makefile new file mode 100644 index 00000000..d23f611d --- /dev/null +++ b/xgui/netserver/makefile @@ -0,0 +1,88 @@ +NAME=netserver +CSYM=netserver + +current: pd_nt pd_linux + +# ----------------------- NT ----------------------- + +pd_nt: $(NAME).dll + +.SUFFIXES: .dll + +PDNTCFLAGS = /W3 /WX /O2 /G6 /DNT /DPD /nologo + +# where is VC++ ??? +VC="C:\Programme\Microsoft Visual Studio\VC98" + +# where is your m_pd.h ??? +PDNTINCLUDE = /I. /Ic:\pd\tcl\include /Ic:\pd\src /I$(VC)\include /Iinclude + +PDNTLDIR = $(VC)\Lib +PDNTLIB = $(PDNTLDIR)\libc.lib \ + $(PDNTLDIR)\oldnames.lib \ + $(PDNTLDIR)\kernel32.lib \ + $(PDNTLDIR)\user32.lib \ + $(PDNTLDIR)\uuid.lib \ + $(PDNTLDIR)\ws2_32.lib \ + $(PDNTLDIR)\pthreadVC.lib \ + c:\pd\bin\pd.lib + +.c.dll: + cl $(PDNTCFLAGS) $(PDNTINCLUDE) /c $*.c + link /dll /export:$(CSYM)_setup $*.obj $(PDNTLIB) + +# ----------------------- IRIX 5.x ----------------------- + +pd_irix5: $(NAME).pd_irix5 + +.SUFFIXES: .pd_irix5 + +SGICFLAGS5 = -o32 -DPD -DUNIX -DIRIX -O2 + +SGIINCLUDE = -I../../src + +.c.pd_irix5: + cc $(SGICFLAGS5) $(SGIINCLUDE) -o $*.o -c $*.c + ld -elf -shared -rdata_shared -o $*.pd_irix5 $*.o + rm $*.o + +# ----------------------- IRIX 6.x ----------------------- + +pd_irix6: $(NAME).pd_irix6 + +.SUFFIXES: .pd_irix6 + +SGICFLAGS6 = -n32 -DPD -DUNIX -DIRIX -DN32 -woff 1080,1064,1185 \ + -OPT:roundoff=3 -OPT:IEEE_arithmetic=3 -OPT:cray_ivdep=true \ + -Ofast=ip32 + +.c.pd_irix6: + cc $(SGICFLAGS6) $(SGIINCLUDE) -o $*.o -c $*.c + ld -n32 -IPA -shared -rdata_shared -o $*.pd_irix6 $*.o + rm $*.o + +# ----------------------- LINUX i386 ----------------------- + +pd_linux: $(NAME).pd_linux + +.SUFFIXES: .pd_linux + +LINUXCFLAGS = -DPD -DUNIX -O2 -funroll-loops -fomit-frame-pointer \ + -Wall -W -Wshadow -Wstrict-prototypes -Werror \ + -Wno-unused -Wno-parentheses -Wno-switch + +# where is your m_pd.h ??? +LINUXINCLUDE = -I../../src + +.c.pd_linux: + cc -O2 -Wall -DPD -fPIC $(LINUXINCLUDE) -c $*.c + ld -export_dynamic -shared -o $*.pd_linux $*.o -lc + strip --strip-unneeded $*.pd_linux + +# ---------------------------------------------------------- + +install: + cp help-*.pd ../../doc/5.reference + +clean: + rm -f *.o *.pd_* so_locations diff --git a/xgui/netserver/netserver.c b/xgui/netserver/netserver.c new file mode 100644 index 00000000..199b7eea --- /dev/null +++ b/xgui/netserver/netserver.c @@ -0,0 +1,563 @@ +/* -------------------------- netserver ------------------------------------- */ +/* */ +/* A server for bidirectional communication from within Pd. */ +/* Allows to send back data to specific clients connected to the server. */ +/* Written by Olaf Matthes */ +/* Get source at http://www.akustische-kunst.org/puredata/maxlib */ +/* */ +/* This program is free software; you can redistribute it and/or */ +/* modify it under the terms of the GNU General Public License */ +/* as published by the Free Software Foundation; either version 2 */ +/* of the License, or (at your option) any later version. */ +/* */ +/* This program is distributed in the hope that it will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with this program; if not, write to the Free Software */ +/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/* */ +/* Based on PureData by Miller Puckette and others. */ +/* */ +/* ---------------------------------------------------------------------------- */ + +#include "m_imp.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef UNIX +#include +#include +#include +#include +#include +#include +#include +#include +#define SOCKET_ERROR -1 +#else +#include +#include +#include +#endif + +#define MAX_CONNECT 32 /* maximum number of connections */ +#define INBUFSIZE 4096 /* size of receiving data buffer */ + +static char *version = "netserver v0.1 :: bidirectional communication for Pd\n" + " written by Olaf Matthes "; + +/* ----------------------------- netserver ------------------------- */ + +static t_class *netserver_class; +static t_binbuf *inbinbuf; + +typedef void (*t_netserver_socketnotifier)(void *x); +typedef void (*t_netserver_socketreceivefn)(void *x, t_binbuf *b); + +typedef struct _netserver +{ + t_object x_obj; + t_outlet *x_msgout; + t_outlet *x_connectout; + t_outlet *x_clientno; + t_outlet *x_connectionip; + t_symbol *x_host[MAX_CONNECT]; + t_int x_fd[MAX_CONNECT]; + t_int x_sock_fd; + t_int x_connectsocket; + t_int x_nconnections; +} t_netserver; + +typedef struct _netserver_socketreceiver +{ + char *sr_inbuf; + int sr_inhead; + int sr_intail; + void *sr_owner; + t_netserver_socketnotifier sr_notifier; + t_netserver_socketreceivefn sr_socketreceivefn; +} t_netserver_socketreceiver; + +static t_netserver_socketreceiver *netserver_socketreceiver_new(void *owner, t_netserver_socketnotifier notifier, + t_netserver_socketreceivefn socketreceivefn) +{ + t_netserver_socketreceiver *x = (t_netserver_socketreceiver *)getbytes(sizeof(*x)); + x->sr_inhead = x->sr_intail = 0; + x->sr_owner = owner; + x->sr_notifier = notifier; + x->sr_socketreceivefn = socketreceivefn; + if (!(x->sr_inbuf = malloc(INBUFSIZE))) bug("t_netserver_socketreceiver"); + return (x); +} + + /* this is in a separately called subroutine so that the buffer isn't + sitting on the stack while the messages are getting passed. */ +static int netserver_socketreceiver_doread(t_netserver_socketreceiver *x) +{ + char messbuf[INBUFSIZE], *bp = messbuf; + int indx; + int inhead = x->sr_inhead; + int intail = x->sr_intail; + char *inbuf = x->sr_inbuf; + if (intail == inhead) return (0); + for (indx = intail; indx != inhead; indx = (indx+1)&(INBUFSIZE-1)) + { + char c = *bp++ = inbuf[indx]; + if (c == ';' && (!indx || inbuf[indx-1] != '\\')) + { + intail = (indx+1)&(INBUFSIZE-1); + binbuf_text(inbinbuf, messbuf, bp - messbuf); + x->sr_inhead = inhead; + x->sr_intail = intail; + return (1); + } + } + return (0); +} + +static void netserver_socketreceiver_read(t_netserver_socketreceiver *x, int fd) +{ + char *semi; + int readto = (x->sr_inhead >= x->sr_intail ? INBUFSIZE : x->sr_intail-1); + int ret; + + t_netserver *y = x->sr_owner; + + y->x_sock_fd = fd; + + /* the input buffer might be full. If so, drop the whole thing */ + if (readto == x->sr_inhead) + { + post("netserver: dropped message"); + x->sr_inhead = x->sr_intail = 0; + readto = INBUFSIZE; + } + else + { + ret = recv(fd, x->sr_inbuf + x->sr_inhead, + readto - x->sr_inhead, 0); + if (ret < 0) + { + sys_sockerror("recv"); + if (x->sr_notifier) (*x->sr_notifier)(x->sr_owner); + sys_rmpollfn(fd); + sys_closesocket(fd); + } + else if (ret == 0) + { + post("netserver: << connection closed on socket %d", fd); + if (x->sr_notifier) (*x->sr_notifier)(x->sr_owner); + sys_rmpollfn(fd); + sys_closesocket(fd); + } + else + { + x->sr_inhead += ret; + if (x->sr_inhead >= INBUFSIZE) x->sr_inhead = 0; + while (netserver_socketreceiver_doread(x)) + { + outlet_setstacklim(); + if (x->sr_socketreceivefn) + (*x->sr_socketreceivefn)(x->sr_owner, inbinbuf); + else binbuf_eval(inbinbuf, 0, 0, 0); + } + } + } +} + +static void netserver_socketreceiver_free(t_netserver_socketreceiver *x) +{ + free(x->sr_inbuf); + freebytes(x, sizeof(*x)); +} + +/* ---------------- main netserver (send) stuff --------------------- */ + +/* send message to client using socket number */ +static void netserver_send(t_netserver *x, t_symbol *s, int argc, t_atom *argv) +{ + int sockfd, client = -1, i; + if(x->x_nconnections < 0) + { + post("netserver: no clients connected"); + return; + } + if(argc < 2) + { + post("netserver: nothing to send"); + return; + } + /* get socket number of connection (first element in list) */ + if(argv[0].a_type == A_FLOAT) + { + sockfd = atom_getfloatarg(0, argc, argv); + for(i = 0; i < x->x_nconnections; i++) /* check if connection exists */ + { + if(x->x_fd[i] == sockfd) + { + client = i; /* the client we're sending to */ + break; + } + } + if(client == -1) + { + post("netserver: no connection on socket %d", sockfd); + return; + } + } + else + { + post("netserver: no socket specified"); + return; + } + /* process & send data */ + if(sockfd > 0) + { + t_binbuf *b = binbuf_new(); + char *buf, *bp; + int length, sent; + t_atom at; + binbuf_add(b, argc - 1, argv + 1); /* skip first element */ + SETSEMI(&at); + binbuf_add(b, 1, &at); + binbuf_gettext(b, &buf, &length); + + post("netserver: sending data to client %d on socket %d", client + 1, sockfd); + // post("netserver: sending \"%s\"", buf); + + for (bp = buf, sent = 0; sent < length;) + { + static double lastwarntime; + static double pleasewarn; + double timebefore = clock_getlogicaltime(); + int res = send(sockfd, buf, length-sent, 0); + double timeafter = clock_getlogicaltime(); + int late = (timeafter - timebefore > 0.005); + if (late || pleasewarn) + { + if (timeafter > lastwarntime + 2) + { + post("netserver blocked %d msec", + (int)(1000 * ((timeafter - timebefore) + pleasewarn))); + pleasewarn = 0; + lastwarntime = timeafter; + } + else if (late) pleasewarn += timeafter - timebefore; + } + if (res <= 0) + { + sys_sockerror("netserver"); + post("netserver: could not send data to client"); + break; + } + else + { + sent += res; + bp += res; + } + } + t_freebytes(buf, length); + binbuf_free(b); + } + else post("netserver: not a valid socket number (%d)", sockfd); +} + +/* send message to client using client number + note that the client numbers might change in case a client disconnects! */ +static void netserver_client_send(t_netserver *x, t_symbol *s, int argc, t_atom *argv) +{ + int sockfd, client; + if(x->x_nconnections < 0) + { + post("netserver: no clients connected"); + return; + } + if(argc < 2) + { + post("netserver: nothing to send"); + return; + } + /* get number of client (first element in list) */ + if(argv[0].a_type == A_FLOAT) + client = atom_getfloatarg(0, argc, argv); + else + { + post("netserver: no client specified"); + return; + } + sockfd = x->x_fd[client - 1]; /* get socket number for that client */ + + /* process & send data */ + if(sockfd > 0) + { + t_binbuf *b = binbuf_new(); + char *buf, *bp; + int length, sent; + t_atom at; + binbuf_add(b, argc - 1, argv + 1); /* skip first element */ + SETSEMI(&at); + binbuf_add(b, 1, &at); + binbuf_gettext(b, &buf, &length); + + post("netserver: sending data to client %d on socket %d", client, sockfd); + // post("netserver: >> sending \"%s\"", buf); + + for (bp = buf, sent = 0; sent < length;) + { + static double lastwarntime; + static double pleasewarn; + double timebefore = clock_getlogicaltime(); + int res = send(sockfd, buf, length-sent, 0); + double timeafter = clock_getlogicaltime(); + int late = (timeafter - timebefore > 0.005); + if (late || pleasewarn) + { + if (timeafter > lastwarntime + 2) + { + post("netserver blocked %d msec", + (int)(1000 * ((timeafter - timebefore) + pleasewarn))); + pleasewarn = 0; + lastwarntime = timeafter; + } + else if (late) pleasewarn += timeafter - timebefore; + } + if (res <= 0) + { + sys_sockerror("netserver"); + post("netserver: could not send data to cient"); + break; + } + else + { + sent += res; + bp += res; + } + } + t_freebytes(buf, length); + binbuf_free(b); + } + else post("netserver: not a valid socket number (%d)", sockfd); +} + + /* broadcasts a message to all connected clients */ +static void netserver_broadcast(t_netserver *x, t_symbol *s, int argc, t_atom *argv) +{ + int i, client = x->x_nconnections; /* number of clients to send to */ + t_atom at[256]; + for(i = 0; i < argc; i++) + { + at[i + 1] = argv[i]; + } + argc++; + /* enumerate through the clients and send each the message */ + while(client--) + { + SETFLOAT(at, client + 1); /* prepend number of client */ + netserver_client_send(x, s, argc, at); + } +} + + +/* ---------------- main netserver (receive) stuff --------------------- */ + +static void netserver_notify(t_netserver *x) +{ + int i, k; + /* remove connection from list */ + for(i = 0; i < x->x_nconnections; i++) + { + if(x->x_fd[i] == x->x_sock_fd) + { + x->x_nconnections--; + post("netserver: \"%s\" removed from list of clients", x->x_host[i]->s_name); + x->x_host[i] = NULL; /* delete entry */ + x->x_fd[i] = -1; + /* rearrange list now: move entries to close the gap */ + for(k = i; k < x->x_nconnections; k++) + { + x->x_host[k] = x->x_host[k + 1]; + x->x_fd[k] = x->x_fd[k + 1]; + } + } + } + outlet_float(x->x_connectout, x->x_nconnections); +} + +static void netserver_doit(void *z, t_binbuf *b) +{ + t_atom messbuf[1024]; + t_netserver *x = (t_netserver *)z; + int msg, natom = binbuf_getnatom(b); + t_atom *at = binbuf_getvec(b); + int i; + /* output clients IP and socket no. */ + for(i = 0; i < x->x_nconnections; i++) /* search for corresponding IP */ + { + if(x->x_fd[i] == x->x_sock_fd) + { + outlet_symbol(x->x_connectionip, x->x_host[i]); + break; + } + } + outlet_float(x->x_clientno, x->x_sock_fd); /* the socket number */ + /* process data */ + for (msg = 0; msg < natom;) + { + int emsg; + for (emsg = msg; emsg < natom && at[emsg].a_type != A_COMMA + && at[emsg].a_type != A_SEMI; emsg++); + + if (emsg > msg) + { + int ii; + for (ii = msg; ii < emsg; ii++) + if (at[ii].a_type == A_DOLLAR || at[ii].a_type == A_DOLLSYM) + { + pd_error(x, "netserver: got dollar sign in message"); + goto nodice; + } + if (at[msg].a_type == A_FLOAT) + { + if (emsg > msg + 1) + outlet_list(x->x_msgout, 0, emsg-msg, at + msg); + else outlet_float(x->x_msgout, at[msg].a_w.w_float); + } + else if (at[msg].a_type == A_SYMBOL) + outlet_anything(x->x_msgout, at[msg].a_w.w_symbol, + emsg-msg-1, at + msg + 1); + } + nodice: + msg = emsg + 1; + } +} + +static void netserver_connectpoll(t_netserver *x) +{ + struct sockaddr_in incomer_address; + int sockaddrl = (int) sizeof( struct sockaddr ); + int fd = accept(x->x_connectsocket, (struct sockaddr*)&incomer_address, &sockaddrl); + if (fd < 0) post("netserver: accept failed"); + else + { + t_netserver_socketreceiver *y = netserver_socketreceiver_new((void *)x, + (t_netserver_socketnotifier)netserver_notify, + (x->x_msgout ? netserver_doit : 0)); + sys_addpollfn(fd, (t_fdpollfn)netserver_socketreceiver_read, y); + x->x_nconnections++; + x->x_host[x->x_nconnections - 1] = gensym(inet_ntoa(incomer_address.sin_addr)); + x->x_fd[x->x_nconnections - 1] = fd; + + post("netserver: ** accepted connection from %s on socket %d", + x->x_host[x->x_nconnections - 1]->s_name, x->x_fd[x->x_nconnections - 1]); + outlet_float(x->x_connectout, x->x_nconnections); + } +} + +static void netserver_print(t_netserver *x) +{ + int i; + if(x->x_nconnections > 0) + { + post("netserver: %d open connections:", x->x_nconnections); + + for(i = 0; i < x->x_nconnections; i++) + { + post(" \"%s\" on socket %d", + x->x_host[i]->s_name, x->x_fd[i]); + } + } else post("netserver: no open connections"); +} + +static void *netserver_new(t_floatarg fportno) +{ + t_netserver *x; + int i; + struct sockaddr_in server; + int sockfd, portno = fportno; + /* create a socket */ + sockfd = socket(AF_INET, SOCK_STREAM, 0); +#if 0 + post("netserver: receive socket %d", sockfd); +#endif + if (sockfd < 0) + { + sys_sockerror("socket"); + return (0); + } + server.sin_family = AF_INET; + server.sin_addr.s_addr = INADDR_ANY; + +#ifdef IRIX + /* this seems to work only in IRIX but is unnecessary in + Linux. Not sure what NT needs in place of this. */ + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0) + post("setsockopt failed\n"); +#endif + + /* assign server port number */ + server.sin_port = htons((u_short)portno); + + /* name the socket */ + if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) + { + sys_sockerror("bind"); + sys_closesocket(sockfd); + return (0); + } + x = (t_netserver *)pd_new(netserver_class); + x->x_msgout = outlet_new(&x->x_obj, &s_anything); + + /* streaming protocol */ + if (listen(sockfd, 5) < 0) + { + sys_sockerror("listen"); + sys_closesocket(sockfd); + sockfd = -1; + } + else + { + sys_addpollfn(sockfd, (t_fdpollfn)netserver_connectpoll, x); + x->x_connectout = outlet_new(&x->x_obj, &s_float); + x->x_clientno = outlet_new(&x->x_obj, &s_float); + x->x_connectionip = outlet_new(&x->x_obj, &s_symbol); + inbinbuf = binbuf_new(); + } + x->x_connectsocket = sockfd; + x->x_nconnections = 0; + for(i = 0; i < MAX_CONNECT; i++)x->x_fd[i] = -1; +#ifndef MAXLIB + post(version); +#endif + return (x); +} + +static void netserver_free(t_netserver *x) +{ + /* LATER make me clean up open connections */ + if (x->x_connectsocket >= 0) + { + sys_rmpollfn(x->x_connectsocket); + sys_closesocket(x->x_connectsocket); + } + binbuf_free(inbinbuf); +} + +void netserver_setup(void) +{ + netserver_class = class_new(gensym("netserver"),(t_newmethod)netserver_new, (t_method)netserver_free, + sizeof(t_netserver), 0, A_DEFFLOAT, 0); + class_addmethod(netserver_class, (t_method)netserver_print, gensym("print"), 0); + class_addmethod(netserver_class, (t_method)netserver_send, gensym("send"), A_GIMME, 0); + class_addmethod(netserver_class, (t_method)netserver_client_send, gensym("client"), A_GIMME, 0); + class_addmethod(netserver_class, (t_method)netserver_broadcast, gensym("broadcast"), A_GIMME, 0); + class_sethelpsymbol(netserver_class, gensym("maxlib/help-netserver.pd")); +} diff --git a/xgui/netserver/netserver.dll b/xgui/netserver/netserver.dll new file mode 100644 index 00000000..16014c93 Binary files /dev/null and b/xgui/netserver/netserver.dll differ diff --git a/xgui/netserver/netserver.exp b/xgui/netserver/netserver.exp new file mode 100644 index 00000000..43d6dee5 Binary files /dev/null and b/xgui/netserver/netserver.exp differ diff --git a/xgui/netserver/netserver.lib b/xgui/netserver/netserver.lib new file mode 100644 index 00000000..f08aa6fa Binary files /dev/null and b/xgui/netserver/netserver.lib differ diff --git a/xgui/netserver/netserver.zip b/xgui/netserver/netserver.zip new file mode 100644 index 00000000..09f1adea Binary files /dev/null and b/xgui/netserver/netserver.zip differ -- cgit v1.2.1