static void netreceive_connectpoll(t_netreceive *x) { int fd = accept(x->x_connectsocket, 0, 0); if (fd < 0) post("netreceive: accept failed"); else { t_socketreceiver *y = socketreceiver_new((void *)x, (t_socketnotifier)netreceive_notify, (x->x_msgout ? netreceive_doit : 0), 0); sys_addpollfn(fd, (t_fdpollfn)socketreceiver_read, y); outlet_float(x->x_connectout, ++x->x_nconnections); } }
static void netreceive_connectpoll(t_netreceive *x) { int fd = accept(x->x_ns.x_sockfd, 0, 0); if (fd < 0) post("netreceive: accept failed"); else { int nconnections = x->x_nconnections+1; x->x_connections = (int *)t_resizebytes(x->x_connections, x->x_nconnections * sizeof(int), nconnections * sizeof(int)); x->x_connections[x->x_nconnections] = fd; if (x->x_ns.x_bin) sys_addpollfn(fd, (t_fdpollfn)netsend_readbin, x); else { t_socketreceiver *y = socketreceiver_new((void *)x, (t_socketnotifier)netreceive_notify, (x->x_ns.x_msgout ? netsend_doit : 0), 0); sys_addpollfn(fd, (t_fdpollfn)socketreceiver_read, y); } outlet_float(x->x_ns.x_connectout, (x->x_nconnections = nconnections)); } }
static void netreceive_listen(t_netreceive *x, t_floatarg fportno) { int portno = fportno, intarg; struct sockaddr_in server; netreceive_closeall(x); if (portno <= 0) return; x->x_ns.x_sockfd = socket(AF_INET, x->x_ns.x_protocol, 0); if (x->x_ns.x_sockfd < 0) { sys_sockerror("socket"); return; } #if 0 fprintf(stderr, "receive socket %d\n", x->x_ sockfd); #endif #if 1 /* ask OS to allow another Pd to repoen this port after we close it. */ intarg = 1; if (setsockopt(x->x_ns.x_sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&intarg, sizeof(intarg)) < 0) post("netreceive: setsockopt (SO_REUSEADDR) failed\n"); #endif #if 0 intarg = 0; if (setsockopt(x->x_ns.x_sockfd, SOL_SOCKET, SO_RCVBUF, &intarg, sizeof(intarg)) < 0) post("setsockopt (SO_RCVBUF) failed\n"); #endif intarg = 1; if (setsockopt(x->x_ns.x_sockfd, SOL_SOCKET, SO_BROADCAST, (const void *)&intarg, sizeof(intarg)) < 0) post("netreceive: failed to sett SO_BROADCAST"); /* Stream (TCP) sockets are set NODELAY */ if (x->x_ns.x_protocol == SOCK_STREAM) { intarg = 1; if (setsockopt(x->x_ns.x_sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&intarg, sizeof(intarg)) < 0) post("setsockopt (TCP_NODELAY) failed\n"); } /* assign server port number etc */ server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons((u_short)portno); /* name the socket */ if (bind(x->x_ns.x_sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) { sys_sockerror("bind"); sys_closesocket(x->x_ns.x_sockfd); x->x_ns.x_sockfd = -1; return; } if (x->x_ns.x_protocol == SOCK_DGRAM) /* datagram protocol */ { if (x->x_ns.x_bin) sys_addpollfn(x->x_ns.x_sockfd, (t_fdpollfn)netsend_readbin, x); else { t_socketreceiver *y = socketreceiver_new((void *)x, (t_socketnotifier)netreceive_notify, (x->x_ns.x_msgout ? netsend_doit : 0), 1); sys_addpollfn(x->x_ns.x_sockfd, (t_fdpollfn)socketreceiver_read, y); x->x_ns.x_connectout = 0; } } else /* streaming protocol */ { if (listen(x->x_ns.x_sockfd, 5) < 0) { sys_sockerror("listen"); sys_closesocket(x->x_ns.x_sockfd); x->x_ns.x_sockfd = -1; } else { sys_addpollfn(x->x_ns.x_sockfd, (t_fdpollfn)netreceive_connectpoll, x); x->x_ns.x_connectout = outlet_new(&x->x_ns.x_obj, &s_float); } } }
static void netsend_connect(t_netsend *x, t_symbol *hostname, t_floatarg fportno) { struct sockaddr_in server; struct hostent *hp; int sockfd; int portno = fportno; int intarg; if (x->x_sockfd >= 0) { error("netsend_connect: already connected"); return; } /* create a socket */ sockfd = socket(AF_INET, x->x_protocol, 0); #if 0 fprintf(stderr, "send socket %d\n", sockfd); #endif if (sockfd < 0) { sys_sockerror("socket"); return; } /* connect socket using hostname provided in command line */ server.sin_family = AF_INET; hp = gethostbyname(hostname->s_name); if (hp == 0) { post("bad host?\n"); return; } #if 0 intarg = 0; if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &intarg, sizeof(intarg)) < 0) post("setsockopt (SO_RCVBUF) failed\n"); #endif intarg = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const void *)&intarg, sizeof(intarg)) < 0) post("setting SO_BROADCAST"); /* for stream (TCP) sockets, specify "nodelay" */ if (x->x_protocol == SOCK_STREAM) { intarg = 1; if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&intarg, sizeof(intarg)) < 0) post("setsockopt (TCP_NODELAY) failed\n"); } memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); /* assign client port number */ server.sin_port = htons((u_short)portno); post("connecting to port %d", portno); /* try to connect. LATER make a separate thread to do this because it might block */ if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { sys_sockerror("connecting stream socket"); sys_closesocket(sockfd); return; } x->x_sockfd = sockfd; if (x->x_bin) sys_addpollfn(sockfd, (t_fdpollfn)netsend_readbin, x); else { t_socketreceiver *y = socketreceiver_new((void *)x, 0, netsend_doit, 0); sys_addpollfn(sockfd, (t_fdpollfn)socketreceiver_read, y); } outlet_float(x->x_obj.ob_outlet, 1); }
static void *netreceive_new(t_symbol *compatflag, t_floatarg fportno, t_floatarg udpflag) { t_netreceive *x; struct sockaddr_in server; int sockfd, portno = fportno, udp = (udpflag != 0); int old = !strcmp(compatflag->s_name , "old"); int intarg; /* create a socket */ sockfd = socket(AF_INET, (udp ? SOCK_DGRAM : SOCK_STREAM), 0); #if 0 fprintf(stderr, "receive socket %d\n", sockfd); #endif if (sockfd < 0) { sys_sockerror("socket"); return (0); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; #if 1 /* ask OS to allow another Pd to repoen this port after we close it. */ intarg = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&intarg, sizeof(intarg)) < 0) post("setsockopt (SO_REUSEADDR) failed\n"); #endif #if 0 intarg = 0; if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &intarg, sizeof(intarg)) < 0) post("setsockopt (SO_RCVBUF) failed\n"); #endif intarg = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const void *)&intarg, sizeof(intarg)) < 0) post("setting SO_BROADCAST"); /* Stream (TCP) sockets are set NODELAY */ if (!udp) { intarg = 1; if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&intarg, sizeof(intarg)) < 0) post("setsockopt (TCP_NODELAY) failed\n"); } /* 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_netreceive *)pd_new(netreceive_class); if (old) { /* old style, nonsecure version */ x->x_msgout = 0; } else x->x_msgout = outlet_new(&x->x_obj, &s_anything); if (udp) /* datagram protocol */ { t_socketreceiver *y = socketreceiver_new((void *)x, (t_socketnotifier)netreceive_notify, (x->x_msgout ? netreceive_doit : 0), 1); sys_addpollfn(sockfd, (t_fdpollfn)socketreceiver_read, y); x->x_connectout = 0; } else /* streaming protocol */ { if (listen(sockfd, 5) < 0) { sys_sockerror("listen"); sys_closesocket(sockfd); sockfd = -1; } else { sys_addpollfn(sockfd, (t_fdpollfn)netreceive_connectpoll, x); x->x_connectout = outlet_new(&x->x_obj, &s_float); } } x->x_connectsocket = sockfd; x->x_nconnections = 0; x->x_udp = udp; return (x); }