aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/tcpclient-help.pd210
-rw-r--r--net/tcpclient.c37
2 files changed, 131 insertions, 116 deletions
diff --git a/net/tcpclient-help.pd b/net/tcpclient-help.pd
index 807c1ae..1803d01 100644
--- a/net/tcpclient-help.pd
+++ b/net/tcpclient-help.pd
@@ -1,102 +1,108 @@
-#N canvas 184 7 1096 649 12;
-#X msg -112 56 disconnect;
-#X obj 219 439 unpack 0 0 0 0;
-#X floatatom 219 462 3 0 0 0 - - -;
-#X floatatom 250 462 3 0 0 0 - - -;
-#X floatatom 282 462 3 0 0 0 - - -;
-#X floatatom 314 462 3 0 0 0 - - -;
-#X text 179 461 from;
-#X msg -175 -7 connect 132.205.142.12 80;
-#X obj 199 387 tcpclient;
-#X obj 239 413 tgl 15 0 empty empty connected 18 7 0 8 -24198 -241291
--1 0 1;
-#X msg -88 80 dump \$1;
-#X obj -140 65 tgl 15 0 empty empty empty 0 -6 0 8 -4034 -257985 -1
-0 1;
-#X msg 11 179 receive;
-#X msg 35 203 recv;
-#X text -59 -58 connect with an IP address and port number;
-#X msg -200 -32 connect www.concordia.ca 80;
-#X text -17 79 print received messages to main window in hexdump format
-;
-#X text 270 386 tcpclient opens a tcp socket to send and receive bytes
-on;
-#X text -217 305 See also:;
-#X obj -212 329 netclient;
-#X msg -225 -57 connect 127.0.0.1 9997;
-#X obj -212 352 tcpreceive;
-#X text -214 374 can receive messages from tcpclient;
-#X text -136 328 is what tcpclient is based on;
-#X text 250 513 Received messages are output as a list of bytes;
-#X text 271 542 Attempting to print long messages output can hang pd!
-;
-#X text 77 176 get any received data (not useful unless you need it
-faster than once per 20ms);
-#X text 220 225 semicolon-terminated string for netserver or netreceive
-;
-#X msg 59 227 send 49 127 128 51 59;
-#X obj -84 352 tcpserver;
-#X text -118 351 and;
-#X text 347 -55 tcpclient can connect to a server and send and receive
-messages as lists of bytes. Any integer value between 0 and 255 can
-be transmitted or received.;
-#X msg -63 105 send ../doc/5.reference/test.txt;
-#X obj -15 129 openpanel;
-#X msg -15 153 send \$1;
-#X obj -101 114 bng 15 250 50 0 empty empty empty 17 7 0 10 -24198
--241291 -1;
-#X text 172 105 send a file;
-#X text 62 128 ...any file;
-#X msg 99 251 71 69 84 32 104 116 116 112 58 47 47 47 105 110 100 101
-120 46 104 116 109 108 13 10;
-#X text 529 257 'send' prefix is optional;
-#X obj 199 514 spigot;
-#X obj 238 491 tgl 15 0 empty empty enable_print 18 7 0 8 -24198 -241291
--1 0 1;
-#X obj 199 543 print >>>;
-#X msg -136 16 send 71 69 84 32 104 116 116 112 58 47 47 47 105 110
-100 101 120 46 112 104 112 13 10;
-#X text 272 24 GET http:///index.phpCRLF;
-#X floatatom 374 433 9 0 0 0 - - -;
-#X text 173 291 set send-buffer size;
-#X obj 374 407 route sent buf;
-#X floatatom 421 457 9 0 0 0 - - -;
-#X text 491 456 Size of the send buffer;
-#X text 448 432 Number of bytes sent (may still be in buffer);
-#X msg 147 315 buf;
-#X text 177 314 get send-buffer size;
-#X msg 123 291 buf 10;
-#X msg 172 340 timeout 100;
-#X text 260 339 set send timeout in microseconds (default is 1000)
-;
-#X text -208 540 2009/04/08 Martin Peach;
-#X connect 0 0 8 0;
-#X connect 1 0 2 0;
-#X connect 1 1 3 0;
-#X connect 1 2 4 0;
-#X connect 1 3 5 0;
-#X connect 7 0 8 0;
-#X connect 8 0 40 0;
-#X connect 8 1 1 0;
-#X connect 8 2 9 0;
-#X connect 8 3 47 0;
-#X connect 10 0 8 0;
-#X connect 11 0 10 0;
-#X connect 12 0 8 0;
-#X connect 13 0 8 0;
-#X connect 15 0 8 0;
-#X connect 20 0 8 0;
-#X connect 28 0 8 0;
-#X connect 32 0 8 0;
-#X connect 33 0 34 0;
-#X connect 34 0 8 0;
-#X connect 35 0 33 0;
-#X connect 38 0 8 0;
-#X connect 40 0 42 0;
-#X connect 41 0 40 1;
-#X connect 43 0 8 0;
-#X connect 47 0 45 0;
-#X connect 47 1 48 0;
-#X connect 51 0 8 0;
-#X connect 53 0 8 0;
-#X connect 54 0 8 0;
+#N canvas 184 70 1096 649 12;
+#X msg -112 56 disconnect;
+#X obj 219 439 unpack 0 0 0 0;
+#X floatatom 219 462 3 0 0 0 - - -;
+#X floatatom 250 462 3 0 0 0 - - -;
+#X floatatom 282 462 3 0 0 0 - - -;
+#X floatatom 314 462 3 0 0 0 - - -;
+#X text 179 461 from;
+#X msg -175 -7 connect 132.205.142.12 80;
+#X obj 199 387 tcpclient;
+#X obj 239 413 tgl 15 0 empty empty connected 18 7 0 8 -24198 -241291
+-1 0 1;
+#X msg -88 80 dump \$1;
+#X obj -140 65 tgl 15 0 empty empty empty 0 -6 0 8 -4034 -257985 -1
+1 1;
+#X msg 11 179 receive;
+#X msg 35 203 recv;
+#X text -59 -58 connect with an IP address and port number;
+#X msg -200 -32 connect www.concordia.ca 80;
+#X text -17 79 print received messages to main window in hexdump format
+;
+#X text 270 386 tcpclient opens a tcp socket to send and receive bytes
+on;
+#X text -217 305 See also:;
+#X obj -212 329 netclient;
+#X msg -225 -57 connect 127.0.0.1 9997;
+#X obj -212 352 tcpreceive;
+#X text -214 374 can receive messages from tcpclient;
+#X text -136 328 is what tcpclient is based on;
+#X text 250 513 Received messages are output as a list of bytes;
+#X text 77 176 get any received data (not useful unless you need it
+faster than once per 20ms);
+#X text 220 225 semicolon-terminated string for netserver or netreceive
+;
+#X msg 59 227 send 49 127 128 51 59;
+#X obj -84 352 tcpserver;
+#X text -118 351 and;
+#X text 347 -55 tcpclient can connect to a server and send and receive
+messages as lists of bytes. Any integer value between 0 and 255 can
+be transmitted or received.;
+#X msg -63 105 send ../doc/5.reference/test.txt;
+#X obj -15 129 openpanel;
+#X msg -15 153 send \$1;
+#X obj -101 114 bng 15 250 50 0 empty empty empty 17 7 0 10 -24198
+-241291 -1;
+#X text 172 105 send a file;
+#X text 62 128 ...any file;
+#X msg 99 251 71 69 84 32 104 116 116 112 58 47 47 47 105 110 100 101
+120 46 104 116 109 108 13 10;
+#X text 529 257 'send' prefix is optional;
+#X obj 199 514 spigot;
+#X obj 238 491 tgl 15 0 empty empty enable_print 18 7 0 8 -24198 -241291
+-1 0 1;
+#X obj 199 543 print >>>;
+#X text 272 24 GET http:///index.phpCRLF;
+#X floatatom 374 433 9 0 0 0 - - -;
+#X text 173 291 set send-buffer size;
+#X obj 374 407 route sent buf;
+#X floatatom 421 457 9 0 0 0 - - -;
+#X text 491 456 Size of the send buffer;
+#X text 448 432 Number of bytes sent (may still be in buffer);
+#X msg 147 315 buf;
+#X text 177 314 get send-buffer size;
+#X msg 123 291 buf 10;
+#X msg 170 338 timeout 100;
+#X text 258 337 set send timeout in microseconds (default is 1000)
+;
+#X msg 194 362 verbosity \$1;
+#X obj 139 347 tgl 15 1 empty empty empty 0 -6 0 8 -4034 -257985 -1
+0 1;
+#X text 289 362 print connection status messages to main window (default)
+;
+#X msg -136 16 send 71 69 84 32 104 116 116 112 58 47 47 47 105 110
+100 101 120 46 112 104 112 13 10 13 10;
+#X text -208 540 2010/03/01 Martin Peach;
+#X text 271 542 Attempting to print long messages can hang Pd!;
+#X connect 0 0 8 0;
+#X connect 1 0 2 0;
+#X connect 1 1 3 0;
+#X connect 1 2 4 0;
+#X connect 1 3 5 0;
+#X connect 7 0 8 0;
+#X connect 8 0 39 0;
+#X connect 8 1 1 0;
+#X connect 8 2 9 0;
+#X connect 8 3 45 0;
+#X connect 10 0 8 0;
+#X connect 11 0 10 0;
+#X connect 12 0 8 0;
+#X connect 13 0 8 0;
+#X connect 15 0 8 0;
+#X connect 20 0 8 0;
+#X connect 27 0 8 0;
+#X connect 31 0 8 0;
+#X connect 32 0 33 0;
+#X connect 33 0 8 0;
+#X connect 34 0 32 0;
+#X connect 37 0 8 0;
+#X connect 39 0 41 0;
+#X connect 40 0 39 1;
+#X connect 45 0 43 0;
+#X connect 45 1 46 0;
+#X connect 49 0 8 0;
+#X connect 51 0 8 0;
+#X connect 52 0 8 0;
+#X connect 54 0 8 0;
+#X connect 55 0 54 0;
+#X connect 57 0 8 0;
diff --git a/net/tcpclient.c b/net/tcpclient.c
index 614d2ab..93fbb91 100644
--- a/net/tcpclient.c
+++ b/net/tcpclient.c
@@ -67,6 +67,7 @@ typedef struct _tcpclient
t_outlet *x_connectout;
t_outlet *x_statusout;
int x_dump; // 1 = hexdump received bytes
+ int x_verbosity; // 1 = post connection state changes to main window
int x_fd; // the socket
int x_fdbuf; // the socket's buffer size
t_int x_timeout_us; /* send timeout in microseconds */
@@ -83,8 +84,9 @@ typedef struct _tcpclient
} t_tcpclient;
static void tcpclient_timeout(t_tcpclient *x, t_float timeout);
+static void tcpclient_verbosity(t_tcpclient *x, t_float verbosity);
static void tcpclient_dump(t_tcpclient *x, t_float dump);
-static void tcp_client_hexdump(unsigned char *buf, long len);
+static void tcp_client_hexdump(t_tcpclient *x, long len);
static void tcpclient_tick(t_tcpclient *x);
static void *tcpclient_child_connect(void *w);
static void tcpclient_connect(t_tcpclient *x, t_symbol *hostname, t_floatarg fportno);
@@ -119,15 +121,20 @@ static void tcpclient_dump(t_tcpclient *x, t_float dump)
x->x_dump = (dump == 0)?0:1;
}
-static void tcp_client_hexdump(unsigned char *buf, long len)
+static void tcpclient_verbosity(t_tcpclient *x, t_float verbosity)
+{
+ x->x_verbosity = (verbosity == 0)?0:1; /* only two states so far */
+}
+
+static void tcp_client_hexdump(t_tcpclient *x, long len)
{
#define BYTES_PER_LINE 16
- char hexStr[(3*BYTES_PER_LINE)+1];
- char ascStr[BYTES_PER_LINE+1];
- long i, j, k = 0L;
-#ifdef DEBUG
- post("tcp_client_hexdump %d", len);
-#endif
+ char hexStr[(3*BYTES_PER_LINE)+1];
+ char ascStr[BYTES_PER_LINE+1];
+ long i, j, k = 0L;
+ unsigned char *buf = x->x_msginbuf;
+
+ if (x->x_verbosity) post("%s_hexdump %d:", objName, len);
while (k < len)
{
for (i = j = 0; i < BYTES_PER_LINE; ++i, ++k, j+=3)
@@ -188,7 +195,7 @@ static void *tcpclient_child_connect(void *w)
/* assign client port number */
server.sin_port = htons((u_short)x->x_port);
- post("%s: connecting socket %d to port %d", objName, sockfd, x->x_port);
+ if (x->x_verbosity) post("%s: connecting socket %d to port %d", objName, sockfd, x->x_port);
/* try to connect */
if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
{
@@ -226,7 +233,7 @@ static void tcpclient_disconnect(t_tcpclient *x)
x->x_fd = -1;
x->x_connectstate = 0;
outlet_float(x->x_connectout, 0);
- post("%s: disconnected", objName);
+ if (x->x_verbosity) post("%s: disconnected", objName);
}
else post("%s: not connected", objName);
}
@@ -296,7 +303,7 @@ static void tcpclient_send(t_tcpclient *x, t_symbol *s, int argc, t_atom *argv)
}
fclose(fptr);
fptr = NULL;
- post("%s_send: read \"%s\" length %d byte%s", objName, fpath, j, ((d==1)?"":"s"));
+ if (x->x_verbosity) post("%s_send: read \"%s\" length %d byte%s", objName, fpath, j, ((d==1)?"":"s"));
}
else
{
@@ -397,7 +404,7 @@ static void tcpclient_buf_size(t_tcpclient *x, t_symbol *s, int argc, t_atom *ar
}
buf_size = atom_getfloatarg(0, argc, argv);
x->x_fdbuf = tcpclient_set_socket_send_buf_size(x, (int)buf_size);
- post("%s_buf_size: set to %d", objName, x->x_fdbuf);
+ if (x->x_verbosity) post("%s_buf_size: set to %d", objName, x->x_fdbuf);
return;
}
x->x_fdbuf = tcpclient_get_socket_send_buf_size(x);
@@ -441,7 +448,7 @@ static void tcpclient_rcv(t_tcpclient *x)
x->x_msginbuf[ret] = 0;
post("%s: received %d bytes ", objName, ret);
#endif
- if (x->x_dump)tcp_client_hexdump(x->x_msginbuf, ret);
+ if (x->x_dump)tcp_client_hexdump(x, ret);
for (i = 0; i < ret; ++i)
{
/* convert the bytes in the buffer to floats in a list */
@@ -466,7 +473,7 @@ static void tcpclient_rcv(t_tcpclient *x)
}
else
{
- post("%s: connection closed for socket %d\n", objName, sockfd);
+ if (x->x_verbosity) post("%s: connection closed for socket %d\n", objName, sockfd);
tcpclient_disconnect(x);
}
}
@@ -493,6 +500,7 @@ static void *tcpclient_new(t_floatarg udpflag)
x->x_statusout = outlet_new(&x->x_obj, &s_anything);/* last outlet for everything else */
x->x_clock = clock_new(x, (t_method)tcpclient_tick);
x->x_poll = clock_new(x, (t_method)tcpclient_poll);
+ x->x_verbosity = 1; /* default post status changes to main window */
x->x_fd = -1;
/* convert the bytes in the buffer to floats in a list */
for (i = 0; i < MAX_UDP_RECEIVE; ++i)
@@ -535,6 +543,7 @@ void tcpclient_setup(void)
class_addmethod(tcpclient_class, (t_method)tcpclient_buf_size, gensym("buf"), A_GIMME, 0);
class_addmethod(tcpclient_class, (t_method)tcpclient_rcv, gensym("receive"), 0);
class_addmethod(tcpclient_class, (t_method)tcpclient_rcv, gensym("rcv"), 0);
+ class_addmethod(tcpclient_class, (t_method)tcpclient_verbosity, gensym("verbosity"), A_FLOAT, 0);
class_addmethod(tcpclient_class, (t_method)tcpclient_dump, gensym("dump"), A_FLOAT, 0);
class_addmethod(tcpclient_class, (t_method)tcpclient_timeout, gensym("timeout"), A_FLOAT, 0);
class_addlist(tcpclient_class, (t_method)tcpclient_send);