From 6df697d3b2c03debb3d53252daf1ce4cb646ebe2 Mon Sep 17 00:00:00 2001 From: Martin Peach Date: Wed, 8 Apr 2009 19:34:15 +0000 Subject: Added timeout message to set send timeout in microseconds, defaults to 1000. Changed help patch to match. svn path=/trunk/externals/mrpeach/; revision=10987 --- net/tcpclient-help.pd | 52 +++++++++++++++++++++++++++------------------------ net/tcpclient.c | 21 +++++++++++++++++++-- 2 files changed, 47 insertions(+), 26 deletions(-) (limited to 'net') diff --git a/net/tcpclient-help.pd b/net/tcpclient-help.pd index 360bb04..807c1ae 100644 --- a/net/tcpclient-help.pd +++ b/net/tcpclient-help.pd @@ -1,25 +1,25 @@ -#N canvas 349 7 1096 608 12; +#N canvas 184 7 1096 649 12; #X msg -112 56 disconnect; -#X obj 186 406 unpack 0 0 0 0; -#X floatatom 186 429 3 0 0 0 - - -; -#X floatatom 217 429 3 0 0 0 - - -; -#X floatatom 249 429 3 0 0 0 - - -; -#X floatatom 281 429 3 0 0 0 - - -; -#X text 146 428 from; +#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 166 354 tcpclient; -#X obj 206 380 tgl 15 0 empty empty connected 18 7 0 8 -24198 -241291 --1 1 1; +#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; +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 237 353 tcpclient opens a tcp socket to send and receive bytes +#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; @@ -27,8 +27,8 @@ on; #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 217 480 Received messages are output as a list of bytes; -#X text 238 509 Attempting to print long messages output can hang pd! +#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); @@ -50,23 +50,26 @@ be transmitted or received.; #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 166 481 spigot; -#X obj 205 458 tgl 15 0 empty empty enable_print 18 7 0 8 -24198 -241291 +#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 166 510 print >>>; +#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 341 400 9 0 0 0 - - -; -#X text 197 291 set send-buffer size; -#X obj 341 374 route sent buf; -#X floatatom 388 424 9 0 0 0 - - -; -#X text 458 423 Size of the send buffer; -#X text 415 399 Number of bytes sent (may still be in buffer); +#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 text -213 475 2009/03/04 Martin Peach; #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; @@ -95,4 +98,5 @@ be transmitted or received.; #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; diff --git a/net/tcpclient.c b/net/tcpclient.c index 727d181..1f14fac 100644 --- a/net/tcpclient.c +++ b/net/tcpclient.c @@ -68,6 +68,7 @@ typedef struct _tcpclient int x_dump; // 1 = hexdump received bytes int x_fd; // the socket int x_fdbuf; // the socket's buffer size + t_int x_timeout_us; /* send timeout in microseconds */ char *x_hostname; // address we want to connect to as text int x_connectstate; // 0 = not connected, 1 = connected int x_port; // port we're connected to @@ -80,6 +81,7 @@ typedef struct _tcpclient pthread_attr_t x_threadattr; /* attributes of child thread */ } t_tcpclient; +static void tcpclient_timeout(t_tcpclient *x, t_float timeout); static void tcpclient_dump(t_tcpclient *x, t_float dump); static void tcp_client_hexdump(unsigned char *buf, long len); static void tcpclient_tick(t_tcpclient *x); @@ -97,6 +99,20 @@ static void *tcpclient_new(t_floatarg udpflag); static void tcpclient_free(t_tcpclient *x); void tcpclient_setup(void); +static void tcpclient_timeout(t_tcpclient *x, t_float timeout) +{ + /* set the timeout on the select call in tcpclient_send_byte */ + /* this is the maximum time in microseconds to wait */ + /* before abandoning attempt to send */ + + t_int timeout_us = 0; + if ((timeout >= 0)&&(timeout < 1000000)) + { + timeout_us = (t_int)timeout; + x->x_timeout_us = timeout_us; + } +} + static void tcpclient_dump(t_tcpclient *x, t_float dump) { x->x_dump = (dump == 0)?0:1; @@ -300,8 +316,8 @@ int tcpclient_send_byte(t_tcpclient *x, char byte) FD_ZERO(&wfds); FD_SET(x->x_fd, &wfds); - timeout.tv_sec = 0; /* give it no time to clear buffer */ - timeout.tv_usec = 0; + timeout.tv_sec = 0; + timeout.tv_usec = x->x_timeout_us; /* give it about a millisecond to clear buffer */ result = select(x->x_fd+1, NULL, &wfds, NULL, &timeout); if (result == -1) { @@ -518,6 +534,7 @@ void tcpclient_setup(void) 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_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); } -- cgit v1.2.1