aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorMartin Peach <mrpeach@users.sourceforge.net>2007-08-02 15:56:10 +0000
committerMartin Peach <mrpeach@users.sourceforge.net>2007-08-02 15:56:10 +0000
commit6a66486589f875fd4ca18eaead3db168c15a1f8e (patch)
treee31caeaf5c8d62320ad8411eb7be6cecf405fb4b /net
parent34ffd2b68b20f69677ff07cff7798560b123c181 (diff)
Added disconnectclient and disconnectsocket methods so server can close connections too.
svn path=/trunk/externals/mrpeach/; revision=8347
Diffstat (limited to 'net')
-rwxr-xr-xnet/tcpserver-help.pd150
-rwxr-xr-xnet/tcpserver.c40
2 files changed, 131 insertions, 59 deletions
diff --git a/net/tcpserver-help.pd b/net/tcpserver-help.pd
index bc20123..dc5fc74 100755
--- a/net/tcpserver-help.pd
+++ b/net/tcpserver-help.pd
@@ -1,62 +1,83 @@
-#N canvas 74 160 1119 648 12;
-#X msg 18 -6 print;
-#X obj 136 109 tcpserver 9997;
-#X floatatom 171 237 5 0 0 0 - - -;
-#X floatatom 286 191 5 0 0 0 - - -;
-#X obj 324 138 unpack 0 0 0 0;
-#X floatatom 324 161 3 0 0 0 - - -;
-#X floatatom 359 161 3 0 0 0 - - -;
-#X floatatom 395 161 3 0 0 0 - - -;
-#X floatatom 431 161 3 0 0 0 - - -;
-#X text 281 160 from;
-#X text 224 236 connections;
-#X obj 43 161 print received;
-#X msg 118 45 client 1 1 2 3;
-#X msg 86 20 broadcast 1 2 3;
-#X text 69 -6 list of connections;
-#X text 226 19 send to all clients;
-#X text 249 45 send to client 1;
-#X text 199 191 on socket;
-#X msg 140 71 send 504 1 2 3;
-#X text 273 71 send to client on socket 504;
-#X msg 564 -365 client 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
+#N canvas 54 184 1138 623 12;
+#X msg 173 7 print;
+#X obj 136 144 tcpserver 9997;
+#X floatatom 176 236 5 0 0 0 - - -;
+#X floatatom 216 171 5 0 0 0 - - -;
+#X obj 257 190 unpack 0 0 0 0;
+#X floatatom 257 213 3 0 0 0 - - -;
+#X floatatom 297 213 3 0 0 0 - - -;
+#X floatatom 337 213 3 0 0 0 - - -;
+#X floatatom 378 213 3 0 0 0 - - -;
+#X text 214 212 from;
+#X text 229 235 connections;
+#X obj 29 240 print received;
+#X msg 173 61 client 1 1 2 3;
+#X msg 173 34 broadcast 1 2 3;
+#X text 225 8 list of connections;
+#X text 315 34 send to all clients;
+#X text 306 61 send to client 1;
+#X text 129 171 on socket;
+#X msg 173 88 send 504 1 2 3;
+#X text 306 88 send to client on socket 504;
+#X msg 589 -278 client 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19;
-#X msg 563 -340 client 1 20 21 22 23 24 25 26 27 28 29 30 31 32 33
+#X msg 589 -255 client 1 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39;
-#X msg 563 -297 client 1 40 41 42 43 44 45 46 47 48 49 50 51 52 53
+#X msg 589 -217 client 1 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59;
-#X msg 563 -255 client 1 60 61 62 63 64 65 66 67 68 69 70 71 72 73
+#X msg 589 -179 client 1 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79;
-#X msg 561 -213 client 1 80 81 82 83 84 85 86 87 88 89 90 91 92 93
+#X msg 589 -141 client 1 80 81 82 83 84 85 86 87 88 89 90 91 92 93
94 95 96 97 98 99;
-#X msg 560 -170 client 1 100 101 102 103 104 105 106 107 108 109 110
+#X msg 589 -103 client 1 100 101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119;
-#X msg 559 -127 client 1 120 121 122 123 124 125 126 127 138 139 140
+#X msg 589 -65 client 1 120 121 122 123 124 125 126 127 138 139 140
141 142 143 144 145 146 147 148 149;
-#X msg 559 -84 client 1 150 151 152 153 154 155 156 157 158 159 160
+#X msg 589 -27 client 1 150 151 152 153 154 155 156 157 158 159 160
161 162 163 164 165 166 167 168 169;
-#X msg 559 -39 client 1 170 171 172 173 174 175 176 177 178 179 180
+#X msg 589 11 client 1 170 171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189;
-#X msg 558 5 client 1 190 191 192 193 194 195 196 197 198 199 200 201
-202 203 204 205 206 207 208 209;
-#X msg 558 47 client 1 210 211 212 213 214 215 216 217 218 219 220
+#X msg 589 49 client 1 190 191 192 193 194 195 196 197 198 199 200
+201 202 203 204 205 206 207 208 209;
+#X msg 589 87 client 1 210 211 212 213 214 215 216 217 218 219 220
221 222 223 224 225 226 227 228 229;
-#X msg 558 89 client 1 230 231 232 233 234 235 236 237 238 239 240
+#X msg 589 125 client 1 230 231 232 233 234 235 236 237 238 239 240
241 242 243 244 245 246 247 248 249;
-#X msg 558 134 client 1 250 251 252 253 254 255;
-#X obj 91 -216 openpanel;
-#X obj 91 -235 bng 15 250 50 0 empty empty empty 17 7 0 10 -24198 -241291
+#X msg 589 163 client 1 250 251 252 253 254 255;
+#X obj 24 -245 openpanel;
+#X obj 24 -264 bng 15 250 50 0 empty empty empty 17 7 0 10 -24198 -241291
-1;
-#X text 391 -262 send a file;
-#X text 173 -217 ...any file;
-#X msg 58 -261 client 1 ../doc/5.reference/test.txt;
-#X msg 91 -192 client 1 \$1;
-#X msg 160 -89 10 1 2 43;
-#X text 252 -88 'send' prefix is optional;
-#X text 167 -68 (the first number is socket number);
-#X text 347 254 2007/06/20 Martin Peach;
+#X text 324 -291 send a file;
+#X text 106 -246 ...any file;
+#X msg -9 -290 client 1 ../doc/5.reference/test.txt;
+#X msg 24 -221 client 1 \$1;
+#X msg 166 -54 10 1 2 43;
+#X text 256 -53 'send' prefix is optional;
+#X text 187 -32 (the first number is socket number);
+#X msg 194 -115 disconnectsocket \$1;
+#X msg 91 -88 disconnectclient \$1;
+#X obj 589 188 s toserver;
+#X obj 157 121 r toserver;
+#X floatatom 113 -156 5 0 0 0 - - -;
+#X obj 91 -137 f;
+#X obj 91 -156 bng 15 250 50 0 empty empty empty 17 7 0 10 -258699
+-241291 -1;
+#X floatatom 216 -156 5 0 0 0 - - -;
+#X obj 194 -137 f;
+#X obj 194 -156 bng 15 250 50 0 empty empty empty 17 7 0 10 -258699
+-241291 -1;
+#X text 62 -175 disconnect by socket or client number;
+#X text 589 -303 tcpserver sends and receives bytes on [0...255];
+#X text 610 246 2007/08/02 Martin Peach;
+#X text 269 143 argument is port number;
+#X text 27 263 Received messages are output as lists of bytes;
+#X text 26 281 ***WARNING*** Attempting to print long messages can
+hang pd!;
+#X obj 29 218 spigot;
+#X obj 78 198 tgl 15 0 empty empty enable_print 17 7 0 10 -24198 -258699
+-258699 1 1;
#X connect 0 0 1 0;
-#X connect 1 0 11 0;
+#X connect 1 0 58 0;
#X connect 1 1 2 0;
#X connect 1 2 3 0;
#X connect 1 3 4 0;
@@ -67,21 +88,32 @@
#X connect 12 0 1 0;
#X connect 13 0 1 0;
#X connect 18 0 1 0;
-#X connect 20 0 1 0;
-#X connect 21 0 1 0;
-#X connect 22 0 1 0;
-#X connect 23 0 1 0;
-#X connect 24 0 1 0;
-#X connect 25 0 1 0;
-#X connect 26 0 1 0;
-#X connect 27 0 1 0;
-#X connect 28 0 1 0;
-#X connect 29 0 1 0;
-#X connect 30 0 1 0;
-#X connect 31 0 1 0;
-#X connect 32 0 1 0;
+#X connect 20 0 44 0;
+#X connect 21 0 44 0;
+#X connect 22 0 44 0;
+#X connect 23 0 44 0;
+#X connect 24 0 44 0;
+#X connect 25 0 44 0;
+#X connect 26 0 44 0;
+#X connect 27 0 44 0;
+#X connect 28 0 44 0;
+#X connect 29 0 44 0;
+#X connect 30 0 44 0;
+#X connect 31 0 44 0;
+#X connect 32 0 44 0;
#X connect 33 0 38 0;
#X connect 34 0 33 0;
#X connect 37 0 1 0;
#X connect 38 0 1 0;
#X connect 39 0 1 0;
+#X connect 42 0 1 0;
+#X connect 43 0 1 0;
+#X connect 45 0 1 0;
+#X connect 46 0 47 1;
+#X connect 47 0 43 0;
+#X connect 48 0 47 0;
+#X connect 49 0 50 1;
+#X connect 50 0 42 0;
+#X connect 51 0 50 0;
+#X connect 58 0 11 0;
+#X connect 59 0 58 1;
diff --git a/net/tcpserver.c b/net/tcpserver.c
index cf03092..ccc59c9 100755
--- a/net/tcpserver.c
+++ b/net/tcpserver.c
@@ -104,6 +104,8 @@ static void tcpserver_socketreceiver_free(t_tcpserver_socketreceiver *x);
static void tcpserver_send(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv);
static void tcp_server_send_bytes(int sockfd, t_tcpserver *x, int argc, t_atom *argv);
static void tcpserver_client_send(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv);
+static void tcpserver_client_disconnect(t_tcpserver *x, t_floatarg fclient);
+static void tcpserver_socket_disconnect(t_tcpserver *x, t_floatarg fsocket);
static void tcpserver_broadcast(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv);
static void tcpserver_notify(t_tcpserver *x);
static void tcpserver_connectpoll(t_tcpserver *x);
@@ -404,6 +406,42 @@ static void tcpserver_send(t_tcpserver *x, t_symbol *s, int argc, t_atom *argv)
tcp_server_send_bytes(client, x, argc-1, &argv[1]);
}
+/* disconnect a client by socket */
+static void tcpserver_socket_disconnect(t_tcpserver *x, t_floatarg fsocket)
+{
+ int sock = (int)fsocket;
+
+ if(x->x_nconnections < 0)
+ {
+ post("%s: no clients connected", objName);
+ return;
+ }
+ x->x_sock_fd = sock;
+ tcpserver_notify(x);
+}
+
+
+/* disconnect a client by number */
+static void tcpserver_client_disconnect(t_tcpserver *x, t_floatarg fclient)
+{
+ int client = (int)fclient;
+
+ if(x->x_nconnections < 0)
+ {
+ post("%s: no clients connected", objName);
+ return;
+ }
+ if (!((client > 0) && (client < MAX_CONNECT)))
+ {
+ post("%s: client %d out of range [1..%d]", objName, client, MAX_CONNECT);
+ return;
+ }
+ --client;/* zero based index*/
+ x->x_sock_fd = x->x_fd[client];
+ tcpserver_notify(x);
+}
+
+
/* send message to client using client number
note that the client numbers might change in case a client disconnects! */
/* clients start at 1 but our index starts at 0 */
@@ -637,6 +675,8 @@ 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_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_broadcast, gensym("broadcast"), A_GIMME, 0);
class_addlist(tcpserver_class, (t_method)tcpserver_send);
}