static void udpreceive_free(t_udpreceive *x) { iemnet__receiver_destroy(x->x_receiver); x->x_connectsocket=0; outlet_free(x->x_msgout); outlet_free(x->x_addrout); outlet_free(x->x_statout); if(x->x_floatlist)iemnet__floatlist_destroy(x->x_floatlist);x->x_floatlist=NULL; }
static void udpclient_disconnect(t_udpclient *x) { if (x->x_fd >= 0) { DEBUG("disconnect %x %x", x->x_sender, x->x_receiver); if(x->x_receiver)iemnet__receiver_destroy(x->x_receiver); x->x_receiver=NULL; if(x->x_sender)iemnet__sender_destroy(x->x_sender); x->x_sender=NULL; sys_closesocket(x->x_fd); x->x_fd = -1; x->x_connectstate = 0; outlet_float(x->x_connectout, 0); } else pd_error(x, "[%s] not connected", objName); }
static void tcpclient_disconnect(t_tcpclient *x) { if (x->x_fd >= 0) { int fd=x->x_fd; x->x_fd = -1; DEBUG("disconnecting %x", x); if(x->x_sender)iemnet__sender_destroy(x->x_sender); x->x_sender=NULL; if(x->x_receiver)iemnet__receiver_destroy(x->x_receiver); x->x_receiver=NULL; DEBUG("disconnect cleaning up %x", x); sys_closesocket(fd); x->x_connectstate = 0; outlet_float(x->x_connectout, 0); } else pd_error(x, "%s: not connected", objName); }
static void udpreceive_port(t_udpreceive*x, t_floatarg fportno) { static t_atom ap[1]; int portno = fportno; struct sockaddr_in server; socklen_t serversize=sizeof(server); int sockfd = x->x_connectsocket; int intarg; SETFLOAT(ap, -1); if(x->x_port == portno) { return; } /* cleanup any open ports */ if(sockfd>=0) { iemnet__receiver_destroy(x->x_receiver); x->x_connectsocket=-1; x->x_port=-1; } sockfd = socket(AF_INET, SOCK_DGRAM, 0); if(sockfd<0) { error("[%s]: unable to create socket", objName); return; } /* ask OS to allow another Pd to reopen this port after we close it. */ #ifdef SO_REUSEADDR intarg = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&intarg, sizeof(intarg)) < 0) { error("[%s]: setsockopt (SO_REUSEADDR) failed", objName); } #endif /* SO_REUSEADDR */ #ifdef SO_REUSEPORT intarg = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (char *)&intarg, sizeof(intarg)) < 0) { error("[%s]: setsockopt (SO_REUSEPORT) failed", objName); } #endif /* SO_REUSEPORT */ server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons((u_short)portno); /* name the socket */ if (bind(sockfd, (struct sockaddr *)&server, serversize) < 0) { sys_sockerror("[udpreceive] bind failed"); sys_closesocket(sockfd); sockfd = -1; outlet_anything(x->x_statout, gensym("port"), 1, ap); return; } x->x_connectsocket = sockfd; x->x_port = portno; // find out which port is actually used (useful when assigning "0") if(!getsockname(sockfd, (struct sockaddr *)&server, &serversize)) { x->x_port=ntohs(server.sin_port); } x->x_receiver=iemnet__receiver_create(sockfd, x, udpreceive_read_callback); SETFLOAT(ap, x->x_port); outlet_anything(x->x_statout, gensym("port"), 1, ap); }
static int udpreceive_setport(t_udpreceive*x, unsigned short portno) { struct sockaddr_in server; socklen_t serversize=sizeof(server); int sockfd = x->x_connectsocket; int intarg; memset(&server, 0, sizeof(server)); if(x->x_port == portno) { return 1; } /* cleanup any open ports */ if(sockfd>=0) { iemnet__receiver_destroy(x->x_receiver, 0); x->x_connectsocket=-1; x->x_port=-1; } sockfd = socket(AF_INET, SOCK_DGRAM, 0); if(sockfd<0) { pd_error(x, "[%s]: unable to create socket", objName); return 0; } /* ask OS to allow another Pd to reopen this port after we close it. */ #ifdef SO_REUSEADDR if(x->x_reuseaddr) { intarg = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&intarg, sizeof(intarg)) < 0) { pd_error(x, "[%s]: setsockopt (SO_REUSEADDR) failed", objName); } } #endif /* SO_REUSEADDR */ #ifdef SO_REUSEPORT if(x->x_reuseport) { intarg = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (void *)&intarg, sizeof(intarg)) < 0) { pd_error(x, "[%s]: setsockopt (SO_REUSEPORT) failed", objName); } } #endif /* SO_REUSEPORT */ server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons((u_short)portno); /* name the socket */ if (bind(sockfd, (struct sockaddr *)&server, serversize) < 0) { sys_sockerror("[udpreceive] bind failed"); sys_closesocket(sockfd); sockfd = -1; return 0; } x->x_connectsocket = sockfd; x->x_port = portno; // find out which port is actually used (useful when assigning "0") if(!getsockname(sockfd, (struct sockaddr *)&server, &serversize)) { x->x_port=ntohs(server.sin_port); } x->x_receiver=iemnet__receiver_create(sockfd, x, udpreceive_read_callback, 0); return 1; }