aboutsummaryrefslogtreecommitdiff
path: root/tcpreceive.c
diff options
context:
space:
mode:
Diffstat (limited to 'tcpreceive.c')
-rw-r--r--tcpreceive.c44
1 files changed, 20 insertions, 24 deletions
diff --git a/tcpreceive.c b/tcpreceive.c
index a115a06..fb7c54d 100644
--- a/tcpreceive.c
+++ b/tcpreceive.c
@@ -1,5 +1,5 @@
/* tcpreceive.c
- * copyright (c) 2010 IOhannes m zmölnig, IEM
+ * copyright © 2010-2015 IOhannes m zmölnig, IEM
* copyright (c) 2006-2010 Martin Peach
* copyright (c) Miller Puckette
*/
@@ -65,6 +65,8 @@ typedef struct _tcpreceive {
t_iemnet_floatlist *x_floatlist;
} t_tcpreceive;
+/* forward declarations */
+static int tcpreceive_disconnect(t_tcpreceive *x, int id);
static int tcpreceive_find_socket(t_tcpreceive *x, int fd)
{
@@ -77,8 +79,6 @@ static int tcpreceive_find_socket(t_tcpreceive *x, int fd)
return -1;
}
-static int tcpreceive_disconnect(t_tcpreceive *x, int id);
-
static void tcpreceive_read_callback(void *w, t_iemnet_chunk*c)
{
t_tcpconnection*y=(t_tcpconnection*)w;
@@ -120,7 +120,6 @@ static int tcpreceive_addconnection(t_tcpreceive *x, int fd, long addr,
x->x_connection+i,
tcpreceive_read_callback,
0);
-
return 1;
}
}
@@ -140,7 +139,8 @@ static void tcpreceive_connectpoll(t_tcpreceive *x)
fd = accept(x->x_connectsocket, (struct sockaddr *)&from, &fromlen);
if (fd < 0) {
- error("[%s] accept failed", objName);
+ iemnet_log(x, IEMNET_ERROR, "could not accept new connection");
+ sys_sockerror("accept");
} else {
/* get the sender's ip */
addr = ntohl(from.sin_addr.s_addr);
@@ -150,13 +150,12 @@ static void tcpreceive_connectpoll(t_tcpreceive *x)
outlet_float(x->x_connectout, x->x_nconnections);
iemnet__addrout(x->x_statout, x->x_addrout, addr, port);
} else {
- error ("[%s] Too many connections", objName);
+ iemnet_log(x, IEMNET_ERROR, "too many connections");
iemnet__closesocket(fd);
}
}
}
-
static int tcpreceive_disconnect(t_tcpreceive *x, int id)
{
if(id>=0 && id < MAX_CONNECTIONS && x->x_connection[id].port>0) {
@@ -176,6 +175,7 @@ static int tcpreceive_disconnect(t_tcpreceive *x, int id)
return 0;
}
+
/* tcpreceive_closeall closes all open sockets and deletes them from the list */
static void tcpreceive_disconnect_all(t_tcpreceive *x)
{
@@ -186,9 +186,6 @@ static void tcpreceive_disconnect_all(t_tcpreceive *x)
}
}
-
-
-
/* tcpreceive_removeconnection tries to delete the socket fd from the list */
/* returns 1 on success, else 0 */
static int tcpreceive_disconnect_socket(t_tcpreceive *x, int fd)
@@ -225,10 +222,10 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno)
x->x_port=-1;
}
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0) {
- error("[%s]: unable to create socket", objName);
+ iemnet_log(x, IEMNET_ERROR, "unable to create socket");
+ sys_sockerror("socket");
return;
}
@@ -238,7 +235,8 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno)
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(char *)&intarg, sizeof(intarg))
< 0) {
- error("[%s]: setsockopt (SO_REUSEADDR) failed", objName);
+ iemnet_log(x, IEMNET_ERROR, "unable to enable address re-using");
+ sys_sockerror("setsockopt:SO_REUSEADDR");
}
#endif /* SO_REUSEADDR */
#ifdef SO_REUSEPORT
@@ -246,7 +244,8 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno)
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
(char *)&intarg, sizeof(intarg))
< 0) {
- error("[%s]: setsockopt (SO_REUSEPORT) failed", objName);
+ iemnet_log(x, IEMNET_ERROR, "unable to enable port re-using");
+ sys_sockerror("setsockopt:SO_REUSEPORT");
}
#endif /* SO_REUSEPORT */
@@ -254,17 +253,18 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno)
intarg = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
(char *)&intarg, sizeof(intarg)) < 0) {
- post("[%s]: setsockopt (TCP_NODELAY) failed", objName);
+ iemnet_log(x, IEMNET_ERROR, "unable to enable immediate sending");
+ sys_sockerror("setsockopt:TCP_NODELAY");
}
-
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("[tcpreceive] bind failed");
+ iemnet_log(x, IEMNET_ERROR, "couldn't bind socket");
+ sys_sockerror("bind");
iemnet__closesocket(sockfd);
sockfd = -1;
outlet_anything(x->x_statout, gensym("port"), 1, ap);
@@ -273,7 +273,8 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno)
/* streaming protocol */
if (listen(sockfd, 5) < 0) {
- sys_sockerror("[tcpreceive] listen");
+ iemnet_log(x, IEMNET_ERROR, "unable to listen on socket");
+ sys_sockerror("listen");
iemnet__closesocket(sockfd);
sockfd = -1;
outlet_anything(x->x_statout, gensym("port"), 1, ap);
@@ -292,7 +293,6 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno)
x->x_port=ntohs(server.sin_port);
}
-
SETFLOAT(ap, x->x_port);
outlet_anything(x->x_statout, gensym("port"), 1, ap);
}
@@ -302,7 +302,6 @@ static void tcpreceive_serialize(t_tcpreceive *x, t_floatarg doit)
x->x_serialize=doit;
}
-
static void tcpreceive_free(t_tcpreceive *x)
{
/* is this ever called? */
@@ -327,7 +326,7 @@ static void *tcpreceive_new(t_floatarg fportno)
x->x_msgout = outlet_new(&x->x_obj, 0);
x->x_addrout = outlet_new(&x->x_obj, gensym("list")); /* legacy */
x->x_connectout = outlet_new(&x->x_obj, gensym("float")); /* legacy */
- x->x_statout = outlet_new(&x->x_obj, 0);/* outlet for everything else */
+ x->x_statout = outlet_new(&x->x_obj, 0); /* outlet for everything else */
x->x_serialize=1;
@@ -371,7 +370,4 @@ IEMNET_EXTERN void tcpreceive_setup(void)
IEMNET_INITIALIZER(tcpreceive_setup);
-
/* end x_net_tcpreceive.c */
-
-