aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/tcpserver-help.pd102
-rw-r--r--net/tcpserver.c118
2 files changed, 165 insertions, 55 deletions
diff --git a/net/tcpserver-help.pd b/net/tcpserver-help.pd
index b3fef7d..0a12bd5 100644
--- a/net/tcpserver-help.pd
+++ b/net/tcpserver-help.pd
@@ -1,20 +1,20 @@
-#N canvas 8 158 1187 613 12;
+#N canvas 278 60 1204 806 12;
#X msg 143 -36 print;
-#X floatatom 322 164 5 0 0 0 - - -;
-#X floatatom 346 214 5 0 0 0 - - -;
-#X obj 370 165 unpack 0 0 0 0;
-#X floatatom 370 188 3 0 0 0 - - -;
-#X floatatom 401 188 3 0 0 0 - - -;
-#X floatatom 433 188 3 0 0 0 - - -;
-#X floatatom 467 188 3 0 0 0 - - -;
-#X text 327 187 from;
-#X text 238 164 connections;
-#X obj 299 289 print received;
+#X floatatom 448 290 5 0 0 0 - - -;
+#X floatatom 472 340 5 0 0 0 - - -;
+#X obj 496 291 unpack 0 0 0 0;
+#X floatatom 496 314 3 0 0 0 - - -;
+#X floatatom 527 314 3 0 0 0 - - -;
+#X floatatom 559 314 3 0 0 0 - - -;
+#X floatatom 593 314 3 0 0 0 - - -;
+#X text 453 313 from;
+#X text 364 290 connections;
+#X obj 425 415 print received;
#X msg 239 60 client 1 1 2 3;
#X msg 167 -12 broadcast 1 2 3;
#X text 195 -35 list of connections;
#X text 290 -12 send to all clients;
-#X text 276 214 on socket;
+#X text 402 340 on socket;
#X msg 93 -86 send 504 1 2 3;
#X text 204 -91 send to client on socket 504;
#X msg 714 -228 client 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@@ -53,7 +53,7 @@
#X msg 63 -116 disconnectsocket \$1;
#X msg 20 -159 disconnectclient \$1;
#X obj 714 238 s toserver;
-#X obj 197 120 r toserver;
+#X obj 242 163 r toserver;
#X floatatom 247 -198 5 0 0 0 - - -;
#X obj 225 -179 f;
#X obj 225 -198 bng 15 250 50 0 empty empty empty 17 7 0 10 -258699
@@ -63,49 +63,53 @@
#X obj 267 -155 bng 15 250 50 0 empty empty empty 17 7 0 10 -258699
-241291 -1;
#X text -5 -136 disconnect by socket or client number;
-#X text 406 139 argument is port number;
-#X text -30 141 Received messages are output as lists of bytes;
-#X text 100 321 ***WARNING*** Attempting to print long messages can
+#X text 532 265 argument is port number;
+#X text 96 267 Received messages are output as lists of bytes;
+#X text 35 371 ***WARNING*** Attempting to print long messages can
hang pd!;
-#X obj 299 267 spigot;
-#X obj 338 247 tgl 15 0 empty empty enable_print 17 7 0 10 -24198 -258699
+#X obj 425 393 spigot;
+#X obj 464 373 tgl 15 0 empty empty enable_print 17 7 0 10 -24198 -258699
-45076 0 1;
#X msg 263 84 dump \$1;
#X obj 87 69 tgl 15 0 empty empty enable_dump 17 7 0 10 -4034 -257985
--1 0 1;
+-1 1 1;
#X text 19 19 dump received;
#X text 19 35 characters to main;
#X text 19 50 window in hexdump;
#X text 19 66 format:;
-#X obj 299 140 tcpserver 9997;
+#X obj 425 266 tcpserver 9997;
#X msg -28 -207 client 1 test.txt;
-#X text 839 323 2009/02/24 Martin Peach;
#X text 714 -253 [tcpserver] sends and receives bytes on [0...255]
;
#X text 262 -243 [tcpserver] waits for clients to connect to its port.
;
#X text -33 -228 send a file to client 1;
#X msg 287 108 client 1 72 101 108 108 111 13 10;
-#X floatatom 562 212 9 0 0 0 - - -;
-#X obj 611 190 print status;
-#X obj 495 187 unpack 0 0;
-#X floatatom 523 235 5 0 0 0 - - -;
-#X text 453 235 to_client;
+#X floatatom 688 338 9 0 0 0 - - -;
+#X obj 737 313 print status;
+#X obj 621 313 unpack 0 0;
+#X floatatom 649 361 5 0 0 0 - - -;
+#X text 579 361 to_client;
#X msg 215 36 client;
#X msg 192 13 client 1;
#X text 260 12 get state of client 1;
-#X text 267 36 get sate of all clients;
#X text 353 60 send (binary) 1 2 3 to client 1;
-#X obj 495 160 route sent client;
-#X floatatom 679 317 5 0 0 0 - - -;
-#X symbolatom 720 290 20 0 0 0 - - -;
-#X text 631 316 socket;
-#X text 697 289 ip;
-#X obj 639 266 unpack 0 0 s;
-#X floatatom 639 290 5 0 0 0 - - -;
-#X text 591 289 client;
-#X text 629 214 bytes;
-#X text 522 212 sent;
+#X obj 621 286 route sent client;
+#X floatatom 796 443 5 0 0 0 - - -;
+#X symbolatom 828 416 20 0 0 0 - - -;
+#X floatatom 765 416 5 0 0 0 - - -;
+#X text 755 340 bytes;
+#X text 648 338 sent;
+#X msg 313 134 clientbuf 1 65536;
+#X text 35 450 2009/03/01 Martin Peach;
+#X obj 765 392 unpack 0 0 s 0;
+#X floatatom 860 464 7 0 0 0 - - -;
+#X text 705 464 length of send buffer:;
+#X text 745 442 socket:;
+#X text 714 415 client:;
+#X text 805 415 ip:;
+#X text 443 133 set send-buffer size for client 1;
+#X text 267 36 get state of all clients (list on right outlet);
#X connect 0 0 60 0;
#X connect 3 0 4 0;
#X connect 3 1 5 0;
@@ -148,16 +152,18 @@ hang pd!;
#X connect 60 1 1 0;
#X connect 60 2 2 0;
#X connect 60 3 3 0;
-#X connect 60 4 77 0;
+#X connect 60 4 75 0;
#X connect 61 0 60 0;
-#X connect 66 0 60 0;
-#X connect 69 0 70 0;
-#X connect 69 1 67 0;
+#X connect 65 0 60 0;
+#X connect 68 0 69 0;
+#X connect 68 1 66 0;
+#X connect 71 0 60 0;
#X connect 72 0 60 0;
-#X connect 73 0 60 0;
-#X connect 77 0 69 0;
-#X connect 77 1 82 0;
-#X connect 77 2 68 0;
-#X connect 82 0 83 0;
-#X connect 82 1 78 0;
-#X connect 82 2 79 0;
+#X connect 75 0 68 0;
+#X connect 75 1 83 0;
+#X connect 75 2 67 0;
+#X connect 81 0 60 0;
+#X connect 83 0 78 0;
+#X connect 83 1 76 0;
+#X connect 83 2 77 0;
+#X connect 83 3 84 0;
diff --git a/net/tcpserver.c b/net/tcpserver.c
index 0d1d390..79fba19 100644
--- a/net/tcpserver.c
+++ b/net/tcpserver.c
@@ -86,6 +86,7 @@ typedef struct _tcpserver
t_int x_dump; // 1 = hexdump received bytes
t_symbol *x_host[MAX_CONNECT];
t_int x_fd[MAX_CONNECT];
+ t_int x_fdbuf[MAX_CONNECT];
u_long x_addr[MAX_CONNECT];
t_tcpserver_socketreceiver *x_sr[MAX_CONNECT];
t_atom x_addrbytes[4];
@@ -105,6 +106,9 @@ static void tcpserver_send(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv);
static void tcpserver_send_bytes(int sockfd, t_tcpserver *x, int argc, t_atom *argv);
static size_t tcpserver_send_buf(int client, int sockfd, char *byte_buf, size_t length);
static void tcpserver_client_send(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv);
+static int tcpserver_get_socket_send_buf_size(int sockfd);
+static int tcpserver_set_socket_send_buf_size(int sockfd, int size);
+static void tcpserver_buf_size(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv);
static void tcpserver_disconnect(t_tcpserver *x);
static void tcpserver_client_disconnect(t_tcpserver *x, t_floatarg fclient);
static void tcpserver_socket_disconnect(t_tcpserver *x, t_floatarg fsocket);
@@ -524,7 +528,7 @@ static void tcpserver_client_disconnect(t_tcpserver *x, t_floatarg fclient)
static void tcpserver_client_send(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv)
{
int client = -1;
- t_atom output_atom[3];
+ t_atom output_atom[4];
if(x->x_nconnections <= 0)
{
@@ -547,7 +551,7 @@ static void tcpserver_client_send(t_tcpserver *x, t_symbol *s, int argc, t_atom
return;
}
}
- if (argc > 2)
+ if (argc > 1)
{
--client;/* zero based index*/
tcpserver_send_bytes(client, x, argc-1, &argv[1]);
@@ -558,24 +562,104 @@ static void tcpserver_client_send(t_tcpserver *x, t_symbol *s, int argc, t_atom
/* output parameters of all connections via status outlet */
for(client = 0; client < x->x_nconnections; client++)
{
+ x->x_fdbuf[client] = tcpserver_get_socket_send_buf_size(x->x_fd[client]);
SETFLOAT(&output_atom[0], client+1);
SETFLOAT(&output_atom[1], x->x_fd[client]);
output_atom[2].a_type = A_SYMBOL;
output_atom[2].a_w.w_symbol = x->x_host[client];
- outlet_anything( x->x_status_outlet, gensym("client"), 3, output_atom);
+ SETFLOAT(&output_atom[3], x->x_fdbuf[client]);
+ outlet_anything( x->x_status_outlet, gensym("client"), 4, output_atom);
}
}
else
{
+ client -= 1;/* zero-based client index conflicts with 1-based user index !!! */
/* output client parameters via status outlet */
- SETFLOAT(&output_atom[0], client);
- SETFLOAT(&output_atom[1], x->x_fd[client-1]);
+ x->x_fdbuf[client] = tcpserver_get_socket_send_buf_size(x->x_fd[client]);
+ SETFLOAT(&output_atom[0], client+1);/* user sees client 0 as 1 */
+ SETFLOAT(&output_atom[1], x->x_fd[client]);
output_atom[2].a_type = A_SYMBOL;
- output_atom[2].a_w.w_symbol = x->x_host[client-1];
- outlet_anything( x->x_status_outlet, gensym("client"), 3, output_atom);
+ output_atom[2].a_w.w_symbol = x->x_host[client];
+ SETFLOAT(&output_atom[3], x->x_fdbuf[client]);
+ outlet_anything( x->x_status_outlet, gensym("client"), 4, output_atom);
}
}
+/* Return the send buffer size of socket */
+static int tcpserver_get_socket_send_buf_size(int sockfd)
+{
+ int optVal = 0;
+ int optLen = sizeof(int);
+#ifdef MSW
+ if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char*)&optVal, &optLen) == SOCKET_ERROR)
+ post("%_get_socket_send_buf_size: getsockopt returned %d\n", objName, WSAGetLastError());
+#else
+ if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char*)&optVal, &optLen) == -1)
+ post("%_get_socket_send_buf_size: getsockopt returned %d\n", objName, errno);
+#endif
+ return optVal;
+}
+
+/* Set the send buffer size of socket, returns actual size */
+static int tcpserver_set_socket_send_buf_size(int sockfd, int size)
+{
+ int optVal = size;
+ int optLen = sizeof(int);
+#ifdef MSW
+ if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char*)&optVal, optLen) == SOCKET_ERROR)
+ post("%s_set_socket_send_buf_size: setsockopt returned %d\n", objName, WSAGetLastError());
+#else
+ if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char*)&optVal, optLen) == -1)
+ post("%s_set_socket_send_buf_size: setsockopt returned %d\n", objName, errno);
+#endif
+ else return (tcpserver_get_socket_send_buf_size(sockfd));
+}
+
+/* Get/set the send buffer of client socket */
+static void tcpserver_buf_size(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv)
+{
+ int client = -1;
+ float buf_size = 0;
+ t_atom output_atom[3];
+
+ if(x->x_nconnections <= 0)
+ {
+ post("%s_buf_size: no clients connected", objName);
+ return;
+ }
+ /* get number of client (first element in list) */
+ if (argc > 0)
+ {
+ if (argv[0].a_type == A_FLOAT)
+ client = atom_getfloatarg(0, argc, argv);
+ else
+ {
+ post("%s_buf_size: specify client by number", objName);
+ return;
+ }
+ if (!((client > 0) && (client < MAX_CONNECT)))
+ {
+ post("%s__buf_size: client %d out of range [1..%d]", objName, client, MAX_CONNECT);
+ return;
+ }
+ }
+ if (argc > 1)
+ {
+ if (argv[1].a_type != A_FLOAT)
+ {
+ post("%s_buf_size: specify buffer size with a float", objName);
+ return;
+ }
+ buf_size = atom_getfloatarg(1, argc, argv);
+ --client;/* zero based index*/
+ x->x_fdbuf[client] = tcpserver_set_socket_send_buf_size(x->x_fd[client], (int)buf_size);
+ post("%s_buf_size: client %d set to %d", objName, client+1, x->x_fdbuf[client]);
+ return;
+ }
+ post("%s_buf_size: specify client and buffer size", objName);
+ return;
+}
+
/* broadcasts a message to all connected clients */
static void tcpserver_broadcast(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv)
{
@@ -625,6 +709,8 @@ static void tcpserver_connectpoll(t_tcpserver *x)
int sockaddrl = (int) sizeof( struct sockaddr );
int fd = accept(x->x_connectsocket, (struct sockaddr*)&incomer_address, &sockaddrl);
int i;
+ int optVal;
+ int optLen = sizeof(int);
if (fd < 0) post("%s: accept failed", objName);
else
@@ -648,6 +734,23 @@ static void tcpserver_connectpoll(t_tcpserver *x)
x->x_sr[i] = y;
post("%s: accepted connection from %s on socket %d",
objName, x->x_host[i]->s_name, x->x_fd[i]);
+/* see how big the send buffer is on this socket */
+ x->x_fdbuf[i] = 0;
+#ifdef MSW
+ if (getsockopt(x->x_fd[i], SOL_SOCKET, SO_SNDBUF, (char*)&optVal, &optLen) != SOCKET_ERROR)
+ {
+ /* post("%s_connectpoll: send buffer is %ld\n", objName, optVal); */
+ x->x_fdbuf[i] = optVal;
+ }
+ else post("%s_connectpoll: getsockopt returned %d\n", objName, WSAGetLastError());
+#else
+ if (getsockopt(x->x_fd[i], SOL_SOCKET, SO_SNDBUF, (char*)&optVal, &optLen) == 0)
+ {
+ /* post("%s_connectpoll: send buffer is %ld\n", objName, optVal); */
+ x->x_fdbuf[i] = optVal;
+ }
+ else post("%s_connectpoll: getsockopt returned %d\n", objName, errno);
+#endif
outlet_float(x->x_connectout, x->x_nconnections);
outlet_float(x->x_sockout, x->x_fd[i]); /* the socket number */
x->x_addr[i] = ntohl(incomer_address.sin_addr.s_addr);
@@ -776,6 +879,7 @@ void tcpserver_setup(void)
class_addmethod(tcpserver_class, (t_method)tcpserver_print, gensym("print"), 0);
class_addmethod(tcpserver_class, (t_method)tcpserver_send, gensym("send"), A_GIMME, 0);
class_addmethod(tcpserver_class, (t_method)tcpserver_client_send, gensym("client"), A_GIMME, 0);
+ class_addmethod(tcpserver_class, (t_method)tcpserver_buf_size, gensym("clientbuf"), A_GIMME, 0);
class_addmethod(tcpserver_class, (t_method)tcpserver_client_disconnect, gensym("disconnectclient"), A_DEFFLOAT, 0);
class_addmethod(tcpserver_class, (t_method)tcpserver_socket_disconnect, gensym("disconnectsocket"), A_DEFFLOAT, 0);
class_addmethod(tcpserver_class, (t_method)tcpserver_dump, gensym("dump"), A_FLOAT, 0);